vagrant-mos 0.9.24 → 0.9.26

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3fb943e2fe26ba7b745aa95fd16b2c5252d46050
4
- data.tar.gz: a7b9148932b560e9751db3ce0b0c45dcb7c601f4
3
+ metadata.gz: 57be0c6f5eff6eefeb4afd72af71c74a7e6fc461
4
+ data.tar.gz: 300d5ec646c56cca793c29aea0eccc2f4ea85cae
5
5
  SHA512:
6
- metadata.gz: 2d98de545c48cd1b20634fd016f4c9641bc75c284019d6151e316876b2765192d4f638d105a5c81d7474b3e426a552548b7bffad5711ca2313ddab905ce413dc
7
- data.tar.gz: f57b7334609697ecdba75fd7267e168cbc369e69435c77a36066a292a75089d702b4db020090fb8d339442e7c28ec8e9996a7784f8c85abed780699e41e7f66f
6
+ metadata.gz: f9551652c353461ca1fa4ebcbb753e3747a69909081490d658fe6ea6f366ad20dbf86788a46cb77aaec31ddd012495698231dc5c008bc12e0071d81b5167843a
7
+ data.tar.gz: 2dc8807b52aa2ef982a299026fb6df6aadfb13667c8de4bc5b01a45f1e85bcf4cc1a58b6a591f2ea3659f71625bd709e71703cbffefb6671442c103c17d671e7
data/README.md CHANGED
@@ -90,6 +90,7 @@ MOS provider设置了若干参数,主要参数说明如下:
90
90
  * `data_disk` - 创建美团云主机的数据盘大小,单位为GB,例如100代表创建100G的数据盘
91
91
  * `band_width` - 创建美团云主机的外网带宽大小,单位为Mbps,例如10代表选择10Mbps的外网带宽
92
92
  * `instance_ready_timeout` - 等待MOS主机创建成功最长时间,单位为秒。默认为120s
93
+ * `instance_package_timeout` - 等待模板创建成功最长时间,单位为秒。默认为600s
93
94
  * `instance_name` - 创建的MOS主机名称,例如 "ubuntu007"。
94
95
  * `instance_type` - 创建的MOS主机类型,例如"C1_M1". 默认配置为 "C1_M2".
95
96
  * `keypair_name` - 用户使用的秘钥名称。通过使用秘钥,用户登录该创建的主机时就不需要在输入繁琐的密码了
@@ -8,6 +8,21 @@ module VagrantPlugins
8
8
  # Include the built-in modules so we can use them as top-level things.
9
9
  include Vagrant::Action::Builtin
10
10
 
11
+ def self.action_package
12
+ Vagrant::Action::Builder.new.tap do |b|
13
+ b.use Call, IsCreated do |env, b2|
14
+ if !env[:result]
15
+ b2.use MessageNotCreated
16
+ next
17
+ end
18
+
19
+ # Connect to MOS and then Create a package from the server instance
20
+ b2.use ConnectMOS
21
+ b2.use PackageInstance
22
+ end
23
+ end
24
+ end
25
+
11
26
  # This action is called to halt the remote machine.
12
27
  def self.action_halt
13
28
  Vagrant::Action::Builder.new.tap do |b|
@@ -188,6 +203,7 @@ module VagrantPlugins
188
203
  autoload :TimedProvision, action_root.join("timed_provision") # some plugins now expect this action to exist
189
204
  autoload :WaitForState, action_root.join("wait_for_state")
190
205
  autoload :WarnNetworks, action_root.join("warn_networks")
206
+ autoload :PackageInstance, action_root.join("package_instance")
191
207
  end
192
208
  end
193
209
  end
