Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/pipectl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ go_library(
srcs = ["main.go"],
importpath = "github.com/pipe-cd/pipe/cmd/pipectl",
visibility = ["//visibility:private"],
deps = ["//pkg/cli:go_default_library"],
deps = [
"//pkg/app/pipectl/cmd/application:go_default_library",
"//pkg/app/pipectl/cmd/deployment:go_default_library",
"//pkg/cli:go_default_library",
],
)

go_binary(
Expand Down
7 changes: 6 additions & 1 deletion cmd/pipectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package main
import (
"log"

"github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/application"
"github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/deployment"
"github.com/pipe-cd/pipe/pkg/cli"
)

Expand All @@ -26,7 +28,10 @@ func main() {
"The command line tool for PipeCD.",
)

app.AddCommands()
app.AddCommands(
application.NewCommand(),
deployment.NewCommand(),
)

if err := app.Run(); err != nil {
log.Fatal(err)
Expand Down
15 changes: 15 additions & 0 deletions pkg/app/pipectl/client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["client.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/client",
visibility = ["//visibility:public"],
deps = [
"//pkg/app/api/service/apiservice:go_default_library",
"//pkg/rpc/rpcauth:go_default_library",
"//pkg/rpc/rpcclient:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
"@org_golang_google_grpc//credentials:go_default_library",
],
)
99 changes: 99 additions & 0 deletions pkg/app/pipectl/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"context"
"crypto/tls"
"errors"
"time"

"github.com/spf13/cobra"
"google.golang.org/grpc/credentials"

"github.com/pipe-cd/pipe/pkg/app/api/service/apiservice"
"github.com/pipe-cd/pipe/pkg/rpc/rpcauth"
"github.com/pipe-cd/pipe/pkg/rpc/rpcclient"
)

type Options struct {
Address string
APIKey string
APIKeyFile string
Insecure bool
CertFile string
}

func (o *Options) RegisterPersistentFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&o.Address, "address", o.Address, "The address to control-plane api.")
cmd.PersistentFlags().StringVar(&o.APIKey, "api-key", o.APIKey, "The API key used while authenticating with control-plane.")
cmd.PersistentFlags().StringVar(&o.APIKeyFile, "api-key-file", o.APIKeyFile, "Path to the file containing API key used while authenticating with control-plane.")
cmd.PersistentFlags().BoolVar(&o.Insecure, "insecure", o.Insecure, "Whether disabling transport security while connecting to control-plane.")
cmd.PersistentFlags().StringVar(&o.CertFile, "cert-file", o.CertFile, "The path to the TLS certificate file.")
}

func (o *Options) Validate() error {
if o.Address == "" {
return errors.New("address must be set")
}
if o.APIKey == "" && o.APIKeyFile == "" {
return errors.New("either api-key or api-key-file must be set")
}
return nil
}

func (o *Options) NewClient(ctx context.Context) (apiservice.Client, error) {
if err := o.Validate(); err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

var creds credentials.PerRPCCredentials
var err error

if o.APIKey != "" {
creds = rpcclient.NewPerRPCCredentials(o.APIKey, rpcauth.APIKeyCredentials, !o.Insecure)
} else {
creds, err = rpcclient.NewPerRPCCredentialsFromFile(o.APIKeyFile, rpcauth.APIKeyCredentials, !o.Insecure)
if err != nil {
return nil, err
}
}

options := []rpcclient.DialOption{
rpcclient.WithBlock(),
rpcclient.WithPerRPCCredentials(creds),
}

if !o.Insecure {
if o.CertFile != "" {
options = append(options, rpcclient.WithTLS(o.CertFile))
} else {
config := &tls.Config{}
options = append(options, rpcclient.WithTransportCredentials(credentials.NewTLS(config)))
}
} else {
options = append(options, rpcclient.WithInsecure())
}

client, err := apiservice.NewClient(ctx, o.Address, options...)
if err != nil {
return nil, err
}

return client, nil
}
20 changes: 20 additions & 0 deletions pkg/app/pipectl/cmd/application/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"add.go",
"application.go",
"sync.go",
],
importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/application",
visibility = ["//visibility:public"],
deps = [
"//pkg/app/api/service/apiservice:go_default_library",
"//pkg/app/pipectl/client:go_default_library",
"//pkg/app/pipectl/log:go_default_library",
"//pkg/cli:go_default_library",
"//pkg/model:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
],
)
108 changes: 108 additions & 0 deletions pkg/app/pipectl/cmd/application/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package application

