Class: Bosh::Providers::Clients::FogProviderClient
- Inherits:
-
Object
- Object
- Bosh::Providers::Clients::FogProviderClient
- Defined in:
- lib/bosh/providers/clients/fog_provider_client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#fog_compute ⇒ Object
readonly
Returns the value of attribute fog_compute.
Instance Method Summary collapse
- #authorize_port_range(sg, port_range, protocol, ip_range) ⇒ Object
-
#cleanup_unused_ip_addresses ⇒ Object
Destroy all IP addresses that aren’t bound to a server.
- #create_key_pair(key_pair_name) ⇒ Object
-
#create_security_group(security_group_name, description, ports) ⇒ Object
Creates or reuses an security group and opens ports.
- #delete_key_pair_if_exists(key_pair_name) ⇒ Object
- #delete_servers_with_name(name) ⇒ Object
- #delete_volumes_with_name(name) ⇒ Object
-
#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. - #find_unused_public_ip_address(options = {}) ⇒ Object
-
#initialize(attributes) ⇒ FogProviderClient
constructor
A new instance of FogProviderClient.
- #ip_permissions(sg) ⇒ Object
- #port_open?(ip_permissions, port_range, protocol, ip_range) ⇒ Boolean
- #provision_or_reuse_public_ip_address(options = {}) ⇒ Object
-
#set_resource_name(resource, name) ⇒ Object
set_resource_name(fog_server, “inception”) set_resource_name(volume, “inception-root”) set_resource_name(volume, “inception-store”).
- #setup_fog_connection ⇒ Object
Constructor Details
#initialize(attributes) ⇒ FogProviderClient
Returns a new instance of FogProviderClient.
10 11 12 13 14 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 10 def initialize(attributes) @attributes = attributes.is_a?(Hash) ? Settingslogic.new(attributes) : attributes raise "@attributes must be Settingslogic (or Hash)" unless @attributes.is_a?(Settingslogic) setup_fog_connection end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 8 def attributes @attributes end |
#fog_compute ⇒ Object (readonly)
Returns the value of attribute fog_compute.
7 8 9 |
# File 'lib/bosh/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
117 118 119 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 117 def (sg, port_range, protocol, ip_range) sg.(port_range, {:ip_protocol => protocol, :cidr_ip => ip_range}) end |
#cleanup_unused_ip_addresses ⇒ Object
Destroy all IP addresses that aren’t bound to a server
55 56 57 58 59 60 61 62 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 55 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
20 21 22 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 20 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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 86 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}" else puts "Reusing security group #{security_group_name}" end = (sg) ports_opened = 0 ports.each do |name, port_defn| (protocol, port_range, ip_range) = extract_port_definition(port_defn) unless port_open?(, port_range, protocol, ip_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 puts " -> no additional ports opened" if ports_opened == 0 true end |
#delete_key_pair_if_exists(key_pair_name) ⇒ Object
31 32 33 34 35 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 31 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
37 38 39 40 41 42 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 37 def delete_servers_with_name(name) fog_compute.servers.select {|s| s.["Name"].downcase == name.downcase }.each do |server| puts "Destroying server #{server.id}..." server.destroy end end |
#delete_volumes_with_name(name) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 44 def delete_volumes_with_name(name) fog_compute.volumes.select do |v| volume_name = v.["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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 137 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
156 157 158 159 160 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 156 def find_unused_public_ip_address(={}) if address = fog_compute.addresses.find { |s| s.server_id.nil? } address.public_ip end end |
#ip_permissions(sg) ⇒ Object
121 122 123 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 121 def (sg) sg. end |
#port_open?(ip_permissions, port_range, protocol, ip_range) ⇒ Boolean
108 109 110 111 112 113 114 115 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 108 def port_open?(, port_range, protocol, ip_range) && .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
152 153 154 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 152 def provision_or_reuse_public_ip_address(={}) provision_public_ip_address() || find_unused_public_ip_address() 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”)
27 28 29 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 27 def set_resource_name(resource, name) fog_compute..create :key => "Name", :value => name, :resource_id => resource.id end |
#setup_fog_connection ⇒ Object
16 17 18 |
# File 'lib/bosh/providers/clients/fog_provider_client.rb', line 16 def setup_fog_connection raise "must implement" end |