lib/routes/main.rb in gaptool-server-0.5.5 vs lib/routes/main.rb in gaptool-server-0.5.6
- old
+ new
@@ -9,48 +9,28 @@
"PONG"
end
post '/init' 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'].chop}.amazonaws.com")
+ AWS.config(:access_key_id => $redis.hget('config', 'aws_id'),
+ :secret_access_key => $redis.hget('config', 'aws_secret'),
+ :ec2_endpoint => "ec2.#{data['zone'].chop}.amazonaws.com")
@ec2 = AWS::EC2.new
# create shared secret to reference in /register
@secret = (0...8).map{65.+(rand(26)).chr}.join
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)
- puts data['zone']
- if data['mirror']
- instance = @ec2.instances.create(
- :image_id => image_id,
- :availability_zone => data['zone'],
- :instance_type => data['itype'],
- :key_name => "gaptool",
- :security_group_ids => sgid,
- :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",
- :block_device_mappings => {
- "/dev/sdf" => {
- :volume_size => data['mirror'].to_i,
- :delete_on_termination => false
- },
- "/dev/sdg" => {
- :volume_size => data['mirror'].to_i,
- :delete_on_termination => false
- }
- }
- )
- else
- instance = @ec2.instances.create(
- :image_id => image_id,
- :availability_zone => data['zone'],
- :instance_type => data['itype'],
- :key_name => "gaptool",
- :security_group_ids => sgid,
- :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"
- )
- end
+ instance = @ec2.instances.create(
+ :image_id => image_id,
+ :availability_zone => data['zone'],
+ :instance_type => data['itype'],
+ :key_name => "gaptool",
+ :security_group_ids => sgid,
+ :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"
+ )
# Add host tag
instance.add_tag('Name', :value => "#{data['role']}-#{data['environment']}-#{instance.id}")
instance.add_tag('gaptool', :value => "yes")
# Create temporary redis entry for /register to pull the instance id
# with an expire of 24h
@@ -62,21 +42,25 @@
"{\"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")
+ 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")
@ec2 = AWS::EC2.new
@instance = @ec2.instances[data['id']]
res = @instance.terminate
res = $redis.del($redis.keys("*#{data['id']}"))
out = {data['id'] => {'status'=> 'terminated'}}
out.to_json
end
put '/register' 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'].chop}.amazonaws.com")
+ AWS.config(:access_key_id => $redis.hget('config', 'aws_id'),
+ :secret_access_key => $redis.hget('config', 'aws_secret'),
+ :ec2_endpoint => "ec2.#{data['zone'].chop}.amazonaws.com")
@ec2 = AWS::EC2.new
host_key = "instance:#{data['role']}:#{data['environment']}:#{data['secret']}"
host_data = $redis.hgetall(host_key)
unless host_data
error 403