Skip to content

Commit

Permalink
Merge pull request #32 from james-nesbitt/operationscale
Browse files Browse the repository at this point in the history
better scale flag handling
  • Loading branch information
james-nesbitt committed Oct 9, 2015
2 parents 0a4d620 + 13640a4 commit 2ba3e0c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 22 deletions.
4 changes: 2 additions & 2 deletions operation_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (nodes *Nodes) Attach(targets []string) {
for _, target := range nodes.GetTargets(targets) {
if target.node.Do("start") {
for _, instance := range target.instances {
if instance.isActive() {
if instance.HasContainer(true) {
instance.Attach()
}
}
Expand All @@ -61,7 +61,7 @@ func (node *Node) Attach(filters []string) {
instances = node.FilterInstances(filters)
}
for _, instance := range instances {
if instance.isActive() {
if instance.HasContainer(true) {
instance.Attach()
}
}
Expand Down
6 changes: 6 additions & 0 deletions operation_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (node *Node) Info_Instances() bool {
"Container",
"Default",
"Active",
"Running",
"Status",
"ID",
"Created",
Expand All @@ -142,6 +143,11 @@ func (node *Node) Info_Instances() bool {
} else {
row = append(row, "no")
}
if instance.HasContainer(true) {
row = append(row, "yes")
} else {
row = append(row, "no")
}

container, found := instance.GetContainer(false)
if found {
Expand Down
120 changes: 100 additions & 20 deletions operation_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ type Operation_Scale struct {

nodes Nodes
targets []string

scale int
}
func (operation *Operation_Scale) Flags(flags []string) {

// default scale value
operation.scale = 1

if len(flags)>0 {
if flags[0]=="up" {
operation.scale = 1
} else if flags[0]=="down" {
operation.scale = -1
}
}

}

func (operation *Operation_Scale) Help(topics []string) {
Expand All @@ -34,36 +47,103 @@ func (operation *Operation_Scale) Run() {
operation.log.Message("running scale operation")
operation.log.DebugObject(LOG_SEVERITY_DEBUG_LOTS, "Targets:", operation.targets)

if operation.scale==0 {
operation.log.Warning("scale operation was told to scale to 0")
return
}

TargetScaleReturn:
for _, target := range operation.nodes.GetTargets(operation.targets) {

InstanceScaleReturn:
for i := 0; i < len(target.node.InstanceMap); i++ {
if instance, ok := target.node.InstanceMap[strconv.FormatInt(int64(i+1), 10)]; ok {

container, hasContainer := instance.GetContainer(false)
if target.node.InstanceType!="scaled" {
operation.log.Warning("Tried to scale non-scaleable node :"+target.node.Name)
continue TargetScaleReturn
}

if hasContainer {
if strings.Contains(container.Status, "Up") {
operation.log.Debug(LOG_SEVERITY_DEBUG_LOTS, "Node instance already has a running container :"+container.ID)
continue InstanceScaleReturn
}
} else {
// create a new container for this instance
instance.Create([]string{}, false)
}
if operation.scale>0 {
count := target.node.ScaleUpNumber(operation.scale)

if count==0 {
target.node.log.Warning("Scale operation could not scale up any new instances of node :"+target.node.Name)
} else if count<operation.scale {
target.node.log.Warning("Scale operation could not scale up all of the requested instances of node :"+target.node.Name)
} else {
target.node.log.Warning("Scale operation scaled up all requested node instances :"+target.node.Name)
}

operation.log.Message("Scaling up instance")
instance.Start(false)
continue TargetScaleReturn
} else {
count := target.node.ScaleDOwnNumber(-operation.scale)

if count==0 {
target.node.log.Warning("Scale operation could not scale down any new instances of node :"+target.node.Name)
} else if count<(-operation.scale) {
target.node.log.Warning("Scale operation could not scale down all of the requested instances of node :"+target.node.Name)
} else {
target.node.log.Warning("Scale operation scaled down all requested node instances :"+target.node.Name)
}
// operation.log.DebugObject(LOG_SEVERITY_DEBUG_LOTS, "SCALE NODE TEST INSTANCE:", *target.node.InstanceMap[strconv.FormatInt(int64(i+1), 10)])
}

operation.log.Warning("Could not find any node instance to scale up to :"+target.node.Name)

}

cache.refresh(false, true)
}

func (node *Node) ScaleUpNumber(number int) int {

count := 0

InstanceScaleReturn:
for i := 0; i < len(node.InstanceMap); i++ {
if instance, ok := node.InstanceMap[strconv.FormatInt(int64(i+1), 10)]; ok {

container, hasContainer := instance.GetContainer(false)

if hasContainer {
if strings.Contains(container.Status, "Up") {
continue InstanceScaleReturn
}
} else {
// create a new container for this instance
instance.Create([]string{}, false)
}

node.log.Message("Node Scaling up. Stopping instance :"+instance.Name)
instance.Start(false)

count++
if count >= number {
return count
}
}
// operation.log.DebugObject(LOG_SEVERITY_DEBUG_LOTS, "SCALE NODE TEST INSTANCE:", *target.node.InstanceMap[strconv.FormatInt(int64(i+1), 10)])
}

return count
}
func (node *Node) ScaleDOwnNumber(number int) int {

count := 0

InstanceScaleReturn:
for i := len(node.InstanceMap); i>=0; i-- {
if instance, ok := node.InstanceMap[strconv.FormatInt(int64(i+1), 10)]; ok {

_, hasContainer := instance.GetContainer(true)

if !hasContainer {
continue InstanceScaleReturn
}

node.log.Message("Node Scaling down. Starting instance :"+instance.Name)
instance.Stop(false,10)

count++
if count >= number {
return count
}
}
// operation.log.DebugObject(LOG_SEVERITY_DEBUG_LOTS, "SCALE NODE TEST INSTANCE:", *target.node.InstanceMap[strconv.FormatInt(int64(i+1), 10)])
}

return count
}

0 comments on commit 2ba3e0c

Please sign in to comment.