vagrant-disksize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c0e70788c6b8f747354f486d19a1e85b3ada6be
4
+ data.tar.gz: dc08aab0dfc7dcd908e6cd2dc6b0a33626f8c26c
5
+ SHA512:
6
+ metadata.gz: 6b6b0e2782b10a6b855f93f8b3af99abe8a6c5aae58c8eed0a1c114e09b06a603a0c0ea3beda2652759822085748b1f8cab0701c032fbc5887f584ae9a4d013c
7
+ data.tar.gz: 925ecacc060eebb5399c722bc53f6b380ffa0d621280a0139cd699726e8ca0f26af516784bf67fcc6cfdd96497bcaf32cf870ca11da4fac87a103f7047df0b50
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .ruby-version
12
+ .ruby-gemset
13
+
14
+ .*.swp
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git', tag: 'v1.8.5'
5
+ end
6
+
7
+ group :plugins do
8
+ gem 'vagrant-disksize', path: '.'
9
+ end
10
+
11
+
12
+ # Specify your gem's dependencies in vagrant-disksize.gemspec
13
+ # Actually, we can't do this as we need to be in the plugins group
14
+ # for vagrant to load us during development
15
+ #gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Simon Protheroe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Vagrant::Disksize
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/vagrant/disksize`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'vagrant-disksize'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install vagrant-disksize
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vagrant-disksize.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/Vagrantfile ADDED
@@ -0,0 +1,73 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ # All Vagrant configuration is done below. The "2" in Vagrant.configure
5
+ # configures the configuration version (we support older styles for
6
+ # backwards compatibility). Please don't change it unless you know what
7
+ # you're doing.
8
+ Vagrant.configure("2") do |config|
9
+ # The most common configuration options are documented and commented below.
10
+ # For a complete reference, please see the online documentation at
11
+ # https://docs.vagrantup.com.
12
+
13
+ # Every Vagrant development environment requires a box. You can search for
14
+ # boxes at https://atlas.hashicorp.com/search.
15
+ config.vm.box = "ubuntu/xenial64"
16
+
17
+ # Disable automatic box update checking. If you disable this, then
18
+ # boxes will only be checked for updates when the user runs
19
+ # `vagrant box outdated`. This is not recommended.
20
+ # config.vm.box_check_update = false
21
+
22
+ # Create a forwarded port mapping which allows access to a specific port
23
+ # within the machine from a port on the host machine. In the example below,
24
+ # accessing "localhost:8080" will access port 80 on the guest machine.
25
+ # config.vm.network "forwarded_port", guest: 80, host: 8080
26
+
27
+ # Create a private network, which allows host-only access to the machine
28
+ # using a specific IP.
29
+ # config.vm.network "private_network", ip: "192.168.33.10"
30
+
31
+ # Create a public network, which generally matched to bridged network.
32
+ # Bridged networks make the machine appear as another physical device on
33
+ # your network.
34
+ # config.vm.network "public_network"
35
+
36
+ # Share an additional folder to the guest VM. The first argument is
37
+ # the path on the host to the actual folder. The second argument is
38
+ # the path on the guest to mount the folder. And the optional third
39
+ # argument is a set of non-required options.
40
+ # config.vm.synced_folder "../data", "/vagrant_data"
41
+
42
+ # Provider-specific configuration so you can fine-tune various
43
+ # backing providers for Vagrant. These expose provider-specific options.
44
+ # Example for VirtualBox:
45
+ #
46
+ # config.vm.provider "virtualbox" do |vb|
47
+ # # Display the VirtualBox GUI when booting the machine
48
+ # vb.gui = true
49
+ #
50
+ # # Customize the amount of memory on the VM:
51
+ # vb.memory = "1024"
52
+ # end
53
+ #
54
+ # View the documentation for the provider you are using for more
55
+ # information on available options.
56
+
57
+ config.disksize.size = '20GB'
58
+
59
+ # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
60
+ # such as FTP and Heroku are also available. See the documentation at
61
+ # https://docs.vagrantup.com/v2/push/atlas.html for more information.
62
+ # config.push.define "atlas" do |push|
63
+ # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
64
+ # end
65
+
66
+ # Enable provisioning with a shell script. Additional provisioners such as
67
+ # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
68
+ # documentation for more information about their specific syntax and use.
69
+ # config.vm.provision "shell", inline: <<-SHELL
70
+ # apt-get update
71
+ # apt-get install -y apache2
72
+ # SHELL
73
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vagrant/disksize"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ #require "vagrant/disksize/version"
2
+ begin
3
+ require 'vagrant'
4
+ rescue LoadError
5
+ raise 'The vagrant-disksize plugin must be run within vagrant.'
6
+ end
7
+
8
+
9
+ module Vagrant
10
+ module Disksize
11
+ class Plugin < Vagrant.plugin('2')
12
+
13
+ name 'vagrant-disksize'
14
+
15
+ description <<-DESC
16
+ Provides the ability to resize VirtualBox disks at creation time,
17
+ so they don't need to be the same size as the default for the box.
18
+ Filesystems are not resized by this code.
19
+ DESC
20
+
21
+ config 'disksize' do
22
+ require_relative 'disksize/config'
23
+ Config
24
+ end
25
+
26
+ action_hook(:disksize, :machine_action_up) do |hook|
27
+ require_relative 'disksize/actions'
28
+
29
+ # TODO Ensure we are using the VirtualBox provider
30
+ hook.before(VagrantPlugins::ProviderVirtualBox::Action::Boot, Action::ResizeDisk)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,140 @@
1
+ module Vagrant
2
+ module Disksize
3
+ class Action
4
+
5
+ class ResizeDisk
6
+
7
+ # NOTE - size is represented in MB throughout this class, for ease of using with VirtualBox
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @machine = env[:machine]
12
+ @enabled = true
13
+ if @machine.provider.to_s !~ /VirtualBox/
14
+ @enabled = false
15
+ env[:ui].error "The vagrant-disksize plugin only supports VirtualBox at present. Disk size will not be changed."
16
+ end
17
+ end
18
+
19
+ def call(env)
20
+ # Resize disk itself before boot
21
+ if @enabled
22
+ size = @machine.config.disksize.size
23
+ ensure_disk_resizable(env)
24
+ resize_disk(env, size)
25
+ end
26
+
27
+ # Allow middleware chain to continue so VM is booted
28
+ @app.call(env)
29
+
30
+ # TODO Possibly resize partition and filesystem here if needed
31
+ end
32
+
33
+ private
34
+
35
+ def ensure_disk_resizable(env)
36
+ driver = @machine.provider.driver
37
+ disks = identify_disks(driver)
38
+ # TODO Shouldn't assume that the first disk is the one we want to resize
39
+ unless disk_resizeable? disks.first
40
+ old_disk = disks.first
41
+ new_disk = generate_resizable_disk(old_disk)
42
+ unless File.exist? new_disk[:file]
43
+ clone_as_vdi(driver, old_disk, new_disk)
44
+ attach_disk(driver, new_disk)
45
+ File.delete(old_disk[:file])
46
+ end
47
+ end
48
+ end
49
+
50
+ def resize_disk(env, req_size)
51
+ driver = @machine.provider.driver
52
+ disks = identify_disks(driver)
53
+ target = disks.first # TODO Shouldn't assume that the first disk is the one we want to resize
54
+
55
+ old_size = get_disk_size(driver, target)
56
+ if old_size < req_size
57
+ grow_vdi(driver, target, req_size)
58
+ new_size = get_disk_size(driver, target)
59
+ env[:ui].success "Resized disk: old #{old_size} MB, req #{req_size} MB, new #{new_size} MB"
60
+ env[:ui].success "You may need to resize the filesystem from within the guest."
61
+ elsif old_size > req_size
62
+ env[:ui].error "Disk cannot be decreased in size. #{req_size} MB requested but disk is already #{old_size} MB."
63
+ end
64
+ end
65
+
66
+ def clone_as_vdi(driver, src, dst)
67
+ driver.execute('clonemedium', src[:file], dst[:file], '--format', 'VDI')
68
+ end
69
+
70
+ def grow_vdi(driver, disk, size)
71
+ driver.execute('modifymedium', disk[:file], '--resize', size.to_s)
72
+ end
73
+
74
+ def attach_disk(driver, disk)
75
+ parts = disk[:name].split('-')
76
+ controller = parts[0]
77
+ port = parts[1]
78
+ device = parts[2]
79
+ driver.execute('storageattach', @machine.id, '--storagectl', controller, '--port', port, '--device', device, '--type', 'hdd', '--medium', disk[:file])
80
+ end
81
+
82
+ def get_disk_size(driver, disk)
83
+ size = nil
84
+ driver.execute('showmediuminfo', disk[:file]).each_line do |line|
85
+ if line =~ /Capacity:\s+([0-9]+)\s+MB/
86
+ size = $1.to_i
87
+ end
88
+ end
89
+ size
90
+ end
91
+
92
+ def identify_disks(driver)
93
+ vminfo = get_vminfo(driver)
94
+ disks = []
95
+ disk_keys = vminfo.keys.select { |k| k =~ /-ImageUUID-/ }
96
+ disk_keys.each do |key|
97
+ uuid = vminfo[key]
98
+ disk_name = key.gsub(/-ImageUUID-/,'-')
99
+ disk_file = vminfo[disk_name]
100
+ disks << {
101
+ uuid: uuid,
102
+ name: disk_name,
103
+ file: disk_file
104
+ }
105
+ end
106
+ disks
107
+ end
108
+
109
+ def get_vminfo(driver)
110
+ vminfo = {}
111
+ driver.execute('showvminfo', @machine.id, '--machinereadable', retryable: true).split("\n").each do |line|
112
+ parts = line.partition('=')
113
+ key = unquoted(parts.first)
114
+ value = unquoted(parts.last)
115
+ vminfo[key] = value
116
+ end
117
+ vminfo
118
+ end
119
+
120
+ def generate_resizable_disk(disk)
121
+ src = disk[:file]
122
+ src_extn = File.extname(src)
123
+ src_path = File.dirname(src)
124
+ src_base = File.basename(src, src_extn)
125
+ dst = File.join(src_path, src_base) + '.vdi'
126
+ disk.merge({ uuid: "(undefined)", file: dst })
127
+ end
128
+
129
+ def disk_resizeable?(disk)
130
+ disk[:file].end_with? '.vdi'
131
+ end
132
+
133
+ def unquoted(s)
134
+ s.gsub(/\A"(.*)"\Z/,'\1')
135
+ end
136
+ end
137
+
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,44 @@
1
+ module Vagrant
2
+ module Disksize
3
+ class Config < Vagrant.plugin('2', :config)
4
+ attr_accessor :size
5
+
6
+ SIZE_REGEX = /^(?<number>[0-9]+)\s?(?<scale>KB|MB|GB|TB)?$/
7
+
8
+ def initialize
9
+ @size = UNSET_VALUE
10
+ end
11
+
12
+ def finalize!
13
+ # Convert from human to machine readable
14
+ size_str = @size.to_s.strip
15
+ matches = SIZE_REGEX.match(size_str)
16
+ if matches
17
+ number = matches[:number]
18
+ scale = matches[:scale]
19
+ @size = number.to_i
20
+ if scale
21
+ pos = %w(KB MB GB TB).index(scale)
22
+ mult = 1 << 10*(pos+1)
23
+ @size *= mult
24
+ end
25
+ end
26
+ # Convert size from bytes to MB
27
+ size_in_mb = (@size.to_i + (1<<20)-1) / (1<<20)
28
+ @size = size_in_mb
29
+ end
30
+
31
+ def validate(machine)
32
+ errors = []
33
+
34
+ unless @size.to_s =~ SIZE_REGEX
35
+ errors << "'#{@size}' is not a valid specification of disk size"
36
+ end
37
+
38
+ return { 'Disksize configuration' => errors }
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,5 @@
1
+ module Vagrant
2
+ module Disksize
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant/disksize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-disksize"
8
+ spec.version = Vagrant::Disksize::VERSION
9
+ spec.authors = ["Simon Protheroe"]
10
+ spec.email = ["protheroe@gmail.com"]
11
+
12
+ spec.summary = %q{Vagrant plugin to resize VirtualBox disks}
13
+ spec.description = %q{Vagrant plugin to resize VirtualBox disks at creation time}
14
+ spec.homepage = "https://github.com/sprotheroe/vagrant-disksize"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-disksize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Protheroe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Vagrant plugin to resize VirtualBox disks at creation time
42
+ email:
43
+ - protheroe@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - Vagrantfile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/vagrant/disksize.rb
57
+ - lib/vagrant/disksize/actions.rb
58
+ - lib/vagrant/disksize/config.rb
59
+ - lib/vagrant/disksize/version.rb
60
+ - vagrant-disksize.gemspec
61
+ homepage: https://github.com/sprotheroe/vagrant-disksize
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ allowed_push_host: https://rubygems.org
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.8
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Vagrant plugin to resize VirtualBox disks
86
+ test_files: []