secured-cloud-vagrant 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +12 -0
  4. data/LICENSE.txt +9 -0
  5. data/README.md +340 -0
  6. data/README.txt +358 -0
  7. data/Rakefile +22 -0
  8. data/Vagrantfile_multipleVMs +124 -0
  9. data/Vagrantfile_singleVM +43 -0
  10. data/dummy.box +0 -0
  11. data/example_box/README.md +13 -0
  12. data/example_box/metadata.json +3 -0
  13. data/lib/secured-cloud-vagrant.rb +22 -0
  14. data/lib/secured-cloud-vagrant/action.rb +259 -0
  15. data/lib/secured-cloud-vagrant/actions/assign_public_ips.rb +142 -0
  16. data/lib/secured-cloud-vagrant/actions/check_state.rb +64 -0
  17. data/lib/secured-cloud-vagrant/actions/create.rb +112 -0
  18. data/lib/secured-cloud-vagrant/actions/delete.rb +105 -0
  19. data/lib/secured-cloud-vagrant/actions/has_public_ips.rb +53 -0
  20. data/lib/secured-cloud-vagrant/actions/power_off.rb +22 -0
  21. data/lib/secured-cloud-vagrant/actions/power_on.rb +22 -0
  22. data/lib/secured-cloud-vagrant/actions/power_vm.rb +100 -0
  23. data/lib/secured-cloud-vagrant/actions/read_ssh_info.rb +148 -0
  24. data/lib/secured-cloud-vagrant/actions/reboot.rb +98 -0
  25. data/lib/secured-cloud-vagrant/actions/release_ips_confirm.rb +25 -0
  26. data/lib/secured-cloud-vagrant/actions/wait_for_state.rb +54 -0
  27. data/lib/secured-cloud-vagrant/actions/warn_networks.rb +32 -0
  28. data/lib/secured-cloud-vagrant/actions/warn_provision.rb +32 -0
  29. data/lib/secured-cloud-vagrant/commands/list.rb +149 -0
  30. data/lib/secured-cloud-vagrant/commands/ssh_config.rb +43 -0
  31. data/lib/secured-cloud-vagrant/configs/authentication_info.rb +49 -0
  32. data/lib/secured-cloud-vagrant/configs/config.rb +87 -0
  33. data/lib/secured-cloud-vagrant/configs/ip_mapping.rb +71 -0
  34. data/lib/secured-cloud-vagrant/configs/virtual_machine.rb +136 -0
  35. data/lib/secured-cloud-vagrant/plugin.rb +77 -0
  36. data/lib/secured-cloud-vagrant/provider.rb +73 -0
  37. data/lib/secured-cloud-vagrant/version.rb +5 -0
  38. data/locales/en.yml +91 -0
  39. data/secured-cloud-vagrant.gemspec +59 -0
  40. data/templates/os_templates.erb +12 -0
  41. metadata +160 -0
