vagrant-softlayer 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,95 +1,95 @@
1
- module VagrantPlugins
2
- module SoftLayer
3
- module Action
4
- # Look for defined load balancers and perform join operations.
5
- class JoinLoadBalancer
6
- include Util::LoadBalancer
7
- include Util::Network
8
- include Util::Warden
9
-
10
- def initialize(app, env)
11
- @app = app
12
- @logger = Log4r::Logger.new("vagrant_softlayer::action::join_load_balancer")
13
- end
14
-
15
- def call(env)
16
- @env = env
17
-
18
- if enabled?
19
- setup
20
- prepare
21
- join!
22
- rebalance!
23
- end
24
-
25
- @app.call(@env)
26
- end
27
-
28
- def append_service_group(cfg, idx)
29
- {}.tap do |virtual_server|
30
- virtual_server["allocation"] = 1
31
- virtual_server["port"] = cfg[:port]
32
- virtual_server["serviceGroups"] = [
33
- {
34
- "routingMethodId" => (@enums["Routing_Method"][cfg[:method]] || 10),
35
- "routingTypeId" => (@enums["Routing_Type"][cfg[:type]] || 3),
36
- "services" => []
37
- }
38
- ]
39
- @load_balancers[idx]["virtualServers"] << virtual_server
40
- end
41
- end
42
-
43
- def join!
44
- @pending = []
45
-
46
- until @queue.empty?
47
- job = @queue.pop
48
- merge(job[:cfg], job[:idx])
49
- end
50
-
51
- # Perform the API calls for join.
52
- @load_balancers.each_with_index do |lb, idx|
53
- next unless @pending[idx]
54
- @logger.debug("Updating VIP #{lb['id']} with: #{lb['virtualServers']}")
55
- vip_id = @services["VirtualIpAddress"].object_with_id(lb["id"])
56
- sl_warden { vip_id.editObject("virtualServers" => lb["virtualServers"]) }
57
- end
58
- end
59
-
60
- def merge(cfg, idx)
61
- # Get the service group. Create it if not found.
62
- sg = @load_balancers[idx]["virtualServers"].find(lambda { append_service_group(cfg, idx) }) { |g| g["port"] == cfg[:port] }
63
- # Get the IP address ID of the current machine.
64
- ip_id = ip_address_id(@env)
65
- unless sg["serviceGroups"].first["services"].index { |s| s["ipAddressId"] == ip_id }
66
- @logger.debug("Merging service: #{cfg[:service]}")
67
- # Add the service to the group.
68
- sg["serviceGroups"].first["services"] << {
69
- "enabled" => 1,
70
- "ipAddressId" => ip_id,
71
- "groupReferences" => [ { "weight" => cfg[:service].weight } ],
72
- "healthChecks" => [ { "healthCheckTypeId" => (@enums["Health_Check_Type"][cfg[:service].health_check] || 21) } ],
73
- "port" => cfg[:service].destination_port
74
- }
75
- # Mark the load balancer object as pending update
76
- @pending[idx] = true
77
- end
78
- end
79
-
80
- def prepare
81
- @env[:ui].info I18n.t("vagrant_softlayer.vm.joining_load_balancers")
82
-
83
- # For each definition, check if the load balancer exists and enqueue
84
- # the join operation.
85
- @queue = []
86
- @env[:machine].provider_config.load_balancers.each do |cfg|
87
- idx = @load_balancers.index { |lb| lb["ipAddress"]["ipAddress"] == cfg[:vip] }
88
- raise Errors::SLLoadBalancerNotFound unless idx
89
- @queue << { :cfg => cfg, :idx => idx }
90
- end
91
- end
92
- end
93
- end
94
- end
95
- end
1
+ module VagrantPlugins
2
+ module SoftLayer
3
+ module Action
4
+ # Look for defined load balancers and perform join operations.
5
+ class JoinLoadBalancer
6
+ include Util::LoadBalancer
7
+ include Util::Network
8
+ include Util::Warden
9
+
10
+ def initialize(app, env)
11
+ @app = app
12
+ @logger = Log4r::Logger.new("vagrant_softlayer::action::join_load_balancer")
13
+ end
14
+
15
+ def call(env)
16
+ @env = env
17
+
18
+ if enabled?
19
+ setup
20
+ prepare
21
+ join!
22
+ rebalance!
23
+ end
24
+
25
+ @app.call(@env)
26
+ end
27
+
28
+ def append_service_group(cfg, idx)
29
+ {}.tap do |virtual_server|
30
+ virtual_server["allocation"] = 1
31
+ virtual_server["port"] = cfg[:port]
32
+ virtual_server["serviceGroups"] = [
33
+ {
34
+ "routingMethodId" => (@enums["Routing_Method"][cfg[:method]] || 10),
35
+ "routingTypeId" => (@enums["Routing_Type"][cfg[:type]] || 3),
36
+ "services" => []
37
+ }
38
+ ]
39
+ @load_balancers[idx]["virtualServers"] << virtual_server
40
+ end
41
+ end
42
+
43
+ def join!
44
+ @pending = []
45
+
46
+ until @queue.empty?
47
+ job = @queue.pop
48
+ merge(job[:cfg], job[:idx])
49
+ end
50
+
51
+ # Perform the API calls for join.
52
+ @load_balancers.each_with_index do |lb, idx|
53
+ next unless @pending[idx]
54
+ @logger.debug("Updating VIP #{lb['id']} with: #{lb['virtualServers']}")
55
+ vip_id = @services["VirtualIpAddress"].object_with_id(lb["id"])
56
+ sl_warden { vip_id.editObject("virtualServers" => lb["virtualServers"]) }
57
+ end
58
+ end
59
+
60
+ def merge(cfg, idx)
61
+ # Get the service group. Create it if not found.
62
+ sg = @load_balancers[idx]["virtualServers"].find(lambda { append_service_group(cfg, idx) }) { |g| g["port"] == cfg[:port] }
63
+ # Get the IP address ID of the current machine.
64
+ ip_id = ip_address_id(@env)
65
+ unless sg["serviceGroups"].first["services"].index { |s| s["ipAddressId"] == ip_id }
66
+ @logger.debug("Merging service: #{cfg[:service]}")
67
+ # Add the service to the group.
68
+ sg["serviceGroups"].first["services"] << {
69
+ "enabled" => 1,
70
+ "ipAddressId" => ip_id,
71
+ "groupReferences" => [ { "weight" => cfg[:service].weight } ],
72
+ "healthChecks" => [ { "healthCheckTypeId" => (@enums["Health_Check_Type"][cfg[:service].health_check] || 21) } ],
73
+ "port" => cfg[:service].destination_port
74
+ }
75
+ # Mark the load balancer object as pending update
76
+ @pending[idx] = true
77
+ end
78
+ end
79
+
80
+ def prepare
81
+ @env[:ui].info I18n.t("vagrant_softlayer.vm.joining_load_balancers")
82
+
83
+ # For each definition, check if the load balancer exists and enqueue
84
+ # the join operation.
85
+ @queue = []
86
+ @env[:machine].provider_config.load_balancers.each do |cfg|
87
+ idx = @load_balancers.index { |lb| lb["ipAddress"]["ipAddress"] == cfg[:vip] }
88
+ raise Errors::SLLoadBalancerNotFound unless idx
89
+ @queue << { :cfg => cfg, :idx => idx }
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module SoftLayer
3
+ module Action
4
+ # This resumes the suspended instance.
5
+ class ResumeInstance
6
+ include Util::Warden
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ env[:ui].info I18n.t("vagrant_softlayer.vm.resuming")
14
+ sl_warden { env[:sl_machine].resume }
15
+
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,37 +1,38 @@
1
- require "log4r"
2
-
3
- module VagrantPlugins
4
- module SoftLayer
5
- module Action
6
- # This action creates the SoftLayer connection object and
7
- # puts it into the `:sl_connection` key in the environment.
8
- # Also, if a machine id is found, another key called
9
- # `:sl_machine` and containing the masked object is created.
10
- class SetupSoftLayer
11
- def initialize(app, env)
12
- @app = app
13
- @logger = Log4r::Logger.new("vagrant_softlayer::action::connect_softlayer")
14
- end
15
-
16
- def call(env)
17
- @logger.info("Creating the SoftLayer connection object...")
18
-
19
- env[:sl_credentials] = {
20
- :api_key => env[:machine].provider_config.api_key,
21
- :endpoint_url => env[:machine].provider_config.endpoint_url,
22
- :username => env[:machine].provider_config.username
23
- }
24
-
25
- env[:sl_connection] = ::SoftLayer::Service.new("SoftLayer_Virtual_Guest", env[:sl_credentials])
26
-
27
- unless env[:machine].id.nil? || env[:machine].id.empty?
28
- env[:sl_machine] = env[:sl_connection].object_with_id(env[:machine].id.to_i)
29
- end
30
-
31
- # Carry on
32
- @app.call(env)
33
- end
34
- end
35
- end
36
- end
37
- end
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module SoftLayer
5
+ module Action
6
+ # This action creates the SoftLayer service objects and
7
+ # puts them into keys in the environment.
8
+ # Also, if a machine id is found, another key called
9
+ # `:sl_machine` and containing the masked object is created.
10
+ class SetupSoftLayer
11
+ def initialize(app, env)
12
+ @app = app
13
+ @logger = Log4r::Logger.new("vagrant_softlayer::action::connect_softlayer")
14
+ end
15
+
16
+ def call(env)
17
+ @logger.info("Creating the SoftLayer service objects...")
18
+
19
+ env[:sl_credentials] = {
20
+ :api_key => env[:machine].provider_config.api_key,
21
+ :endpoint_url => env[:machine].provider_config.endpoint_url,
22
+ :username => env[:machine].provider_config.username
23
+ }
24
+
25
+ env[:sl_virtual_guest] = ::SoftLayer::Service.new("SoftLayer_Virtual_Guest", env[:sl_credentials])
26
+ env[:sl_product_order] = ::SoftLayer::Service.new("SoftLayer_Product_Order", env[:sl_credentials])
27
+
28
+ unless env[:machine].id.nil? || env[:machine].id.empty?
29
+ env[:sl_machine] = env[:sl_virtual_guest].object_with_id(env[:machine].id.to_i)
30
+ end
31
+
32
+ # Carry on
33
+ @app.call(env)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module SoftLayer
3
+ module Action
4
+ # This suspends the running instance.
5
+ class SuspendInstance
6
+ include Util::Warden
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ env[:ui].info I18n.t("vagrant_softlayer.vm.suspending")
14
+ sl_warden { env[:sl_machine].pause }
15
+
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -74,7 +74,7 @@ module VagrantPlugins
74
74
  command = [
75
75
  "rsync", "--verbose", "--archive", "-z",
76
76
  "--exclude", ".vagrant/", "--exclude", "Vagrantfile",
77
- "-e", "ssh -p #{ssh_info[:port]} -o StrictHostKeyChecking=no -i '#{ssh_info[:private_key_path]}'",
77
+ "-e", "ssh -p #{ssh_info[:port]} -o StrictHostKeyChecking=no -i '#{ssh_info[:private_key_path].join("' -i '")}'",
78
78
  hostpath,
79
79
  "#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"]
