vagrant-export 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: 972a8fee7388353a022cd1e9f8e75d324bafa269
4
+ data.tar.gz: 4df09cfe8e06af3ef5511c277d218b1bc57877cf
5
+ SHA512:
6
+ metadata.gz: 51948fb3fc9df0f489eb2cbad87430e36b90c2794a03eca400c9199efe3e400e89cf33b73dbb7c6868e85c01c979e15ffd5acefc94f502aa1103908539633ce7
7
+ data.tar.gz: ca28ad5ec9b52aced5ac660c81afdf5d279ab66cdac5cba79c466fe4be79cf633d16a643f386282221745b086dcaadcf433802b3da658a1796cb85ddc1192fbf
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ /.idea
2
+ /.vagrant
3
+ /Gemfile.lock
4
+ Vagrantfile
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git'
5
+ end
6
+
7
+ group :plugins do
8
+ gem 'vagrant-export', path: '.'
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,100 @@
1
+ GIT
2
+ remote: https://github.com/mitchellh/vagrant.git
3
+ revision: bde0e3fb2a577f58df98d053eb89a40b88d8fed0
4
+ specs:
5
+ vagrant (1.6.5)
6
+ bundler (>= 1.5.2, < 1.7.0)
7
+ childprocess (~> 0.5.0)
8
+ erubis (~> 2.7.0)
9
+ hashicorp-checkpoint (~> 0.1.1)
10
+ i18n (~> 0.6.0)
11
+ listen (~> 2.7.1)
12
+ log4r (~> 1.1.9, < 1.1.11)
13
+ net-scp (~> 1.1.0)
14
+ net-ssh (>= 2.6.6, < 2.10.0)
15
+ nokogiri (= 1.6.3.1)
16
+ rb-kqueue (~> 0.2.0)
17
+ wdm (~> 0.1.0)
18
+ winrm (~> 1.1.3)
19
+
20
+ PATH
21
+ remote: .
22
+ specs:
23
+ vagrant-export (0.1.0)
24
+
25
+ GEM
26
+ remote: https://rubygems.org/
27
+ specs:
28
+ akami (1.2.2)
29
+ gyoku (>= 0.4.0)
30
+ nokogiri
31
+ builder (3.2.2)
32
+ celluloid (0.16.0)
33
+ timers (~> 4.0.0)
34
+ childprocess (0.5.3)
35
+ ffi (~> 1.0, >= 1.0.11)
36
+ erubis (2.7.0)
37
+ ffi (1.9.3)
38
+ gssapi (1.0.3)
39
+ ffi (>= 1.0.1)
40
+ gyoku (1.1.1)
41
+ builder (>= 2.1.2)
42
+ hashicorp-checkpoint (0.1.4)
43
+ hitimes (1.2.2)
44
+ httpclient (2.4.0)
45
+ httpi (0.9.7)
46
+ rack
47
+ i18n (0.6.11)
48
+ listen (2.7.9)
49
+ celluloid (>= 0.15.2)
50
+ rb-fsevent (>= 0.9.3)
51
+ rb-inotify (>= 0.9)
52
+ little-plugger (1.1.3)
53
+ log4r (1.1.10)
54
+ logging (1.8.2)
55
+ little-plugger (>= 1.1.3)
56
+ multi_json (>= 1.8.4)
57
+ mini_portile (0.6.0)
58
+ multi_json (1.10.1)
59
+ net-scp (1.1.2)
60
+ net-ssh (>= 2.6.5)
61
+ net-ssh (2.9.1)
62
+ nokogiri (1.6.3.1)
63
+ mini_portile (= 0.6.0)
64
+ nori (1.1.5)
65
+ rack (1.5.2)
66
+ rb-fsevent (0.9.4)
67
+ rb-inotify (0.9.5)
68
+ ffi (>= 0.5.0)
69
+ rb-kqueue (0.2.3)
70
+ ffi (>= 0.5.0)
71
+ rubyntlm (0.1.1)
72
+ savon (0.9.5)
73
+ akami (~> 1.0)
74
+ builder (>= 2.1.2)
75
+ gyoku (>= 0.4.0)
76
+ httpi (~> 0.9)
77
+ nokogiri (>= 1.4.0)
78
+ nori (~> 1.0)
79
+ wasabi (~> 1.0)
80
+ timers (4.0.1)
81
+ hitimes
82
+ uuidtools (2.1.5)
83
+ wasabi (1.0.0)
84
+ nokogiri (>= 1.4.0)
85
+ wdm (0.1.0)
86
+ winrm (1.1.3)
87
+ gssapi (~> 1.0.0)
88
+ httpclient (~> 2.2, >= 2.2.0.2)
89
+ logging (~> 1.6, >= 1.6.1)
90
+ nokogiri (~> 1.5)
91
+ rubyntlm (~> 0.1.1)
92
+ savon (= 0.9.5)
93
+ uuidtools (~> 2.1.2)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ vagrant!
100
+ vagrant-export!
@@ -0,0 +1,7 @@
1
+ # #
2
+ # This is free software; you can redistribute it and/or modify it under #
3
+ # the terms of the MIT- / X11 - License #
4
+ # #
5
+
6
+ require "vagrant-export/plugin"
7
+ require "vagrant-export/version"
@@ -0,0 +1,45 @@
1
+ # #
2
+ # This is free software; you can redistribute it and/or modify it under #
3
+ # the terms of the MIT- / X11 - License #
4
+ # #
5
+
6
+ module VagrantPlugins
7
+ module Export
8
+ class Command < Vagrant.plugin '2', :command
9
+
10
+ def self.synopsis
11
+ 'exports a box file with additional actions taken'
12
+ end
13
+
14
+ def execute
15
+ options = {}
16
+ options[:quick] = false
17
+ options[:bare] = false
18
+
19
+ opts = OptionParser.new do |o|
20
+ o.banner = 'Usage: vagrant export [switches]'
21
+ o.separator ''
22
+
23
+ o.on('-f', '--fast', 'Do not perform cleanups.') do |f|
24
+ options[:fast] = f
25
+ end
26
+
27
+ o.on('-b', '--bare', 'Do not include additional files.') do |b|
28
+ options[:bare] = b
29
+ end
30
+ end
31
+
32
+ argv = parse_options(opts)
33
+ return 1 unless argv
34
+
35
+ require_relative 'exporter'
36
+ ex = Exporter.new(@env, @logger)
37
+
38
+ with_target_vms argv, reverse: true do |machine|
39
+ ex.handle machine, options[:fast], options[:bare]
40
+ end
41
+ 0
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,183 @@
1
+ # #
2
+ # This is free software; you can redistribute it and/or modify it under #
3
+ # the terms of the MIT- / X11 - License #
4
+ # #
5
+
6
+ require 'fileutils'
7
+
8
+ module VagrantPlugins
9
+ module Export
10
+ class Exporter
11
+
12
+ # @param env Vagrant::Environment
13
+ # @param logger Log4r::Logger
14
+ def initialize env, logger
15
+ @env = env
16
+ @logger = logger
17
+ end
18
+
19
+ # @param vm Vagrant::Machine
20
+ # @param fast Boolean
21
+ # @param bare Boolean
22
+ def handle vm, fast, bare
23
+ @vm = vm
24
+ @did_run = false
25
+
26
+ unless fast
27
+ if can_compress
28
+ compress
29
+ else
30
+ @env.uid.error 'Cannot compress this type of machine'
31
+ return 1
32
+ end
33
+ end
34
+
35
+ return 1 unless export
36
+
37
+ unless bare
38
+ return 1 unless files
39
+ end
40
+
41
+ finalize
42
+ end
43
+
44
+ protected
45
+
46
+
47
+ def can_compress
48
+ if @vm.state.short_description == 'running'
49
+ @did_run = true
50
+ else
51
+ @env.ui.info 'Machine not running, bringing it up'
52
+ @vm.action :up
53
+ end
54
+
55
+ compress_supported = false
56
+
57
+ if @vm.config.vm.communicator != :winrm
58
+ @vm.communicate.execute 'lsb_release -i -s', error_key: :ssh_bad_exit_status_muted do |type, data|
59
+ if type == :stdout && data.to_s =~ /mint|ubuntu|debian/i
60
+ compress_supported = true
61
+ end
62
+ end
63
+ end
64
+
65
+ compress_supported
66
+ end
67
+
68
+ def compress
69
+
70
+ target_script = "/tmp/_cleanup_#{Time.now.to_i.to_s}.sh"
71
+ source_script = File.expand_path('../../../res/cleanup.sh', __FILE__)
72
+ comm = @vm.communicate
73
+
74
+ @logger.debug "Uploading #{source_script} to #{target_script}"
75
+ comm.upload source_script, target_script
76
+
77
+ sudo comm, "chmod +x #{target_script}"
78
+ sudo comm, "#{target_script}"
79
+
80
+ 0
81
+ end
82
+
83
+ def sudo communicator, command
84
+ @logger.debug "Execute '#{command}'"
85
+ communicator.sudo command, error_key: :ssh_bad_exit_status_muted do |type, data|
86
+ if [:stderr, :stdout].include?(type)
87
+ return if data.empty?
88
+ data = data.to_s.chomp.strip
89
+ if type == :stdout
90
+ @vm.ui.info data
91
+ else
92
+ @vm.ui.error data
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ def export
99
+ # Halt the machine
100
+ if @vm.state.short_description == 'running'
101
+ @env.ui.info 'Halting VM for export'
102
+ @vm.action(:halt)
103
+ end
104
+
105
+ # Export to file
106
+ exported_path = File.join @env.tmp_path, Time.now.to_i.to_s
107
+ @tmp_path = exported_path
108
+ FileUtils.mkpath exported_path
109
+
110
+ @env.ui.info I18n.t 'vagrant.actions.vm.export.exporting'
111
+ @vm.provider.driver.export File.join(exported_path, 'box.ovf') do |progress|
112
+ @env.ui.clear_line
113
+ @env.ui.report_progress progress.percent, 100, false
114
+ end
115
+
116
+ @logger.debug "Exported VM to #{exported_path}"
117
+ end
118
+
119
+ def files
120
+
121
+ # Add metadata json
122
+ begin
123
+ metadata = File.open(File.join(@tmp_path, 'metadata.json'), 'wb')
124
+ metadata.write '{"provider":"' + @vm.provider_name.to_s + '"}'
125
+ ensure
126
+ metadata.close
127
+ end
128
+
129
+ target_include_path = File.join @tmp_path, 'include'
130
+ source_include_path = File.join @vm.box.directory, 'include'
131
+
132
+ # Copy includes
133
+ if Dir.exist? source_include_path
134
+ FileUtils.cp_r source_include_path, @tmp_path
135
+
136
+ # Add the orignal vagrant file as include
137
+ else
138
+ FileUtils.mkpath target_include_path
139
+ Dir.glob(File.join(@vm.box.directory, '**', '*')).each do |file|
140
+ if file.to_s =~ /Vagrantfile$/
141
+ FileUtils.cp file.to_s, File.join(target_include_path, '_Vagrantfile')
142
+ end
143
+ end
144
+ end
145
+
146
+ # Add the mac address setting as a Vagrantfile
147
+ File.open(File.join(@tmp_path, 'Vagrantfile'), 'wb') do |f|
148
+ f.write(Vagrant::Util::TemplateRenderer.render('package_Vagrantfile', {
149
+ base_mac: @vm.provider.driver.read_mac_address
150
+ }))
151
+ end
152
+
153
+ # Make a box file out of it
154
+ @box_file_name = @tmp_path + '.box'
155
+
156
+ Vagrant::Util::SafeChdir.safe_chdir(@tmp_path) do
157
+ files = Dir.glob File.join('.', '**', '*')
158
+ Vagrant::Util::Subprocess.execute('bsdtar', '-czf', @box_file_name, *files)
159
+ end
160
+
161
+ 0
162
+ end
163
+
164
+ def finalize
165
+ # Rename the box file
166
+ if File.exist? @box_file_name
167
+ box_name = @vm.box.name.gsub '/', '_'
168
+ target_box = File.join @env.cwd, box_name + '.box'
169
+ File.rename @box_file_name, target_box
170
+ end
171
+
172
+ # Remove the tmp files
173
+ FileUtils.rm_rf @tmp_path
174
+
175
+ # Resume the machine
176
+ if @did_run
177
+ @env.ui.info 'Bringing the machine back up'
178
+ @vm.action :up
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,25 @@
1
+ # #
2
+ # This is free software; you can redistribute it and/or modify it under #
3
+ # the terms of the MIT- / X11 - License #
4
+ # #
5
+
6
+ module VagrantPlugins
7
+ module Export
8
+ class Plugin < Vagrant.plugin '2'
9
+
10
+ name 'Vagrant Export'
11
+
12
+ description <<-EOF
13
+ Export an existing box to a .box file, like the vagrant repackage command
14
+ Additionally it will include the original Vagrantfile and other included
15
+ files and perform several cleanup operations inside the VM to reduce its
16
+ exported size. The latter requires a Ubuntu or compatible guest.
17
+ EOF
18
+
19
+ command 'export' do
20
+ require_relative 'command'
21
+ Command
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ # #
2
+ # This is free software; you can redistribute it and/or modify it under #
3
+ # the terms of the MIT- / X11 - License #
4
+ # #
5
+
6
+ module VagrantPlugins
7
+ module Export
8
+ VERSION = '0.1.0'
9
+ end
10
+ end
data/res/cleanup.sh ADDED
@@ -0,0 +1,83 @@
1
+ #!/bin/bash -eux
2
+
3
+ # Removing old kernel packages
4
+ apt-get -y --purge remove $(dpkg --list | grep '^rc' | awk '{print $2}')
5
+ apt-get -y --purge remove $(dpkg --list | egrep 'linux-image-[0-9]' | awk '{print $3,$2}' | sort -nr | tail -n +2 | grep -v $(uname -r) | awk '{ print $2}')
6
+
7
+ # Cleaning up apt
8
+ apt-get -y --purge autoremove
9
+ apt-get -y clean
10
+ rm -rf /var/lib/apt/lists/*
11
+ rm -rf /var/lib/aptitude/*
12
+
13
+ # Cleaning up home
14
+ rm -rf $HOME/.cache
15
+ rm -rf $HOME/.local
16
+ rm -rf $HOME/.npm
17
+ rm -rf $HOME/tmp
18
+
19
+ # Empty TYPO3 CMS temp files
20
+ if [[ -d /var/www/typo3temp ]]; then
21
+ find /var/www/typo3temp -type f -exec rm -f {} \;
22
+ fi
23
+
24
+ # Empty Flow temp files
25
+ if [[ -d /var/www/Data/Temporary ]]; then
26
+ rm -rf /var/www/Data/Temporary
27
+ fi
28
+
29
+
30
+ # Empty Magento temp files
31
+ if [[ -d /var/www/downloader/.cache ]]; then
32
+ rm -rf /var/www/downloader/.cache/*
33
+ fi
34
+
35
+ if [[ -d /var/www/downloader/pearlib/cache ]]; then
36
+ rm -rf /var/www/downloader/pearlib/cache/*
37
+ fi
38
+
39
+ if [[ -d /var/www/downloader/pearlib/download ]]; then
40
+ rm -rf /var/www/downloader/pearlib/download/*
41
+ fi
42
+
43
+ if [[ -d /var/www/var/cache ]]; then
44
+ rm -rf /var/www/var/cache/*
45
+ fi
46
+
47
+ if [[ -d /var/www/var/locks ]]; then
48
+ rm -rf /var/www/var/locks/*
49
+ fi
50
+
51
+ if [[ -d /var/www/var/log ]]; then
52
+ rm -rf /var/www/var/log/*
53
+ fi
54
+
55
+ if [[ -d /var/www/var/report ]]; then
56
+ rm -rf /var/www/var/report/*
57
+ fi
58
+
59
+ if [[ -d /var/www/var/cache ]]; then
60
+ rm -rf /var/www/var/cache/*
61
+ fi
62
+
63
+ if [[ -d /var/www/var/session ]]; then
64
+ rm -rf /var/www/var/session/*
65
+ fi
66
+
67
+ if [[ -d /var/www/var/tmp ]]; then
68
+ rm -rf /var/www/var/tmp/*
69
+ fi
70
+
71
+
72
+ # Empty tmp
73
+ rm -rf /tmp/*
74
+
75
+ # Make sure we sync before the next step
76
+ sync
77
+
78
+ # Zero out the free space to save space in the final image:
79
+ echo "Zeroing device to make space..."
80
+ dd if=/dev/zero of=/EMPTY bs=1M
81
+ rm -f /EMPTY
82
+
83
+ exit 0
@@ -0,0 +1,49 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ require 'vagrant-export/version'
4
+
5
+ Gem::Specification.new do |g|
6
+ g.name = 'vagrant-export'
7
+ g.version = VagrantPlugins::Export::VERSION
8
+ g.platform = Gem::Platform::RUBY
9
+ g.license = 'MIT'
10
+ g.authors = 'Georg Großberger'
11
+ g.email = 'contact@grossberger-ge.org'
12
+ g.homepage = 'https://github.com/trenker/vagrant-export'
13
+ g.summary = 'Export boxes to .box files including the original Vagrantfile and some cleanups inside the VM'
14
+ g.description = 'Export boxes to .box files including the original Vagrantfile and some cleanups inside the VM'
15
+
16
+ # The following block of code determines the files that should be included
17
+ # in the gem. It does this by reading all the files in the directory where
18
+ # this gemspec is, and parsing out the ignored files from the gitignore.
19
+ # Note that the entire gitignore(5) syntax is not supported, specifically
20
+ # the "!" syntax, but it should mostly work correctly.
21
+ root_path = File.dirname(__FILE__)
22
+ all_files = Dir.chdir(root_path) { Dir.glob('**/{*,.*}') }
23
+ all_files.reject! { |file| %w(. ..).include?(File.basename(file)) }
24
+ gitignore_path = File.join(root_path, '.gitignore')
25
+ gitignore = File.readlines(gitignore_path)
26
+ gitignore.map! { |line| line.chomp.strip }
27
+ gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ }
28
+
29
+ unignored_files = all_files.reject do |file|
30
+ # Ignore any directories, the gemspec only cares about files
31
+ next true if File.directory?(file)
32
+
33
+ # Ignore any paths that match anything in the gitignore. We do
34
+ # two tests here:
35
+ #
36
+ # - First, test to see if the entire path matches the gitignore.
37
+ # - Second, match if the basename does, this makes it so that things
38
+ # like '.DS_Store' will match sub-directories too (same behavior
39
+ # as git).
40
+ #
41
+ gitignore.any? do |ignore|
42
+ File.fnmatch(ignore, file, File::FNM_PATHNAME) ||
43
+ File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME)
44
+ end
45
+ end
46
+
47
+ g.files = unignored_files
48
+ g.require_path = 'lib'
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-export
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Georg Großberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Export boxes to .box files including the original Vagrantfile and some
14
+ cleanups inside the VM
15
+ email: contact@grossberger-ge.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - lib/vagrant-export.rb
24
+ - lib/vagrant-export/command.rb
25
+ - lib/vagrant-export/exporter.rb
26
+ - lib/vagrant-export/plugin.rb
27
+ - lib/vagrant-export/version.rb
28
+ - res/cleanup.sh
29
+ - vagrant-export.gemspec
30
+ homepage: https://github.com/trenker/vagrant-export
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.2.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Export boxes to .box files including the original Vagrantfile and some cleanups
54
+ inside the VM
55
+ test_files: []