-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
306 lines (271 loc) · 9.59 KB
/
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"encoding/base64"
"flag"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/soominhyunwoo/tools/lib/runsimgh"
"github.com/soominhyunwoo/tools/lib/runsimslack"
)
const (
// EC2 config values
awsRegion = "us-east-1"
ec2InstanceType = "c4.8xlarge"
ec2KeyPair = "wallet-nodes"
ec2InstanceProfile = "gaia-simulation"
gaiaAmiIdPrefix = "gaia-sim"
// simulation config values
genesisFilePath = "/home/ec2-user/genesis.json"
// security token ID
ghAppTokenID = "github-sim-app-key"
slackAppTokenID = "slack-app-key"
slackIntegrationType = "slack"
ghIntegrationType = "github"
)
var (
notifyOnly bool
// simulation parameters
blocks, period, seeds, sdkGitRev string
genesis bool
// ec2 instance properties
shutdownBehavior string
// integration variables and structs
integrationType string
github = new(runsimgh.Integration)
slack = new(runsimslack.Integration)
// CircleCI environment variables
buildUrl, buildNum string
)
func init() {
flag.BoolVar(&genesis, "Genesis", false, "Use genesis file in simulation")
flag.BoolVar(¬ifyOnly, "Notify", false, "Send notification and exit")
blocks = os.Getenv("BLOCKS")
period = os.Getenv("PERIOD")
seeds = os.Getenv("SEEDS")
// Not using os.LookupEnv on purpose to reduce the number of variables.
integrationType = os.Getenv("INTEGRATION")
shutdownBehavior = os.Getenv("SHUTDOWN_BEHAVIOR")
buildUrl = os.Getenv("CIRCLE_BUILD_URL")
buildNum = os.Getenv("CIRCLE_BUILD_NUM")
sdkGitRev = os.Getenv("GAIA_COMMIT_HASH")
}
func main() {
flag.Parse()
if integrationType == ghIntegrationType {
if err := github.ConfigFromState(awsRegion, ghAppTokenID); err != nil {
log.Fatalf("ERROR: github.ConfigFromState: %v", err)
}
if err := github.SetActiveCheckRun(); err != nil || github.ActiveCheckRun == nil {
log.Fatalf("ERROR: github.SetActiveCheckRun: %v", err)
}
} else if integrationType == slackIntegrationType {
err := slack.ConfigFromState(awsRegion, slackAppTokenID)
if err != nil {
log.Fatalf("ERROR: slack.ConfigFromState: %v", err)
}
} else if integrationType == "CI" {
log.Println("Just CircleCI things!")
os.Exit(0)
} else {
log.Fatalf("ERROR: missing integration type parameter")
}
// Update github check or send slack message to notify that the image build has started.
if notifyOnly {
if integrationType == slackIntegrationType {
pushNotification(false, buildInitMessage())
os.Exit(0)
}
pushNotification(false, buildInitMessage())
os.Exit(0)
}
sessionEC2 := ec2.New(session.Must(session.NewSession(&aws.Config{Region: aws.String(awsRegion)})))
amiId, err := getAmiId(sdkGitRev, sessionEC2)
if err != nil {
cleanup()
log.Fatalf("ERROR: getAmiId: %v", err)
}
if amiId == "" {
cleanup()
log.Fatalf("ERROR: simulation AMI not found")
}
seedLists := makeSeedLists(seeds)
instanceIds := make([]*string, len(seedLists))
msgQueue := make([]int, len(seedLists))
for index := range seedLists {
// Here be bash dragons. Modify with extreme caution. Ensure you add adequate line endings.
// User data is a shell script that will be run during EC2 instance startup
var userData strings.Builder
userData.WriteString("#!/bin/bash \n")
userData.WriteString("cd /home/ec2-user/go/src/github.com/soominhyunwoo/chain-sdk || exit 1\n")
// Setup environment variables for golang.
// Script can be found here: https://github.com/tendermint/images/blob/master/ami-gaia-sim/set_env.sh
userData.WriteString("source /etc/profile.d/set_env.sh\n")
userData.WriteString(buildRunsimCommand(seedLists[index], strconv.Itoa(index), buildNum))
userData.WriteString("shutdown -h now")
// Separate variable to make this code actually readable
input := &ec2.RunInstancesInput{
InstanceInitiatedShutdownBehavior: aws.String(shutdownBehavior),
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: aws.String(ec2InstanceProfile)},
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: []*ec2.Tag{{
Key: aws.String("Name"),
Value: aws.String("SimID-" + buildNum)}}},
},
InstanceType: aws.String(ec2InstanceType),
ImageId: aws.String(amiId),
KeyName: aws.String(ec2KeyPair),
MaxCount: aws.Int64(1),
MinCount: aws.Int64(1),
UserData: aws.String(base64.StdEncoding.EncodeToString([]byte(userData.String()))),
}
ec2Reservation, err := sessionEC2.RunInstances(input)
if err != nil {
summary := fmt.Sprintf("ERROR: RunInstances: %v", err)
// Checking aws error code to see if we have reached the EC2 limit for this instance type
// Crashing out of the program is not desirable. We can run simulation with a lower number of seeds
// TODO: make this more robust. If we reach the instance limit, switch to a different instance type/aws region
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "InstanceLimitExceeded" {
// If no instances have been started yet, mark the simulation as failed
if index == 1 {
pushNotification(true, summary)
cleanup()
os.Exit(1)
}
// Continue the simulation with the seeds that have already started
break
}
}
// If it's not a limit error, crash out.
// Terminate any instances that are already running, otherwise the github integration will possibly report
// incorrect results.
if index > 1 {
terminateInstances(sessionEC2, instanceIds)
}
pushNotification(true, summary)
cleanup()
os.Exit(1)
}
// Saving these just in case we need to terminate them prematurely
instanceIds[index] = ec2Reservation.Instances[0].InstanceId
msgQueue[index] = index
}
if len(msgQueue) > 1 {
sendSqsMsg(msgQueue, fmt.Sprintf("sim-%s-", integrationType))
}
}
func makeSeedLists(seeds string) map[int]string {
var str strings.Builder
lists := make(map[int]string)
seedsInt, err := strconv.Atoi(seeds)
if err != nil {
cleanup()
log.Fatalf("ERROR: seed list contains invalid values")
}
index := 0
for i := 0; i <= seedsInt; i++ {
// TODO: make this a configurable value. Requires some more logic around AWS instance startup
// Each machine has 36 cores. Allocate one core per seed
if i != 0 && math.Mod(float64(i), 35) == 0 {
lists[index] = strings.TrimRight(str.String(), ",")
str.Reset()
index++
}
str.WriteString(strconv.Itoa(i) + ",")
}
if str.String() != "" {
lists[index] = strings.TrimRight(str.String(), ",")
}
return lists
}
func getAmiId(gitRevision string, svc *ec2.EC2) (amiID string, err error) {
var imageID *ec2.DescribeImagesOutput
input := &ec2.DescribeImagesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("name"),
Values: []*string{aws.String(fmt.Sprintf("%s-%s", gaiaAmiIdPrefix, gitRevision))},
}},
}
if imageID, err = svc.DescribeImages(input); err != nil {
return
}
if len(imageID.Images) > 0 {
amiID = *imageID.Images[0].ImageId
}
return
}
func terminateInstances(svc *ec2.EC2, instanceIds []*string) {
if _, err := svc.TerminateInstances(&ec2.TerminateInstancesInput{
InstanceIds: instanceIds,
}); err != nil {
log.Printf("ERROR: TerminateInstances: %v", err)
}
}
func pushNotification(failed bool, message string) {
// If the simulation failed, and pushing the notification also fails after this point, record the failure message.
if failed {
log.Print(message)
}
if integrationType == slackIntegrationType {
if err := slack.PostMessage(message); err != nil {
cleanup()
log.Fatalf("ERROR: slack.PostMessage: %v", err)
}
} else if failed {
if err := github.ConcludeCheckRun(&message, aws.String("failure")); err != nil {
cleanup()
log.Fatalf("ERROR: github.ConcludeCheckRun: %v", err)
}
} else {
if err := github.UpdateCheckRunStatus(aws.String("in_progress"), &message); err != nil {
cleanup()
log.Fatalf("ERROR: github.UpdateCheckRunStatus: %v", err)
}
}
}
func buildRunsimCommand(seeds, hostId, simId string) string {
logObjKey := fmt.Sprintf("sim-id-%s", os.Getenv("CIRCLE_BUILD_NUM"))
integration := "-Github"
if integrationType == slackIntegrationType {
integration = "-Slack"
}
if genesis {
return fmt.Sprintf("runsim -SimId %s -HostId %s -LogObjPrefix %s -SimAppPkg ./simapp %s -Seeds \"%s\" -Genesis %s %s %s TestFullAppSimulation;",
simId, hostId, logObjKey, integration, seeds, genesisFilePath, blocks, period)
}
log.Printf("runsim -SimId %s -HostId %s -LogObjPrefix %s -SimAppPkg ./simapp %s -Seeds \"%s\" %s %s TestFullAppSimulation;",
simId, hostId, logObjKey, integration, seeds, blocks, period)
return fmt.Sprintf("runsim -SimId %s -HostId %s -LogObjPrefix %s -SimAppPkg ./simapp %s -Seeds \"%s\" %s %s TestFullAppSimulation;",
simId, hostId, logObjKey, integration, seeds, blocks, period)
}
func buildInitMessage() string {
if integrationType == slackIntegrationType {
return fmt.Sprintf("*ID #%s*\n SDK hash/tag/branch: `%s`\n <%s|Build URL>\nblocks:\t`%s`\nperiod:\t`%s`\nseeds:\t`%s`\n",
buildNum, sdkGitRev, buildUrl, blocks, period, seeds)
} else {
return fmt.Sprintf("**ID #%s.**\n SDK commit: `%s`\n [Build URL](%s)\nblocks:\t`%s`\nperiod:\t`%s`\nseeds:\t`%s`\n",
buildNum, sdkGitRev, buildUrl, blocks, period, seeds)
}
}
// Function used if the program crashes out. Attempts to remove the state information from dynamoDB
func cleanup() {
if integrationType == slackIntegrationType {
if err := slack.DeleteState(); err != nil {
log.Println(err)
}
} else {
if err := github.DeleteState(); err != nil {
log.Println(err)
}
}
}