/
ec2.go
119 lines (98 loc) · 3.28 KB
/
ec2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package awsEc2
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/thomasobenaus/sokar/aws"
)
// AdjustScalingObjectCount will scale the AutoScalingGroup to the desired count (amount of instances)
func (c *Connector) AdjustScalingObjectCount(autoScalingGroup string, min uint, max uint, from uint, to uint) error {
sess, err := c.createSession()
if err != nil {
return err
}
autoScalingIF := c.autoScalingFactory.CreateAutoScaling(sess)
asgQuery := aws.AutoScalingGroupQuery{
AsgIF: autoScalingIF,
TagKey: c.tagKey,
TagValue: autoScalingGroup,
}
asgName, err := asgQuery.GetAutoScalingGroupName()
if err != nil {
return err
}
size := int64(to)
minSize := int64(min)
maxSize := int64(max)
input := &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: &asgName,
MinSize: &minSize,
MaxSize: &maxSize,
DesiredCapacity: &size,
}
_, err = autoScalingIF.UpdateAutoScalingGroup(input)
if err != nil {
return err
}
c.log.Info().Msgf("Adjusted min=max=desiredCapacity of %s from %d to %d.", asgName, from, size)
return nil
}
// GetScalingObjectCount will return the count of the nomad workers
func (c *Connector) GetScalingObjectCount(datacenter string) (uint, error) {
sess, err := c.createSession()
if err != nil {
return 0, err
}
autoScalingIF := c.autoScalingFactory.CreateAutoScaling(sess)
asgQuery := aws.AutoScalingGroupQuery{
AsgIF: autoScalingIF,
TagKey: c.tagKey,
TagValue: datacenter,
}
min, desired, max, err := asgQuery.GetScaleNumbers()
if err != nil {
return 0, err
}
c.log.Info().Msgf("Current scale numbers min=%d, desired=%d, max=%d.", min, desired, max)
return desired, nil
}
// IsScalingObjectDead will return if the nomad workers of the actual data-center are still available.
func (c *Connector) IsScalingObjectDead(datacenter string) (bool, error) {
// Currently the nomad worker is assumed to be dead in case the according AutoScalingGroup can't be found.
sess, err := c.createSession()
if err != nil {
return true, err
}
autoScalingIF := c.autoScalingFactory.CreateAutoScaling(sess)
asgs, err := aws.GetAutoScalingGroups(autoScalingIF)
if err != nil {
return true, err
}
asg := aws.FilterAutoScalingGroupByTag(c.tagKey, datacenter, asgs)
if asg == nil {
c.log.Warn().Msgf("AutoScalingGroup with %s=%s not found", c.tagKey, datacenter)
return true, nil
}
c.log.Debug().Msgf("AutoScalingGroup with %s=%s found", c.tagKey, datacenter)
return false, nil
}
// createSession wrapper function to create the aws session.
// Internally based on the awsprofile set or not the appropriate method is used.
func (c *Connector) createSession() (*session.Session, error) {
var session *session.Session
var err error
if len(c.awsProfile) > 0 {
c.log.Debug().Msgf("Create aws session based on profile '%s'", c.awsProfile)
if c.fnCreateSessionFromProfile == nil {
return nil, fmt.Errorf("fnCreateSessionFromProfile is nil")
}
session, err = c.fnCreateSessionFromProfile(c.awsProfile)
} else {
c.log.Debug().Msg("Create aws session based on default credentials")
if c.fnCreateSession == nil {
return nil, fmt.Errorf("fnCreateSession is nil")
}
session, err = c.fnCreateSession(c.awsRegion)
}
return session, err
}