gofmt and added the ability to have a batch size and upgrade interval

This commit is contained in:
Joachim Hill-Grannec
2017-01-24 14:42:11 -08:00
parent debf13c598
commit 27ad82d9af
3 changed files with 72 additions and 52 deletions
+2
View File
@@ -10,6 +10,8 @@ The following parameters are used to configure this plugin:
- `start_first` - start the new container before stopping the old one, defaults to `true`
- `confirm` - auto confirm the service upgrade if successful, defaults to `false`
- `timeout` - the maximum wait time in seconds for the service to upgrade, default to `30`
- `interval_millis` - the upgrade interval in milli seconds, defaults to `1000`
- `batch_size` - the upgrade batch size, defaults to `1`
The following is a sample Rancher configuration in your `.drone.yml` file:
+30 -15
View File
@@ -1,20 +1,22 @@
package main
import (
"os"
"github.com/codegangsta/cli"
"log"
"os"
)
type Rancher struct {
Url string `json:"url"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Service string `json:"service"`
Image string `json:"docker_image"`
StartFirst bool `json:"start_first"`
Confirm bool `json:"confirm"`
Timeout int `json:"timeout"`
Url string `json:"url"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Service string `json:"service"`
Image string `json:"docker_image"`
StartFirst bool `json:"start_first"`
Confirm bool `json:"confirm"`
Timeout int `json:"timeout"`
IntervalMillis int64 `json:"interval_millis"`
BatchSize int64 `json:"batch_size"`
}
var version string // build number set at compile-time
@@ -68,6 +70,18 @@ func main() {
Value: 30,
EnvVar: "PLUGIN_TIMEOUT",
},
cli.IntFlag{
Name: "interval-millis",
Usage: "The interval for batch size upgrade",
Value: 1000,
EnvVar: "PLUGIN_INTERVAL_MILLIS",
},
cli.IntFlag{
Name: "batch-size",
Usage: "The upgrade batch size",
Value: 1,
EnvVar: "PLUGIN_BATCH_SIZE",
},
cli.BoolTFlag{
Name: "yaml-verified",
Usage: "Ensure the yaml was signed",
@@ -82,16 +96,17 @@ func main() {
func run(c *cli.Context) error {
plugin := Plugin{
URL: c.String("url"),
Key: c.String("access-key"),
Secret: c.String("secret-key"),
Service: c.String("service"),
URL: c.String("url"),
Key: c.String("access-key"),
Secret: c.String("secret-key"),
Service: c.String("service"),
DockerImage: c.String("docker-image"),
StartFirst: c.BoolT("start-first"),
Confirm: c.Bool("confirm"),
Timeout: c.Int("timeout"),
YamlVerified: c.BoolT("yaml-verified"),
IntervalMillis: c.Int("interval-millis"),
BatchSize: c.Int("batch-size"),
YamlVerified: c.BoolT("yaml-verified"),
}
return plugin.Exec()
}
+40 -37
View File
@@ -1,30 +1,32 @@
package main
import (
"fmt"
"errors"
"strings"
"github.com/rancher/go-rancher/client"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/rancher/go-rancher/client"
"strings"
"time"
)
type Plugin struct {
URL string
Key string
Secret string
Service string
DockerImage string
StartFirst bool
Confirm bool
Timeout int
YamlVerified bool
URL string
Key string
Secret string
Service string
DockerImage string
StartFirst bool
Confirm bool
Timeout int
IntervalMillis int
BatchSize int
YamlVerified bool
}
func (p *Plugin) Exec() error {
log.Info("Drone Rancher Plugin built")
if (p.URL == "" || p.Key == "" || p.Secret == "") {
if p.URL == "" || p.Key == "" || p.Secret == "" {
return errors.New("Eek: Must have url, key, secret, and service definied")
}
@@ -41,7 +43,7 @@ func (p *Plugin) Exec() error {
}
rancher, err := client.NewRancherClient(&client.ClientOpts{
Url: p.URL,
Url: p.URL,
AccessKey: p.Key,
SecretKey: p.Secret,
})
@@ -83,9 +85,11 @@ func (p *Plugin) Exec() error {
service.LaunchConfig.ImageUuid = p.DockerImage
upgrade := &client.ServiceUpgrade{}
upgrade.InServiceStrategy = &client.InServiceUpgradeStrategy{
LaunchConfig: service.LaunchConfig,
LaunchConfig: service.LaunchConfig,
SecondaryLaunchConfigs: service.SecondaryLaunchConfigs,
StartFirst: p.StartFirst,
StartFirst: p.StartFirst,
IntervalMillis: p.IntervalMillis,
BatchSize: p.BatchSize,
}
upgrade.ToServiceStrategy = &client.ToServiceUpgradeStrategy{}
_, err = rancher.Service.ActionUpgrade(&service, upgrade)
@@ -94,32 +98,31 @@ func (p *Plugin) Exec() error {
}
log.Info(fmt.Sprintf("Upgraded %s to %s\n", p.Service, p.DockerImage))
if p.Confirm {
srv, err := retry(func() (interface{}, error) {
s, e := rancher.Service.ById(service.Id)
if e != nil {
return nil, e
}
if s.State != "upgraded" {
return nil, errors.New(fmt.Sprintf("Service not upgraded: %s", s.State))
}
return s, nil
}, time.Duration(p.Timeout)*time.Second, 3*time.Second)
if err != nil {
return errors.New(fmt.Sprintf("Error waiting for service upgrade to complete: %s", err))
if p.Confirm {
srv, err := retry(func() (interface{}, error) {
s, e := rancher.Service.ById(service.Id)
if e != nil {
return nil, e
}
_, err = rancher.Service.ActionFinishupgrade(srv.(*client.Service))
if err != nil {
return errors.New(fmt.Sprintf("Unable to finish upgrade %s: %s\n", p.Service, err))
if s.State != "upgraded" {
return nil, errors.New(fmt.Sprintf("Service not upgraded: %s", s.State))
}
log.Info(fmt.Printf("Finished upgrade %s\n", p.Service))
return s, nil
}, time.Duration(p.Timeout)*time.Second, 3*time.Second)
if err != nil {
return errors.New(fmt.Sprintf("Error waiting for service upgrade to complete: %s", err))
}
_, err = rancher.Service.ActionFinishupgrade(srv.(*client.Service))
if err != nil {
return errors.New(fmt.Sprintf("Unable to finish upgrade %s: %s\n", p.Service, err))
}
log.Info(fmt.Printf("Finished upgrade %s\n", p.Service))
}
return nil
}
type retryFunc func() (interface{}, error)
func retry(f retryFunc, timeout time.Duration, interval time.Duration) (interface{}, error) {
@@ -135,4 +138,4 @@ func retry(f retryFunc, timeout time.Duration, interval time.Duration) (interfac
case <-time.After(interval):
}
}
}
}