Class: CloudJobAws::AwsConnector

Inherits:
CloudJobBase::CloudConnector
  • Object
show all
Defined in:
lib/cloud_job_aws.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ec2clientObject (readonly)

Returns the value of attribute ec2client.



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

def ec2client
  @ec2client
end

#ec2resourceObject (readonly)

Returns the value of attribute ec2resource.



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

def ec2resource
  @ec2resource
end

#key_pairObject (readonly)

Returns the value of attribute key_pair.



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

def key_pair
  @key_pair
end

#security_groupObject (readonly)

Returns the value of attribute security_group.



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

def security_group
  @security_group
end

Instance Method Details

#check_credentialsObject



20
21
22
23
24
25
# File 'lib/cloud_job_aws.rb', line 20

def check_credentials
    raise "Key not set." if ENV['AWS_ACCESS_KEY_ID'].nil?
    @key = ENV['AWS_ACCESS_KEY_ID']
    raise "Secret not set." if ENV['AWS_SECRET_ACCESS_KEY'].nil?
    @secret = ENV['AWS_SECRET_ACCESS_KEY']
end

#create_clientObject



27
28
29
30
31
32
33
# File 'lib/cloud_job_aws.rb', line 27

def create_client
    # credentials aren't mandatory if they are set in ENV, though having it here for extendability 
    @ec2client = Aws::EC2::Client.new(region: @region, credentials: Aws::Credentials.new(@key, @secret))
    @ec2resource = Aws::EC2::Resource.new(client: @ec2client)
    create_security_group
    create_key_pair
end

#create_key_pairObject



46
47
48
49
# File 'lib/cloud_job_aws.rb', line 46

def create_key_pair
    @key_pair = @ec2resource.create_key_pair({ key_name: "key-pair #{SecureRandom.uuid}"})
    puts @key_pair.inspect
end

#create_security_groupObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/cloud_job_aws.rb', line 35

def create_security_group
    begin
        @security_group = @ec2resource.create_security_group({ group_name: "SSH #{SecureRandom.uuid}", description: "ssh" })
        puts @security_group.inspect
        # allow port for ssh
        @security_group.authorize_ingress({ip_permissions: [{ ip_protocol: 'tcp', from_port: 22, to_port: 22, ip_ranges: [{ cidr_ip: '0.0.0.0/0'}] }] })
    rescue => ex
        puts ex.message
    end
end

#set_log_level(log_level) ⇒ Object



12
13
14
# File 'lib/cloud_job_aws.rb', line 12

def set_log_level(log_level)
    Aws.config.update({log_level: log_level})
end

#set_region(region) ⇒ Object



16
17
18
# File 'lib/cloud_job_aws.rb', line 16

def set_region(region)
    @region = region
end