vagrant-mutate 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,8 +4,17 @@ Vagrant-mutate is a [vagrant](http://www.vagrantup.com/) plugin to convert vagra
4
4
 
5
5
  ## Supported Conversions
6
6
 
7
- * Virtualbox to [kvm](https://github.com/adrahon/vagrant-kvm) (tested against 0.1.4)
8
- * Virtualbox to [libvirt](https://github.com/pradels/vagrant-libvirt) (tested against 0.0.11)
7
+ * Virtualbox to kvm
8
+ * Virtualbox to libvirt
9
+ * Libvirt to kvm
10
+
11
+ ## Compatibility
12
+
13
+ Vagrant-mutate has been tested against the following versions. It may work with other versions too.
14
+
15
+ * vagrant 1.3.5
16
+ * [vagrant-kvm](https://github.com/adrahon/vagrant-kvm) 0.1.4
17
+ * [vagrant-libvirt](https://github.com/pradels/vagrant-libvirt) 0.0.11
9
18
 
10
19
  ## Installation
11
20
 
@@ -51,10 +60,17 @@ Or if you had already downloaded it
51
60
 
52
61
  vagrant mutate precise32.box libvirt
53
62
 
54
- Or if you had already added the virtualbox version of the box to vagrant and now want to use it with libvirt
63
+ Or if you had already added the box to vagrant and now want to use it with libvirt
55
64
 
56
65
  vagrant mutate precise32 libvirt
57
66
 
67
+ If you have a box for multiple providers, you must specify the provider to use for input by prepending it to the name with a slash, e.g.
68
+
69
+ $ vagrant box list
70
+ precise32 (kvm)
71
+ precise32 (virtualbox)
72
+ $ vagrant mutate virtualbox/precise32 libvirt
73
+
58
74
  To export a box you created with vagrant mutate, just repackage it, e.g.
59
75
 
60
76
  vagrant box repackage precise32 libvirt
@@ -7,9 +7,9 @@ module VagrantMutate
7
7
  def initialize(env, name, dir)
8
8
  super
9
9
  @provider_name = 'kvm'
10
- @supported_input = false,
11
- @supported_output = true,
12
- @image_format = 'qcow2',
10
+ @supported_input = false
11
+ @supported_output = true
12
+ @image_format = 'qcow2'
13
13
  @image_name = 'box-disk1.img'
14
14
  end
15
15
 
@@ -7,12 +7,37 @@ module VagrantMutate
7
7
  def initialize(env, name, dir)
8
8
  super
9
9
  @provider_name = 'libvirt'
10
- @supported_input = false,
11
- @supported_output = true,
12
- @image_format = 'qcow2',
10
+ @supported_input = true
11
+ @supported_output = true
12
+ @image_format = 'qcow2'
13
13
  @image_name = 'box.img'
14
14
  end
15
15
 
16
+ # since none of below can be determined from the box
17
+ # we just generate sane values
18
+
19
+ def architecture
20
+ return 'x86_64'
21
+ end
22
+
23
+ # kvm prefix is 52:54:00
24
+ def mac_address
25
+ octets = 3.times.map { rand(255).to_s(16) }
26
+ return "52:54:00:#{octets[0]}:#{octets[1]}:#{octets[2]}"
27
+ end
28
+
29
+ def cpus
30
+ return 1
31
+ end
32
+
33
+ def memory
34
+ return 536870912
35
+ end
36
+
37
+ def disk_interface
38
+ return 'virtio'
39
+ end
40
+
16
41
  end
17
42
  end
18
43
  end
@@ -8,9 +8,9 @@ module VagrantMutate
8
8
  def initialize(env, name, dir)
9
9
  super
10
10
  @provider_name = 'virtualbox'
11
- @supported_input = true,
12
- @supported_output = false,
13
- @image_format = 'vmdk',
11
+ @supported_input = true
12
+ @supported_output = false
13
+ @image_format = 'vmdk'
14
14
  @image_name = 'box-disk1.vmdk'
15
15
  end
16
16
 
@@ -48,13 +48,13 @@ module VagrantMutate
48
48
  elsif box_arg =~ /\.box$/
49
49
  box = load_from_file(box_arg)
50
50
  else
51
- box = load_by_name(box_arg)
51
+ box = load_from_boxes_path(box_arg)
52
52
  end
53
53
 
54
54
  if box.supported_input
55
55
  return box
56
56
  else
57
- raise Errors::ProviderNotSupported, :provider => provider_name, :direction => 'input'
57
+ raise Errors::ProviderNotSupported, :provider => box.provider_name, :direction => 'input'
58
58
  end
59
59
  end
60
60
 
@@ -105,11 +105,15 @@ module VagrantMutate
105
105
  box = create_box(provider_name, name, dir)
106
106
  end
107
107
 
108
- def load_by_name(name)
109
- @logger.info "Loading box from name #{name}"
110
- dir = find_input_dir(name)
111
- # cheat for now since only supported input is virtualbox
112
- box = create_box('virtualbox', name, dir)
108
+ def load_from_boxes_path(identifier)
109
+ @logger.info "Loading box #{identifier} from vagrants box path"
110
+ provider_name, name = parse_identifier(identifier)
111
+ if provider_name
112
+ dir = verify_input_dir(provider_name, name)
113
+ else
114
+ provider_name, dir = find_input_dir(name)
115
+ end
116
+ box = create_box(provider_name, name, dir)
113
117
  end
114
118
 
115
119
  def cleanup
@@ -140,14 +144,46 @@ module VagrantMutate
140
144
  end
141
145
  end
142
146
 
147
+ def parse_identifier(identifier)
148
+ if identifier =~ /^([\w-]+)#{File::SEPARATOR}([\w-]+)$/
149
+ @logger.info "Parsed provider name as #{$1} and box name as #{$2}"
150
+ return $1, $2
151
+ else
152
+ @logger.info "Parsed provider name as not given and box name as #{identifier}"
153
+ return nil, identifier
154
+ end
155
+ end
156
+
143
157
  def find_input_dir(name)
144
- # cheat for now since only supported input is virtualbox
145
- in_dir = File.join( @env.boxes_path, name, 'virtualbox' )
146
- if File.directory?(in_dir)
147
- @logger.info "Found input directory #{in_dir}"
148
- return in_dir
158
+ box_parent_dir = File.join( @env.boxes_path, name)
159
+
160
+ if Dir.exist?(box_parent_dir)
161
+ providers = Dir.entries(box_parent_dir).reject { |entry| entry =~ /^\./ }
162
+ @logger.info "Found potential providers #{providers}"
163
+ else
164
+ providers = []
165
+ end
166
+
167
+ case
168
+ when providers.length < 1
169
+ raise Errors::BoxNotFound, :box => name
170
+ when providers.length > 1
171
+ raise Errors::TooManyBoxesFound, :box => name
172
+ else
173
+ provider_name = providers.first
174
+ input_dir = File.join( box_parent_dir, provider_name)
175
+ @logger.info "Found source for box #{name} from provider #{provider_name} at #{input_dir}"
176
+ return provider_name, input_dir
177
+ end
178
+ end
179
+
180
+ def verify_input_dir(provider_name, name)
181
+ input_dir = File.join( @env.boxes_path, name, provider_name)
182
+ if File.directory?(input_dir)
183
+ @logger.info "Found input directory #{input_dir}"
184
+ return input_dir
149
185
  else
150
- raise Errors::BoxNotFound, :box => in_dir
186
+ raise Errors::BoxNotFound, :box => input_dir
151
187
  end
152
188
  end
153
189
 
@@ -27,6 +27,10 @@ module VagrantMutate
27
27
  end
28
28
 
29
29
  def convert()
30
+ if @input_box.provider_name == @output_box.provider_name
31
+ raise Errors::ProvidersMatch
32
+ end
33
+
30
34
  @env.ui.info "Converting #{@input_box.name} from #{@input_box.provider_name} "\
31
35
  "to #{@output_box.provider_name}."
32
36
 
@@ -6,6 +6,10 @@ module VagrantMutate
6
6
  error_namespace('vagrant_mutate.errors')
7
7
  end
8
8
 
9
+ class ProvidersMatch < VagrantMutateError
10
+ error_key(:providers_match)
11
+ end
12
+
9
13
  class ProviderNotSupported < VagrantMutateError
10
14
  error_key(:provider_not_supported)
11
15
  end
@@ -18,6 +22,10 @@ module VagrantMutate
18
22
  error_key(:box_not_found)
19
23
  end
20
24
 
25
+ class TooManyBoxesFound < VagrantMutateError
26
+ error_key(:too_many_boxes_found)
27
+ end
28
+
21
29
  class ExtractBoxFailed < VagrantMutateError
22
30
  error_key(:extract_box_failed)
23
31
  end
@@ -20,7 +20,6 @@ module VagrantMutate
20
20
  box_arg = argv[0]
21
21
  output_provider_arg = argv[1]
22
22
 
23
-
24
23
  input_loader = BoxLoader.new(@env)
25
24
  input_box = input_loader.load(box_arg)
26
25
 
@@ -1,3 +1,3 @@
1
1
  module VagrantMutate
2
- VERSION = '0.1.5'
2
+ VERSION = '0.2.0'
3
3
  end
data/locales/en.yml CHANGED
@@ -1,12 +1,16 @@
1
1
  en:
2
2
  vagrant_mutate:
3
3
  errors:
4
+ providers_match: |-
5
+ Input and output provider are the same. Aborting.
4
6
  provider_not_supported: |-
5
7
  Vagrant-mutate does not support %{provider} for %{direction}
6
8
  qemu_not_found: |-
7
9
  Qemu-img was not found in your path
8
10
  box_not_found: |-
9
- The box at %{box} was not found
11
+ The box %{box} was not found
12
+ too_many_boxes_found: |-
13
+ More than one box named %{box} was found. Please specify the input provider as well.
10
14
  extract_box_failed: |-
11
15
  Extracting box failed with error:
12
16
  %{error_message}
@@ -0,0 +1,11 @@
1
+ Vagrant::Config.run do |config|
2
+ # This Vagrantfile is auto-generated by `vagrant package` to contain
3
+ # the MAC address of the box. Custom configuration should be placed in
4
+ # the actual `Vagrantfile` in this box.
5
+ config.vm.base_mac = "08002755B88D"
6
+ end
7
+
8
+ # Load include vagrant file if it exists after the auto-generated
9
+ # so it can override any of the settings
10
+ include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
11
+ load include_vagrantfile if File.exist?(include_vagrantfile)
@@ -0,0 +1,62 @@
1
+ <domain type='kvm'>
2
+ <name>mutate-test</name>
3
+
4
+ <memory unit='KiB'>524288</memory>
5
+ <currentMemory unit='KiB'>524288</currentMemory>
6
+ <vcpu placement='static'>1</vcpu>
7
+ <os>
8
+ <type arch='x86_64' machine='pc-1.2'>hvm</type>
9
+ <boot dev='hd'/>
10
+ </os>
11
+ <features>
12
+ <acpi/>
13
+ <apic/>
14
+ <pae/>
15
+ </features>
16
+ <clock offset='utc'/>
17
+ <on_poweroff>destroy</on_poweroff>
18
+ <on_reboot>restart</on_reboot>
19
+ <on_crash>restart</on_crash>
20
+ <devices>
21
+ <emulator>/usr/bin/qemu-system-x86_64</emulator>
22
+ <disk type='file' device='disk'>
23
+ <driver name='qemu' type='qcow2'/>
24
+ <source file='box-disk1.img'/>
25
+ <target dev='vda' bus='virtio'/>
26
+
27
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
28
+ </disk>
29
+
30
+ <controller type='usb' index='0'>
31
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
32
+ </controller>
33
+ <controller type='virtio-serial' index='0'>
34
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
35
+ </controller>
36
+ <interface type='network'>
37
+ <mac address='52:54:00:cb:b2:80'/>
38
+ <source network='vagrant'/>
39
+ <model type='virtio'/>
40
+ </interface>
41
+ <serial type='pty'>
42
+ <target port='0'/>
43
+ </serial>
44
+ <console type='pty'>
45
+ <target type='serial' port='0'/>
46
+ </console>
47
+ <input type='mouse' bus='ps2'/>
48
+ <sound model='ich6'>
49
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
50
+ </sound>
51
+
52
+ <graphics type='vnc' port='-1' autoport='yes'/>
53
+
54
+ <video>
55
+ <model type='cirrus' vram='9216' heads='1'/>
56
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
57
+ </video>
58
+ <memballoon model='virtio'>
59
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
60
+ </memballoon>
61
+ </devices>
62
+ </domain>
@@ -0,0 +1 @@
1
+ {"provider":"kvm"}
data/test/test.rb CHANGED
@@ -29,6 +29,21 @@ def install_plugin
29
29
  Dir.chdir working_dir
30
30
  end
31
31
 
32
+ # convering libvirt to kvm has some random output which we must replace
33
+ # with the static "random" value in the sampe output we are comapring against
34
+ def derandomize_output(input, output_dir)
35
+ if input == 'libvirt'
36
+ if File.split(output_dir).last == 'kvm'
37
+ path = File.join(output_dir, 'box.xml')
38
+ contents = File.read(path)
39
+ contents.gsub!(/52:54:00:[0-9a-f:]+/, '52:54:00:cb:b2:80')
40
+ File.open(path, 'w') do |f|
41
+ f.write(contents)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
32
47
  def test(input, outputs)
33
48
  failures = []
34
49
  test_dir = File.expand_path( File.dirname(__FILE__) )
@@ -45,13 +60,14 @@ def test(input, outputs)
45
60
  system("vagrant mutate #{input_box} #{output}")
46
61
  output_dir = File.join(vagrant_dir, 'boxes', 'mutate-test', output)
47
62
  expected_output_dir = File.join(test_dir, 'expected_output', input, output)
63
+ derandomize_output(input, output_dir)
48
64
  Dir.foreach(expected_output_dir) do |f|
49
65
  next if f == '.' or f == '..'
50
66
  output = File.join(output_dir, f)
51
67
  expected_output = File.join(expected_output_dir, f)
52
68
  test_passed = FileUtils.compare_file(output, expected_output)
53
69
  unless test_passed
54
- failures.push "#{output} does not match #{expected_output}"
70
+ failures.push "These two files do not match #{output} #{expected_output}"
55
71
  end
56
72
  end
57
73
  end
@@ -62,6 +78,7 @@ end
62
78
  cleanup
63
79
  build_plugin
64
80
  failures = test( 'virtualbox', ['kvm', 'libvirt'] )
81
+ failures += test( 'libvirt', ['kvm'] )
65
82
 
66
83
  unless failures.empty?
67
84
  puts "\nTESTS FAILED"
@@ -18,8 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "archive-tar-minitar", "~> 0.5.0"
22
-
23
21
  spec.add_development_dependency "bundler", "~> 1.3"
24
22
  spec.add_development_dependency "rake"
25
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-mutate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-17 00:00:00.000000000 Z
12
+ date: 2014-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: archive-tar-minitar
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 0.5.0
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 0.5.0
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: bundler
32
16
  requirement: !ruby/object:Gem::Requirement
@@ -86,6 +70,10 @@ files:
86
70
  - lib/vagrant-mutate/version.rb
87
71
  - locales/en.yml
88
72
  - templates/kvm/box.xml.erb
73
+ - test/expected_output/libvirt/kvm/Vagrantfile
74
+ - test/expected_output/libvirt/kvm/box-disk1.img
75
+ - test/expected_output/libvirt/kvm/box.xml
76
+ - test/expected_output/libvirt/kvm/metadata.json
89
77
  - test/expected_output/virtualbox/kvm/Vagrantfile
90
78
  - test/expected_output/virtualbox/kvm/box-disk1.img
91
79
  - test/expected_output/virtualbox/kvm/box.xml
@@ -124,6 +112,10 @@ signing_key:
124
112
  specification_version: 3
125
113
  summary: Convert vagrant boxes to work with different providers
126
114
  test_files:
115
+ - test/expected_output/libvirt/kvm/Vagrantfile
116
+ - test/expected_output/libvirt/kvm/box-disk1.img
117
+ - test/expected_output/libvirt/kvm/box.xml
118
+ - test/expected_output/libvirt/kvm/metadata.json
127
119
  - test/expected_output/virtualbox/kvm/Vagrantfile
128
120
  - test/expected_output/virtualbox/kvm/box-disk1.img
129
121
  - test/expected_output/virtualbox/kvm/box.xml