func (repo CloudControllerApplicationRepository) formatAppJSON(input cf.AppParams) (data string, apiResponse net.ApiResponse) { params := generic.NewMap() for _, allowedKey := range allowedAppKeys { if input.Has(allowedKey) { params.Set(allowedKey, input.Get(allowedKey)) } } if params.Has("command") && params.Get("command").(string) == "null" { params.Set("command", "") } else if params.Has("command") { params.Set("command", stringOrNull(params.Get("command"))) } if params.Has("buildpack") { params.Set("buildpack", stringOrNull(params.Get("buildpack"))) } if params.Has("stack_guid") { params.Set("stack_guid", stringOrNull(params.Get("stack_guid"))) } if params.Has("state") { params.Set("state", strings.ToUpper(params.Get("state").(string))) } if params.Has("name") { reg := regexp.MustCompile("^[0-9a-zA-Z\\-_]*$") if !reg.MatchString(params.Get("name").(string)) { apiResponse = net.NewApiResponseWithMessage("App name is invalid: name can only contain letters, numbers, underscores and hyphens") return } } vals := []string{} if !params.IsEmpty() { vals = append(vals, mapToJsonValues(params)...) } if input.Has("env") { envVars := input.Get("env").(generic.Map) if !envVars.IsEmpty() { envVal := fmt.Sprintf(`"environment_json":{%s}`, strings.Join(mapToJsonValues(envVars), ",")) vals = append(vals, envVal) } } data = fmt.Sprintf("{%s}", strings.Join(vals, ",")) return }
func (cmd *Push) fetchStackGuid(appParams cf.AppParams) { if !appParams.Has("stack") { return } stackName := appParams.Get("stack").(string) cmd.ui.Say("Using stack %s...", terminal.EntityNameColor(stackName)) stack, apiResponse := cmd.stackRepo.FindByName(stackName) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() appParams.Set("stack_guid", stack.Guid) }
func (cmd *Push) restart(app cf.Application, params cf.AppParams, c *cli.Context) { if app.State != "stopped" { cmd.ui.Say("") app, _ = cmd.stopper.ApplicationStop(app) } cmd.ui.Say("") if c.Bool("no-start") { return } if params.Has("health_check_timeout") { timeout := params.Get("health_check_timeout").(int) cmd.starter.SetStartTimeoutSeconds(timeout) } cmd.starter.ApplicationStart(app) }
func (cmd *Push) createAppSetFromContextAndManifest(c *cli.Context, contextParams cf.AppParams, rootAppPath string, m *manifest.Manifest) (appSet cf.AppSet, err error) { if contextParams.Has("name") && len(m.Applications) > 1 { err = errors.New("APP_NAME command line argument is not allowed when pushing multiple apps from a manifest file.") return } appSet = make([]cf.AppParams, 0, len(m.Applications)) if len(m.Applications) == 0 { if !contextParams.Has("name") || contextParams.Get("name") == "" { cmd.ui.FailWithUsage(c, "push") return } appSet = append(appSet, contextParams) } else { for _, manifestAppParams := range m.Applications { appFields := cf.NewAppParams(generic.Merge(manifestAppParams, contextParams)) path := rootAppPath if manifestAppParams.Has("path") { pathFromManifest := manifestAppParams.Get("path").(string) if filepath.IsAbs(pathFromManifest) { path = filepath.Clean(pathFromManifest) } else { path = filepath.Join(rootAppPath, pathFromManifest) } } appFields.Set("path", path) appSet = append(appSet, appFields) } } for _, appParams := range appSet { if !appParams.Has("name") { err = errors.New("app name is a required field") } } return }
func (cmd *Push) app(appParams cf.AppParams) (app cf.Application, didCreate bool) { if !appParams.Has("name") { cmd.ui.Failed("Error: No name found for app") return } appName := appParams.Get("name").(string) app, apiResponse := cmd.appRepo.Read(appName) if apiResponse.IsError() { cmd.ui.Failed(apiResponse.Message) return } if apiResponse.IsNotFound() { app, apiResponse = cmd.createApp(appParams) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } didCreate = true } return }
func (repo CloudControllerApplicationRepository) formatAppJSON(input cf.AppParams) (data string, apiResponse net.ApiResponse) { params := generic.NewMap() for _, allowedKey := range allowedAppKeys { if input.Has(allowedKey) { params.Set(allowedKey, input.Get(allowedKey)) } } if params.Has("buildpack") { params.Set("buildpack", stringOrNull(params.Get("buildpack"))) } if params.Has("stack_guid") { params.Set("stack_guid", stringOrNull(params.Get("stack_guid"))) } if params.Has("state") { params.Set("state", strings.ToUpper(params.Get("state").(string))) } vals := []string{} if !params.IsEmpty() { vals = append(vals, mapToJsonValues(params)...) } if input.Has("env") { envVars := input.Get("env").(generic.Map) if !envVars.IsEmpty() { envVal := fmt.Sprintf(`"environment_json":{%s}`, strings.Join(mapToJsonValues(envVars), ",")) vals = append(vals, envVal) } } data = fmt.Sprintf("{%s}", strings.Join(vals, ",")) return }
func (cmd *Push) bindAppToRoute(app cf.Application, params cf.AppParams, didCreateApp bool, c *cli.Context) { if c.Bool("no-route") { return } if params.Has("no-route") && params.Get("no-route") == true { cmd.ui.Say("App %s is a worker, skipping route creation", terminal.EntityNameColor(app.Name)) return } routeFlagsPresent := c.String("n") != "" || c.String("d") != "" || c.Bool("no-hostname") if len(app.Routes) > 0 && !routeFlagsPresent { return } if len(app.Routes) == 0 && didCreateApp == false && !routeFlagsPresent { cmd.ui.Say("App %s currently exists as a worker, skipping route creation", terminal.EntityNameColor(app.Name)) return } var defaultHostname string if params.Has("host") { defaultHostname = params.Get("host").(string) } else { defaultHostname = app.Name } var domainName string if params.Has("domain") { domainName = params.Get("domain").(string) } else { domainName = c.String("d") } hostName := cmd.hostname(c, defaultHostname) domain := cmd.domain(c, domainName) route := cmd.route(hostName, domain.DomainFields) for _, boundRoute := range app.Routes { if boundRoute.Guid == route.Guid { return } } cmd.ui.Say("Binding %s to %s...", terminal.EntityNameColor(domain.UrlForHost(hostName)), terminal.EntityNameColor(app.Name)) apiResponse := cmd.routeRepo.Bind(route.Guid, app.Guid) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("") }