Class: CORL::Machine::AWS

Inherits:
FogBase
  • Object
show all
Defined in:
lib/CORL/machine/AWS.rb

Instance Method Summary collapse

Methods inherited from FogBase

#compute, #compute=, #created?, #download, #exec, #image, #images, #load, #machine_type, #machine_types, #normalize, #private_ip, #public_ip, #running?, #server, #server=, #ssh_wait_for_ready, #state, #stop, #terminal, #upload

Methods included from CORL::Mixin::Machine::SSH

#close_ssh_session, #init_ssh_session, #ssh_download, #ssh_exec, #ssh_terminal, #ssh_upload, #ssh_wait_for_ready

Instance Method Details

#create(options = {}) ⇒ Object




48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/CORL/machine/AWS.rb', line 48

def create(options = {})
  super do |config|
    # Keypair initialization
    if key_pair = compute.key_pairs.get(keypair_name)
      key_pair.destroy
    end
    compute.key_pairs.create(
      :name       => keypair_name,
      :public_key => Util::Disk.read(node.public_key)
    )
    config[:key_name] = keypair_name
  end
end

#create_image(options = {}) ⇒ Object




75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/CORL/machine/AWS.rb', line 75

def create_image(options = {})
  super do |image_name, config, success|
    image_name        = image_name.gsub(/[^A-Za-z0-9\(\)\.\-\_\/]+/, '_')
    image_description = config.get(:description, "CORL backup image")

    data     = compute.create_image(server.identity, image_name, image_description)
    image_id = data.body['imageId']

    Fog.wait_for do
      compute.describe_images('ImageId' => image_id).body['imagesSet'].first['imageState'] == 'available'
    end

    if image_id
      node[:image] = image_id
      success      = true
    end
    success
  end
end

#destroy(options = {}) ⇒ Object




97
98
99
100
101
102
103
104
105
106
107
# File 'lib/CORL/machine/AWS.rb', line 97

def destroy(options = {})
  super do |config|
    unless config.get(:stop, false)
      # Keypair destruction
      if key_pair = compute.key_pairs.get(keypair_name)
        key_pair.destroy
      end
    end
    true
  end
end

#ensure_security_group(group_name, from_port, to_port = nil, options = {}) ⇒ Object




118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/CORL/machine/AWS.rb', line 118

def ensure_security_group(group_name, from_port, to_port = nil, options = {})
  config         = Config.ensure(options)
  security_group = compute.security_groups.get(group_name)
  cidrip         = config.get(:cidrip, '0.0.0.0/0')
  protocol       = config.get(:protocol, 'tcp')
  to_port        = from_port if to_port.nil?

  if security_group.nil?
    security_group = compute.security_groups.create(
      :name        => group_name,
      :description => config.get(:description, "Opening port range: #{from_port} to #{to_port}")
    )
    raise unless security_group # TODO: Better error class
  end

  authorized = false
  if security_group.ip_permissions
    authorized = security_group.ip_permissions.detect do |ip_permission|
      ip_permission['ipRanges'].first && ip_permission['ipRanges'].first['cidrIp'] == cidrip &&
      ip_permission['fromPort'] == from_port &&
      ip_permission['ipProtocol'] == protocol &&
      ip_permission['toPort'] == to_port
    end
  end
  unless authorized
    security_group.authorize_port_range(Range.new(from_port, to_port))
  end

  if server
    server.groups = [ group_name ] | server.groups
    server.save
  end
end

#init_serverObject


Management



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/CORL/machine/AWS.rb', line 21

def init_server
  super do
    myself.plugin_name = @server.id

    node[:id]           = plugin_name
    node[:public_ip]    = @server.public_ip_address
    node[:private_ip]   = @server.private_ip_address
    node[:machine_type] = @server.flavor_id
    node[:image]        = @server.image_id
    node.user           = @server.username unless node.user

    @server.private_key_path = node.private_key if node.private_key
    @server.public_key_path  = node.public_key if node.public_key
  end
end

#init_ssh(ssh_port) ⇒ Object




39
40
41
42
43
44
# File 'lib/CORL/machine/AWS.rb', line 39

def init_ssh(ssh_port)
  # Security group initialization
  if compute && ssh_port != 22
    ensure_security_group("CORL_SSH_#{ssh_port}", ssh_port)
  end
end

#keypair_nameObject


Utilities



112
113
114
# File 'lib/CORL/machine/AWS.rb', line 112

def keypair_name
  "CORL_#{node.plugin_name}"
end

#reload(options = {}) ⇒ Object




64
65
66
67
68
69
70
71
# File 'lib/CORL/machine/AWS.rb', line 64

def reload(options = {})
  super do |config|
    success = server.reboot

    server.wait_for { ready? } if success
    success
  end
end

#set_connectionObject


Property accessors / modifiers



12
13
14
15
16
# File 'lib/CORL/machine/AWS.rb', line 12

def set_connection
  require 'unf'
  super
  Kernel.load File.join(File.dirname(__FILE__), '..', '..', 'core', 'mod', 'fog_aws_server.rb')
end