lib/routes/main.rb in gaptool-server-0.5.8 vs lib/routes/main.rb in gaptool-server-0.5.10
- old
+ new
@@ -20,10 +20,16 @@
data.merge!("secret" => @secret)
security_group = data['security_group'] || $redis.hget("role:#{data['role']}", "security_group")
sgid = gt_securitygroup(data['role'], data['environment'], data['zone'], security_group)
image_id = data['ami'] || $redis.hget("amis:#{data['role']}", data['zone'].chop) || $redis.hget("amis", data['zone'].chop)
chef_runlist = $redis.hget("role:#{data['role']}", "chef_runlist")
+
+ terminate = true
+ unless data['terminate'].nil?
+ terminate = false
+ end
+
unless data['chef_runlist'].nil?
chef_runlist = data['chef_runlist'].to_json
end
instance = @ec2.instances.create(
:image_id => image_id,
@@ -40,20 +46,33 @@
# with an expire of 24h
host_key = "instance:#{data['role']}:#{data['environment']}:#{@secret}"
$redis.hmset(host_key, 'instance_id', instance.id,
'chef_branch', data['chef_branch'],
'chef_repo', data['chef_repo'],
- 'chef_runlist', chef_runlist)
+ 'chef_runlist', chef_runlist,
+ 'terminate', terminate)
$redis.expire(host_key, 86400)
"{\"instance\":\"#{instance.id}\"}"
end
post '/terminate' do
data = JSON.parse request.body.read
AWS.config(:access_key_id => $redis.hget('config', 'aws_id'),
:secret_access_key => $redis.hget('config', 'aws_secret'),
:ec2_endpoint => "ec2.#{data['zone']}.amazonaws.com")
+ keys = $redis.keys("*:*:#{data['id']}")
+ if keys.nil? || keys.empty?
+ error 404
+ end
+ if keys.length >= 1
+ error 409
+ end
+ data = $redis.hgetall(keys.first)
+ if data['terminate'] == false
+ error 403
+ end
+
@ec2 = AWS::EC2.new
@instance = @ec2.instances[data['id']]
res = @instance.terminate
res = $redis.del($redis.keys("*#{data['id']}"))
out = {data['id'] => {'status'=> 'terminated'}}
@@ -81,21 +100,22 @@
end
end
init_recipe = 'recipe[init]'
@run_list = [init_recipe]
- unless host_data['chef_runlist'].nil? || host_data['chef_runlist'].empty?
- @run_list = [*host_data['chef_runlist']]
+ unless host_data['chef_runlist'].nil?
+ @run_list = [*eval(host_data['chef_runlist'])]
unless @run_list.include? init_recipe
@run_list.unshift(init_recipe)
end
data['chef_runlist'] = @run_list.to_json
end
data.merge!("capacity" => $redis.hget('capacity', data['itype']))
data.merge!("hostname" => hostname)
data.merge!("apps" => @apps.to_json)
data.merge!("instance" => @instance.id)
+ data['terminate'] = host_data['terminate'].nil? ? 'true' : host_data['terminate']
hash2redis("host:#{data['role']}:#{data['environment']}:#{@instance.id}", data)
@chef_repo = host_data['chef_repo'] && !host_data['chef_repo'].empty? ? host_data['chef_repo'] : $redis.hget('config', 'chefrepo')
@chef_branch = host_data['chef_branch'] && !host_data['chef_branch'].empty? ? host_data['chef_branch'] : $redis.hget('config', 'chefbranch')
@initkey = $redis.hget('config', 'initkey')