forked from koding/koding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch
More file actions
executable file
·139 lines (97 loc) · 3.26 KB
/
Copy pathlaunch
File metadata and controls
executable file
·139 lines (97 loc) · 3.26 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
#!/usr/bin/env coffee
fs = require 'fs'
argv = require('minimist')(process.argv.slice(2))
unless subnetId = process.env.TEST_AWS_SUBNET_ID
console.error 'error: TEST_AWS_SUBNET_ID is not set'
process.exit 1
unless securityGroupId = process.env.TEST_AWS_SECURITY_GROUP_ID
console.error 'error: TEST_AWS_SECURITY_GROUP_ID is not set'
process.exit 1
unless keyName = process.env.TEST_AWS_KEY_NAME
console.error 'error: TEST_AWS_KEY_NAME is not set'
process.exit 1
AWS = require './aws'
EC2 = new AWS.EC2()
revision = 'FETCH_HEAD' unless revision = argv.commit
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 = (branchName = 'master') ->
"git clone --branch #{branchName} --depth 1 git@github.com:koding/vault.git"
argv.count ?= 1
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-name']} || #{git_clone_vault()}
- git submodule update --recursive
- /opt/koding/scripts/test-instance/init
- echo $(git rev-parse HEAD) > REVISION
"""
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}"
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