/
ec2_main.go
205 lines (179 loc) · 7.3 KB
/
ec2_main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package ec2
//-----------------------------------------------------------------------------
// Package factored import statement:
//-----------------------------------------------------------------------------
import (
// Stdlib:
"encoding/json"
"io/ioutil"
"os"
"strings"
"time"
// Community:
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/imdario/mergo"
"github.com/katosys/kato/pkg/tools"
)
//-----------------------------------------------------------------------------
// Typedefs:
//-----------------------------------------------------------------------------
// AWS API endpoints:
type svc struct {
ec2 *ec2.EC2
iam *iam.IAM
elb *elb.ELB
}
// Instance data.
type Instance struct {
AmiID string `json:"AmiID"` // deploy | add | run
HostName string `json:"HostName"` // | add |
HostID string `json:"HostID"` // | add |
Roles string `json:"Roles"` // | add |
ClusterState string `json:"ClusterState"` // | add |
InstanceType string `json:"InstanceType"` // | add | run
SrcDstCheck string `json:"SrcDstCheck"` // | | run
InstanceID string `json:"InstanceID"` // | | run
SubnetID string `json:"SubnetID"` // | | run
SecGrpIDs string `json:"SecGrpIDs"` // | | run
PublicIP string `json:"PublicIP"` // | | run
PrivateIP string `json:"PrivateIP"` // | | run
IAMRole string `json:"IAMRole"` // | | run
InterfaceID string `json:"InterfaceID"` // | | run
ELBName string `json:"ELBName"` // | | run
TagName string `json:"TagName"` // | | run
}
// State data.
type State struct {
Quadruplets []string `json:"-"` // deploy | | add |
StubZones []string `json:"StubZones"` // deploy | | add |
QuorumCount int `json:"QuorumCount"` // deploy | | add |
MasterCount int `json:"MasterCount"` // deploy | | add |
CoreOSChannel string `json:"CoreOSChannel"` // deploy | | add |
EtcdToken string `json:"EtcdToken"` // deploy | | add |
DNSProvider string `json:"DNSProvider"` // deploy | | add |
DNSApiKey string `json:"DNSApiKey"` // deploy | | add |
SysdigAccessKey string `json:"SysdigAccessKey:"` // deploy | | add |
DatadogAPIKey string `json:"DatadogAPIKey:"` // deploy | | add |
SlackWebhook string `json:"SlackWebhook:"` // deploy | | add |
SMTPURL string `json:"SMTPURL:"` // deploy | | add |
AdminEmail string `json:"AdminEmail:"` // deploy | | add |
CaCertPath string `json:"CaCertPath"` // deploy | | add |
CalicoIPPool string `json:"CalicoIPPool"` // deploy | | |
Domain string `json:"Domain"` // deploy | setup | add |
ClusterID string `json:"ClusterID"` // deploy | setup | add |
Region string `json:"Region"` // deploy | setup | add | run
Zone string `json:"Zone"` // deploy | setup | add | run
VpcCidrBlock string `json:"VpcCidrBlock"` // deploy | setup | |
IntSubnetCidr string `json:"IntSubnetCidr"` // deploy | setup | |
ExtSubnetCidr string `json:"ExtSubnetCidr"` // deploy | setup | |
AllocationID string `json:"AllocationID"` // | setup | | run
VpcID string `json:"VpcID"` // | setup | |
MainRouteTableID string `json:"MainRouteTableID"` // | setup | |
InetGatewayID string `json:"InetGatewayID"` // | setup | |
NatGatewayID string `json:"NatGatewayID"` // | setup | |
RouteTableID string `json:"RouteTableID"` // | setup | |
KatoRoleID string `json:"KatoRoleID"` // | setup | |
RexrayPolicy string `json:"RexrayPolicy"` // | setup | |
QuorumSecGrp string `json:"QuorumSecGrp"` // | setup | |
MasterSecGrp string `json:"MasterSecGrp"` // | setup | |
WorkerSecGrp string `json:"WorkerSecGrp"` // | setup | |
BorderSecGrp string `json:"BorderSecGrp"` // | setup | |
ELBSecGrp string `json:"ELBSecGrp"` // | setup | |
IntSubnetID string `json:"IntSubnetID"` // | setup | |
ExtSubnetID string `json:"ExtSubnetID"` // | setup | |
DNSName string `json:"DNSName"` // | setup | |
KeyPair string `json:"KeyPair"` // | | add | run
}
// Data struct for EC2 endpoints, instance and state data.
type Data struct {
command string
svc
Instance
State
}
//-----------------------------------------------------------------------------
// func: dumpState
//-----------------------------------------------------------------------------
func (d *Data) dumpState() error {
// Marshal the data:
data, err := json.MarshalIndent(d.State, "", " ")
if err != nil {
log.WithField("cmd", "ec2:"+d.command).Error(err)
return err
}
// Create the state directory:
path := os.Getenv("HOME") + "/.kato"
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(path, 0700)
if err != nil {
log.WithField("cmd", "ec2:"+d.command).Error(err)
return err
}
}
}
// Write the state file:
err = ioutil.WriteFile(path+"/"+d.ClusterID+".json", data, 0600)
if err != nil {
log.WithField("cmd", "ec2:"+d.command).Error(err)
return err
}
return nil
}
//-----------------------------------------------------------------------------
// func: loadState
//-----------------------------------------------------------------------------
func (d *Data) loadState() error {
// Load raw data from state file:
raw, err := tools.LoadState(d.ClusterID)
if err != nil {
return err
}
// Decode the loaded JSON data:
dat := State{}
if err := json.Unmarshal(raw, &dat); err != nil {
log.WithField("cmd", "ec2:"+d.command).Error(err)
return err
}
// Merge the decoded data into the current state:
if err := mergo.Map(&d.State, dat); err != nil {
log.WithField("cmd", "ec2:"+d.command).Error(err)
return err
}
return nil
}
//-----------------------------------------------------------------------------
// func: tag
//-----------------------------------------------------------------------------
func (d *Data) tag(resource, key, value string) error {
// Forge the tag request:
params := &ec2.CreateTagsInput{
Resources: []*string{
aws.String(resource),
},
Tags: []*ec2.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
}
// Send the tag request:
for i := 0; i < 5; i++ {
if _, err := d.ec2.CreateTags(params); err != nil {
ec2err, ok := err.(awserr.Error)
if ok && strings.Contains(ec2err.Code(), ".NotFound") {
time.Sleep(1e9)
continue
}
log.WithField("cmd", "ec2:"+d.command).Error(err)
return err
}
}
return nil
}