func (p TarballBuild) Run(data manifest.Manifest) error { utils.RunCmd("rm -rf ./tarball.tmp && mkdir ./tarball.tmp") for _, f := range data.GetArray("files") { if file, ok := f.Unwrap().(string); ok { utils.RunCmd("cp -a %s ./tarball.tmp/", file) } else if files, ok := f.Unwrap().(map[string]interface{}); ok { for from, to := range files { utils.RunCmd("cp -a %s ./tarball.tmp/%s", from, to) } } } if err := utils.RunCmd("tar -zcf tarball.tar.gz -C ./tarball.tmp/ ."); err != nil { return err } return utils.RunCmd("curl -vsSf -XPUT -T tarball.tar.gz %s", data.GetString("registry-url")) }
func (p DeployTarball) Install(data manifest.Manifest) error { tmp := "tarball-" + utils.RandomString(16) dest := data.GetString("install-root") + "/" + data.GetString("package-name") hooks := make([]string, 0) for _, hook := range data.GetArray("hooks") { if hook.Has("postinstall") { hooks = append(hooks, "sudo "+hook.GetString("postinstall")) } } hookCmd := strings.Join(hooks, " && ") if hookCmd != "" { hookCmd = " && " + hookCmd } /** * todo: register package in consul services catalog */ if err := utils.RunParallelSshCmd( data.GetString("cluster"), data.GetString("ssh-user"), fmt.Sprint( "curl -vsSf -o /tmp/"+tmp+".tar.gz "+data.GetString("package-uri"), " && rm -rf /tmp/"+tmp+"/", " && mkdir -p /tmp/"+tmp+"/", " && tar xzf /tmp/"+tmp+".tar.gz -C /tmp/"+tmp+"/", " && sudo rm -rf "+dest, " && sudo mkdir -p "+dest, " && sudo mv /tmp/"+tmp+"/* "+dest+"/", " && sudo chown -R "+data.GetString("user")+":"+data.GetString("group")+" "+dest+"/", " && rm -rf /tmp/"+tmp+".tar.gz /tmp/"+tmp+"/", hookCmd, ), 50, ); err != nil { return err } return utils.RegisterPluginData("deploy.tarball", data.GetString("package-name"), data.String(), data.GetString("consul-address")) }
func (p DeployMarathon) Install(data manifest.Manifest) error { marathonApi, err := MarathonClient(data.GetString("marathon-address")) if err != nil { return err } fullName := data.GetString("app-name") bs, bf, bmax, grace := 5.0, 2.0, 120.0, 30.0 app := &marathon.Application{ BackoffSeconds: &bs, BackoffFactor: &bf, MaxLaunchDelaySeconds: &bmax, TaskKillGracePeriodSeconds: &grace, UpgradeStrategy: &marathon.UpgradeStrategy{ MinimumHealthCapacity: 0.5, // rolling update MaximumOverCapacity: 0.0, }, } portArgs := "" if port := data.GetString("listen-port"); port != "" { portArgs = "--port " + port } app.Name(fullName) app.Command(fmt.Sprintf("exec serve-tools consul supervisor --service '%s' %s start %s", fullName, portArgs, data.GetString("cmd"))) app.Count(data.GetInt("instances")) app.Memory(float64(data.GetInt("mem"))) if cpu, err := strconv.ParseFloat(data.GetString("cpu"), 64); err == nil { app.CPU(cpu) } if cluster := data.GetString("cluster"); cluster != "" { cs := strings.SplitN(cluster, ":", 2) app.AddConstraint(cs[0], "CLUSTER", cs[1]) app.AddLabel(cs[0], cs[1]) } for _, cons := range data.GetArray("constraints") { if consArr, ok := cons.Unwrap().([]interface{}); ok { consStrings := make([]string, len(consArr)) for i, c := range consArr { consStrings[i] = fmt.Sprintf("%s", c) } app.AddConstraint(consStrings...) } } for _, port := range data.GetArray("ports") { app.AddPortDefinition(marathon.PortDefinition{Name: port.GetStringOr("name", "")}.SetPort(port.GetIntOr("port", 0))) } app.AddEnv("SERVICE_DEPLOY_TIME", time.Now().Format(time.RFC3339)) // force redeploy app for k, v := range data.GetMap("environment") { app.AddEnv(k, fmt.Sprintf("%s", v.Unwrap())) } app.AddUris(data.GetString("package-uri")) // todo: в манифесте задавать массив healthchecks, их использовтаь в марафоне и консул-супервизоре // todo: открыть сетевой доступ от марафона до мезос-агентов, чтобы марафон мог хелсчеки посылать //if portArgs != "" { // health := marathon.NewDefaultHealthCheck() // health.Protocol = "TCP" // health.IntervalSeconds = 5 // *health.PortIndex = 0 // app.AddHealthCheck(*health) //} if _, err := marathonApi.UpdateApplication(app, false); err != nil { color.Yellow("marathon <- %s", app) return err } color.Green("marathon <- %s", app) consulApi, err := utils.ConsulClient(data.GetString("consul-address")) if err != nil { return err } if err := utils.RegisterPluginData("deploy.marathon", data.GetString("app-name"), data.String(), data.GetString("consul-address")); err != nil { return err } return backoff.Retry(func() error { services, _, err := consulApi.Health().Service(fullName, "", true, nil) if err != nil { log.Println(color.RedString("Error in check health in consul: %v", err)) return err } if len(services) == 0 { log.Printf("Service `%s` not started yet! Retry...", fullName) return fmt.Errorf("Service `%s` not started!", fullName) } log.Println(color.GreenString("Service `%s` successfully started!", fullName)) return nil }, backoff.NewExponentialBackOff()) }
func (p ReleaseHttp) Run(data manifest.Manifest) error { if !data.Has("routes") { log.Println("No routes configured for release.") return nil } consul, err := utils.ConsulClient(data.GetString("consul-address")) if err != nil { return err } fullName := data.GetString("full-name") // check current service is alive and healthy if err := backoff.Retry(func() error { services, _, err := consul.Health().Service(fullName, "", true, nil) if err != nil { log.Println(color.RedString("Error in check health in consul: %v", err)) return err } if len(services) == 0 { log.Printf("Service `%s` not started yet! Retry...", fullName) return fmt.Errorf("Service `%s` not started!", fullName) } else { log.Printf("Service `%s` started with %v instances.", fullName, len(services)) return nil } }, backoff.NewExponentialBackOff()); err != nil { return err } routeVars := make(map[string]string, 0) if data.Has("route") { if err := json.Unmarshal([]byte(data.GetString("route")), &routeVars); err != nil { log.Println(color.RedString("Error parse route json: %v, %s", err, data.GetString("route"))) return err } } // collect routes routes := make([]map[string]string, 0) for _, route := range data.GetArray("routes") { if !route.Has("host") { log.Printf("Not found 'host': %s, skip...", route.String()) continue } fields := make(map[string]string) for k, v := range route.Unwrap().(map[string]interface{}) { fields[k] = fmt.Sprintf("%v", v) } routes = append(routes, utils.MergeMaps(fields, routeVars)) } if len(routes) == 0 { log.Println("No routes configured for release.") return nil } routesJson, err := json.MarshalIndent(routes, "", " ") if err != nil { return err } // write routes to consul kv if err := utils.PutConsulKv(consul, "services/routes/"+fullName, string(routesJson)); err != nil { return err } log.Println(color.GreenString("Service `%s` released with routes: %s", fullName, string(routesJson))) // find old services with the same routes existsRoutes, err := utils.ListConsulKv(consul, "services/routes/"+data.GetString("name-prefix"), nil) if err != nil { return err } for _, existsRoute := range existsRoutes { if existsRoute.Key != fmt.Sprintf("services/routes/%s", fullName) { // skip current service oldRoutes := make([]map[string]string, 0) if err := json.Unmarshal(existsRoute.Value, &oldRoutes); err != nil { return err } OuterLoop: for _, route := range routes { for _, oldRoute := range oldRoutes { if utils.MapsEqual(route, oldRoute) { outdated := strings.TrimPrefix(existsRoute.Key, "services/routes/") log.Println(color.GreenString("Found %s with the same routes %v. Remove it!", outdated, string(existsRoute.Value))) if err := utils.DelConsulKv(consul, existsRoute.Key); err != nil { return err } if err := utils.MarkAsOutdated(consul, outdated, 10*time.Minute); err != nil { return err } break OuterLoop } } } } } return nil }