@@ -0,0 +1,204 @@
1
+ require "log4r"
2
+ require 'vagrant/util/template_renderer'
3
+ require 'vagrant-mos/util/timer'
4
+ require 'vagrant/action/general/package'
5
+
6
+ module VagrantPlugins
7
+ module MOS
8
+ module Action
9
+ # This action packages a running mos-based server into an
10
+ # mos-based vagrant box. It does so by burning the associated
11
+ # vagrant-mos server instance, into an template via fog. Upon
12
+ # successful template burning, the action will create a .box tarball
13
+ # writing a Vagrantfile with the fresh template id into it.
14
+
15
+ # Vagrant itself comes with a general package action, which
16
+ # this plugin action does call. The general action provides
17
+ # the actual packaging as well as other options such as
18
+ # --include for including additional files and --vagrantfile
19
+ # which is pretty much not useful here anyway.
20
+
21
+ # The virtualbox package plugin action was loosely used
22
+ # as a model for this class.
23
+
24
+ class PackageInstance < Vagrant::Action::General::Package
25
+ include Vagrant::Util::Retryable
26
+
27
+ def initialize(app, env)
28
+ @app = app
29
+ @logger = Log4r::Logger.new("vagrant_mos::action::package_instance")
30
+ env["package.include"] ||= []
31
+ env["package.output"] ||= "package.box"
32
+ end
33
+
34
+ alias_method :general_call, :call
35
+
36
+ def call(env)
37
+ # Initialize metrics if they haven't been
38
+ env[:metrics] ||= {}
39
+
40
+ # This block attempts to burn the server instance into an template
41
+ begin
42
+ # Get the server object for given machine
43
+ #server = env[:mos_compute].servers.get(env[:machine].id)
44
+ server = (env[:mos_compute].describe_instances([env[:machine].id]))["Instance"]
45
+
46
+ env[:ui].info(I18n.t("vagrant_mos.packaging_instance", :instance_id => server["instanceId"]))
47
+
48
+
49
+ env[:mos_compute].create_template(server["instanceId"], "template_name", "template_notes")
50
+
51
+
52
+ =begin
53
+
54
+
55
+ # Make the request to MOS to create an template from machine's instance
56
+ ami_response = server.service.create_image server.id, "#{server.tags["Name"]} Package - #{Time.now.strftime("%Y%m%d-%H%M%S")}", ""
57
+
58
+ # Find ami id
59
+ @ami_id = ami_response.data[:body]["imageId"]
60
+
61
+ # Attempt to burn the mos instance into an template within timeout
62
+ env[:metrics]["instance_ready_time"] = Util::Timer.time do
63
+
64
+ # Get the config, to set the ami burn timeout
65
+ region = env[:machine].provider_config.region
66
+ region_config = env[:machine].provider_config.get_region_config(region)
67
+ tries = region_config.instance_package_timeout / 2
68
+
69
+ env[:ui].info(I18n.t("vagrant_mos.burning_ami", :ami_id => @ami_id))
70
+
71
+ # Check the status of the template every 2 seconds until the ami burn timeout has been reached
72
+ begin
73
+ retryable(:on => Fog::Errors::TimeoutError, :tries => tries) do
74
+ # If we're interrupted don't worry about waiting
75
+ next if env[:interrupted]
76
+
77
+ # Need to update the ami_obj on each cycle
78
+ ami_obj = server.service.images.get(@ami_id)
79
+
80
+ # Wait for the server to be ready, raise error if timeout reached
81
+ server.wait_for(2) {
82
+ if ami_obj.state == "failed"
83
+ raise Errors::InstancePackageError,
84
+ ami_id: ami_obj.id,
85
+ err: ami_obj.state
86
+ return
87
+ else
88
+ # Successful template burn will result in true here
89
+ ami_obj.ready?
90
+ end
91
+ }
92
+ end
93
+ rescue Fog::Errors::TimeoutError
94
+ # Notify the user upon timeout
95
+ raise Errors::InstancePackageTimeout,
96
+ timeout: region_config.instance_package_timeout
97
+ end
98
+ end
99
+ env[:ui].info(I18n.t("vagrant_mos.packaging_instance_complete", :time_seconds => env[:metrics]["instance_ready_time"].to_i))
100
+
101
+ =end
102
+ rescue MOS::Error => e
103
+ raise Errors::MosError, :message => e.message
104
+ end
105
+
106
+ # Handles inclusions from --include and --vagrantfile options
107
+ setup_package_files(env)
108
+
109
+ # Setup the temporary directory for the tarball files
110
+ @temp_dir = env[:tmp_path].join(Time.now.to_i.to_s)
111
+ env["export.temp_dir"] = @temp_dir
112
+ FileUtils.mkpath(env["export.temp_dir"])
113
+
114
+ # Create the Vagrantfile and metadata.json files from templates to go in the box
115
+ create_vagrantfile(env)
116
+ create_metadata_file(env)
117
+
118
+ # Just match up a couple environmental variables so that
119
+ # the superclass will do the right thing. Then, call the
120
+ # superclass to actually create the tarball (.box file)
121
+ env["package.directory"] = env["export.temp_dir"]
122
+ general_call(env)
123
+
124
+ # Always call recover to clean up the temp dir
125
+ clean_temp_dir
126
+ end
127
+
128
+ protected
129
+
130
+ # Cleanup temp dir and files
131
+ def clean_temp_dir
132
+ if @temp_dir && File.exist?(@temp_dir)
133
+ FileUtils.rm_rf(@temp_dir)
134
+ end
135
+ end
136
+
137
+ # This method generates the Vagrantfile at the root of the box. Taken from
138
+ # VagrantPlugins::ProviderVirtualBox::Action::PackageVagrantfile
139
+ def create_vagrantfile env
140
+ File.open(File.join(env["export.temp_dir"], "Vagrantfile"), "w") do |f|
141
+ f.write(TemplateRenderer.render("vagrant-mos_package_Vagrantfile", {
142
+ region: env[:machine].provider_config.region,
143
+ ami: @ami_id,
144
+ template_root: template_root
145
+ }))
146
+ end
147
+ end
148
+
149
+ # This method generates the metadata.json file at the root of the box.
150
+ def create_metadata_file env
151
+ File.open(File.join(env["export.temp_dir"], "metadata.json"), "w") do |f|
152
+ f.write(TemplateRenderer.render("metadata.json", {
153
+ template_root: template_root
154
+ }))
155
+ end
156
+ end
157
+
158
+ # Sets up --include and --vagrantfile files which may be added as optional
159
+ # parameters. Taken from VagrantPlugins::ProviderVirtualBox::Action::SetupPackageFiles
160
+ def setup_package_files(env)
161
+ files = {}
162
+ env["package.include"].each do |file|
163
+ source = Pathname.new(file)
164
+ dest = nil
165
+
166
+ # If the source is relative then we add the file as-is to the include
167
+ # directory. Otherwise, we copy only the file into the root of the
168
+ # include directory. Kind of strange, but seems to match what people
169
+ # expect based on history.
170
+ if source.relative?
171
+ dest = source
172
+ else
173
+ dest = source.basename
174
+ end
175
+
176
+ # Assign the mapping
177
+ files[file] = dest
178
+ end
179
+
180
+ if env["package.vagrantfile"]
181
+ # Vagrantfiles are treated special and mapped to a specific file
182
+ files[env["package.vagrantfile"]] = "_Vagrantfile"
183
+ end
184
+
185
+ # Verify the mapping
186
+ files.each do |from, _|
187
+ raise Vagrant::Errors::PackageIncludeMissing,
188
+ file: from if !File.exist?(from)
189
+ end
190
+
191
+ # Save the mapping
192
+ env["package.files"] = files
193
+ end
194
+
195
+ # Used to find the base location of mos-vagrant templates
196
+ def template_root
197
+ MOS.source_root.join("templates")
198
+ end
199
+
200
+
201
+ end
202
+ end
203
+ end
204
+ end
@@ -28,6 +28,11 @@ module VagrantPlugins
28
28
  # @return [String]
