lib/helpers/ec2.rb in gaptool-server-0.6.6 vs lib/helpers/ec2.rb in gaptool-server-0.7.0
- old
+ new
@@ -52,11 +52,11 @@
if ENV['DRYRUN']
id = "i-test#{SecureRandom.hex(2)}"
return {id: id,
hostname: "test-#{id}.#{data[:zone].chop}.compute.amazonaws.com",
instance: nil,
- launch_time: Time.now.to_s}
+ launch_time: Time.now.to_i}
end
configure_ec2 data[:zone].chop
ec2 = AWS::EC2.new
i = 0
@@ -69,17 +69,10 @@
@@logger.error("Error while creating instance: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})")
sleep sleeptime
retry
end
- begin
- launch_time = instance.launch_time.to_s
- launch_time = launch_time.empty? ? Time.now.to_s : launch_time
- rescue
- launch_time = Time.now.to_s
- end
-
i = 0
begin
hostname = instance.public_dns_name
rescue => e
i += 1
@@ -87,18 +80,43 @@
@@logger.error("Could not get hostname for instance #{instance} after #{retries} retries, setting to nil")
hostname = nil
Airbrake.notify_or_ignore(
e,
error_class: "EC2 public dns fail",
- parameters: {instance: instance[:id], role: data['role'], environment: data['environment'], hostname: nil}
+ parameters: {instance: instance.id, role: data['role'], environment: data['environment'], hostname: nil}
)
else
@@logger.error("Error getting hostname for instance: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})")
sleep sleeptime
retry
end
end
+
+ i = 0
+ begin
+ launch_time = instance.launch_time.to_i
+ raise unless launch_time.is_a? Integer
+
+ rescue => e
+ i += 1
+ if i > retries
+ @@logger.error("Cloud not get launch time for instance #{instance} after #{retries} retries. Setting to now()")
+ launch_time = Time.now.to_i
+ Airbrake.notify_or_ignore(
+ e,
+ error_class: "EC2 get launch_time fail",
+ parameters: {instance: instance.id, role: data['role'], environment: data['environment'], hostname: nil}
+ )
+ else
+ @@logger.error("Error getting launch_time for instance: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})")
+ sleep sleeptime
+ retry
+ end
+ end
+
+ @@logger.info {"Instance #{instance.id} launch_time: #{launch_time} #{instance.launch_time}"}
+
{
id: instance.id,
instance: instance,
hostname: hostname,
launch_time: launch_time
@@ -139,8 +157,23 @@
ec2 = AWS::EC2.new
instance = ec2.instances[id]
return {
hostname: instance.dns_name
}
+ end
+
+ def self.retag()
+ logger = Logger.new(STDOUT)
+ Gaptool::Data::servers.each do |id|
+ data = Gaptool::Data.get_server_data(id)
+ next if data.nil? || data['zone'].nil? || data['zone'].empty?
+ Gaptool::EC2::configure_ec2 data['zone'].chop
+ ec2 = AWS::EC2.new(region: data['zone'].chop)
+ instance = ec2.instances[id]
+ next if instance.tags.role && instance.tags.environment
+ logger.info("Retagging instance #{id} in zone #{data['zone'].chop}")
+ instance.tags.role = data['role']
+ instance.tags.environment = data['environment']
+ end
end
end
end