Class: Vanagon::Engine::Ec2

Inherits:
Base
  • Object
show all
Defined in:
lib/vanagon/engine/ec2.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#remote_workdir, #target

Instance Method Summary collapse

Methods inherited from Base

#build_host_name, #dispatch, #get_remote_workdir, #retrieve_built_artifact, #setup, #ship_workdir, #startup, #validate_platform

Constructor Details

#initialize(platform, target = nil, **opts) ⇒ Ec2

rubocop:disable Metrics/AbcSize



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vanagon/engine/ec2.rb', line 13

def initialize(platform, target = nil, **opts) # rubocop:disable Metrics/AbcSize
  super

  @ami = @platform.aws_ami
  @target_user = @platform.target_user
  @subnet_id = @platform.aws_subnet_id
  @userdata = @platform.aws_user_data
  @region = @platform.aws_region || 'us-east-1'
  @key_name = @platform.aws_key_name
  @key = @platform.aws_key
  @instance_type = @platform.aws_instance_type || "t1.micro"
  @required_attributes = ["ssh_port", "aws_ami", "aws_key_name"]
  @shutdown_behavior = @platform.aws_shutdown_behavior

  @ec2 = ::Aws::EC2::Client.new(region: @region)
  @resource = ::Aws::EC2::Resource.new(client: @ec2)
end

Instance Attribute Details

#amiObject

Returns the value of attribute ami.



10
11
12
# File 'lib/vanagon/engine/ec2.rb', line 10

def ami
  @ami
end

#instance_typeObject

Returns the value of attribute instance_type.



11
12
13
# File 'lib/vanagon/engine/ec2.rb', line 11

def instance_type
  @instance_type
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/vanagon/engine/ec2.rb', line 10

def key
  @key
end

#key_nameObject

Returns the value of attribute key_name.



10
11
12
# File 'lib/vanagon/engine/ec2.rb', line 10

def key_name
  @key_name
end

#shutdown_behaviorObject

Returns the value of attribute shutdown_behavior.



10
11
12
# File 'lib/vanagon/engine/ec2.rb', line 10

def shutdown_behavior
  @shutdown_behavior
end

#subnet_idObject

Returns the value of attribute subnet_id.



11
12
13
# File 'lib/vanagon/engine/ec2.rb', line 11

def subnet_id
  @subnet_id
end

#userdataObject

Returns the value of attribute userdata.



10
11
12
# File 'lib/vanagon/engine/ec2.rb', line 10

def userdata
  @userdata
end

Instance Method Details

#get_userdataObject



35
36
37
38
39
# File 'lib/vanagon/engine/ec2.rb', line 35

def get_userdata
  unless @userdata.nil?
    Base64.encode64(ERB.new(@userdata).result(binding))
  end
end

#instanceObject



56
57
58
# File 'lib/vanagon/engine/ec2.rb', line 56

def instance
  @instance ||= instances.first
end

#instancesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vanagon/engine/ec2.rb', line 41

def instances
  @instances ||= @resource.create_instances({
    image_id: ami,
    min_count: 1,
    max_count: 1,
    key_name: key_name,
    instance_type: instance_type,
    subnet_id: subnet_id,
    user_data: get_userdata,
    monitoring: {
      enabled: false,
    }
  })
end

#nameObject



31
32
33
# File 'lib/vanagon/engine/ec2.rb', line 31

def name
  'ec2'
end

#select_targetObject



60
61
62
63
64
65
66
67
68
# File 'lib/vanagon/engine/ec2.rb', line 60

def select_target
  VanagonLogger.info "Instance created id: #{instance.id}"
  VanagonLogger.info "Created instance waiting for status ok"
  @ec2.wait_until(:instance_status_ok, instance_ids: [instance.id])
  VanagonLogger.info "Instance running"
  @target = instance.private_ip_address
rescue ::Aws::Waiters::Errors::WaiterFailed => error
  fail "Failed to wait for ec2 instance to start got error #{error}"
end

#teardownObject



70
71
72
73
# File 'lib/vanagon/engine/ec2.rb', line 70

def teardown
  VanagonLogger.info "Destroying instance on AWS id: #{instance.id}"
  instances.batch_terminate!
end