29
29
  attr_accessor :name
30
30
 
31
+ # The timeout to wait for an instance to successfully burn into an template.
32
+ #
33
+ # @return [Fixnum]
34
+ attr_accessor :instance_package_timeout
35
+
31
36
  # The timeout to wait for an instance to become ready.
32
37
  #
33
38
  # @return [Fixnum]
@@ -85,6 +90,7 @@ module VagrantPlugins
85
90
  @data_disk = UNSET_VALUE
86
91
  @band_width = UNSET_VALUE
87
92
  @instance_ready_timeout = UNSET_VALUE
93
+ @instance_package_timeout = UNSET_VALUE
88
94
  @name = UNSET_VALUE
89
95
  @instance_type = UNSET_VALUE
90
96
  @keypair_name = UNSET_VALUE
@@ -182,7 +188,8 @@ module VagrantPlugins
182
188
  # Set the default timeout for waiting for an instance to be ready
183
189
  @instance_ready_timeout = 120 if @instance_ready_timeout == UNSET_VALUE
184
190
 
185
-
191
+ # Set the default timeout for waiting for an instance to burn into a template
192
+ @instance_package_timeout = 600 if @instance_package_timeout == UNSET_VALUE
186
193
  # Default instance type is an C1_M2
187
194
  @instance_type = "C1_M2" if @instance_type == UNSET_VALUE
