Class: AmiSpec::AwsInstance

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ami_spec/aws_instance.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AwsInstance

Returns a new instance of AwsInstance.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ami_spec/aws_instance.rb', line 15

def initialize(options)
  @role = options.fetch(:role)
  @ami = options.fetch(:ami)
  @subnet_id = options.fetch(:subnet_id)
  @key_name = options.fetch(:key_name)
  @instance_type = options.fetch(:aws_instance_type)
  @public_ip = options.fetch(:aws_public_ip)
  @associate_public_ip = options.fetch(:associate_public_ip)
  @region = options.fetch(:aws_region)
  @security_group_ids = options.fetch(:aws_security_groups)
  @tags = ec2ify_tags(options.fetch(:tags))
  @user_data_file = options.fetch(:user_data_file, nil)
  @iam_instance_profile_arn = options.fetch(:iam_instance_profile_arn, nil)
  @logger = options.fetch(:logger)
end

Class Method Details

.start(args) ⇒ Object



9
10
11
12
13
# File 'lib/ami_spec/aws_instance.rb', line 9

def self.start(args)
  new(args).tap do |instance|
    instance.start
  end
end

Instance Method Details

#startObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ami_spec/aws_instance.rb', line 33

def start
  @logger.info "Creating AWS EC2 instance for #{@ami}"
  client = Aws::EC2::Client.new(client_options)
  placeholder_instance = client.run_instances(instances_options).instances.first

  @instance = Aws::EC2::Instance.new(placeholder_instance.instance_id, client_options)
  @logger.info "Waiting for AWS EC2 instance to start: #{@instance.id}"
  @instance.wait_until_running
  tag_instance
  @logger.info "AWS EC2 instance started: #{@instance.id}"
end

#terminateObject



45
46
47
48
49
50
# File 'lib/ami_spec/aws_instance.rb', line 45

def terminate
  @logger.info "Terminating AWS EC2 instance: #{@instance.id}"
  @instance.terminate
  @instance.wait_until_terminated
  @logger.info "AWS EC2 instance terminated: #{@instance.id}"
end