80
80
 
@@ -1,94 +1,94 @@
1
- module VagrantPlugins
2
- module SoftLayer
3
- module Action
4
- # Look for the DNS zone relative to the configured domain and,
5
- # on return path, perform an API call to add or remove the A
6
- # resource record for the host.
7
- class UpdateDNS
8
- include Util::Network
9
- include Util::Warden
10
-
11
- def initialize(app, env)
12
- @app = app
13
- @logger = Log4r::Logger.new("vagrant_softlayer::action::update_dns")
14
- end
15
-
16
- def call(env)
17
- @env = env
18
-
19
- update_dns
20
-
21
- @app.call(@env)
22
- end
23
-
24
- def add_record
25
- template = {
26
- "data" => ip_address(@env),
27
- "domainId" => @dns_zone["id"],
28
- "host" => hostname(@env),
29
- "ttl" => 86400,
30
- "type" => "a"
31
- }
32
- @env[:ui].info I18n.t("vagrant_softlayer.vm.creating_dns_record")
33
- @logger.debug("Creating DNS A record for #{template['host']}.#{@dns_zone[:name]} (IP address #{template['data']}).")
34
- resource = sl_warden { @resource.createObject(template) }
35
- self.dns_id = resource["id"]
36
- end
37
-
38
- def delete_record
39
- @env[:ui].info I18n.t("vagrant_softlayer.vm.deleting_dns_record")
40
- @logger.debug("Deleting stored DNS A record (ID #{self.dns_id}).")
41
- warn_msg = lambda { @env[:ui].warn I18n.t("vagrant_softlayer.errors.dns_record_not_found") }
42
- sl_warden(warn_msg) { @resource.object_with_id(self.dns_id).deleteObject }
43
- end
44
-
45
- def dns_id
46
- id = nil
47
- id_file = @env[:machine].data_dir.join("dns_id")
48
- id = id_file.read.chomp.to_i if id_file.file?
49
- return id
50
- end
51
-
52
- def dns_id=(value)
53
- @logger.info("New machine DNS ID: #{value.inspect}")
54
-
55
- # The file that will store the id if we have one. This allows the
56
- # ID to persist across Vagrant runs.
57
- id_file = @env[:machine].data_dir.join("dns_id")
58
-
59
- if value
60
- # Write the "id" file with the id given.
61
- id_file.open("w+") do |f|
62
- f.write(value)
63
- end
64
- end
65
- end
66
-
67
- def update_dns
68
- unless @env[:machine].provider_config.manage_dns
69
- @logger.debug("Not managing DNS. Going ahead.")
70
- return
71
- end
72
-
73
- # Lookup the DNS zone
74
- zone = ::SoftLayer::Service.new("SoftLayer_Dns_Domain", @env[:sl_credentials])
75
- domain = @env[:machine].provider_config.domain
76
-
77
- @logger.debug("Looking for #{domain} zone into the SoftLayer zone list.")
78
- @dns_zone = sl_warden { zone.getByDomainName(domain).first }
79
- raise Errors::SLDNSZoneNotFound, :zone => domain unless @dns_zone
80
- @logger.debug("Found DNS zone: #{@dns_zone.inspect}")
81
-
82
- # Add or remove the resource record
83
- @resource = ::SoftLayer::Service.new("SoftLayer_Dns_Domain_ResourceRecord", @env[:sl_credentials])
84
- case @env[:machine_action]
85
- when :up
86
- add_record unless self.dns_id
87
- when :destroy
88
- delete_record
89
- end
90
- end
91
- end
92
- end
93
- end
94
- end
1
+ module VagrantPlugins
2
+ module SoftLayer
3
+ module Action
4
+ # Look for the DNS zone relative to the configured domain and,
5
+ # on return path, perform an API call to add or remove the A
6
+ # resource record for the host.
7
+ class UpdateDNS
8
+ include Util::Network
9
+ include Util::Warden
10
+
11
+ def initialize(app, env)
12
+ @app = app
13
+ @logger = Log4r::Logger.new("vagrant_softlayer::action::update_dns")
14
+ end
15
+
16
+ def call(env)
17
+ @env = env
18
+
19
+ update_dns
20
+
21
+ @app.call(@env)
22
+ end
23
+
24
+ def add_record
25
+ template = {
26
+ "data" => ip_address(@env),
27
+ "domainId" => @dns_zone["id"],
28
+ "host" => hostname(@env),
29
+ "ttl" => 86400,
30
+ "type" => "a"
31
+ }
32
+ @env[:ui].info I18n.t("vagrant_softlayer.vm.creating_dns_record")
33
+ @logger.debug("Creating DNS A record for #{template['host']}.#{@dns_zone[:name]} (IP address #{template['data']}).")
34
+ resource = sl_warden { @resource.createObject(template) }
35
+ self.dns_id = resource["id"]
36
+ end
37
+
38
+ def delete_record
39
+ @env[:ui].info I18n.t("vagrant_softlayer.vm.deleting_dns_record")
40
+ @logger.debug("Deleting stored DNS A record (ID #{self.dns_id}).")
41
+ warn_msg = lambda { @env[:ui].warn I18n.t("vagrant_softlayer.errors.dns_record_not_found") }
42
+ sl_warden(warn_msg) { @resource.object_with_id(self.dns_id).deleteObject }
43
+ end
44
+
45
+ def dns_id
46
+ id = nil
47
+ id_file = @env[:machine].data_dir.join("dns_id")
48
+ id = id_file.read.chomp.to_i if id_file.file?
49
+ return id
50
+ end
51
+
52
+ def dns_id=(value)
53
+ @logger.info("New machine DNS ID: #{value.inspect}")
54
+
55
+ # The file that will store the id if we have one. This allows the
56
+ # ID to persist across Vagrant runs.
57
+ id_file = @env[:machine].data_dir.join("dns_id")
58
+
59
+ if value
60
+ # Write the "id" file with the id given.
61
+ id_file.open("w+") do |f|
62
+ f.write(value)
63
+ end
64
+ end
65
+ end
66
+
67
+ def update_dns
68
+ unless @env[:machine].provider_config.manage_dns
69
+ @logger.debug("Not managing DNS. Going ahead.")
70
+ return
71
+ end
72
+
73
+ # Lookup the DNS zone
74
+ zone = ::SoftLayer::Service.new("SoftLayer_Dns_Domain", @env[:sl_credentials])
75
+ domain = @env[:machine].provider_config.domain
76
+
77
+ @logger.debug("Looking for #{domain} zone into the SoftLayer zone list.")
78
+ @dns_zone = sl_warden { zone.getByDomainName(domain).first }
79
+ raise Errors::SLDNSZoneNotFound, :zone => domain unless @dns_zone
80
+ @logger.debug("Found DNS zone: #{@dns_zone.inspect}")
81
+
82
+ # Add or remove the resource record
83
+ @resource = ::SoftLayer::Service.new("SoftLayer_Dns_Domain_ResourceRecord", @env[:sl_credentials])
84
+ case @env[:machine_action]
85
+ when :up
86
+ add_record unless self.dns_id
87
+ when :destroy
88
+ delete_record
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -1,40 +1,40 @@
1
- require "log4r"
2
-
3
- module VagrantPlugins
4
- module SoftLayer
5
- module Action
6
- # Waits until the new machine has been provisioned.
7
- class WaitForProvision
8
- include Util::Warden
9
-
10
- def initialize(app, env)
11
- @app = app
12
- @logger = Log4r::Logger.new("vagrant_softlayer::action::wait_for_provision")
13
- end
14
-
15
- def call(env)
16
- env[:ui].info I18n.t("vagrant_softlayer.vm.wait_for_provision")
17
-
18
- env[:sl_machine] = env[:sl_connection].object_with_id(env[:machine].id.to_i)
19
-
20
- retry_msg = lambda { @logger.debug("Object not found, retrying in 10 seconds.") }
21
-
22
- # 20 minutes timeout
23
- Timeout::timeout(1200) do
24
- @logger.debug("Checking if the newly ordered machine has been provisioned.")
25
- sl_warden(retry_msg, 10) do
26
- while env[:sl_machine].getPowerState["name"] != "Running" || env[:sl_machine].object_mask( { "provisionDate" => "" } ).getObject == {}
27
- @logger.debug("The machine is still provisioning. Retrying in 10 seconds.")
28
- sleep 10
29
- end
30
- end
31
- end
32
-
33
- env[:ui].info I18n.t("vagrant_softlayer.vm.provisioned")
34
-
35
- @app.call(env)
36
- end
37
- end
38
- end
39
- end
40
- end
1
+ require "log4r"
2
+
3
+ module VagrantPlugins
4
+ module SoftLayer
5
+ module Action
6
+ # Waits until the new machine has been provisioned.
7
+ class WaitForProvision
8
+ include Util::Warden
9
+
10
+ def initialize(app, env)
11
+ @app = app
12
+ @logger = Log4r::Logger.new("vagrant_softlayer::action::wait_for_provision")
13
+ end
14
+
15
+ def call(env)
16
+ env[:ui].info I18n.t("vagrant_softlayer.vm.wait_for_provision")
17
+
18
+ env[:sl_machine] = env[:sl_virtual_guest].object_with_id(env[:machine].id.to_i)
19
+
20
+ retry_msg = lambda { @logger.debug("Object not found, retrying in 10 seconds.") }
21
+
22
+ # 20 minutes timeout
23
+ Timeout::timeout(1200) do
24
+ @logger.debug("Checking if the newly ordered machine has been provisioned.")
25
+ sl_warden(retry_msg, 10) do
26
+ while env[:sl_machine].getPowerState["name"] != "Running" || env[:sl_machine].object_mask( { "provisionDate" => "" } ).getObject == {}
27
+ @logger.debug("The machine is still provisioning. Retrying in 10 seconds.")
28
+ sleep 10
29
+ end
30
+ end
31
+ end
32
+
33
+ env[:ui].info I18n.t("vagrant_softlayer.vm.provisioned")
34
+
35
+ @app.call(env)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end