188
195
  # Keypair defaults to nil
@@ -19,6 +19,14 @@ module VagrantPlugins
19
19
  error_key(:instance_ready_timeout)
20
20
  end
21
21
 
22
+ class InstancePackageError < VagrantMOSError
23
+ error_key(:instance_package_error)
24
+ end
25
+
26
+ class InstancePackageTimeout < VagrantMOSError
27
+ error_key(:instance_package_timeout)
28
+ end
29
+
22
30
  class RsyncError < VagrantMOSError
23
31
  error_key(:rsync_error)
24
32
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module MOS
3
- VERSION = '0.9.24'
3
+ VERSION = '0.9.26'
4
4
  end
5
5
  end
data/locales/en.yml CHANGED
@@ -69,6 +69,18 @@ en:
69
69
  set waiting for the instance to become ready is %{timeout} seconds.
70
70
  Please verify that the machine properly boots. If you need more time
71
71
  set the `instance_ready_timeout` configuration on the MOS provider.
72
+
73
+ instance_package_error: |-
74
+ There was an error packaging the instance. See details below for more info.
75
+
76
+ Template Id: %{template_id}
77
+ Error: %{err}
78
+ instance_package_timeout: |-
79
+ The Template failed to become "ready" in MOS. The timeout currently
80
+ set waiting for the instance to become ready is %{timeout} seconds. For
81
+ larger instances Template burning may take long periods of time. Please
82
+ ensure the timeout is set high enough, it can be changed by adjusting
83
+ the `instance_package_timeout` configuration on the MOS provider.
72
84
  rsync_error: |-
73
85
  There was an error when attempting to rsync a shared folder.
74
86
  Please inspect the error message below for more info.
@@ -18,6 +18,7 @@ describe VagrantPlugins::MOS::Config do
18
18
  its("access_key") { should be_nil }
19
19
  its("template_id") { should be_nil }
20
20
  its("instance_ready_timeout") { should == 120 }
21
+ its("instance_package_timeout") { should == 600 }
21
22
  its("name") { should be_nil }
22
23
  its("instance_type") { should == "C1_M2" }
23
24
  its("keypair_name") { should be_nil }
@@ -34,7 +35,7 @@ describe VagrantPlugins::MOS::Config do
34
35
  # each of these attributes to "foo" in isolation, and reads the value
35
36
  # and asserts the proper result comes back out.
36
37
  [:access_key, :template_id, :data_disk, :band_width, :instance_ready_timeout, :name,
37
- :instance_type, :keypair_name, :ssh_host_attribute,
38
+ :instance_package_timeout, :instance_type, :keypair_name, :ssh_host_attribute,
38
39
  :region, :access_secret, :access_url,
39
40
  :use_iam_profile].each do |attribute|
40
41
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-mos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.24
4
+ version: 0.9.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - yangcs2009
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-06 00:00:00.000000000 Z
11
+ date: 2015-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mos-sdk
@@ -103,6 +103,7 @@ files:
103
103
  - lib/vagrant-mos/action/message_already_created.rb
104
104
  - lib/vagrant-mos/action/message_not_created.rb
105
105
  - lib/vagrant-mos/action/message_will_not_destroy.rb
106
+ - lib/vagrant-mos/action/package_instance.rb
106
107
  - lib/vagrant-mos/action/read_ssh_info.rb
107
108
  - lib/vagrant-mos/action/read_state.rb
108
109
  - lib/vagrant-mos/action/run_instance.rb