Class: VagrantPlugins::AWS::Action::RunInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-aws/action/run_instance.rb

Overview

This runs the configured instance.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RunInstance

Returns a new instance of RunInstance.



14
15
16
17
# File 'lib/vagrant-aws/action/run_instance.rb', line 14

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_aws::action::run_instance")
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/vagrant-aws/action/run_instance.rb', line 19

def call(env)
  # Initialize metrics if they haven't been
  env[:metrics] ||= {}

  # Get the region we're going to booting up in
  region = env[:machine].provider_config.region

  # Get the configs
  region_config      = env[:machine].provider_config.get_region_config(region)
  ami                = region_config.ami
  availability_zone  = region_config.availability_zone
  instance_type      = region_config.instance_type
  ssh_port           = region_config.ssh_port
  keypair            = region_config.keypair_name
  private_ip_address = region_config.private_ip_address
  security_groups    = region_config.security_groups
  subnet_id          = region_config.subnet_id
  tags               = region_config.tags

  # If there is no keypair then warn the user
  if !keypair
    env[:ui].warn(I18n.t("vagrant_aws.launch_no_keypair"))
  end

  # If there is a subnet ID then warn the user
  if subnet_id
    env[:ui].warn(I18n.t("vagrant_aws.launch_vpc_warning"))
  end

  # Launch!
  env[:ui].info(I18n.t("vagrant_aws.launching_instance"))
  env[:ui].info(" -- Type: #{instance_type}")
  env[:ui].info(" -- AMI: #{ami}")
  env[:ui].info(" -- Region: #{region}")
  env[:ui].info(" -- Availability Zone: #{availability_zone}") if availability_zone
  env[:ui].info(" -- SSH Port: #{ssh_port}") if ssh_port
  env[:ui].info(" -- Keypair: #{keypair}") if keypair
  env[:ui].info(" -- Subnet ID: #{subnet_id}") if subnet_id
  env[:ui].info(" -- Private IP: #{private_ip_address}") if private_ip_address
  env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?

  begin
    options = {
      :availability_zone  => availability_zone,
      :flavor_id          => instance_type,
      :image_id           => ami,
      :key_name           => keypair,
      :ssh_port           => ssh_port,
      :private_ip_address => private_ip_address,
      :subnet_id          => subnet_id,
      :tags               => tags
    }

    if !security_groups.empty?
      security_group_key = options[:subnet_id].nil? ? :groups : :security_group_ids
      options[security_group_key] = security_groups
    end

    server = env[:aws_compute].servers.create(options)
  rescue Fog::Compute::AWS::NotFound => e
    # Invalid subnet doesn't have its own error so we catch and
    # check the error message here.
    if e.message =~ /subnet ID/
      raise Errors::FogError,
        :message => "Subnet ID not found: #{subnet_id}"
    end

    raise
  rescue Fog::Compute::AWS::Error => e
    raise Errors::FogError, :message => e.message
  end

  # Immediately save the ID since it is created at this point.
  env[:machine].id = server.id

  # Wait for the instance to be ready first
  env[:metrics]["instance_ready_time"] = Util::Timer.time do
    env[:ui].info(I18n.t("vagrant_aws.waiting_for_ready"))
    retryable(:on => Fog::Errors::TimeoutError, :tries => 30) do
      # If we're interrupted don't worry about waiting
      next if env[:interrupted]

      # Wait for the server to be ready
      server.wait_for(2) { ready? }
    end
  end

  @logger.info("Time to instance ready: #{env[:metrics]["instance_ready_time"]}")

  if !env[:interrupted]
    env[:metrics]["instance_ssh_time"] = Util::Timer.time do
      # Wait for SSH to be ready.
      env[:ui].info(I18n.t("vagrant_aws.waiting_for_ssh"))
      while true
        # If we're interrupted then just back out
        break if env[:interrupted]
        break if env[:machine].communicate.ready?
        sleep 2
      end
    end

    @logger.info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}")

    # Ready and booted!
    env[:ui].info(I18n.t("vagrant_aws.ready"))
  end

  # Terminate the instance if we were interrupted
  terminate(env) if env[:interrupted]

  @app.call(env)
end

#recover(env) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/vagrant-aws/action/run_instance.rb', line 132

def recover(env)
  return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)

  if env[:machine].provider.state.id != :not_created
    # Undo the import
    terminate(env)
  end
end

#terminate(env) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/vagrant-aws/action/run_instance.rb', line 141

def terminate(env)
  destroy_env = env.dup
  destroy_env.delete(:interrupted)
  destroy_env[:config_validate] = false
  destroy_env[:force_confirm_destroy] = true
  env[:action_runner].run(Action.action_destroy, destroy_env)
end