This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathlaunch
More file actions
executable file
·156 lines (111 loc) · 3.43 KB
/
launch
File metadata and controls
executable file
·156 lines (111 loc) · 3.43 KB
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
#!/usr/bin/env coffee
fs = require 'fs'
argv = require('minimist')(process.argv.slice(2))
KONFIG = require 'koding-config-manager'
unless KONFIG.test?.credentials?.AWS
console.error 'error: test aws credentials are not set'
process.exit 1
{
subnetId
securityGroupId
keyName
} = KONFIG.test.credentials.AWS
unless subnetId
console.error 'error: subnet id is not set'
process.exit 1
unless securityGroupId
console.error 'error: security group id is not set'
process.exit 1
unless keyName
console.error 'error: key name is not set'
process.exit 1
AWS = require './aws'
EC2 = new AWS.EC2()
R53 = new AWS.Route53()
unless revision = argv.commit
revision = try
require('git-rev-sync').long()
catch
'FETCH_HEAD'
if argv['pull-request']
refspec = "+refs/pull/#{argv['pull-request']}/head"
git_fetch = "git fetch origin #{refspec or ''}"
git_checkout = "git checkout -fq #{revision}"
git_clone_vault = (branch = 'master') ->
"git clone --branch #{branch} --depth 1 git@github.com:koding/vault.git"
argv.count ?= require('./parallel-sets').length
if typeof argv.count isnt 'number'
console.error 'error: Non-number value is given as instance count'
process.exit 1
userData =
"""
#cloud-config
disable_root: false
hostname: wercker-test-instance
runcmd:
- echo 127.0.0.1 `hostname` >> /etc/hosts
- echo 127.0.0.1 freegeoip.net >> /etc/hosts
- ip -s -s neigh flush all
- cd /opt/koding
- #{git_fetch}
- #{git_checkout}
- rm -rf ./vault # delete vault folder if exists
- #{git_clone_vault argv['branch']} || #{git_clone_vault()}
- git submodule update --recursive
- /opt/koding/scripts/test-instance/init
"""
params =
ImageId : (require './ami').id
InstanceType : 'm4.large'
MinCount : argv.count
MaxCount : argv.count
SubnetId : subnetId
SecurityGroupIds : [securityGroupId]
KeyName : keyName
UserData : new Buffer(userData).toString 'base64'
InstanceIds = []
RunningInstances = []
monitorInterval = null
EC2.runInstances params, (err, data) ->
if err
console.error JSON.stringify err
process.exit 1
return
data.Instances.forEach ({InstanceId}) -> InstanceIds.push InstanceId
tag()
monitorInterval = setInterval monitor, 5000
tag = ->
refspec = switch
when argv['pull-request']
"pr-#{argv['pull-request']}"
when argv['commit']
"rv-#{argv['commit'].substring 0, 7}"
else "rv-#{revision}"
Resources = InstanceIds
Tags = []
addTag = (Key, Value) -> Tags.push { Key, Value }
addTag 'Name', "test-#{refspec}"
addTag 'Role', 'test-instance'
EC2.createTags { Resources, Tags }, (err, res) ->
monitor = ->
EC2.describeInstances {InstanceIds}, (err, data) ->
return console.error JSON.stringify err if err
data.Reservations.forEach checkInstances
checkInstances = ({Instances}) ->
Instances.forEach checkInstanceState
if RunningInstances.length is InstanceIds.length
exit()
checkInstanceState = (Instance) ->
for RunningInstance in RunningInstances \
when Instance.InstanceId is RunningInstance.InstanceId
return
if Instance.State.Name is 'running'
RunningInstances.push Instance if Instance
exit = ->
clearInterval monitorInterval
printInstanceInfo()
process.exit()
printInstanceInfo = ->
RunningInstances.forEach (Instance) ->
{InstanceId, PublicIpAddress} = Instance
console.log InstanceId, PublicIpAddress