Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions cmd/pipectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package main

import (
"log"
"fmt"
"os"

"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,9 +29,13 @@ 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)
fmt.Println("Error:", err)
os.Exit(1)
}
}
21 changes: 21 additions & 0 deletions pkg/app/pipectl/client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"application.go",
"client.go",
"deployment.go",
],
importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/client",
visibility = ["//visibility:public"],
deps = [
"//pkg/app/api/service/apiservice:go_default_library",
"//pkg/model: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",
"@org_uber_go_zap//:go_default_library",
],
)
104 changes: 104 additions & 0 deletions pkg/app/pipectl/client/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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"
"fmt"
"time"

"go.uber.org/zap"

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

// SyncApplication sents a command to sync a given application and waits until it has been triggered.
// The deployment ID will be returned or an error.
func SyncApplication(
ctx context.Context,
cli apiservice.Client,
appID string,
checkInterval, timeout time.Duration,
logger *zap.Logger,
) (string, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

req := &apiservice.SyncApplicationRequest{
ApplicationId: appID,
}
resp, err := cli.SyncApplication(ctx, req)
if err != nil {
return "", fmt.Errorf("failed to sync application %w", err)
}

logger.Info("Sent a request to sync application and waiting to be accepted...")

ticker := time.NewTicker(checkInterval)
defer ticker.Stop()

check := func() (deploymentID string, shouldRetry bool) {
const triggeredDeploymentIDKey = "TriggeredDeploymentID"

cmd, err := getCommand(ctx, cli, resp.CommandId)
if err != nil {
logger.Error(fmt.Sprintf("Failed while retrieving command information. Try again. (%v)", err))
shouldRetry = true
return
}

if cmd.Type != model.Command_SYNC_APPLICATION {
logger.Error(fmt.Sprintf("Unexpected command type, want: %s, got: %s", model.Command_SYNC_APPLICATION.String(), cmd.Type.String()))
return
}

switch cmd.Status {
case model.CommandStatus_COMMAND_SUCCEEDED:
deploymentID = cmd.Metadata[triggeredDeploymentIDKey]
return

case model.CommandStatus_COMMAND_FAILED:
logger.Error("The request was unable to handle")
return

case model.CommandStatus_COMMAND_TIMEOUT:
logger.Error("The request was timed out")
return

default:
shouldRetry = true
return
}
}

for {
select {
case <-ctx.Done():
return "", ctx.Err()

case <-ticker.C:
deploymentID, shouldRetry := check()
if shouldRetry {
logger.Info("...")
continue
}
if deploymentID == "" {
return "", fmt.Errorf("failed to detect the triggered deployment ID")
}
return deploymentID, nil
}
}
}
111 changes: 111 additions & 0 deletions pkg/app/pipectl/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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/model"
"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
}

func getCommand(ctx context.Context, cli apiservice.Client, cmdID string) (*model.Command, error) {
req := &apiservice.GetCommandRequest{
CommandId: cmdID,
}
resp, err := cli.GetCommand(ctx, req)
if err != nil {
return nil, err
}
return resp.Command, nil
}
96 changes: 96 additions & 0 deletions pkg/app/pipectl/client/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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"
"fmt"
"strings"
"time"

"go.uber.org/zap"

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

// WaitDeploymentStatuses waits a given deployment until it reaches one of the specified statuses.
func WaitDeploymentStatuses(
ctx context.Context,
cli apiservice.Client,
deploymentID string,
statuses []model.DeploymentStatus,
checkInterval, timeout time.Duration,
logger *zap.Logger,
) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

statusMap := makeDeploymentStatusesMap(statuses)
ticker := time.NewTicker(checkInterval)
defer ticker.Stop()

check := func() (status string, shouldRetry bool) {
req := &apiservice.GetDeploymentRequest{
DeploymentId: deploymentID,
}
resp, err := cli.GetDeployment(ctx, req)
if err != nil {
logger.Error(fmt.Sprintf("Failed while retrieving deployment information. Try again. (%v)", err))
shouldRetry = true
return
}

if _, ok := statusMap[resp.Deployment.Status]; !ok {
shouldRetry = true
return
}

status = strings.TrimPrefix(resp.Deployment.Status.String(), "DEPLOYMENT_")
return
}

// Do the first check immediately.
status, shouldRetry := check()
if !shouldRetry {
logger.Info(fmt.Sprintf("Deployment is at %s status", status))
return nil
}

for {
select {
case <-ctx.Done():
return ctx.Err()

case <-ticker.C:
status, shouldRetry := check()
if shouldRetry {
logger.Info("...")
continue
}

logger.Info(fmt.Sprintf("Deployment is at %s status", status))
return nil
}
}
}

func makeDeploymentStatusesMap(statuses []model.DeploymentStatus) map[model.DeploymentStatus]struct{} {
out := make(map[model.DeploymentStatus]struct{}, len(statuses))
for _, s := range statuses {
out[model.DeploymentStatus(s)] = struct{}{}
}
return out
}
Loading