Class: Inception::Providers::Clients::FogProviderClient

Inherits:
Object
  • Object
show all
Defined in:
lib/inception/providers/clients/fog_provider_client.rb

Direct Known Subclasses

AwsProviderClient, OpenStackProviderClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ FogProviderClient

Returns a new instance of FogProviderClient.



10
11
12
13
14
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 10

def initialize(attributes)
  @attributes = attributes.is_a?(Hash) ? ReadWriteSettings.new(attributes) : attributes
  raise "@attributes must be ReadWriteSettings (or Hash)" unless @attributes.is_a?(ReadWriteSettings)
  setup_fog_connection
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 8

def attributes
  @attributes
end

#fog_computeObject (readonly)

Returns the value of attribute fog_compute.



7
8
9
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 7

def fog_compute
  @fog_compute
end

Instance Method Details

#authorize_port_range(sg, port_range, protocol, ip_range) ⇒ Object



122
123
124
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 122

def authorize_port_range(sg, port_range, protocol, ip_range)
  sg.authorize_port_range(port_range, {:ip_protocol => protocol, :cidr_ip => ip_range})
end

#bootstrap(new_attributes) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 167

def bootstrap(new_attributes)
  server = fog_compute.servers.new(new_attributes)

  unless new_attributes[:key_name]
    raise "please provide :key_name attribute"
  end
  unless private_key_path = new_attributes.delete(:private_key_path)
    raise "please provide :private_key_path attribute"
  end

  server.save
  unless Fog.mocking?
    server.wait_for { ready? }
    server.setup(keys: [private_key_path])
  end
  server
end

#cleanup_unused_ip_addressesObject

Destroy all IP addresses that aren’t bound to a server



61
62
63
64
65
66
67
68
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 61

def cleanup_unused_ip_addresses
  fog_compute.addresses.each do |a|
    unless a.server
      puts "Deleting unused IP address #{a.public_ip}... "
      a.destroy
    end
  end
end

#create_key_pair(key_pair_name) ⇒ Object



26
27
28
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 26

def create_key_pair(key_pair_name)
  fog_compute.key_pairs.create(:name => key_pair_name)
end

#create_security_group(security_group_name, description, ports) ⇒ Object

Creates or reuses an security group and opens ports.

security_group_name is the name to be created or reused ports is a hash of name/port for ports to open, for example:

ssh: 22,
http: 80,
https: 443

protocol defaults to TCP You can also use a more verbose ports using the format: {

ssh: 22,
http: { ports: (80..82) },
mosh: { protocol: "udp", ports: (60000..60050) }
mosh: { protocol: "rdp", ports: (3398..3398), ip_ranges: [ { cidrIp: "196.212.12.34/32" } ] }

} In this example,

* TCP 22 will be opened for ssh from any ip_range,
* TCP ports 80, 81, 82 for http from any ip_range,
* UDP 60000 -> 60050 for mosh from any ip_range and
* TCP 3398 for RDP from ip range: 96.212.12.34/32


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 92

def create_security_group(security_group_name, description, ports)
  security_groups = fog_compute.security_groups
  unless sg = security_groups.find { |s| s.name == security_group_name }
    sg = fog_compute.security_groups.create(name: security_group_name, description: description)
    puts "Created security group #{security_group_name}"
    ip_permissions = ip_permissions(sg)
    ports_opened = 0
    ports.each do |name, port_defn|
      (protocol, port_range, ip_range) = extract_port_definition(port_defn)
      unless port_open?(ip_permissions, port_range, protocol, ip_range)
        authorize_port_range(sg, port_range, protocol, ip_range)
        puts " -> opened #{name} ports #{protocol.upcase} #{port_range.min}..#{port_range.max} from IP range #{ip_range}"
        ports_opened += 1
      end
    end
  else
    puts "Reusing security group #{security_group_name}"
  end
  true
end

#delete_key_pair_if_exists(key_pair_name) ⇒ Object



