Skip to content

Commit

Permalink
Merge pull request #58 from chuhlomin/develop
Browse files Browse the repository at this point in the history
Wait until deploy completes
  • Loading branch information
tonglil authored Jan 24, 2018
2 parents 4034045 + abfdd73 commit 41313cf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The following parameters are used to configure this plugin:
* *optional* `namespace` - Kubernetes namespace to operate in (defaults to `default`)
* *optional* `template` - Kubernetes manifest template (defaults to `.kube.yml`)
* *optional* `secret_template` - Kubernetes [_Secret_ resource](http://kubernetes.io/docs/user-guide/secrets/) manifest template (defaults to `.kube.sec.yml`)
* *optional* `wait_deployments` - list of Deployments to wait for successful rollout, using `kubectl rollout status`
* *optional* `wait_seconds` - if `wait_deployments` is set, number of seconds to wait before failing the build
* `vars` - variables to use in `template` and `secret_template`
* `secrets` - credential and variables to use in `secret_template` (see [below](#secrets) for details)

Expand Down
48 changes: 47 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"text/template"

"github.com/urfave/cli"
"strconv"
)

var (
Expand All @@ -20,6 +21,7 @@ var (
const (
gcloudCmd = "gcloud"
kubectlCmd = "kubectl"
timeoutCmd = "timeout"

keyPath = "/tmp/gcloud.json"
nsPath = "/tmp/namespace.json"
Expand Down Expand Up @@ -126,6 +128,17 @@ func wrapMain() error {
Usage: "Git tag",
EnvVar: "DRONE_TAG",
},
cli.StringSliceFlag{
Name: "wait_deployments",
Usage: "List of Deployments to wait for successful rollout, using kubectl rollout status",
EnvVar: "PLUGIN_WAIT_DEPLOYMENTS",
},
cli.IntFlag{
Name: "wait_seconds",
Usage: "If wait_deployments is set, number of seconds to wait before failing the build",
EnvVar: "PLUGIN_WAIT_SECONDS",
Value: 0,
},
}

if err := app.Run(os.Args); err != nil {
Expand Down Expand Up @@ -410,6 +423,39 @@ func run(c *cli.Context) error {
return fmt.Errorf("Error: %s\n", err)
}

// Waiting for rollout to finish

waitDeployments := c.StringSlice("wait_deployments")
waitSeconds := c.Int("wait_seconds")
waitDeploymentsCount := len(waitDeployments)
counterProgress := ""

for counter, deployment := range waitDeployments {
if waitDeploymentsCount > 1 {
counterProgress = fmt.Sprintf(" %d/%d", counter + 1, waitDeploymentsCount)
}

log(fmt.Sprintf("Waiting until rollout completes for %s%s\n", deployment, counterProgress))

command := []string{"rollout", "status", "deployment", deployment}

if namespace != "" {
command = append(command, "--namespace", namespace)
}

path := kubectlCmd

if waitSeconds != 0 {
command = append([]string{"-t", strconv.Itoa(waitSeconds), path}, command...)
path = timeoutCmd
}

err = runner.Run(path, command...)
if err != nil {
return fmt.Errorf("Error: %s\n", err)
}
}

return nil
}

Expand Down Expand Up @@ -443,5 +489,5 @@ func applyArgs(dryrun bool, file string) []string {
}

func log(format string, a ...interface{}) {
fmt.Printf("\n"+format, a...)
fmt.Printf("\n" + format, a...)
}

0 comments on commit 41313cf

Please sign in to comment.