vagrant-hp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require 'vagrant/util/retryable'
7
+ require "fog"
8
+ require "log4r"
9
+
10
+
11
+ module VagrantPlugins
12
+ module HP
13
+ module Action
14
+ # This creates server on HP Cloud.
15
+ class CreateServer
16
+ include Vagrant::Util::Retryable
17
+
18
+ def initialize(app, env)
19
+ @app = app
20
+ @logger = Log4r::Logger.new("vagrant_hp::action::create_server")
21
+ end
22
+
23
+ def call(env)
24
+ # Get the configs
25
+ config = env[:machine].provider_config
26
+
27
+ # Find the flavor
28
+ env[:ui].info(I18n.t("vagrant_hp.finding_flavor"))
29
+ flavor = find_match(env[:hp_compute].flavors.all, config.flavor)
30
+ raise Errors::NoMatchingFlavor if !flavor
31
+
32
+ # Find the image
33
+ env[:ui].info(I18n.t("vagrant_hp.finding_image"))
34
+ image = find_match(env[:hp_compute].images, config.image)
35
+ raise Errors::NoMatchingImage if !image
36
+
37
+ # Figure out the name for the server
38
+ server_name = config.server_name || env[:machine].name if env[:machine].name != "default" || get_server_name()
39
+
40
+ # Output the settings we're going to use to the user
41
+ env[:ui].info(I18n.t("vagrant_hp.launching_server"))
42
+ env[:ui].info(" -- Flavor: #{flavor.name}")
43
+ env[:ui].info(" -- Image: #{image.name}")
44
+ env[:ui].info(" -- Name: #{server_name}")
45
+
46
+ # Build the options for launching...
47
+ options = {
48
+ :flavor_id => flavor.id,
49
+ :image_id => image.id,
50
+ :name => server_name,
51
+ :key_name => config.keypair_name
52
+ }
53
+
54
+ # Create the server
55
+ server = env[:hp_compute].servers.create(options)
56
+
57
+ # Store the ID right away so we can track it
58
+ env[:machine].id = server.id
59
+
60
+ # Wait for the server to finish building
61
+ env[:ui].info(I18n.t("vagrant_hp.waiting_for_build"))
62
+ retryable(:on => Timeout::Error, :tries => 200) do
63
+ # If we're interrupted don't worry about waiting
64
+ next if env[:interrupted]
65
+
66
+ # Set the progress
67
+ env[:ui].clear_line
68
+ env[:ui].report_progress(server.progress, 100, false)
69
+
70
+ # Wait for the server to be ready
71
+ begin
72
+ server.wait_for(15) { ready? }
73
+ rescue RuntimeError, Fog::Errors::TimeoutError => e
74
+ # If we don't have an error about a state transition, then
75
+ # we just move on.
76
+ #raise if e.message !~ /should have transitioned/
77
+ env[:ui].info("Error: #{e.message}")
78
+ end
79
+ end
80
+
81
+ if !env[:interrupted]
82
+ # Clear the line one more time so the progress is removed
83
+ env[:ui].clear_line
84
+
85
+ # Wait for SSH to become available
86
+ env[:ui].info(I18n.t("vagrant_hp.waiting_for_ssh"))
87
+ while true
88
+ begin
89
+ # If we're interrupted then just back out
90
+ break if env[:interrupted]
91
+ break if env[:machine].communicate.ready?
92
+ rescue Errno::ENETUNREACH
93
+ end
94
+ sleep 2
95
+ end
96
+
97
+ env[:ui].info(I18n.t("vagrant_hp.ready"))
98
+ end
99
+
100
+ @app.call(env)
101
+ end
102
+
103
+ protected
104
+
105
+ # generate a random name if server_name is empty
106
+ def get_server_name()
107
+ server_name = "vagrant_hp-#{rand.to_s.split('.')[1]}"
108
+ server_name.to_s
109
+ end
110
+
111
+ def find_match(collection, name)
112
+ collection.each do |single|
113
+ return single if single.id == name
114
+ return single if single.name == name
115
+ end
116
+ nil
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "log4r"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ module Action
11
+ # This deletes the running server, if there is one.
12
+ class DeleteServer
13
+ def initialize(app, env)
14
+ @app = app
15
+ @logger = Log4r::Logger.new("vagrant_hp::action::delete_server")
16
+ end
17
+
18
+ def call(env)
19
+ if env[:machine].id
20
+ @logger.info(I18n.t("vagrant_hp.deleting_server"))
21
+ server = env[:hp_compute].servers.get(env[:machine].id)
22
+ server.destroy
23
+ env[:machine].id = nil
24
+ end
25
+ @app.call(env)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "log4r"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ module Action
11
+ # This deletes the running server, if there is one.
12
+ class HaltServer
13
+ def initialize(app, env)
14
+ @app = app
15
+ @logger = Log4r::Logger.new("vagrant_hp::action::delete_server")
16
+ end
17
+
18
+ def call(env)
19
+ if env[:machine].id
20
+ @logger.info(I18n.t("vagrant_hp.deleting_server"))
21
+ server = env[:hp_compute].servers.get(env[:machine].id)
22
+ server.stop # verify this
23
+ env[:machine].id = nil
24
+ end
25
+ @app.call(env)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ module VagrantPlugins
7
+ module HP
8
+ module Action
9
+ # This can be used with "Call" built-in to check if the machine
10
+ # is created and branch in the middleware.
11
+ class IsCreated
12
+ def initialize(app, env)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ env[:result] = env[:machine].state.id != :not_created
18
+ @app.call(env)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ module VagrantPlugins
7
+ module HP
8
+ module Action
9
+ # This can be used with "Call" built-in to check if the machine
10
+ # is created and branch in the middleware.
11
+ class IsRunning
12
+ def initialize(app, env)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ env[:result] = env[:machine].state.id != :running
18
+ @app.call(env)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ module VagrantPlugins
7
+ module HP
8
+ module Action
9
+ class MessageAlreadyCreated
10
+ def initialize(app, env)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ env[:ui].info(I18n.t("vagrant_hp.already_created"))
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ module VagrantPlugins
7
+ module HP
8
+ module Action
9
+ class MessageNotCreated
10
+ def initialize(app, env)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ env[:ui].info(I18n.t("vagrant_hp.not_created"))
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "log4r"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ module Action
11
+ # This action reads the SSH info for the machine and puts it into the
12
+ # `:machine_ssh_info` key in the environment.
13
+ class ReadSSHInfo
14
+ def initialize(app, env)
15
+ @app = app
16
+ @logger = Log4r::Logger.new("vagrant_hp::action::read_ssh_info")
17
+ end
18
+
19
+ def call(env)
20
+ env[:machine_ssh_info] = read_ssh_info(env[:hp_compute], env[:machine])
21
+
22
+ @app.call(env)
23
+ end
24
+
25
+ def read_ssh_info(hp, machine)
26
+ return nil if machine.id.nil?
27
+
28
+ # Find the machine
29
+ server = hp.servers.get(machine.id)
30
+ if server.nil?
31
+ # The machine can't be found
32
+ @logger.info("Machine couldn't be found, assuming it got destroyed.")
33
+ machine.id = nil
34
+ return nil
35
+ end
36
+
37
+ config = machine.provider_config
38
+
39
+ # Read the DNS info
40
+ return {
41
+ # Usually there should only be one public IP
42
+ :host => server.public_ip_address,
43
+ :port => 22,
44
+ :username => config.ssh_username,
45
+ :private_key_path => config.ssh_private_key_path,
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "log4r"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ module Action
11
+ # This action reads the state of the machine and puts it in the
12
+ # `:machine_state_id` key in the environment.
13
+ class ReadState
14
+ def initialize(app, env)
15
+ @app = app
16
+ @logger = Log4r::Logger.new("vagrant_hp::action::read_state")
17
+ end
18
+
19
+ def call(env)
20
+ env[:machine_state_id] = read_state(env[:hp_compute], env[:machine])
21
+
22
+ @app.call(env)
23
+ end
24
+
25
+ def read_state(hp, machine)
26
+ return :not_created if machine.id.nil?
27
+
28
+ # Find the machine
29
+ server = hp.servers.get(machine.id)
30
+ if server.nil? || [:"shutting-down", :terminated].include?(server.state.to_sym)
31
+ # The machine can't be found
32
+ @logger.info("Machine not found or terminated, assuming it got destroyed.")
33
+ machine.id = nil
34
+ return :not_created
35
+ end
36
+
37
+ # Return the state
38
+ return server.state.to_sym
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,63 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "log4r"
7
+
8
+ require "vagrant/util/subprocess"
9
+
10
+ module VagrantPlugins
11
+ module HP
12
+ module Action
13
+ # This middleware uses `rsync` to sync the folders over to the
14
+ # HP instance.
15
+ class SyncFolders
16
+ def initialize(app, env)
17
+ @app = app
18
+ @logger = Log4r::Logger.new("vagrant_hp::action::sync_folders")
19
+ end
20
+
21
+ def call(env)
22
+ @app.call(env)
23
+
24
+ ssh_info = env[:machine].ssh_info
25
+
26
+ env[:machine].config.vm.synced_folders.each do |id, data|
27
+ next if data[:hostpath] == "."
28
+ hostpath = File.expand_path(data[:hostpath], env[:root_path])
29
+ guestpath = data[:guestpath]
30
+
31
+ # Make sure there is a trailing slash on the host path to
32
+ # avoid creating an additional directory with rsync
33
+ hostpath = "#{hostpath}/" if hostpath !~ /\/$/
34
+
35
+ env[:ui].info(I18n.t("vagrant_hp.rsync_folder",
36
+ :hostpath => hostpath,
37
+ :guestpath => guestpath))
38
+
39
+ # Create the guest path
40
+ env[:machine].communicate.sudo("mkdir -p '#{guestpath}'")
41
+ env[:machine].communicate.sudo(
42
+ "chown #{ssh_info[:username]} '#{guestpath}'")
43
+
44
+ # Rsync over to the guest path using the SSH info
45
+ command = [
46
+ "rsync", "--verbose", "--archive", "-z",
47
+ "-e", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p #{ssh_info[:port]} -i '#{ssh_info[:private_key_path]}'",
48
+ hostpath,
49
+ "#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"]
50
+
51
+ r = Vagrant::Util::Subprocess.execute(*command)
52
+ if r.exit_code != 0
53
+ raise Errors::RsyncError,
54
+ :guestpath => guestpath,
55
+ :hostpath => hostpath,
56
+ :stderr => r.stderr
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "vagrant-hp/util/timer"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ module Action
11
+ # This is the same as the builtin provision except it times the
12
+ # provisioner runs.
13
+ class TimedProvision < Vagrant::Action::Builtin::Provision
14
+ def run_provisioner(env, p)
15
+ env[:ui].info("Insde TimedProvision")
16
+ timer = Util::Timer.time do
17
+ super
18
+ end
19
+
20
+ env[:metrics] ||= {}
21
+ env[:metrics]["provisioner_times"] ||= []
22
+ env[:metrics]["provisioner_times"] << [p.class.to_s, timer]
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end