Class: Forger::Create::Waiter

Inherits:
Base
  • Object
show all
Includes:
AwsServices
Defined in:
lib/forger/create/waiter.rb

Constant Summary

Constants inherited from Base

Base::BUILD_ROOT, Base::SCRIPTS_INFO_PATH

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #ec2, #s3

Methods inherited from Base

#derandomize, #initialize, #randomize

Constructor Details

This class inherits a constructor from Forger::Base

Instance Method Details

#build_ssh_command(host) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/forger/create/waiter.rb', line 55

def build_ssh_command(host)
  user = @options[:ssh_user] || "ec2-user"
  [
    "ssh",
    ENV['SSH_OPTIONS'],
    "#{user}@#{host}"
  ].compact
end

#display_ssh(command) ⇒ Object



64
65
66
# File 'lib/forger/create/waiter.rb', line 64

def display_ssh(command)
  puts "=> #{command.join(' ')}".color(:green)
end

#handle_sshObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/forger/create/waiter.rb', line 37

def handle_ssh
  instance = handle_wait
  unless instance.public_dns_name
    puts "This instance does not have a public dns for ssh."
    return
  end

  command = build_ssh_command(instance.public_dns_name)
  display_ssh(command)
  retry_until_success(command)
  Kernel.exec(*command) unless @options[:noop]
end

#handle_waitObject



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
# File 'lib/forger/create/waiter.rb', line 11

def handle_wait
  puts "Waiting for instance #{@instance_id} to be ready."
  # https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/using-waiters.html
  ec2.wait_until(:instance_running, instance_ids:[@instance_id]) do |w|
    w.interval = 5
    w.before_wait do |n, resp|
      print '.'
    end
  end
  puts "" # newline

  resp = ec2.describe_instances(instance_ids:[@instance_id])
  i = resp.reservations.first.instances.first
  puts "Instance #{@instance_id} is ready"
  dns = i.public_dns_name ? i.public_dns_name : 'nil'
  puts "Instance public dns name: #{dns}"

  if i.public_dns_name && !@options[:ssh]
    command = build_ssh_command(i.public_dns_name)
    puts "Ssh command below. Note the user might be different. You can specify --ssh-user=USER.  You can also ssh automatically into the instance with the --ssh flag."
    display_ssh(command)
  end

  i
end

#retry_until_success(*command) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/forger/create/waiter.rb', line 68

def retry_until_success(*command)
  retries = 0
  uptime = command + ['uptime', '2>&1']
  uptime = uptime.join(' ')
  out = `#{uptime}`
  while out !~ /load average/ do
    puts "Can't ssh into the server yet.  Retrying until success. (Timeout 10m)" if retries == 0
    print '.'
    retries += 1
    if retries > 600 # Timeout after 10 minutes
      raise "ERROR: Timeout after 600 seconds, cannot connect to the server."
    end
    sleep 1
    out = `#{uptime}`
  end
  puts ""
end

#waitObject



5
6
7
8
9
# File 'lib/forger/create/waiter.rb', line 5

def wait
  @instance_id = @options[:instance_id]
  handle_wait if wait?
  handle_ssh if @options[:ssh]
end

#wait?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/forger/create/waiter.rb', line 50

def wait?
  return false if @options[:ssh]
  @options[:wait]
end