func deployAppOrGroup(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } filename := args[0] wait, _ := cmd.Flags().GetBool(WAIT_FLAG) force, _ := cmd.Flags().GetBool(FORCE_FLAG) paramsFile, _ := cmd.Flags().GetString(ENV_FILE_FLAG) params, _ := cmd.Flags().GetStringSlice(PARAMS_FLAG) ignore, _ := cmd.Flags().GetBool(IGNORE_MISSING) stop_deploy, _ := cmd.Flags().GetBool(STOP_DEPLOYS_FLAG) tempctx, _ := cmd.Flags().GetString(TEMPLATE_CTX_FLAG) dryrun, _ := cmd.Flags().GetBool(DRYRUN_FLAG) options := &marathon.CreateOptions{Wait: wait, Force: force, ErrorOnMissingParams: !ignore, StopDeploy: stop_deploy, DryRun: dryrun} descriptor := parseDescriptor(tempctx, filename) et, err := encoding.NewEncoderFromFileExt(filename) if err != nil { exitWithError(err) } ag := &marathon.AppOrGroup{} if err := et.UnMarshalStr(descriptor, ag); err != nil { exitWithError(err) } if paramsFile != "" { envParams, _ := parseParamsFile(paramsFile) options.EnvParams = envParams } else { options.EnvParams = make(map[string]string) } if params != nil { for _, p := range params { if strings.Contains(p, "=") { v := strings.Split(p, "=") options.EnvParams[v[0]] = v[1] } } } if ag.IsApplication() { result, e := client(cmd).CreateApplicationFromString(filename, descriptor, options) outputDeployment(result, e) cli.Output(templateFor(T_APPLICATION, result), e) } else { result, e := client(cmd).CreateGroupFromString(filename, descriptor, options) outputDeployment(result, e) if e != nil { cli.Output(nil, e) } arr := flattenGroup(result, []*marathon.Group{}) cli.Output(templateFor(T_GROUPS, arr), e) } }
func createGroup(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } wait, _ := cmd.Flags().GetBool(WAIT_FLAG) force, _ := cmd.Flags().GetBool(FORCE_FLAG) params, _ := cmd.Flags().GetStringSlice(PARAMS_FLAG) ignore, _ := cmd.Flags().GetBool(IGNORE_MISSING) stop_deploy, _ := cmd.Flags().GetBool(STOP_DEPLOYS_FLAG) dryrun, _ := cmd.Flags().GetBool(DRYRUN_FLAG) tempctx, _ := cmd.Flags().GetString(TEMPLATE_CTX_FLAG) options := &marathon.CreateOptions{Wait: wait, Force: force, ErrorOnMissingParams: !ignore, StopDeploy: stop_deploy, DryRun: dryrun} if params != nil { envmap := make(map[string]string) for _, p := range params { if strings.Contains(p, "=") { v := strings.Split(p, "=") envmap[v[0]] = v[1] } } options.EnvParams = envmap } var result *marathon.Group = nil var e error if TemplateExists(tempctx) { b := &bytes.Buffer{} r, err := LoadTemplateContext(tempctx) if err != nil { exitWithError(err) } if err := r.Transform(b, args[0]); err != nil { exitWithError(err) } result, e = client(cmd).CreateGroupFromString(args[0], b.String(), options) } else { result, e = client(cmd).CreateGroupFromFile(args[0], options) } if e != nil { if e == marathon.ErrorGroupExists { cli.Output(nil, fmt.Errorf("%s, consider using the --force flag to update when group exists", e.Error())) } else { cli.Output(nil, e) } return } arr := flattenGroup(result, []*marathon.Group{}) cli.Output(templateFor(T_GROUPS, arr), e) }
func deployBlueGreenCmd(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } a, err := bgc(cmd).DeployBlueGreenFromFile(args[0]) if err != nil { cli.Output(nil, err) os.Exit(1) } cli.Output(templateFor(T_APPLICATION, a), err) }
func scaleApp(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 2) { os.Exit(1) } instances, err := strconv.Atoi(args[1]) if err != nil { cli.Output(nil, err) os.Exit(1) } v, e := client(cmd).ScaleApplication(args[0], instances) cli.Output(templateFor(T_DEPLOYMENT_ID, v), e) waitForDeploymentIfFlagged(cmd, v.DeploymentID) }
func destroyGroup(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } v, e := client(cmd).DestroyGroup(args[0]) cli.Output(templateFor(T_DEPLOYMENT_ID, v), e) }
func updateAppMemory(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 2) { os.Exit(1) } wait, _ := cmd.Flags().GetBool(WAIT_FLAG) mem, err := strconv.ParseFloat(args[1], 64) if err != nil { cli.Output(nil, err) os.Exit(1) } update := marathon.NewApplication(args[0]).Memory(mem) v, e := client(cmd).UpdateApplication(update, wait) cli.Output(templateFor(T_APPLICATION, v), e) }
func appKillTask(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } scale, _ := cmd.Flags().GetBool(SCALE_FLAG) v, e := client(cmd).KillAppTask(args[0], scale) cli.Output(templateFor(T_TASK, v), e) }
func destroyApp(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { os.Exit(1) } v, e := client(cmd).DestroyApplication(args[0]) cli.Output(templateFor(T_DEPLOYMENT_ID, v), e) waitForDeploymentIfFlagged(cmd, v.DeploymentID) }
func getGroup(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } v, e := client(cmd).GetGroup(args[0]) arr := flattenGroup(v, []*marathon.Group{}) cli.Output(templateFor(T_GROUPS, arr), e) }
func listGroups(cmd *cobra.Command, args []string) { v, e := client(cmd).ListGroups() arr := []*marathon.Group{} for _, group := range v.Groups { arr = flattenGroup(group, arr) } cli.Output(templateFor(T_GROUPS, arr), e) }
func convertFile(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 2) { os.Exit(1) } if err := encoding.ConvertFile(args[0], args[1], &marathon.Application{}); err != nil { cli.Output(nil, err) os.Exit(1) } fmt.Printf("Source file %s has been re-written into new format in %s\n\n", args[0], args[1]) }
func restartApp(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { os.Exit(1) } force, _ := cmd.Flags().GetBool(FORCE_FLAG) v, e := client(cmd).RestartApplication(args[0], force) cli.Output(templateFor(T_DEPLOYMENT_ID, v), e) waitForDeploymentIfFlagged(cmd, v.DeploymentID) }
func appTasks(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } detailed, _ := cmd.Flags().GetBool(DETAIL_FLAG) v, e := client(cmd).GetTasks(args[0]) if detailed && e == nil { fmt.Println("") for _, t := range v { fmt.Printf("::: Task: %s\n\n", t.ID) cli.Output(templateFor(T_TASKS, v), e) } } else { cli.Output(templateFor(T_TASKS, v), e) } }
func execActionWithPreHook(action ComposeAction, preHook ComposePreHook) func(cmd *cobra.Command, args []string) { return func(cmd *cobra.Command, args []string) { composeFile, _ := cmd.Flags().GetString(COMPOSE_FILE_FLAG) projName, _ := cmd.Flags().GetString(PROJECT_NAME_FLAG) compose := preHook(composeFile, projName, cmd) err := action(compose, cmd, args) if err != nil { cli.Output(nil, err) } } }
func rollbackAppVersion(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { os.Exit(1) } wait, _ := cmd.Flags().GetBool(WAIT_FLAG) version := "" if len(args) > 1 { version = args[1] } else { versions, e := client(cmd).ListVersions(args[0]) if e == nil && len(versions.Versions) > 1 { version = versions.Versions[1] } } update := marathon.NewApplication(args[0]).RollbackVersion(version) v, e := client(cmd).UpdateApplication(update, wait) cli.Output(templateFor(T_APPLICATION, v), e) }
) var serverCmd = &cobra.Command{ Use: "server", Short: "Marathon server information", Long: `View Marathon server information and current leader See server's subcommands for available choices`, } var serverInfoCmd = &cobra.Command{ Use: "info", Short: "Get info about the Marathon Instance", Run: func(cmd *cobra.Command, args []string) { v, e := client(cmd).GetMarathonInfo() cli.Output(templateFor(T_MARATHON_INFO, v), e) }, } var serverLeaderCmd = &cobra.Command{ Use: "leader", Short: "Marathon leader management", } var serverLeaderGetCmd = &cobra.Command{ Use: "get", Short: "Show the current leader", Run: func(cmd *cobra.Command, args []string) { v, e := client(cmd).GetCurrentLeader() cli.Output(templateFor(T_LEADER_INFO, v), e) },
Short: "Configuration environments define remote Marathon and other supported services by a name. ", Long: `Manage configuration environments (eg. default, list, adding and removing of environments) See env's subcommands for available choices`, } var configRemoveCmd = &cobra.Command{ Use: "delete [name]", Short: "Remove a defined environment by it's [name]", Run: func(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } _, err := configFile.GetEnvironment(args[0]) if err != nil { cli.Output(nil, err) } else { err := configFile.RemoveEnvironment(args[0], false) if err != nil { cli.Output(nil, err) } } }, } var configAddCmd = &cobra.Command{ Use: "add", Short: "Adds a new environment (cli prompts)", Run: func(cmd *cobra.Command, args []string) { configFile.AddEnvironment() },
) var taskCmd = &cobra.Command{ Use: "task", Short: "Marathon task management", Long: `Manage tasks in a marathon cluster (eg. creating, listing, monitoring, kill) See tasks's subcommands for available choices`, } var taskListCmd = &cobra.Command{ Use: "list", Short: "List all tasks", Run: func(cmd *cobra.Command, args []string) { v, e := client(cmd).ListTasks() cli.Output(templateFor(T_TASKS, v), e) }, } var taskQueueCmd = &cobra.Command{ Use: "queue", Short: "List all queued tasks", Run: func(cmd *cobra.Command, args []string) { v, e := client(cmd).ListQueue() cli.Output(templateFor(T_QUEUED_TASKS, v), e) }, } var appTaskGetCmd = &cobra.Command{ Use: "get [applicationId]", Short: "List tasks for the application [applicationId]",
func exitWithError(err error) { cli.Output(nil, err) os.Exit(1) }
func createApp(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } wait, _ := cmd.Flags().GetBool(WAIT_FLAG) force, _ := cmd.Flags().GetBool(FORCE_FLAG) paramsFile, _ := cmd.Flags().GetString(ENV_FILE_FLAG) params, _ := cmd.Flags().GetStringSlice(PARAMS_FLAG) ignore, _ := cmd.Flags().GetBool(IGNORE_MISSING) stop_deploy, _ := cmd.Flags().GetBool(STOP_DEPLOYS_FLAG) tempctx, _ := cmd.Flags().GetString(TEMPLATE_CTX_FLAG) dryrun, _ := cmd.Flags().GetBool(DRYRUN_FLAG) options := &marathon.CreateOptions{Wait: wait, Force: force, ErrorOnMissingParams: !ignore, StopDeploy: stop_deploy, DryRun: dryrun} if paramsFile != "" { envParams, _ := parseParamsFile(paramsFile) options.EnvParams = envParams } else { options.EnvParams = make(map[string]string) } if params != nil { for _, p := range params { if strings.Contains(p, "=") { v := strings.Split(p, "=") options.EnvParams[v[0]] = v[1] } } } var result *marathon.Application = nil var e error if TemplateExists(tempctx) { b := &bytes.Buffer{} r, err := LoadTemplateContext(tempctx) if err != nil { exitWithError(err) } if err := r.Transform(b, args[0]); err != nil { exitWithError(err) } result, e = client(cmd).CreateApplicationFromString(args[0], b.String(), options) } else { result, e = client(cmd).CreateApplicationFromFile(args[0], options) } if e != nil && e == marathon.ErrorAppExists { exitWithError(errors.New(fmt.Sprintf("%s, consider using the --force flag to update when an application exists", e.Error()))) } if result == nil { if e != nil { fmt.Printf("[ERROR] %s\n", e.Error()) } os.Exit(1) } cli.Output(templateFor(T_APPLICATION, result), e) }
Use: "mem [applicationId] [amount]", Short: "Updates [applicationId] to have [amount] of memory in MB", Run: updateAppMemory, } var appListCmd = &cobra.Command{ Use: "list (optional filtering - label=mylabel | id=/services | cmd=java ...)", Short: "List all applications", Run: func(cmd *cobra.Command, args []string) { filter := "" if len(args) > 0 { filter = args[0] } v, e := client(cmd).ListApplicationsWithFilters(filter) cli.Output(templateFor(templateFormat(T_APPLICATIONS, cmd), v), e) }, } var appGetCmd = &cobra.Command{ Use: "get [applicationId]", Short: "Gets an application details by Id", Long: `Retrieves the specified [appliationId] application`, Run: func(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } v, e := client(cmd).GetApplication(args[0]) cli.Output(templateFor(templateFormat(T_APPLICATION, cmd), v), e) }, }
) var deployCmd = &cobra.Command{ Use: "deploy", Short: "Marathon deployment management", Long: `Manage deployments in a marathon cluster (eg. creating, listing, monitoring) See deploy's subcommands for available choices`, } var deployListCmd = &cobra.Command{ Use: "list", Short: "List all deployments", Run: func(cmd *cobra.Command, args []string) { v, e := client(cmd).ListDeployments() cli.Output(templateFor(T_DEPLOYMENTS, v), e) }, } var deployDeleteCmd = &cobra.Command{ Use: "delete [deploymentId]", Short: "Delete a deployment by [deploymentID]", Run: func(cmd *cobra.Command, args []string) { if cli.EvalPrintUsage(Usage(cmd), args, 1) { return } force, _ := cmd.Flags().GetBool(FORCE_FLAG) v, e := client(cmd).DeleteDeployment(args[0], force) cli.Output(templateFor(T_DEPLOYMENT_ID, v), e) },