Skip to content

Commit

Permalink
Merge pull request #96 from james-nesbitt/restartoperation
Browse files Browse the repository at this point in the history
Add restart operation
  • Loading branch information
james-nesbitt committed Mar 7, 2016
2 parents 507bc61 + bfdf9da commit ad4aeb6
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 14 deletions.
3 changes: 3 additions & 0 deletions operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -134,6 +136,7 @@ func ListOperations() []string {
"scale",
"create",
"remove",
"restart",
"start",
"status",
"stop",
Expand Down
101 changes: 101 additions & 0 deletions operation/operation_restart.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion operation/operation_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (operation *StartOperation) Flags(flags []string) bool {
case "-f":
fallthrough
case "--force":
operation.force = true
operation.force = true
}
}
return true
Expand Down
43 changes: 30 additions & 13 deletions operation/operation_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -84,28 +86,43 @@ 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) {
for _, id := range instances.InstancesOrder() {
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)
}
}
}
}
Expand Down

0 comments on commit ad4aeb6

Please sign in to comment.