tupperware 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: 941b14902342a2f491f032ea42910df9b5b7c764
4
+ data.tar.gz: 2575cb2b70d2a751d10c036d7d5ce652edfd4d01
5
+ SHA512:
6
+ metadata.gz: 7b61de6706c34665cd1fdce57bd35efe247c634edc6c918c9b95890156af65f4ff95a044a3813c756ca99400deae3ce5dbb9d27bc8239737de99ec5c1578af73
7
+ data.tar.gz: 3c6be30e713ab5955ad2891911911276cf9a7be54ce90a04f258ac2ca39cef4f7277a7883f5c4cc2c75f591f1f6c1744e8ae7ee484fea3e52754d7be63a93828
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tupperware.gemspec
4
+ gemspec
5
+
6
+ gem 'mixlib-shellout'
7
+ gem 'test-kitchen'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Demitri Swan
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,40 @@
1
+ # Tupperware
2
+
3
+ Package Test Kitchen instances
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install tupperware
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ Usage: tupperware [-h][-i INSTANCE][-p EXECUTABLE][-k EXECUTABLE]
15
+ -h, --help Display this help output
16
+ -i, --instance Optional: Kitchen instance to be packaged
17
+ -p, --provision Optional: Path to script to provision the instance
18
+ -k, --pre-package Optional: Path to Ruby code to execute on the workstation prior to packaging
19
+ ```
20
+
21
+ ## Supported test-kitchen drivers
22
+ * Vagrant (VirtualBox Provider)
23
+ * More coming soon!
24
+
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tupperware. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'bundler'
4
+
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc "Build and install tupperware"
9
+ task :dev => [:build, :install]
10
+
11
+ task :default => :dev
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tupperware"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bin/tupperware ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tupperware'
4
+
5
+ Tupperware.main
data/lib/tupperware.rb ADDED
@@ -0,0 +1,212 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "tupperware/version"
4
+ require 'optparse'
5
+ require 'kitchen'
6
+ require 'kitchen/transport'
7
+ require 'mixlib/shellout'
8
+ require 'fileutils'
9
+
10
+
11
+ module Tupperware
12
+
13
+ module Utils
14
+
15
+ class << self
16
+
17
+ def run_cmd(s)
18
+ cmd = Mixlib::ShellOut.new(s)
19
+ cmd.run_command
20
+ begin
21
+ cmd.error!
22
+ rescue
23
+ puts "Failed executing #{s}"
24
+ exit 1
25
+ end
26
+ return cmd
27
+ end
28
+
29
+ def check_path(p)
30
+ unless File.exist?(p)
31
+ puts "#{p} does not exist"
32
+ exit 1
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ class Base
40
+
41
+ def initialize(options = {}, kitchen_config = Kitchen::Config.new)
42
+ @kitchen_config = kitchen_config
43
+ @options = options
44
+ @supported_drivers = {'vagrant' => VagrantVirtualBox}
45
+ end
46
+
47
+ def check_driver
48
+ hash = @kitchen_config.loader.read
49
+ driver = hash[:driver][:name]
50
+ unless @supported_drivers.has_key?(driver)
51
+ puts "#{driver} is not yet supported"
52
+ exit
53
+ end
54
+ return @supported_drivers[driver]
55
+ end
56
+
57
+ def package
58
+ cls = check_driver
59
+ instance = nil
60
+ if cls == VagrantVirtualBox
61
+ if @options[:instance]
62
+ @kitchen_config.instances.each do |i|
63
+ if i.name.to_s == @options[:instance]
64
+ instance = i
65
+ break
66
+ end
67
+ end
68
+ unless instance
69
+ puts "#{@options[:instance]} is not a valid kitchen instance"
70
+ exit 1
71
+ end
72
+ instance_name = instance.name.to_s
73
+ puts "\n*** Converging #{instance_name} ***\n"
74
+ instance.converge
75
+ packager = VagrantVirtualBox.new(@options, @kitchen_config)
76
+ packager.provision_local_box(instance)
77
+ packager.halt(instance_name)
78
+ packager.package(instance_name)
79
+ else
80
+ packager = VagrantVirtualBox.new(@options, @kitchen_config)
81
+ packager.kitchen_config.instances.each do |i|
82
+ instance_name = i.name.to_s
83
+ puts "\n*** Converging #{i.name.to_s} ***\n"
84
+ i.converge
85
+ packager.provision_local_box(i)
86
+ packager.halt(i.name.to_s)
87
+ packager.package(i.name.to_s)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ class VagrantVirtualBox
95
+
96
+ attr_reader :options, :kitchen_config, :kitchen_root
97
+
98
+ def initialize(options={}, kitchen_config=Kitchen::Config.new)
99
+ @options = options
100
+ @kitchen_config = kitchen_config
101
+ @kitchen_root = @kitchen_config.kitchen_root
102
+ end
103
+
104
+ def vagrant_root(instance_name)
105
+ File.join(
106
+ @kitchen_root, %w[.kitchen kitchen-vagrant],
107
+ "kitchen-#{File.basename(@kitchen_root)}-#{instance_name}"
108
+ )
109
+ end
110
+
111
+ def get_id(instance_name)
112
+ v_root = vagrant_root(instance_name)
113
+ id_file = "#{v_root}/.vagrant/machines/default/virtualbox/id"
114
+ Utils.check_path(id_file)
115
+ IO.read("#{v_root}/.vagrant/machines/default/virtualbox/id")
116
+ end
117
+
118
+ def provision_local_box(instance)
119
+ if @options[:provision]
120
+ Utils.check_path(@options[:provision])
121
+ script = IO.read(@options[:provision])
122
+ else
123
+ script = <<-SCRIPT
124
+ #!/bin/bash
125
+ echo -n "\n*** Deploying the pulibic key ***\n"
126
+ echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key' >> /home/vagrant/.ssh/authorized_keys
127
+ echo "\n*** Setting up passwordless SSH for the vagrant user ***\n"
128
+ echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.conf
129
+ echo "\n*** Removing locale files ***\n"
130
+ sudo find /usr/share/locale/ -mindepth 1 ! -iregex "/.*en/.*" -type d -exec rm -rf {} +
131
+ echo "\n*** Cleaning logs ***\n"
132
+ sudo find /var/log -type f | while read f; do echo -n '' > $f; done;
133
+ echo "\n*** Cleaning history ***\n"
134
+ sudo rm -f /root/.bash_history
135
+ sudo rm -f /home/vagrant/.bash_history
136
+ SCRIPT
137
+ end
138
+ bin = File.join(vagrant_root(instance.name.to_s), 'kitchen-provision')
139
+ File.open(bin, 'w') {|f| f.write(script)}
140
+ state = Kitchen::StateFile.new(@kitchen_root, instance.name.to_s)
141
+ state_hash = state.read
142
+ options = {
143
+ keys: [state_hash[:ssh_key]],
144
+ port: state_hash[:port],
145
+ username: state_hash[:username],
146
+ hostname: state_hash[:hostname]
147
+ }
148
+ Kitchen::Transport::Ssh::Connection.new(options) do |c|
149
+ c.upload(bin, '/tmp/kitchen-provision')
150
+ end
151
+ instance.remote_exec('sudo chmod +x /tmp/kitchen-provision')
152
+ instance.remote_exec('sudo /tmp/kitchen-provision')
153
+ instance.remote_exec('sudo rm -f /tmp/kitchen-provision')
154
+ File.delete(bin)
155
+ end
156
+
157
+ def setup_vagrant_env(instance_name)
158
+ current_dir = ENV['PWD']
159
+ Dir.chdir(vagrant_root(instance_name))
160
+ yield
161
+ Dir.chdir(current_dir)
162
+ end
163
+
164
+ def halt(instance_name)
165
+ setup_vagrant_env(instance_name) do
166
+ Utils.run_cmd('vagrant halt')
167
+ end
168
+ end
169
+
170
+ def package(instance_name)
171
+ setup_vagrant_env(instance_name) do
172
+ if @options[:pre_package]
173
+ Utils.check_path()
174
+ load(@options[:pre_package])
175
+ end
176
+ id = get_id(instance_name)
177
+ puts "\n*** Packaging #{instance_name} ***\n"
178
+ Utils.run_cmd("vagrant package --base #{id} --output #{instance_name}.box")
179
+ current_path = File.absolute_path("#{instance_name}.box")
180
+ desired_path = File.join(@kitchen_root, "#{instance_name}.box")
181
+ FileUtils.mv(current_path, desired_path)
182
+ end
183
+ end
184
+ end
185
+
186
+ def self.main
187
+ options = {instance: nil, provision: nil, pre_package: nil}
188
+ OptionParser.new do |opts|
189
+ opts.banner = "Usage: tupperware [-h][-i INSTANCE][-p EXECUTABLE][-k EXECUTABLE]"
190
+ opts.program_name = "tupperware"
191
+ opts.on("-h", "--help", "Display this help output") do |h|
192
+ puts opts.help
193
+ exit
194
+ end
195
+ opts.on("-i", "--instance", "Optional: Kitchen instance to be packaged", :REQUIRED) do |i|
196
+ options[:instance] = i
197
+ end
198
+ opts.on("-p", "--provision", "Optional: Path to script to provision the instance", :REQUIRED) do |p|
199
+ options[:provision] = p
200
+ end
201
+ opts.on("-k","--pre-package", "Optional: Path to Ruby code to execute on the workstation prior to packaging", :REQUIRED) do |k|
202
+ options[:pre_package] = k
203
+ end
204
+
205
+ end.parse!
206
+
207
+ packager = Tupperware::Base.new(options)
208
+ packager.package
209
+
210
+ end
211
+
212
+ end
@@ -0,0 +1,3 @@
1
+ module Tupperware
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tupperware/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tupperware"
8
+ spec.version = Tupperware::VERSION
9
+ spec.authors = ["Demitri Swan"]
10
+ spec.email = ["demitri@moz.com"]
11
+
12
+ spec.summary = 'Package Test Kitchen instances'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+
17
+ spec.executables = ['tupperware']
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.10"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_runtime_dependency "test-kitchen"
24
+ spec.add_runtime_dependency "mixlib-shellout"
25
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tupperware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Demitri Swan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-12 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-kitchen
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mixlib-shellout
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - demitri@moz.com
86
+ executables:
87
+ - tupperware
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - bin/tupperware
102
+ - lib/tupperware.rb
103
+ - lib/tupperware/version.rb
104
+ - tupperware.gemspec
105
+ homepage:
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Package Test Kitchen instances
129
+ test_files: []