Skip to content

Commit

Permalink
cleanup, better app log support for finding completed task logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gondor committed Dec 20, 2016
1 parent 0fa3066 commit ac1eb64
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 223 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ build/
.goxc.local.json
.DS_Store
samples/*
.vscode
.vscode
dist/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 0.9.2
VERSION = 0.9.3

GO_FMT = gofmt -s -w -l .
GO_XC = goxc -os="linux darwin windows" -tasks-="rmbin"
Expand Down
84 changes: 5 additions & 79 deletions commands/marathon/app_cmds.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package marathon

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -36,7 +34,8 @@ var appCmd = &cobra.Command{
var appCreateCmd = &cobra.Command{
Use: "create [file(.json | .yaml)]",
Short: "Create a new application with the [file(.json | .yaml)]",
Run: createApp,
Long: "Creates a new App in the cluster. This is an alias for the 'deploy create' command",
Run: deployAppOrGroup,
}

var appUpdateCmd = &cobra.Command{
Expand Down Expand Up @@ -134,84 +133,11 @@ func init() {
appCmd.AddCommand(appListCmd, appGetCmd, logCmd, appCreateCmd, appUpdateCmd, appDestroyCmd, appRollbackCmd, bgCmd, appRestartCmd, appScaleCmd, appVersionsCmd, appConvertFileCmd)

// Create Flags
appCreateCmd.Flags().String(TEMPLATE_CTX_FLAG, "", "Provides data per environment in JSON form to do a first pass parse of descriptor as template")
appCreateCmd.Flags().BoolP(FORCE_FLAG, "f", false, "Force deployment (updates application if it already exists)")
appCreateCmd.Flags().Bool(STOP_DEPLOYS_FLAG, false, "Stop an existing deployment for this app (if exists) and use this revision")
appCreateCmd.Flags().BoolP(IGNORE_MISSING, "i", false, `Ignore missing ${PARAMS} that are declared in app config that could not be resolved
CAUTION: This can be dangerous if some params define versions or other required information.`)
appCreateCmd.Flags().StringP(ENV_FILE_FLAG, "c", "", `Adds a file with a param(s) that can be used for substitution.
These take precidence over env vars`)
appCreateCmd.Flags().StringSliceP(PARAMS_FLAG, "p", nil, `Adds a param(s) that can be used for substitution.
eg. -p MYVAR=value would replace ${MYVAR} with "value" in the application file.
These take precidence over env vars`)

appCreateCmd.Flags().Bool(DRYRUN_FLAG, false, "Preview the parsed template - don't actually deploy")
addDeployCreateFlags(appCreateCmd)

appListCmd.Flags().String(FORMAT_FLAG, "", "Custom output format. Example: '{{range .Apps}}{{ .Container.Docker.Image }}{{end}}'")
appGetCmd.Flags().String(FORMAT_FLAG, "", "Custom output format. Example: '{{ .ID }}'")
applyCommonAppFlags(appCreateCmd, appUpdateCPUCmd, appUpdateMemoryCmd, appRollbackCmd, appDestroyCmd, appRestartCmd, appScaleCmd)
}

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)
applyCommonAppFlags(appUpdateCPUCmd, appUpdateMemoryCmd, appRollbackCmd, appDestroyCmd, appRestartCmd, appScaleCmd)
}

func exitWithError(err error) {
Expand Down
34 changes: 15 additions & 19 deletions commands/marathon/app_log_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
)

const (
STDERR_FLAG = "stderr"
FOLLOW_FLAG = "follow"
POLL_FLAG = "poll"
STDERR_FLAG = "stderr"
FOLLOW_FLAG = "follow"
POLL_FLAG = "poll"
COMPLETED_FLAG = "completed"
LATEST_FLAG = "latest"
)

var logCmd = &cobra.Command{
Expand All @@ -30,6 +32,9 @@ func init() {
logCmd.Flags().BoolP(STDERR_FLAG, "s", false, "Show StdErr vs default StdOut log")
logCmd.Flags().BoolP(FOLLOW_FLAG, "f", false, "Tail/Follow log")
logCmd.Flags().IntP(POLL_FLAG, "p", 5, "Log poll time (duration) in seconds")
logCmd.Flags().BoolP(COMPLETED_FLAG, "c", false, "Use completed tasks (default: running tasks)")
logCmd.Flags().BoolP(LATEST_FLAG, "l", false, "Use latest task (single) (default: all tasks)")

}

func showLogCmd(cmd *cobra.Command, args []string) {
Expand All @@ -39,12 +44,17 @@ func showLogCmd(cmd *cobra.Command, args []string) {

host := getMesosHost()
logType := ml.STDOUT
completedTasks, _ := cmd.Flags().GetBool(COMPLETED_FLAG)
latestTasks, _ := cmd.Flags().GetBool(LATEST_FLAG)

if stderr, _ := cmd.Flags().GetBool(STDERR_FLAG); stderr {
logType = ml.STDERR
}

c, _ := ml.NewMesosClient(host, 5050)
c, _ := ml.NewMesosClientWithOptions(host, 5050, &ml.MesosClientOptions{
SearchCompletedTasks: completedTasks,
ShowLatestOnly: latestTasks,
})
appId := getMesosAppIdentifier(cmd, c, args[0])

if follow, _ := cmd.Flags().GetBool(FOLLOW_FLAG); follow {
Expand Down Expand Up @@ -77,21 +87,7 @@ func showLogCmd(cmd *cobra.Command, args []string) {
}

func getMesosAppIdentifier(cmd *cobra.Command, c *ml.MesosClient, appId string) string {
tasks, err := client(cmd).GetTasks(appId)
if err != nil {
log.Fatal(err)
}

if len(tasks) > 0 {
name, err := c.GetAppNameForTaskID(tasks[0].ID)
if err != nil {
log.Fatal(err)
}
return name
}

log.Fatal("Currently no tasks found for application: %s\n", appId)
return ""
return c.GetAppNameForPath(appId)
}

func getMesosHost() string {
Expand Down
26 changes: 15 additions & 11 deletions commands/marathon/deploy_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,28 @@ the other for delegation to the origin (app or group)
}

func init() {
addDeployCreateFlags(deployCreateCmd)
deployDeleteCmd.Flags().BoolP(FORCE_FLAG, "f", false, "If set to true, then the deployment is still canceled but no rollback deployment is created.")
deployCmd.AddCommand(deployCreateCmd, deployListCmd, deployDeleteCmd, deleteIfDeployingCmd)
}

deployCreateCmd.Flags().BoolP(WAIT_FLAG, "w", false, "Wait for group to become healthy")
deployCreateCmd.Flags().String(TEMPLATE_CTX_FLAG, DEFAULT_CTX, "Provides data per environment in JSON form to do a first pass parse of descriptor as template")
deployCreateCmd.Flags().BoolP(FORCE_FLAG, "f", false, "Force deployment (updates application if it already exists)")
deployCreateCmd.Flags().Bool(STOP_DEPLOYS_FLAG, false, "Stop an existing deployment for this app (if exists) and use this revision")
deployCreateCmd.Flags().BoolP(IGNORE_MISSING, "i", false, `Ignore missing ${PARAMS} that are declared in app config that could not be resolved
func addDeployCreateFlags(cmd *cobra.Command) {
cmd.Flags().BoolP(WAIT_FLAG, "w", false, "Wait for deployment to become healthy")
cmd.Flags().String(TEMPLATE_CTX_FLAG, DEFAULT_CTX, "Provides data per environment in JSON form to do a first pass parse of descriptor as template")
cmd.Flags().BoolP(FORCE_FLAG, "f", false, "Force deployment (updates application if it already exists)")
cmd.Flags().Bool(STOP_DEPLOYS_FLAG, false, "Stop an existing deployment for this app (if exists) and use this revision")
cmd.Flags().BoolP(IGNORE_MISSING, "i", false, `Ignore missing ${PARAMS} that are declared in app config that could not be resolved
CAUTION: This can be dangerous if some params define versions or other required information.`)
deployCreateCmd.Flags().StringP(ENV_FILE_FLAG, "c", "", `Adds a file with a param(s) that can be used for substitution.
cmd.Flags().StringP(ENV_FILE_FLAG, "c", "", `Adds a file with a param(s) that can be used for substitution.
These take precidence over env vars`)
deployCreateCmd.Flags().StringSliceP(PARAMS_FLAG, "p", nil, `Adds a param(s) that can be used for substitution.
cmd.Flags().StringSliceP(PARAMS_FLAG, "p", nil, `Adds a param(s) that can be used for substitution.
eg. -p MYVAR=value would replace ${MYVAR} with "value" in the application file.
These take precidence over env vars`)

deployCreateCmd.Flags().Bool(DRYRUN_FLAG, false, "Preview the parsed template - don't actually deploy")
cmd.Flags().Bool(DRYRUN_FLAG, false, "Preview the parsed template - don't actually deploy")

cmd.Flags().DurationP(TIMEOUT_FLAG, "t", time.Duration(0), "Max duration to wait for application health (ex. 90s | 2m). See docs for ordering")

deployCreateCmd.Flags().DurationP(TIMEOUT_FLAG, "t", time.Duration(0), "Max duration to wait for application health (ex. 90s | 2m). See docs for ordering")
deployDeleteCmd.Flags().BoolP(FORCE_FLAG, "f", false, "If set to true, then the deployment is still canceled but no rollback deployment is created.")
deployCmd.AddCommand(deployCreateCmd, deployListCmd, deployDeleteCmd, deleteIfDeployingCmd)
}

func deployAppOrGroup(cmd *cobra.Command, args []string) {
Expand Down
79 changes: 3 additions & 76 deletions commands/marathon/group_cmds.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package marathon

import (
"bytes"
"fmt"
"github.com/ContainX/depcon/marathon"
"github.com/ContainX/depcon/pkg/cli"
"github.com/ContainX/depcon/pkg/encoding"
"github.com/spf13/cobra"
"os"
"strings"
"time"
)

var groupCmd = &cobra.Command{
Expand Down Expand Up @@ -41,7 +38,8 @@ var groupDestroyCmd = &cobra.Command{
var groupCreateCmd = &cobra.Command{
Use: "create [file(.json | .yaml)]",
Short: "Create a new Group with the [file(.json | .yaml)]",
Run: createGroup,
Long: "Creates a new Group in the cluster. This is an alias for the 'deploy create' command",
Run: deployAppOrGroup,
}

var groupConvertFileCmd = &cobra.Command{
Expand All @@ -56,20 +54,7 @@ func init() {
// Destroy Flags
groupDestroyCmd.Flags().BoolP(WAIT_FLAG, "w", false, "Wait for destroy to complete")
// Create Flags
groupCreateCmd.Flags().String(TEMPLATE_CTX_FLAG, DEFAULT_CTX, "Provides data per environment in JSON form to do a first pass parse of descriptor as template")
groupCreateCmd.Flags().BoolP(WAIT_FLAG, "w", false, "Wait for group to become healthy")
groupCreateCmd.Flags().Bool(STOP_DEPLOYS_FLAG, false, "Stop an existing deployment for this group (if exists) and use this revision")
groupCreateCmd.Flags().BoolP(FORCE_FLAG, "f", false, "Force deployment (updates group if it already exists)")
groupCreateCmd.Flags().DurationP(TIMEOUT_FLAG, "t", time.Duration(0), "Max duration to wait for application health (ex. 90s | 2m). See docs for ordering")

groupCreateCmd.Flags().BoolP(IGNORE_MISSING, "i", false, `Ignore missing ${PARAMS} that are declared in app config that could not be resolved
CAUTION: This can be dangerous if some params define versions or other required information.`)
groupCreateCmd.Flags().StringSliceP(PARAMS_FLAG, "p", nil, `Adds a param(s) that can be used for substitution.
eg. -p MYVAR=value would replace ${MYVAR} with "value" in the application file.
These take precidence over env vars and params in file`)

groupCreateCmd.Flags().Bool(DRYRUN_FLAG, false, "Preview the parsed template - don't actually deploy")

addDeployCreateFlags(groupCreateCmd)
}

func listGroups(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -101,64 +86,6 @@ func destroyGroup(cmd *cobra.Command, args []string) {
cli.Output(templateFor(T_DEPLOYMENT_ID, v), 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 flattenGroup(g *marathon.Group, arr []*marathon.Group) []*marathon.Group {
arr = append(arr, g)
for _, cg := range g.Groups {
Expand Down
36 changes: 1 addition & 35 deletions commands/marathon/templatectx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"io/ioutil"
"os"
"reflect"
"text/template"

"github.com/ContainX/depcon/pkg/encoding"
Expand All @@ -18,40 +17,7 @@ const (
)

// Templated based Functions
var Funcs = template.FuncMap{
"default": func(args ...interface{}) interface{} {
arg := args[0]
if len(args) < 2 {
return arg
}
value := args[1]

defer recovery()

v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
if v.Len() == 0 {
return arg
}
case reflect.Bool:
if !v.Bool() {
return arg
}
default:
return value
}

return value
},
"isEnv": func(value string) bool {
if len(value) > 0 {
current := strings.ToLower(viper.GetString(ENV_NAME))
return current == strings.ToLower(value)
}
return false
},
}
var Funcs = FuncMap()

type TemplateContext struct {
Environments map[string]*TemplateEnvironment `json:"environments,omitempty"`
Expand Down
Loading

0 comments on commit ac1eb64

Please sign in to comment.