gaptool-server 0.5.19 → 0.6.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/routes/main.rb DELETED
@@ -1,206 +0,0 @@
1
- # encoding: utf-8
2
- class GaptoolServer < Sinatra::Application
3
-
4
- get '/' do
5
- "You must be lost. Read the instructions."
6
- end
7
-
8
- get '/ping' do
9
- "PONG"
10
- end
11
-
12
- post '/init' do
13
- data = JSON.parse request.body.read
14
- AWS.config(:access_key_id => $redis.hget('config', 'aws_id'),
15
- :secret_access_key => $redis.hget('config', 'aws_secret'),
16
- :ec2_endpoint => "ec2.#{data['zone'].chop}.amazonaws.com")
17
- @ec2 = AWS::EC2.new
18
- # create shared secret to reference in /register
19
- @secret = (0...8).map{65.+(rand(26)).chr}.join
20
- data.merge!("secret" => @secret)
21
- security_group = data['security_group'] || $redis.hget("role:#{data['role']}", "security_group")
22
- sgid = gt_securitygroup(data['role'], data['environment'], data['zone'], security_group)
23
- image_id = data['ami'] || $redis.hget("amis:#{data['role']}", data['zone'].chop) || $redis.hget("amis", data['zone'].chop)
24
- chef_runlist = $redis.hget("role:#{data['role']}", "chef_runlist")
25
-
26
- terminate = true
27
- unless data['terminate'].nil?
28
- terminate = false
29
- end
30
-
31
- unless data['chef_runlist'].nil?
32
- chef_runlist = data['chef_runlist'].to_json
33
- end
34
- instance = @ec2.instances.create(
35
- :image_id => image_id,
36
- :availability_zone => data['zone'],
37
- :instance_type => data['itype'],
38
- :key_name => "gaptool",
39
- :security_group_ids => sgid,
40
- :user_data => "#!/bin/bash\ncurl --silent -H 'X-GAPTOOL-USER: #{env['HTTP_X_GAPTOOL_USER']}' -H 'X-GAPTOOL-KEY: #{env['HTTP_X_GAPTOOL_KEY']}' #{$redis.hget('config', 'url')}/register -X PUT --data '#{data.to_json}' | bash"
41
- )
42
- # Add host tag
43
- instance.add_tag('Name', :value => "#{data['role']}-#{data['environment']}-#{instance.id}")
44
- instance.add_tag('gaptool', :value => "yes")
45
- # Create temporary redis entry for /register to pull the instance id
46
- # with an expire of 24h
47
- host_key = "instance:#{data['role']}:#{data['environment']}:#{@secret}"
48
- $redis.hmset(host_key, 'instance_id', instance.id,
49
- 'chef_branch', data['chef_branch'],
50
- 'chef_repo', data['chef_repo'],
51
- 'chef_runlist', chef_runlist,
52
- 'terminate', terminate)
53
- $redis.expire(host_key, 86400)
54
- "{\"instance\":\"#{instance.id}\"}"
55
- end
56
-
57
- post '/terminate' do
58
- data = JSON.parse request.body.read
59
- AWS.config(:access_key_id => $redis.hget('config', 'aws_id'),
60
- :secret_access_key => $redis.hget('config', 'aws_secret'),
61
- :ec2_endpoint => "ec2.#{data['zone']}.amazonaws.com")
62
- keys = $redis.keys("*:*:#{data['id']}")
63
- if keys.nil? || keys.empty?
64
- error 404
65
- end
66
- if keys.length > 1
67
- error 409
68
- end
69
- hdata = $redis.hgetall(keys.first)
70
- if hdata['terminate'] == false
71
- error 403
72
- end
73
-
74
- @ec2 = AWS::EC2.new
75
- @instance = @ec2.instances[data['id']]
76
- res = @instance.terminate
77
- res = $redis.del($redis.keys("*#{data['id']}"))
78
- out = {data['id'] => {'status'=> 'terminated'}}
79
- out.to_json
80
- end
81
-
82
- put '/register' do
83
- data = JSON.parse request.body.read
84
- AWS.config(:access_key_id => $redis.hget('config', 'aws_id'),
85
- :secret_access_key => $redis.hget('config', 'aws_secret'),
86
- :ec2_endpoint => "ec2.#{data['zone'].chop}.amazonaws.com")
87
- @ec2 = AWS::EC2.new
88
- host_key = "instance:#{data['role']}:#{data['environment']}:#{data['secret']}"
89
- host_data = $redis.hgetall(host_key)
90
- unless host_data
91
- error 403
92
- end
93
- @instance = @ec2.instances[host_data['instance_id']]
94
- hostname = @instance.dns_name
95
- $redis.del(host_key)
96
- @apps = []
97
- $redis.keys("app:*").each do |app|
98
- if $redis.hget(app, 'role') == data['role']
99
- @apps << app.gsub('app:', '')
100
- end
101
- end
102
-
103
- init_recipe = 'recipe[init]'
104
- @run_list = [init_recipe]
105
- unless host_data['chef_runlist'].nil?
106
- @run_list = [*eval(host_data['chef_runlist'])]
107
- unless @run_list.include? init_recipe
108
- @run_list.unshift(init_recipe)
109
- end
110
- data['chef_runlist'] = @run_list.to_json
111
- end
112
-
113
- if @run_list.length == 1 && @run_list[0] == init_recipe
114
- data.delete('chef_runlist')
115
- end
116
-
117
- data.merge!("capacity" => $redis.hget('capacity', data['itype']))
118
- data.merge!("hostname" => hostname)
119
- data.merge!("apps" => @apps.to_json)
120
- data.merge!("instance" => @instance.id)
121
- data['terminate'] = host_data['terminate'].nil? ? 'true' : host_data['terminate']
122
- hash2redis("host:#{data['role']}:#{data['environment']}:#{@instance.id}", data)
123
-
124
- @chef_repo = host_data['chef_repo'] && !host_data['chef_repo'].empty? ? host_data['chef_repo'] : $redis.hget('config', 'chefrepo')
125
- @chef_branch = host_data['chef_branch'] && !host_data['chef_branch'].empty? ? host_data['chef_branch'] : $redis.hget('config', 'chefbranch')
126
- @chef_environment = data['environment']
127
- @initkey = $redis.hget('config', 'initkey')
128
- @chef_version = $redis.hget("config", "chef_version") || "11.16.4"
129
- @json = {
130
- 'hostname' => hostname,
131
- 'recipe' => 'init',
132
- 'number' => @instance.id,
133
- 'instance' => @instance.id,
134
- 'run_list' => @run_list,
135
- 'role' => data['role'],
136
- 'environment' => data['environment'],
137
- 'chefrepo' => @chef_repo,
138
- 'chefbranch' => @chef_branch,
139
- 'identity' => $redis.hget('config','initkey'),
140
- 'appuser' => $redis.hget('config','appuser'),
141
- 'apps' => @apps
142
- }.to_json
143
-
144
- erb :init
145
- end
146
-
147
- get '/hosts' do
148
- out = []
149
- $redis.keys("host:*").each do |host|
150
- out << $redis.hgetall(host)
151
- end
152
- out.to_json
153
- end
154
-
155
- get '/apps' do
156
- out = {}
157
- $redis.keys("app:*").each do |app|
158
- out.merge!(app => $redis.hgetall(app))
159
- end
160
- out.to_json
161
- end
162
-
163
- get '/hosts/:role' do
164
- out = []
165
- $redis.keys("host:#{params[:role]}:*").each do |host|
166
- out << $redis.hgetall(host)
167
- end
168
- out.to_json
169
- end
170
-
171
- get '/instance/:id' do
172
- $redis.hgetall($redis.keys("host:*:*:#{params[:id]}").first).to_json
173
- end
174
-
175
- get '/hosts/:role/:environment' do
176
- out = []
177
- unless params[:role] == "ALL"
178
- $redis.keys("host:#{params[:role]}:#{params[:environment]}*").each do |host|
179
- out << $redis.hgetall(host)
180
- end
181
- else
182
- $redis.keys("host:*:#{params[:environment]}:*").each do |host|
183
- out << $redis.hgetall(host)
184
- end
185
- end
186
- out.to_json
187
- end
188
-
189
- get '/host/:role/:environment/:instance' do
190
- $redis.hgetall("host:#{params[:role]}:#{params[:environment]}:#{params[:instance]}").to_json
191
- end
192
-
193
- get '/ssh/:role/:environment/:instance' do
194
- @host = $redis.hget("host:#{params[:role]}:#{params[:environment]}:#{params[:instance]}", 'hostname')
195
- @key = putkey(@host)
196
- {'hostname' => @host, 'key' => @key, 'pubkey' => @pubkey}.to_json
197
- end
198
-
199
- get '/version' do
200
- version = File.read(File.realpath(
201
- File.join(File.dirname(__FILE__), "..", "..", 'VERSION')
202
- )).strip
203
- return {"server_version" => version, "api" => {"v0"=> "/"}}.to_json
204
- end
205
-
206
- end
data/lib/routes/rehash.rb DELETED
@@ -1,6 +0,0 @@
1
- # encoding: utf-8
2
- class GaptoolServer < Sinatra::Application
3
- post '/rehash' do
4
- rehash().to_json
5
- end
6
- end