From 3a070a27b40200b6cd28cb644f93174df858445a Mon Sep 17 00:00:00 2001 From: James Nesbitt Date: Mon, 7 Mar 2016 09:20:50 +0200 Subject: [PATCH 1/2] some more up operation message refinements --- operation/operation_up.go | 43 +++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/operation/operation_up.go b/operation/operation_up.go index 3487d97..3fc249f 100644 --- a/operation/operation_up.go +++ b/operation/operation_up.go @@ -73,6 +73,8 @@ func (operation *UpOperation) Run(logger log.Log) bool { instances, hasInstances := target.Instances() nodeLogger := logger.MakeChild(targetID) + build := node.Can("build") + pull := node.Can("pull") create := node.Can("create") start := node.Can("start") @@ -84,14 +86,21 @@ func (operation *UpOperation) Run(logger log.Log) bool { nodeLogger.Message("Bringing node up") nodeClient := node.Client() - - if node.Can("build") && (operation.force || !nodeClient.HasImage()) { - nodeLogger.Message("Building node image") - nodeClient.Build(nodeLogger, operation.force) + if build { + if operation.force || !nodeClient.HasImage() { + nodeLogger.Message("Building node image") + nodeClient.Build(nodeLogger, operation.force) + } else { + nodeLogger.Info("Node already has an image built") + } } - if node.Can("pull") && (operation.force || !nodeClient.HasImage()) { - nodeLogger.Message("Pulling node image") - nodeClient.Pull(nodeLogger, operation.force) + if pull { + if operation.force || !nodeClient.HasImage() { + nodeLogger.Message("Pulling node image") + nodeClient.Pull(nodeLogger, operation.force) + } else { + nodeLogger.Info("Node already has an image pulled") + } } if hasInstances && (create || start) { @@ -99,13 +108,21 @@ func (operation *UpOperation) Run(logger log.Log) bool { instance, _ := instances.Instance(id) instanceClient := instance.Client() - if create && !instanceClient.HasContainer() { - nodeLogger.Message("Creating node instance container : " + id) - instanceClient.Create(nodeLogger, []string{}, operation.force) + if create { + if operation.force || !instanceClient.HasContainer() { + nodeLogger.Message("Creating node instance container : " + id) + instanceClient.Create(nodeLogger, []string{}, operation.force) + } else { + nodeLogger.Info("Instance already has an container created : "+id) + } } - if start && !instanceClient.IsRunning() { - nodeLogger.Message("Starting node instance container : " + id) - instanceClient.Start(nodeLogger, operation.force) + if start { + if operation.force || !instanceClient.IsRunning() { + nodeLogger.Message("Starting node instance container : " + id) + instanceClient.Start(nodeLogger, operation.force) + } else { + nodeLogger.Info("Instance already has an container running : "+id) + } } } } From bfdf9da961a2908ff3d2c550b8c06bd35d4e6cbc Mon Sep 17 00:00:00 2001 From: James Nesbitt Date: Mon, 7 Mar 2016 12:18:03 +0200 Subject: [PATCH 2/2] Added new restart operation --- operation/operation.go | 3 + operation/operation_restart.go | 101 +++++++++++++++++++++++++++++++++ operation/operation_start.go | 2 +- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 operation/operation_restart.go diff --git a/operation/operation.go b/operation/operation.go index 3fb319c..44792d4 100644 --- a/operation/operation.go +++ b/operation/operation.go @@ -38,6 +38,8 @@ func MakeOperation(logger log.Log, project *conf.Project, name string, flags []s operation = Operation(&StartOperation{log: opLogger, targets: targets}) case "stop": operation = Operation(&StopOperation{log: opLogger, targets: targets}) + case "restart": + operation = Operation(&RestartOperation{log: opLogger, targets: targets}) case "scale": operation = Operation(&ScaleOperation{log: opLogger, targets: targets}) case "pause": @@ -134,6 +136,7 @@ func ListOperations() []string { "scale", "create", "remove", + "restart", "start", "status", "stop", diff --git a/operation/operation_restart.go b/operation/operation_restart.go new file mode 100644 index 0000000..51c4ece --- /dev/null +++ b/operation/operation_restart.go @@ -0,0 +1,101 @@ +package operation + +import ( + "github.com/james-nesbitt/coach/libs" + "github.com/james-nesbitt/coach/log" +) + +type RestartOperation struct { + log log.Log + targets *libs.Targets + + force bool + timeout uint +} + +func (operation *RestartOperation) Id() string { + return "Restart" +} +func (operation *RestartOperation) Flags(flags []string) bool { + for _, flag := range flags { + switch flag { + case "-f": + fallthrough + case "--force": + operation.force = true + + + case "-q": + fallthrough + case "--quick": + operation.timeout = 1 + } + } + return true +} + +func (operation *RestartOperation) Help(topics []string) { + operation.log.Message(`Operation: Restart + +Coach will attempt to Restart target node containers. + +SYNTAX: + $/> coach {targets} Restart + + $/> coach {targets} Restart--quick + - makes docker stop the containers with --time=1 + + {targets} what target node instances the operation should process ($/> coach help targets) + +ACCESS: + - This operation processed only nodes with the "Restart" access. This excludes build, volume and command containers. +`) +} +func (operation *RestartOperation) Run(logger log.Log) bool { + logger.Info("Running operation: Restart") + logger.Debug(log.VERBOSITY_DEBUG, "Run:Targets", operation.targets.TargetOrder()) + + for _, targetID := range operation.targets.TargetOrder() { + target, targetExists := operation.targets.Target(targetID) + if !targetExists { + // this is strange + logger.Warning("Internal target error, was told to use a target that doesn't exist") + continue + } + + node, hasNode := target.Node() + instances, hasInstances := target.Instances() + + nodeLogger := logger.MakeChild(targetID) + + if !hasNode { + nodeLogger.Warning("No node [" + node.MachineName() + "]") + } else if !node.Can("start") { + nodeLogger.Info("Node doesn't Restart [" + node.MachineName() + ":" + node.Type() + "]") + } else if !hasInstances { + nodeLogger.Info("No valid instances specified in target list [" + node.MachineName() + "]") + } else { + nodeLogger.Message("Restarting instance containers") + + for _, id := range instances.InstancesOrder() { + instance, _ := instances.Instance(id) + instanceClient := instance.Client() + + // Stop + if instanceClient.IsRunning() { + nodeLogger.Info("Stopping instance: "+id) + instanceClient.Stop(nodeLogger, operation.force, operation.timeout) + } + + // Start + if !instanceClient.IsRunning() { + nodeLogger.Info("Starting instance: "+id) + instanceClient.Start(nodeLogger, operation.force) + } + + } + } + } + + return true +} diff --git a/operation/operation_start.go b/operation/operation_start.go index 9b09610..995f002 100644 --- a/operation/operation_start.go +++ b/operation/operation_start.go @@ -21,7 +21,7 @@ func (operation *StartOperation) Flags(flags []string) bool { case "-f": fallthrough case "--force": - operation.force = true + operation.force = true } } return true