Skip to content

Commit

Permalink
Merge pull request #31 from james-nesbitt/operationscale
Browse files Browse the repository at this point in the history
overhaul and added scale operation
  • Loading branch information
james-nesbitt committed Oct 9, 2015
2 parents 6b63487 + d801705 commit 0a4d620
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 79 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func MatchContainers(client *docker.Client, containerPrefix string, running bool
containers := []docker.APIContainers{}

for _, container := range cache.getContainers(false) {
if running && !strings.Contains(container.Status, "RUNNING") {
if running && !strings.Contains(container.Status, "Up") {
continue
}

Expand Down
6 changes: 3 additions & 3 deletions dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func (node *Node) DependencyInstanceMatches(targets []string, fallbackInstance s

if targetInstanceName=="%ALL" {
// in this case we have a meta request to link to all active instances of a target
targetInstances = targetNode.GetInstances(true)
targetInstances = targetNode.GetInstances()
} else if targetInstanceName=="%RANDOM" {
if randomTarget := targetNode.GetRandomInstance(true); randomTarget!=nil {
targetInstances = []*Instance{ randomTarget }
}
} else if targetNode.InstanceType=="single" && ( targetInstanceName=="" || targetInstanceName=="single" || fallbackInstance=="single" ) {
targetInstances = targetNode.FilterInstances([]string{"single"}, true)
targetInstances = targetNode.FilterInstances([]string{"single"})
} else {
targetInstances = targetNode.FilterInstances([]string{targetInstanceName, fallbackInstance}, true)
targetInstances = targetNode.FilterInstances([]string{targetInstanceName, fallbackInstance})
}

for _, target := range targetInstances {
Expand Down
40 changes: 19 additions & 21 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (node *Node) ConfigureInstances_Scaled(min int, max int) bool {
* first, which it cannot do until all nodes have been created/added
*/

func (node *Node) AddInstance(name string, active bool) {
func (node *Node) AddInstance(name string, isDefault bool) {
if node.InstanceMap==nil {
node.InstanceMap = map[string]*Instance{} // an actual map of instance objects, starts empty to be filled in by the main node handler
}
Expand All @@ -57,7 +57,7 @@ func (node *Node) AddInstance(name string, active bool) {
Name: name,
MachineName: node.MachineName+"_"+name,

active: active,
DefaultInstance: isDefault,
}

/**
Expand All @@ -83,36 +83,26 @@ func (node *Node) AddInstance(name string, active bool) {
func (node *Node) AddTemporaryInstance(name string) {
node.AddInstance(name, true)
}
func (node *Node) AddInstances(instances []string, active bool) {
func (node *Node) AddInstances(instances []string, isDefault bool) {
for _, instance := range instances {
node.AddInstance( instance, active )
node.AddInstance( instance, isDefault )
}
}


func (node *Node) GetInstances(onlyActive bool) []*Instance {
func (node *Node) GetInstances() []*Instance {
instances := []*Instance{}
for name, _ := range node.InstanceMap {
instance := node.GetInstance(name)

if onlyActive && !instance.active {
continue
}

instances = append(instances, instance)
}
return instances
}
func (node *Node) FilterInstances(filters []string, onlyActive bool) []*Instance {
func (node *Node) FilterInstances(filters []string) []*Instance {
instances := []*Instance{}

for name, _ := range node.InstanceMap {
instance := node.GetInstance(name)

if onlyActive && !instance.active {
continue
}

for _, filter := range filters {
if filter==name {
instances = append(instances, instance)
Expand All @@ -122,11 +112,10 @@ func (node *Node) FilterInstances(filters []string, onlyActive bool) []*Instance
}
return instances
}
func (node *Node) GetRandomInstance(onlyActive bool) *Instance {
func (node *Node) GetRandomInstance(onlyDefault bool) *Instance {
for name, _ := range node.InstanceMap {
instance := node.GetInstance(name)

if onlyActive && !instance.active {
if (onlyDefault && !instance.isDefault()) {
continue
}

Expand All @@ -140,7 +129,7 @@ func (node *Node) GetInstance(name string) *Instance {
// shortcut to get any random instance
if name=="" {
for _, instance := range node.InstanceMap {
if instance.active {
if instance.isActive() {
if instance.Process() {
return instance
}
Expand Down Expand Up @@ -172,8 +161,9 @@ type Instance struct {

Config docker.Config
HostConfig docker.HostConfig

DefaultInstance bool // this instance is a default instance, and should always be created/started/stopped

active bool // should this instance be active for operations (or is it dormant, perhaps for scaling)
processed bool // has this instance run .Process()
}
func (instance *Instance) Init() bool {
Expand All @@ -194,6 +184,14 @@ func (instance *Instance) GetContainerName() string {
return strings.ToLower(instance.MachineName)
}

func (instance *Instance) isDefault() bool {
return instance.DefaultInstance
}
func (instance *Instance) isActive() bool {
return instance.isDefault() || instance.HasContainer(true)
}


/**
* Elements in the nodes struct are used directly as docker configuration. but are keyed
* to the nodes map keys. To properly use them, these mapped elements have to be changed
Expand Down
3 changes: 3 additions & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func GetOperation(name string, nodes Nodes, targets []string, conf *Conf, log Lo
case "up":
return Operation(&Operation_Up{log:log.ChildLog("UP"), nodes:nodes, targets:targets})

case "scale":
return Operation(&Operation_Scale{log:log.ChildLog("SCALE"), nodes:nodes, targets:targets})

case "create":
return Operation(&Operation_Create{log:log.ChildLog("CREATE"), nodes:nodes, targets:targets})
case "remove":
Expand Down
12 changes: 7 additions & 5 deletions operation_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func (operation *Operation_Attach) Run() {
}

func (nodes *Nodes) Attach(targets []string) {
for _, target := range nodes.GetTargets(targets, true) {
for _, target := range nodes.GetTargets(targets) {
if target.node.Do("start") {
for _, instance := range target.instances {
instance.Attach()
if instance.isActive() {
instance.Attach()
}
}
}
}
Expand All @@ -54,12 +56,12 @@ func (node *Node) Attach(filters []string) {
var instances []*Instance

if len(filters)==0 {
instances = node.GetInstances(false)
instances = node.GetInstances()
} else {
instances = node.FilterInstances(filters, false)
instances = node.FilterInstances(filters)
}
for _, instance := range instances {
if instance.active {
if instance.isActive() {
instance.Attach()
}
}
Expand Down
2 changes: 1 addition & 1 deletion operation_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (operation *Operation_Build) Run() {
}

func (nodes *Nodes) Build(targets []string, force bool) {
for _, target := range nodes.GetTargets(targets, true) {
for _, target := range nodes.GetTargets(targets) {
target.node.log = nodes.log.ChildLog("NODE:"+target.node.Name)

// ignore target instances, and just build the node
Expand Down
8 changes: 5 additions & 3 deletions operation_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ func (operation *Operation_Commit) Run() {
}

func (nodes *Nodes) Commit(targets []string, repo string, tag string, message string) {
for _, target := range nodes.GetTargets(targets,false) {
for _, target := range nodes.GetTargets(targets) {
if target.node.Do("commit") {
for _, instance := range target.instances {
instance.Commit(repo, tag, message)
if instance.HasContainer(false) {
instance.Commit(repo, tag, message)
}
}
}
}
Expand All @@ -92,7 +94,7 @@ func (nodes *Nodes) Commit(targets []string, repo string, tag string, message st
func (node *Node) Commit(repo string, instance string, tag string, message string) {
if node.Do("commit") {

for _, instance := range node.FilterInstances([]string{instance}, false) {
for _, instance := range node.FilterInstances([]string{instance}) {
if instance.HasContainer(false) {
instance.Commit(repo, tag, message)
}
Expand Down
25 changes: 19 additions & 6 deletions operation_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,41 @@ func (operation *Operation_Create) Run() {
operation.nodes.Create(operation.targets, operation.cmd, true, force)
}

func (nodes *Nodes) Create(targets []string, cmdOverride []string, onlyActive bool, force bool) {
for _, target := range nodes.GetTargets(targets, onlyActive) {
func (nodes *Nodes) Create(targets []string, cmdOverride []string, onlyDefault bool, force bool) {
for _, target := range nodes.GetTargets(targets) {
if target.node.Do("create") {
for _, instance := range target.instances {
if instance.HasContainer(false) {
continue
}
if onlyDefault && !instance.isDefault() {
continue
}
instance.Create(cmdOverride, force)
}
}
}
}

func (node *Node) Create(filters []string, onlyActive bool, force bool) {
func (node *Node) Create(filters []string, cmdOverride []string, onlyDefault bool, force bool) {
if node.Do("create") {

var instances []*Instance

if len(filters)==0 {
instances = node.GetInstances(onlyActive)
instances = node.GetInstances()
} else {
instances = node.FilterInstances(filters, onlyActive)
instances = node.FilterInstances(filters)
}

for _, instance := range instances {
instance.Create([]string{}, force)
if instance.HasContainer(false) {
continue
}
if onlyDefault && !instance.isDefault() {
continue
}
instance.Create(cmdOverride, force)
}

}
Expand Down
2 changes: 1 addition & 1 deletion operation_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (operation *Operation_Destroy) Run() {
}

func (nodes *Nodes) Destroy(targets []string, force bool) {
for _, target := range nodes.GetTargets(targets, true) {
for _, target := range nodes.GetTargets(targets) {
target.node.log = nodes.log.ChildLog("NODE:"+target.node.Name)
target.node.Destroy(force)
}
Expand Down
12 changes: 9 additions & 3 deletions operation_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (operation *Operation_Info) Run() {
}

func (nodes *Nodes) Info(targets []string) {
for _, target := range nodes.GetTargets(targets, false) {
for _, target := range nodes.GetTargets(targets) {
target.node.log = nodes.log.ChildLog("NODE:"+target.node.Name)
target.node.Info()
}
Expand Down Expand Up @@ -115,6 +115,7 @@ func (node *Node) Info_Instances() bool {
"",
"Name",
"Container",
"Default",
"Active",
"Status",
"ID",
Expand All @@ -123,15 +124,20 @@ func (node *Node) Info_Instances() bool {
}
w.Write([]byte(strings.Join(row, "\t")+"\n"))

instances := node.GetInstances(false)
instances := node.GetInstances()

for index, instance := range instances {
row := []string{
strconv.FormatInt(int64(index+1), 10),
instance.Name,
instance.GetContainerName(),
}
if instance.active {
if instance.isDefault() {
row = append(row, "yes")
} else {
row = append(row, "no")
}
if instance.isActive() {
row = append(row, "yes")
} else {
row = append(row, "no")
Expand Down
13 changes: 8 additions & 5 deletions operation_pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func (operation *Operation_Pause) Run() {
}

func (nodes *Nodes) Pause(targets []string) {
for _, target := range nodes.GetTargets(targets, true) {
for _, target := range nodes.GetTargets(targets) {
if target.node.Do("start") {
for _, instance := range target.instances {
instance.Pause(false)
if instance.HasContainer(true) {
instance.Pause(false)
}
}
}
}
Expand All @@ -49,12 +51,13 @@ func (node *Node) Pause(filters []string, force bool) {
var instances []*Instance

if len(filters)==0 {
instances = node.GetInstances(false)
instances = node.GetInstances()
} else {
instances = node.FilterInstances(filters, false)
instances = node.FilterInstances(filters)
}

for _, instance := range instances {
if instance.active {
if instance.HasContainer(true) {
instance.Pause(force)
}
}
Expand Down
2 changes: 1 addition & 1 deletion operation_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (operation *Operation_Pull) Run() {
}

func (nodes *Nodes) Pull(targets []string, registry string) {
for _, target := range nodes.GetTargets(targets, false) {
for _, target := range nodes.GetTargets(targets) {
target.node.log = nodes.log.ChildLog("NODE:"+target.node.Name)
target.node.Pull(registry)
}
Expand Down
15 changes: 10 additions & 5 deletions operation_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ func (operation *Operation_Remove) Run() {
}

func (nodes *Nodes) Remove(targets []string, force bool) {
for _, target := range nodes.GetTargets(targets, !force) {
for _, target := range nodes.GetTargets(targets) {
if target.node.Do("create") {
for _, instance := range target.instances {
instance.Remove(force)
if instance.HasContainer(false) {
instance.Remove(force)
}
}
}
}
Expand All @@ -67,12 +69,15 @@ func (node *Node) Remove(filters []string, force bool) {
var instances []*Instance

if len(filters)==0 {
instances = node.GetInstances(true)
instances = node.GetInstances()
} else {
instances = node.FilterInstances(filters, true)
instances = node.FilterInstances(filters)
}

for _, instance := range instances {
instance.Remove(force)
if instance.HasContainer(false) {
instance.Remove(force)
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion operation_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (operation *Operation_Run) Run() {
operation.log.Message("running run operation")
operation.log.DebugObject(LOG_SEVERITY_DEBUG_LOTS, "Targets:", operation.targets)

for _, target := range operation.nodes.GetTargets(operation.targets, true) {
for _, target := range operation.nodes.GetTargets(operation.targets) {
target.node.log = operation.nodes.log.ChildLog("NODE:"+target.node.Name)
target.node.Run(operation.instance, operation.cmd)
}
Expand Down
Loading

0 comments on commit 0a4d620

Please sign in to comment.