@@ -0,0 +1,22 @@
1
+ require "log4r"
2
+
3
+ require_relative "power_vm"
4
+
5
+ module VagrantPlugins
6
+ module SecuredCloud
7
+ module Action
8
+
9
+ # This can be used with "Call" built-in to check if the machine
10
+ # is created and branch in the middleware.
11
+ class PowerOff < PowerVm
12
+
13
+ def initialize(app, env)
14
+ env[:ui].info(I18n.t("secured_cloud_vagrant.info.powering_off"))
15
+ super(app, env, "off")
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require "log4r"
2
+
3
+ require_relative "power_vm"
4
+
5
+ module VagrantPlugins
6
+ module SecuredCloud
7
+ module Action
8
+
9
+ # This can be used with "Call" built-in to check if the machine
10
+ # is created and branch in the middleware.
11
+ class PowerOn < PowerVm
12
+
13
+ def initialize(app, env)
14
+ env[:ui].info(I18n.t("secured_cloud_vagrant.info.powering_on"))
15
+ super(app, env, "on")
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,100 @@
1
+ require "log4r"
2
+ require "secured_cloud_api_client"
3
+
4
+ module VagrantPlugins
5
+ module SecuredCloud
6
+ module Action
7
+ # This can be used with "Call" built-in to check if the machine
8
+ # is created and branch in the middleware.
9
+ class PowerVm
10
+ def initialize(app, env, powerStatus)
11
+ @app = app
12
+ @machine = env[:machine]
13
+ @powerStatus = powerStatus
14
+ @logger = Log4r::Logger.new('vagrant::secured_cloud::action::power_vm')
15
+ end
16
+
17
+ def call(env)
18
+
19
+ @logger.debug("Powering #{@powerStatus.upcase} VM ...")
20
+
21
+ vm_resource_url = @machine.id
22
+
23
+ if !vm_resource_url.nil? && !vm_resource_url.empty?
24
+
25
+ # Create a Secured Cloud Connection instance to connect tot he SecuredCloud API
26
+ authInfo = @machine.provider_config.auth
27
+ sc_connection = SecuredCloudConnection.new(authInfo.url, authInfo.applicationKey, authInfo.sharedSecret)
28
+
29
+ begin
30
+
31
+ # Send request to power VM
32
+ response = SecuredCloudRestClient.powerVM(sc_connection, vm_resource_url, @powerStatus)
33
+
34
+ #Monitor the transaction.
35
+ if (response[0] == "202")
36
+
37
+ #Task successful.
38
+ taskResource = response[1]
39
+ taskStatus = SecuredCloudRestClient::getTaskStatus(sc_connection, taskResource)
40
+
41
+ @logger.info("Task Status:\n#{taskStatus.get_details()}")
42
+
43
+ while ((taskStatus.instance_variable_get(:@requestStateEnum) == nil) || (taskStatus.instance_variable_get(:@requestStateEnum) == "OPEN")) do
44
+ sleep(20)
45
+ taskStatus = SecuredCloudRestClient.getTaskStatus(sc_connection, taskResource)
46
+ env[:ui].info(I18n.t("secured_cloud_vagrant.info.task_status", :percentage => taskStatus.get_percentage_completed,
47
+ :task_desc => taskStatus.get_latest_task_description))
48
+ @logger.info("Task Status:\n#{taskStatus.get_details()}")
49
+ end
50
+
51
+ if(taskStatus.get_result.nil?)
52
+
53
+ #Task unsuccessful.
54
+ @logger.debug("VM Power #{@powerStatus.upcase} failed with the following error:\n#{taskStatus.get_error_code}: #{taskStatus.get_error_message}")
55
+
56
+ error_code = (taskStatus.get_error_code.nil?) ? "" : "#{taskStatus.get_error_code} "
57
+ error_message = (taskStatus.get_error_message.nil?) ? I18n.t('secured_cloud_vagrant.errors.internal_server_error') :
58
+ error_code + taskStatus.get_error_message
59
+
60
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.powering_vm", :power_status => @powerStatus.upcase,
61
+ :vm_name => env[:vm_name], :error_message => error_message))
62
+
63
+ else
64
+
65
+ # Task successful
66
+ @logger.debug("VM '#{env[:machine].id}' has been powered #{@powerStatus.upcase}")
67
+ env[:ui].info(I18n.t("secured_cloud_vagrant.info.success.power_vm", :power_status => @powerStatus.upcase,
68
+ :vm_name => env[:vm_name]))
69
+
70
+ end
71
+
72
+ else
73
+
74
+ #Task unsuccessful.
75
+ @logger.debug("VM Power #{@powerStatus.upcase} failed with the following error:\n#{response}")
76
+
77
+ error_message = (response[2].nil?) ? I18n.t('secured_cloud_vagrant.errors.internal_server_error') : response[2]
78
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.powering_vm", :power_status => @powerStatus.upcase,
79
+ :vm_name => env[:vm_name], :error_message => error_message))
80
+
81
+ end
82
+
83
+ rescue Errno::ETIMEDOUT
84
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.request_timed_out", :request => "power #{@powerStatus} VM '#{env[:vm_name]}'"))
85
+ rescue Exception => e
86
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
87
+ end
88
+
89
+ else
90
+ @logger.debug("No VM found to be powered #{@powerStatus.upcase}")
91
+ end
92
+
93
+ @app.call(env)
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,148 @@
1
+ require "log4r"
2
+ require "secured_cloud_api_client"
3
+
4
+ module VagrantPlugins
5
+ module SecuredCloud
6
+ module Action
7
+ # This can be used with "Call" built-in to check if the machine
8
+ # is created and branch in the middleware.
9
+ class ReadSshInfo
10
+
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @machine = env[:machine]
15
+ @logger = Log4r::Logger.new('vagrant::secured_cloud::action::read_ssh_info')
16
+ end
17
+
18
+ def call(env)
19
+
20
+ @logger.debug("Reading SSH info for VM #{env[:vm_name]} ...")
21
+ env[:vm_conn_info] = read_ssh_info(env)
22
+
23
+ @app.call(env)
24
+ end
25
+
26
+ def read_ssh_info(env)
27
+
28
+ # If the VM ID is not in the environment return null
29
+ if @machine.id.nil? || @machine.id.empty?
30
+ @logger.error("VM has not yet been created")
31
+ return nil
32
+ end
33
+
34
+ # Initialize the public IP, port and username to those defined in the Vagrantfile
35
+ publicIp = @machine.config.ssh.host
36
+ port = @machine.config.ssh.port
37
+ username = @machine.config.ssh.username
38
+
39
+ # If they are all defined return those values
40
+ if(!publicIp.nil? && !port.nil? && !username.nil?)
41
+ return { :host => publicIp, :port => port, :username => username, :private_key_path => nil }
42
+ end
43
+
44
+ begin
45
+
46
+ # Create a Secured Cloud Connection instance to connect tot he SecuredCloud API
47
+ authInfo = @machine.provider_config.auth
48
+ @sc_connection = SecuredCloudConnection.new(authInfo.url, authInfo.applicationKey, authInfo.sharedSecret)
49
+
50
+ # Get the VM details
51
+ virtualMachine = SecuredCloudRestClient.getVMDetails(@sc_connection, @machine.id)
52
+
53
+ # If the VM is not found return null
54
+ if virtualMachine.nil?
55
+ @logger.error("VM '#{@machine.id}' not found")
56
+ return nil
57
+ end
58
+
59
+ # Get a public IP assigned to the VM if it is not nil
60
+ if publicIp.nil?
61
+
62
+ publicIp = get_public_ip(virtualMachine)
63
+
64
+ # If no public IP has been found yet return nil and show an error message
65
+ if publicIp.nil?
66
+ @logger.error("Cannot connect to a private VM")
67
+ env[:ui].warn(I18n.t('secured_cloud_vagrant.warnings.no_public_ips', :vm_name => virtualMachine.get_name))
68
+ return nil
69
+ end
70
+
71
+ end
72
+
73
+ # Get the username to connect to the VM
74
+ if username.nil?
75
+
76
+ username = get_username(virtualMachine)
77
+
78
+ if(username.nil?)
79
+ @logger.warn("No username could be determined to SSH to the VM.")
80
+ end
81
+ end
82
+
83
+ # If the port is not defined set it to 22
84
+ port = 22 if port.nil?
85
+
86
+ return { :host => publicIp, :port => port, :username => username, :private_key_path => nil }
87
+
88
+ rescue Errno::ETIMEDOUT
89
+ env[:ui].error(I18n.t('secured_cloud_vagrant.errors.request_timed_out', :request => "get the SSH information for VM '#{virtualMachine.get_name}'"))
90
+ rescue Exception => e
91
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
92
+ end
93
+
94
+ end
95
+
96
+ # Returns a public IP which is assigned to the VM
97
+ def get_public_ip(virtualMachine)
98
+
99
+ publicIp = nil
100
+
101
+ # Process the IP mappings of the VM
102
+ if !virtualMachine.get_ip_mappings.nil?
103
+ virtualMachine.get_ip_mappings.each do |ipMapping|
104
+
105
+ if !ipMapping.get_public_ips.nil?
106
+ publicIp = ipMapping.get_public_ips[0]
107
+ @logger.debug("Public IP to SSH to VM: #{publicIp}")
108
+ break
109
+ end
110
+
111
+ end
112
+ end
113
+
114
+ return publicIp
115
+
116
+ end
117
+
118
+
119
+ # Returns the username to SSH to the VM
120
+ def get_username(virtualMachine)
121
+
122
+ username = nil
123
+
124
+ # Get username of VM
125
+ osTemplateUrl = virtualMachine.get_os_template_resource_url
126
+
127
+ if !osTemplateUrl.nil?
128
+
129
+ osTemplate = SecuredCloudRestClient.getOsTemplateDetails(@sc_connection, osTemplateUrl)
130
+
131
+ if osTemplate.nil?
132
+ @logger.error("OsTemplate '#{osTemplateUrl}' not found")
133
+ return nil
134
+ end
135
+
136
+ username = osTemplate.get_administrator_username
137
+ @logger.debug("Username to connect to VM: #{username}")
138
+
139
+ end
140
+
141
+ return username
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,98 @@
1
+ require "log4r"
2
+ require "secured_cloud_api_client"
3
+
4
+ module VagrantPlugins
5
+ module SecuredCloud
6
+ module Action
7
+ # This can be used with "Call" built-in to check if the machine
8
+ # is created and branch in the middleware.
9
+ class Reboot
10
+ def initialize(app, env)
11
+ @app = app
12
+ @machine = env[:machine]
13
+ @logger = Log4r::Logger.new('vagrant::secured_cloud::action::reboot_vm')
14
+ end
15
+
16
+ def call(env)
17
+
18
+ @logger.debug("Rebooting the VM ...")
19
+ env[:ui].info(I18n.t("secured_cloud_vagrant.info.reloading"))
20
+
21
+ vm_resource_url = @machine.id
22
+
23
+ if !vm_resource_url.nil? && !vm_resource_url.empty?
24
+
25
+ begin
26
+
27
+ # Create a Secured Cloud Connection instance to connect tot he SecuredCloud API
28
+ authInfo = @machine.provider_config.auth
29
+ sc_connection = SecuredCloudConnection.new(authInfo.url, authInfo.applicationKey, authInfo.sharedSecret)
30
+
31
+ # Send request to reboot VM
32
+ response = SecuredCloudRestClient.rebootVM(sc_connection, vm_resource_url)
33
+
34
+ # Monitor the transaction.
35
+ if (response[0] == "202")
36
+
37
+ #Task successful.
38
+ taskResource = response[1]
39
+ taskStatus = SecuredCloudRestClient::getTaskStatus(sc_connection, taskResource)
40
+
41
+ @logger.info("Task Status:\n#{taskStatus.get_details()}")
42
+
43
+ while ((taskStatus.instance_variable_get(:@requestStateEnum) == nil) || (taskStatus.instance_variable_get(:@requestStateEnum) == "OPEN")) do
44
+ sleep(20)
45
+ taskStatus = SecuredCloudRestClient.getTaskStatus(sc_connection, taskResource)
46
+ env[:ui].info(I18n.t('secured_cloud_vagrant.info.task_status', :percentage => taskStatus.get_percentage_completed,
47
+ :task_desc => taskStatus.get_latest_task_description))
48
+ @logger.info("Task Status:\n#{taskStatus.get_details()}")
49
+ end
50
+
51
+ if(taskStatus.get_result. nil?)
52
+
53
+ #Task unsuccessful.
54
+ @logger.debug("VM Reboot failed with the following error:\n#{taskStatus.get_error_code}: #{taskStatus.get_error_message}")
55
+
56
+ error_code = (taskStatus.get_error_code.nil?) ? "" : "#{taskStatus.get_error_code} "
57
+ error_message = (taskStatus.get_error_message.nil?) ? I18n.t('secured_cloud_vagrant.errors.internal_server_error') :
58
+ error_code + taskStatus.get_error_message
59
+
60
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.rebooting_vm", :vm_name => env[:vm_name], :error_message => error_message))
61
+
62
+ else
63
+
64
+ # Task successful
65
+ @logger.debug("VM '#{env[:machine].id}' has been rebooted")
66
+ env[:ui].info(I18n.t("secured_cloud_vagrant.info.success.reboot_vm", :vm_name => env[:vm_name]))
67
+
68
+ end
69
+ else
70
+
71
+ #Task unsuccessful.
72
+ @logger.debug("VM Reboot failed with the following error:\n#{response}")
73
+
74
+ error_message = (response[2].nil?) ? I18n.t('secured_cloud_vagrant.errors.internal_server_error') : response[2]
75
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.rebooting_vm", :vm_name => env[:vm_name], :error_message => error_message))
76
+
77
+ end
78
+
79
+ rescue Errno::ETIMEDOUT
80
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.request_timed_out", :request => "reboot VM '#{env[:vm_name]}'"))
81
+ rescue Exception => e
82
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
83
+ end
84
+
85
+ else
86
+
87
+ @logger.debug("No VM found to be rebooted")
88
+
89
+ end
90
+
91
+ @app.call(env)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,25 @@
1
+ require "vagrant/action/builtin/confirm"
2
+ require "log4r"
3
+
4
+ module VagrantPlugins
5
+ module SecuredCloud
6
+ module Action
7
+
8
+ # This class asks the user to confirm whether the public IPs of
9
+ # the VM managed by vagrant are to be released or put in the reserve
10
+ # pool of the VDC.
11
+ class ReleaseIpsConfirm < Confirm
12
+
13
+ def initialize(app, env)
14
+
15
+ @logger = Log4r::Logger.new('vagrant::secured_cloud::action::release_ips_confirm')
16
+ @logger.debug("Confirming whether public IPs are to be released ...")
17
+
18
+ message = I18n.t('secured_cloud_vagrant.commands.release_ips_confirmation')
19
+ super(app, env, message)
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ require "log4r"
2
+ require "secured_cloud_api_client"
3
+
4
+ module VagrantPlugins
5
+ module SecuredCloud
6
+ module Action
7
+
8
+ # This can be used with "Call" built-in to check if the machine
9
+ # is created and branch in the middleware.
10
+ class WaitForState
11
+
12
+ def initialize(app, env)
13
+ @app = app
14
+ @machine = env[:machine]
15
+ @logger = Log4r::Logger.new('vagrant::secured_cloud::action::wait_for_state')
16
+ end
17
+
18
+ def call(env)
19
+
20
+ @logger.debug("Waiting for VM state to be powered OFF ...")
21
+
22
+ vm_resource_url = @machine.id
23
+
24
+ if !vm_resource_url.nil? && !vm_resource_url.empty?
25
+
26
+ begin
27
+
28
+ # Create a Secured Cloud Connection instance to connect tot he SecuredCloud API
29
+ authInfo = @machine.provider_config.auth
30
+ sc_connection = SecuredCloudConnection.new(authInfo.url, authInfo.applicationKey, authInfo.sharedSecret)
31
+
32
+ # Get the VM details and check the power status
33
+ while (SecuredCloudRestClient.getVMDetails(sc_connection, vm_resource_url).get_power_status == "POWERED_ON") do
34
+
35
+ # Sleep for 2 seconds
36
+ sleep 2
37
+
38
+ end
39
+
40
+ rescue Errno::ETIMEDOUT
41
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.request_timed_out", :request => "get the VM details"))
42
+ rescue Exception => e
43
+ env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
44
+ end
45
+ end
46
+
47
+ @app.call(env)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end