import (
"context"

"github.com/spf13/cobra"

"github.com/pipe-cd/pipe/pkg/app/api/service/apiservice"
"github.com/pipe-cd/pipe/pkg/cli"
"github.com/pipe-cd/pipe/pkg/model"
)

type add struct {
root *command

appName string
appKind string
envID string
pipedID string
cloudProvider string

gitPathRepoID string
gitPathAppDir string
gitPathConfigFileName string
}

func newAddCommand(root *command) *cobra.Command {
c := &add{
root: root,
gitPathConfigFileName: ".pipe.yaml",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it should be changeable via --config-file-name or something like that. And the default value should refer to pkg.model.DefaultDeploymentConfigFileName.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I just now found it has been already changeable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I changed to use the value defined in the model package and simplified the flags.

}
cmd := &cobra.Command{
Use: "add",
Short: "Add a new application.",
RunE: cli.WithContext(c.run),
}

cmd.Flags().StringVar(&c.appName, "app-name", c.appName, "The application name.")
cmd.Flags().StringVar(&c.appKind, "app-kind", c.appKind, "The kind of application. (KUBERNETES|TERRAFORM|LAMBDA|CLOUDRUN)")
cmd.Flags().StringVar(&c.envID, "env-id", c.envID, "The ID of environment where this application should belong to.")
cmd.Flags().StringVar(&c.pipedID, "piped-id", c.pipedID, "The ID of piped that should handle this applicaiton.")
cmd.Flags().StringVar(&c.cloudProvider, "cloud-provider", c.cloudProvider, "The cloud provider name. One of the registered providers in the piped configuration.")

cmd.Flags().StringVar(&c.gitPathRepoID, "gitpath-repo-id", c.gitPathRepoID, "The repository ID. One the registered repositories in the piped configuration.")
cmd.Flags().StringVar(&c.gitPathAppDir, "gitpath-app-dir", c.gitPathAppDir, "The relative path from the root of repository to the application directory.")
cmd.Flags().StringVar(&c.gitPathConfigFileName, "gitpath-config-file-name", c.gitPathConfigFileName, "The configuration file name. Default is .pipe.yaml")

cmd.MarkFlagRequired("app-name")
cmd.MarkFlagRequired("app-kind")
cmd.MarkFlagRequired("env-id")
cmd.MarkFlagRequired("piped-id")
cmd.MarkFlagRequired("cloud-provider")
cmd.MarkFlagRequired("gitpath-repo-id")
cmd.MarkFlagRequired("gitpath-app-dir")

return cmd
}

func (c *add) run(ctx context.Context, _ cli.Telemetry) error {
logger := c.root.logOptions.NewLogger()
cli, err := c.root.clientOptions.NewClient(ctx)
if err != nil {
logger.Fatal("Failed to initialize client: %v", err)
}
defer cli.Close()

appKind, ok := model.ApplicationKind_value[c.appKind]
if !ok {
logger.Fatal("Unsupported application kind %s", c.appKind)
}

req := &apiservice.AddApplicationRequest{
Name: c.appName,
EnvId: c.envID,
PipedId: c.pipedID,
GitPath: &model.ApplicationGitPath{
Repo: &model.ApplicationGitRepository{
Id: c.gitPathRepoID,
},
Path: c.gitPathAppDir,
ConfigFilename: c.gitPathConfigFileName,
},
Kind: model.ApplicationKind(appKind),
CloudProvider: c.cloudProvider,
}

resp, err := cli.AddApplication(ctx, req)
if err != nil {
logger.Fatal("Failed to add application: %v", err)
}

logger.Info("Successfully added application id = %s", resp.ApplicationId)
return nil
}
47 changes: 47 additions & 0 deletions pkg/app/pipectl/cmd/application/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package application

import (
"github.com/spf13/cobra"

"github.com/pipe-cd/pipe/pkg/app/pipectl/client"
"github.com/pipe-cd/pipe/pkg/app/pipectl/log"
)

type command struct {
clientOptions *client.Options
logOptions *log.Options
}

func NewCommand() *cobra.Command {
c := &command{
clientOptions: &client.Options{},
logOptions: &log.Options{},
}
cmd := &cobra.Command{
Use: "application",
Short: "Manage application resources.",
SilenceUsage: true,
}

cmd.AddCommand(newAddCommand(c))
cmd.AddCommand(newSyncCommand(c))

c.clientOptions.RegisterPersistentFlags(cmd)
c.logOptions.RegisterPersistentFlags(cmd)

return cmd
}
Loading