Class: AWSMine::AWSHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_minecraft/aws_helper.rb

Overview

Main wrapper for AWS commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAWSHelper

Returns a new instance of AWSHelper.



12
13
14
15
16
17
18
19
20
21
# File 'lib/aws_minecraft/aws_helper.rb', line 12

def initialize
  # region: AWS_REGION
  # Credentials are loaded from environment properties
  config = MineConfig.new
  credentials = Aws::SharedCredentials.new(profile_name: config.profile)
  @ec2_client = Aws::EC2::Client.new(credentials: credentials)
  @ec2_resource = Aws::EC2::Resource.new(client: @ec2_client)
  @logger = Logger.new($stdout)
  @logger.level = Logger.const_get(config.loglevel)
end

Instance Attribute Details

#ec2_clientObject

Returns the value of attribute ec2_client.



10
11
12
# File 'lib/aws_minecraft/aws_helper.rb', line 10

def ec2_client
  @ec2_client
end

#ec2_resourceObject

Returns the value of attribute ec2_resource.



10
11
12
# File 'lib/aws_minecraft/aws_helper.rb', line 10

def ec2_resource
  @ec2_resource
end

Instance Method Details

#create_ec2Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



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
# File 'lib/aws_minecraft/aws_helper.rb', line 25

def create_ec2
  @logger.info('Creating new EC2 instance.')
  config = File.read(File.join(__dir__, '../../cfg/ec2_conf.json'))
  @logger.debug("Configuration loaded: #{config}.")
  ec2_config = symbolize(JSON.parse(config))
  @logger.debug("Configuration symbolized: #{ec2_config}.")
  @logger.info('Importing keys.')
  import_keypair
  @logger.info('Creating security group.')
  sg_id = create_security_group
  ec2_config[:security_group_ids] = [sg_id]
  ec2_config[:user_data] = retrieve_user_data
  @logger.info('Creating instance.')
  instance = @ec2_resource.create_instances(ec2_config)[0]
  @logger.info('Instance created. Waiting for it to become available.')
  @ec2_resource.client.wait_until(:instance_status_ok,
                                  instance_ids: [instance.id]) do |w|
    w.before_wait do |_, _|
      @logger << '.'
    end
  end
  @logger.info("\n")
  @logger.info('Instance in running state.')
  pub_ip = @ec2_resource.instances(instance_ids: [instance.id]).first
  @logger.info('Instance started with ip | id: ' \
                "#{pub_ip.public_ip_address} | #{instance.id}.")
  [pub_ip.public_ip_address, instance.id]
end

#start_ec2(id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aws_minecraft/aws_helper.rb', line 78

def start_ec2(id)
  @ec2_client.start_instances(dry_run: false, instance_ids: [id])
  @ec2_resource.client.wait_until(:instance_running,
                                  instance_ids: [id]) do |w|
    w.before_wait do |_, _|
      @logger << '.'
    end
  end
  pub_ip = @ec2_resource.instances(instance_ids: [id]).first
  @logger.info("Instance started. New ip is:#{pub_ip.public_ip_address}.")
  pub_ip.public_ip_address
end

#state(id) ⇒ Object



91
92
93
94
95
# File 'lib/aws_minecraft/aws_helper.rb', line 91

def state(id)
  instance = @ec2_resource.instances(instance_ids: [id]).first
  @logger.debug("Response from describe_instances: #{instance}.")
  instance.state.name
end

#stop_ec2(id) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aws_minecraft/aws_helper.rb', line 66

def stop_ec2(id)
  @ec2_client.stop_instances(dry_run: false, instance_ids: [id])
  @ec2_resource.client.wait_until(:instance_stopped,
                                  instance_ids: [id]) do |w|
    w.before_wait do |_, _|
      @logger << '.'
    end
  end
  @logger.info("\n")
  @logger.info('Instance stopped. Goodbye.')
end

#terminate_ec2(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aws_minecraft/aws_helper.rb', line 54

def terminate_ec2(id)
  @ec2_client.terminate_instances(dry_run: false, instance_ids: [id])
  @ec2_resource.client.wait_until(:instance_terminated,
                                  instance_ids: [id]) do |w|
    w.before_wait do |_, _|
      @logger << '.'
    end
  end
  @logger.info("\n")
  @logger.info('Instance terminated. Goodbye.')
end