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,24 @@
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 WarnNetworks
10
+ def initialize(app, env)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ if env[:machine].config.vm.networks.length > 0
16
+ env[:ui].warn(I18n.t("vagrant_hp.warn_networks"))
17
+ end
18
+
19
+ @app.call(env)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,157 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "vagrant"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ class Config < Vagrant.plugin("2", :config)
11
+
12
+ attr_accessor :access_key
13
+
14
+ attr_accessor :secret_key
15
+
16
+ attr_accessor :tenant_id
17
+
18
+ attr_accessor :availability_zone
19
+
20
+ attr_accessor :flavor
21
+
22
+ attr_accessor :image
23
+
24
+ attr_accessor :server_name
25
+
26
+ attr_accessor :keypair_name
27
+
28
+ attr_accessor :ssh_private_key_path
29
+
30
+ attr_accessor :ssh_username
31
+
32
+ def initialize(region_specific=false)
33
+ @access_key = UNSET_VALUE
34
+ @secret_key= UNSET_VALUE
35
+ @server_name = UNSET_VALUE
36
+ @private_ip_address = UNSET_VALUE
37
+ @keypair_name = UNSET_VALUE
38
+ @tenant_id = UNSET_VALUE
39
+ @availability_zone = UNSET_VALUE
40
+ @image = UNSET_VALUE
41
+ @ssh_private_key_path = UNSET_VALUE
42
+ @ssh_username = UNSET_VALUE
43
+ @flavor = UNSET_VALUE
44
+
45
+ @__compiled_region_configs = {}
46
+ @__finalized = false
47
+ @__region_config = {}
48
+ @__region_specific = region_specific
49
+ end
50
+
51
+ #-------------------------------------------------------------------
52
+ # Internal methods.
53
+ #-------------------------------------------------------------------
54
+
55
+ def merge(other)
56
+ super.tap do |result|
57
+ # Copy over the region specific flag. "True" is retained if either
58
+ # has it.
59
+ new_region_specific = other.instance_variable_get(:@__region_specific)
60
+ result.instance_variable_set(
61
+ :@__region_specific, new_region_specific || @__region_specific)
62
+
63
+ # Go through all the region configs and prepend ours onto
64
+ # theirs.
65
+ new_region_config = other.instance_variable_get(:@__region_config)
66
+ @__region_config.each do |key, value|
67
+ new_region_config[key] ||= []
68
+ new_region_config[key] = value + new_region_config[key]
69
+ end
70
+
71
+ # Set it
72
+ result.instance_variable_set(:@__region_config, new_region_config)
73
+
74
+ # Merge in the tags
75
+ result.tags.merge!(self.tags)
76
+ result.tags.merge!(other.tags)
77
+ end
78
+ end
79
+
80
+ def finalize!
81
+ # The access keys default to nil
82
+ @access_key = nil if @access_key == UNSET_VALUE
83
+ @secret_key = nil if @secret_key == UNSET_VALUE
84
+ @tenant_id = nil if @tenant_id == UNSET_VALUE
85
+
86
+ @server_name = nil if @server_name == UNSET_VALUE
87
+
88
+ # AMI must be nil, since we can't default that
89
+ @image = nil if @image == UNSET_VALUE
90
+
91
+ # Default instance type is an standard.small
92
+ @flavor = "standard.small" if @flavor == UNSET_VALUE
93
+
94
+ # Keypair defaults to nil
95
+ @keypair_name = nil if @keypair_name == UNSET_VALUE
96
+
97
+ # Default the private IP to nil since VPC is not default
98
+ @private_ip_address = nil if @private_ip_address == UNSET_VALUE
99
+
100
+ # Default availability-zone is az1. This is sensible because HP Cloud
101
+ # generally defaults to this as well.
102
+ @availability_zone = "az1" if @availability_zone == UNSET_VALUE
103
+
104
+ # The SSH values by default are nil, and the top-level config
105
+ # `config.ssh` values are used.
106
+ @ssh_private_key_path = nil if @ssh_private_key_path == UNSET_VALUE
107
+ @ssh_username = nil if @ssh_username == UNSET_VALUE
108
+
109
+ # Mark that we finalized
110
+ @__finalized = true
111
+ end
112
+
113
+ def validate(machine)
114
+ errors = []
115
+ warnings = []
116
+ messages = []
117
+
118
+ # access_key: required
119
+ errors << I18n.t("vagrant_hp.config.access_key_required") \
120
+ if @access_key.nil?
121
+
122
+ # secret_key: required
123
+ errors << I18n.t("vagrant_hp.config.secret_key_required") \
124
+ if @secret_key.nil?
125
+
126
+ # tenant_id: required
127
+ errors << I18n.t("vagrant_hp.config.tenant_id_required") \
128
+ if @tenant_id.nil?
129
+
130
+ # keypair_name: required
131
+ errors << I18n.t("vagrant_hp.config.keypair_name_required") \
132
+ if @keypair_name.nil?
133
+
134
+ # image: required
135
+ errors << I18n.t("vagrant_hp.config.image_required") \
136
+ if @image.nil?
137
+
138
+ # ssh_private_key_path: required
139
+ errors << I18n.t("vagrant_hp.config.ssh_private_key_path") \
140
+ if @ssh_private_key_path.nil?
141
+
142
+ { "HP Provider" => errors }
143
+ end
144
+
145
+ # This gets the configuration for a specific region. It shouldn't
146
+ # be called by the general public and is only used internally.
147
+ def get_region_config(name)
148
+ if !@__finalized
149
+ raise "Configuration must be finalized before calling this method."
150
+ end
151
+
152
+ # Return the compiled region config
153
+ @__compiled_region_configs[name] || self
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,24 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "vagrant"
7
+
8
+ module VagrantPlugins
9
+ module HP
10
+ module Errors
11
+ class VagrantHPError < Vagrant::Errors::VagrantError
12
+ error_namespace("vagrant_hp.errors")
13
+ end
14
+
15
+ class FogError < VagrantHPError
16
+ error_key(:fog_error)
17
+ end
18
+
19
+ class RsyncError < VagrantHPError
20
+ error_key(:rsync_error)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,78 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ begin
7
+ require "vagrant"
8
+ rescue LoadError
9
+ raise "The Vagrant HP plugin must be run within Vagrant."
10
+ end
11
+
12
+ # This is a sanity check to make sure no one is attempting to install
13
+ # this into an early Vagrant version.
14
+ if Vagrant::VERSION < "1.1.0"
15
+ raise "The Vagrant HP plugin is only compatible with Vagrant 1.1+"
16
+ end
17
+
18
+ module VagrantPlugins
19
+ module HP
20
+ class Plugin < Vagrant.plugin("2")
21
+ name "HP"
22
+ description <<-DESC
23
+ This plugin installs a provider that allows Vagrant to manage
24
+ machines in HP Cloud.
25
+ DESC
26
+
27
+ config(:hp, :provider) do
28
+ require_relative "config"
29
+ Config
30
+ end
31
+
32
+ provider(:hp) do
33
+ # Setup logging and i18n
34
+ setup_logging
35
+ setup_i18n
36
+
37
+ # Return the provider
38
+ require_relative "provider"
39
+ Provider
40
+ end
41
+
42
+ # This initializes the internationalization strings.
43
+ def self.setup_i18n
44
+ I18n.load_path << File.expand_path("locales/en.yml", HP.source_root)
45
+ I18n.reload!
46
+ end
47
+
48
+ # This sets up our log level to be whatever VAGRANT_LOG is.
49
+ def self.setup_logging
50
+ require "log4r"
51
+
52
+ level = nil
53
+ begin
54
+ level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
55
+ rescue NameError
56
+ # This means that the logging constant wasn't found,
57
+ # which is fine. We just keep `level` as `nil`. But
58
+ # we tell the user.
59
+ level = nil
60
+ end
61
+
62
+ # Some constants, such as "true" resolve to booleans, so the
63
+ # above error checking doesn't catch it. This will check to make
64
+ # sure that the log level is an integer, as Log4r requires.
65
+ level = nil if !level.is_a?(Integer)
66
+
67
+ # Set the logging level on all "vagrant" namespaced
68
+ # logs as long as we have a valid level.
69
+ if level
70
+ logger = Log4r::Logger.new("vagrant_hp")
71
+ logger.outputters = Log4r::Outputter.stderr
72
+ logger.level = level
73
+ logger = nil
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,55 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ require "log4r"
7
+ require "vagrant"
8
+
9
+ module VagrantPlugins
10
+ module HP
11
+ class Provider < Vagrant.plugin("2", :provider)
12
+ def initialize(machine)
13
+ @machine = machine
14
+ end
15
+
16
+ def action(name)
17
+ # Attempt to get the action method from the Action class if it
18
+ # exists, otherwise return nil to show that we don't support the
19
+ # given action.
20
+ action_method = "action_#{name}"
21
+ return Action.send(action_method) if Action.respond_to?(action_method)
22
+ nil
23
+ end
24
+
25
+ def ssh_info
26
+ # Run a custom action called "read_ssh_info" which does what it
27
+ # says and puts the resulting SSH info into the `:machine_ssh_info`
28
+ # key in the environment.
29
+ env = @machine.action("read_ssh_info")
30
+ env[:machine_ssh_info]
31
+ end
32
+
33
+ def state
34
+ # Run a custom action we define called "read_state" which does
35
+ # what it says. It puts the state in the `:machine_state_id`
36
+ # key in the environment.
37
+ env = @machine.action("read_state")
38
+
39
+ state_id = env[:machine_state_id]
40
+
41
+ # Get the short and long description
42
+ short = I18n.t("vagrant_hp.states.short_#{state_id.to_s.downcase}")
43
+ long = I18n.t("vagrant_hp.states.long_#{state_id.to_s.downcase}")
44
+
45
+ # Return the MachineState object
46
+ Vagrant::MachineState.new(state_id, short, long)
47
+ end
48
+
49
+ def to_s
50
+ id = @machine.id.nil? ? "new" : @machine.id
51
+ "HP (#{id})"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module HP
3
+ module Util
4
+ class Timer
5
+ # A basic utility method that times the execution of the given
6
+ # block and returns it.
7
+ def self.time
8
+ start_time = Time.now.to_f
9
+ yield
10
+ end_time = Time.now.to_f
11
+
12
+ end_time - start_time
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ #
2
+ # Author:: Mohit Sethi (<mohit@sethis.in>)
3
+ # Copyright:: Copyright (c) 2013 Mohit Sethi.
4
+ #
5
+
6
+ module VagrantPlugins
7
+ module HP
8
+ VERSION = "0.1.0"
9
+ end
10
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,91 @@
1
+ en:
2
+ vagrant_hp:
3
+ already_created: |-
4
+ The machine is already created.
5
+ launching_server: |-
6
+ Launching a server with the following settings...
7
+ launch_no_keypair: |-
8
+ Warning! You didn't specify a keypair to launch your instance with.
9
+ This can sometimes result in not being able to access your instance.
10
+ launch_vpc_warning: |-
11
+ Warning! You're launching this instance into a VPC without an
12
+ elastic IP. Please verify you're properly connected to a VPN so
13
+ you can access this machine, otherwise Vagrant will not be able
14
+ to SSH into it.
15
+ not_created: |-
16
+ Instance is not created. Please run `vagrant up` first.
17
+ ready: |-
18
+ Machine is booted and ready for use!
19
+ rsync_folder: |-
20
+ Rsyncing folder: %{hostpath} => %{guestpath}
21
+ terminating: |-
22
+ Terminating the instance...
23
+ waiting_for_build: |-
24
+ Waiting for the server to be built...
25
+ waiting_for_ready: |-
26
+ Waiting for instance to become "ready"...
27
+ waiting_for_ssh: |-
28
+ Waiting for SSH to become available...
29
+ warn_networks: |-
30
+ Warning! The HP provider doesn't support any of the Vagrant
31
+ high-level network configurations (`config.vm.network`). They
32
+ will be silently ignored.
33
+ deleting_server: |-
34
+ "Attemping to delete server on HP Cloud"
35
+ finding_flavor: |-
36
+ Finding flavor for server...
37
+ finding_image: |-
38
+ Finding image for server...
39
+
40
+ default:
41
+ ssh_username_default: |-
42
+ No ssh_username provided, using "vagrant" as ssh_username
43
+
44
+ config:
45
+ access_key_required: |-
46
+ An access key is required, please specify via "access_key_id"
47
+ image_required: |-
48
+ An image is required, please specify via "image=<IMAGE_ID>" or "image=<IMAGE-NAME>"
49
+ ssh_private_key_path: |-
50
+ A private key is required, please specify via "ssh_private_key_path"
51
+ secret_key_required: |-
52
+ A secret key is required, please specify via "secret_key"
53
+ keypair_name_required: |-
54
+ A keypair name is required, please specify via "keypair_name".
55
+ tenant_id_required: |-
56
+ A tenant id is required, please specify via "tenant_id"
57
+
58
+ errors:
59
+ fog_error: |-
60
+ There was an error talking to HP. The error message is shown
61
+ below:
62
+
63
+ %{message}
64
+ rsync_error: |-
65
+ There was an error when attemping to rsync a share folder.
66
+ Please inspect the error message below for more info.
67
+
68
+ Host path: %{hostpath}
69
+ Guest path: %{guestpath}
70
+ Error: %{stderr}
71
+
72
+ states:
73
+ short_active: |-
74
+ active
75
+ long_active: |-
76
+ The server is up and running. Run `vagrant ssh` to access it.
77
+ short_build: |-
78
+ building
79
+ long_build: |-
80
+ The server is currently being built. You must wait for this to
81
+ complete before you can access it. You can delete the server, however,
82
+ by running `vagrant destroy`.
83
+ short_error: |-
84
+ error
85
+ long_error: |-
86
+ The server is in an erroneous state. Destroy the machine with
87
+ `vagrant destroy`.
88
+ short_not_created: |-
89
+ not created
90
+ long_not_created: |-
91
+ The server is not created. Run `vagrant up` to create it.