37
38
39
40
41
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 37

def delete_key_pair_if_exists(key_pair_name)
  if fog_key_pair = fog_compute.key_pairs.get(key_pair_name)
    fog_key_pair.destroy
  end
end

#delete_servers_with_name(name) ⇒ Object



43
44
45
46
47
48
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 43

def delete_servers_with_name(name)
  fog_compute.servers.select {|s| s.tags["Name"] && (s.tags["Name"].downcase == name.downcase) }.each do |server|
    puts "Destroying server #{server.id}... "
    server.destroy
  end
end

#delete_volumes_with_name(name) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 50

def delete_volumes_with_name(name)
  fog_compute.volumes.select do |v|
    volume_name = v.tags["Name"]
    volume_name && volume_name.downcase == name.downcase
  end.each do |volume|
    puts "Destroying volume #{volume.id}... "
    volume.destroy
  end
end

#extract_port_definition(port_defn) ⇒ Object

Any of the following port_defn can be used: {

ssh: 22,
http: { ports: (80..82) },
mosh: { protocol: "udp", ports: (60000..60050) }
mosh: { protocol: "rdp", ports: (3398..3398), ip_range: "196.212.12.34/32" }

} In this example,

* TCP 22 will be opened for ssh from any ip_range,
* TCP ports 80, 81, 82 for http from any ip_range,
* UDP 60000 -> 60050 for mosh from any ip_range and
* TCP 3398 for RDP from ip range: 96.212.12.34/32


142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 142

def extract_port_definition(port_defn)
  protocol = "tcp"
  ip_range = "0.0.0.0/0"
  if port_defn.is_a? Integer
    port_range = (port_defn..port_defn)
  elsif port_defn.is_a? Range
    port_range = port_defn
  elsif port_defn.is_a? Hash
    protocol = port_defn[:protocol] if port_defn[:protocol]
    port_range = port_defn[:ports]  if port_defn[:ports]
    ip_range = port_defn[:ip_range] if port_defn[:ip_range]
  end
  [protocol, port_range, ip_range]
end

#find_unused_public_ip_address(options = {}) ⇒ Object



161
162
163
164
165
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 161

def find_unused_public_ip_address(options={})
  if address = fog_compute.addresses.find { |s| s.server_id.nil? }
    address.public_ip
  end
end

#flavor_id(flavor_name) ⇒ Object



16
17
18
19
20
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 16

def flavor_id(flavor_name)
  if flavor = fog_compute.flavors.find { |fl| fl.name == flavor_name }
    flavor.id
  end
end

#ip_permissions(sg) ⇒ Object



126
127
128
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 126

def ip_permissions(sg)
  sg.ip_permissions
end

#port_open?(ip_permissions, port_range, protocol, ip_range) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 113

def port_open?(ip_permissions, port_range, protocol, ip_range)
  ip_permissions && ip_permissions.find do |ip| 
    ip["ipProtocol"] == protocol \
    && ip["ipRanges"].detect { |range| range["cidrIp"] == ip_range } \
    && ip["fromPort"] <= port_range.min \
    && ip["toPort"] >= port_range.max
  end
end

#provision_or_reuse_public_ip_address(options = {}) ⇒ Object



157
158
159
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 157

def provision_or_reuse_public_ip_address(options={})
  provision_public_ip_address(options) || find_unused_public_ip_address(options)
end

#set_resource_name(resource, name) ⇒ Object

set_resource_name(fog_server, “inception”) set_resource_name(volume, “inception-root”) set_resource_name(volume, “inception-store”)



33
34
35
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 33

def set_resource_name(resource, name)
  fog_compute.tags.create :key => "Name", :value => name, :resource_id => resource.id
end

#setup_fog_connectionObject



22
23
24
# File 'lib/inception/providers/clients/fog_provider_client.rb', line 22

def setup_fog_connection
  raise "must implement"
end