pack_rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f65fc75dcf838baeccbb94bcdf3501939611b96c
4
+ data.tar.gz: 3a2a1392b3708a5d2441a3f4517270b60046f9b1
5
+ SHA512:
6
+ metadata.gz: 643b458325d3d61049f7ae01bd1024705bf2101aa99f37a5f0682edc20630bdd1302167164ef2d252ffb6882f7bfbe4e9be62c837ec52571054d6ea31ffd141b
7
+ data.tar.gz: 3505a8f2c6a9d65ae8bb0f4028c1923e7e0178bb33fb2501354e6e3a50fa8fc8f7edeb58c55c2d8aa664ca907431006941934c236fc4f2b879f2df2219226d59
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
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.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pack_rb.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,26 @@
1
+ guard :bundler do
2
+ require 'guard/bundler'
3
+ require 'guard/bundler/verify'
4
+ helper = Guard::Bundler::Verify.new
5
+
6
+ files = ['Gemfile']
7
+ files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }
8
+
9
+ # Assume files are symlinked from somewhere
10
+ files.each { |file| watch(helper.real_path(file)) }
11
+ end
12
+
13
+ guard :rspec, cmd: "bundle exec rspec" do
14
+ require "guard/rspec/dsl"
15
+ dsl = Guard::RSpec::Dsl.new(self)
16
+
17
+ # RSpec files
18
+ rspec = dsl.rspec
19
+ watch(rspec.spec_helper) { rspec.spec_dir }
20
+ watch(rspec.spec_support) { rspec.spec_dir }
21
+ watch(rspec.spec_files)
22
+
23
+ # Ruby files
24
+ ruby = dsl.ruby
25
+ dsl.watch_spec_files_for(ruby.lib_files)
26
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jonathan Niesen
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,84 @@
1
+ # PackRb
2
+
3
+ [![Circle CI](https://circleci.com/gh/manheim/pack_rb.svg?style=svg)](https://circleci.com/gh/manheim/pack_rb)
4
+
5
+ A gem for driving the [Packer][1] command line tool from within your Ruby project.
6
+
7
+ This gem was built against version ``0.9.0`` of [Packer][1]
8
+
9
+ ## Pre-Requisites
10
+
11
+ You must have a version of [Packer][1] installed.
12
+
13
+ Go [download a copy][2] for your OS.
14
+
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'pack_rb'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install pack_rb
31
+
32
+ ## Usage
33
+
34
+ ### With In Memory Template
35
+
36
+ ```ruby
37
+ require 'pack_rb'
38
+ require 'json'
39
+
40
+ template = {
41
+ variables: { foo: 'bar'},
42
+ builders: [
43
+ {
44
+ type: 'null',
45
+ ssh_host: '127.0.0.1',
46
+ ssh_username: 'foo',
47
+ ssh_password: 'bar'
48
+ }
49
+ ]
50
+ }.to_json
51
+
52
+ packer = PackRb::Packer.new(tpl: template, machine_readable: true)
53
+ packer.build(debug: true)
54
+ ```
55
+
56
+ __Note:__ A plain old ruby hash can be used for template also. Just leave off
57
+ the JSON bits.
58
+
59
+ ### With Template File
60
+
61
+ ```ruby
62
+ require 'pack_rb'
63
+
64
+ packer = PackRb::Packer.new(tpl: 'config/template.json', machine_readable: true)
65
+ packer.build(debug: true)
66
+ ```
67
+
68
+ ## Development
69
+
70
+ 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. Run `bundle exec pack_rb` to use the gem in this directory, ignoring other installed copies of this gem.
71
+
72
+ 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).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pack_rb.
77
+
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
82
+
83
+ [1]: https://www.packer.io/
84
+ [2]: https://www.packer.io/downloads.html
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pack_rb"
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
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.1
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+ require 'pack_rb/sub_commands'
3
+
4
+ module PackRb
5
+ class Packer
6
+ def initialize(opts = {})
7
+ @machine_readable = opts[:machine_readable]
8
+ @bin_path = opts[:bin_path]
9
+ @tpl = opts[:tpl]
10
+ end
11
+
12
+ def command
13
+ if machine_readable
14
+ "#{bin} -machine-readable"
15
+ else
16
+ bin
17
+ end
18
+ end
19
+
20
+ def bin
21
+ bin_path || 'packer'
22
+ end
23
+
24
+ def template
25
+ obj = @tpl
26
+ json?(obj) || path?(obj) || hash?(obj)
27
+ end
28
+
29
+ def commander
30
+ @commander ||= PackRb::SubCommands.new
31
+ end
32
+
33
+ def method_missing(name, *args, &block)
34
+ return super unless commander.respond_to?(name)
35
+
36
+ opts = {
37
+ base_cmd: command,
38
+ tpl: template,
39
+ args: args.first
40
+ }
41
+
42
+ commander.send(name, opts)
43
+ end
44
+
45
+ private
46
+
47
+ def can_match?(obj)
48
+ obj.respond_to?(:match)
49
+ end
50
+
51
+ def like_json?(obj)
52
+ obj.match(/{.*}/)
53
+ end
54
+
55
+ def json?(obj)
56
+ can_match?(obj) && like_json?(obj) && obj
57
+ end
58
+
59
+ def path?(obj)
60
+ can_match?(obj) && File.read(obj)
61
+ end
62
+
63
+ def hash?(obj)
64
+ obj.to_json
65
+ end
66
+
67
+ def bin_path
68
+ @bin_path
69
+ end
70
+
71
+ def machine_readable
72
+ @machine_readable
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,61 @@
1
+ require 'pack_rb/sub_commands/error/errors'
2
+ module PackRb
3
+ class SubCommands
4
+ module Build
5
+ def build(opts)
6
+ base_cmd = opts[:base_cmd]
7
+ args = opts[:args]
8
+ tpl = opts[:tpl]
9
+
10
+ cmd_arr = [ base_cmd, 'build' ]
11
+ cmd_arr << parse_options(args) if args
12
+
13
+ execute(cmd: cmd_arr.join(' '), tpl: tpl)
14
+ end
15
+
16
+ def parse_options(opts)
17
+ transformed_opts = opts.inject([]) do |arr, kv|
18
+ key = kv.first
19
+ val = kv.last
20
+
21
+ raise Error::UnsupportedOption unless supported?(key)
22
+
23
+ arr << transform(key).call(key.to_s, val)
24
+ end
25
+
26
+ transformed_opts.join(' ')
27
+ end
28
+
29
+ private
30
+ def transformation_strategy
31
+ {
32
+ except: array_type,
33
+ only: array_type,
34
+ color: boolean_type,
35
+ force: flag_type,
36
+ debug: flag_type
37
+ }
38
+ end
39
+
40
+ def transform(opt)
41
+ transformation_strategy[opt]
42
+ end
43
+
44
+ def supported?(opt)
45
+ transformation_strategy.keys.include?(opt)
46
+ end
47
+
48
+ def array_type
49
+ Proc.new { |opt, val| "-#{opt}=#{val.join(',')}" }
50
+ end
51
+
52
+ def boolean_type
53
+ Proc.new { |opt, val| "-#{opt}=#{val}" }
54
+ end
55
+
56
+ def flag_type
57
+ Proc.new { |opt, val| val ? "-#{opt}" : '' }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,8 @@
1
+ module PackRb
2
+ class SubCommands
3
+ module Error
4
+ class UnsupportedOption < StandardError
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module PackRb
2
+ class SubCommands
3
+ module Inspect
4
+ def inspect_tpl(opts)
5
+ base_cmd = opts[:base_cmd]
6
+ tpl = opts[:tpl]
7
+
8
+ cmd_arr = [base_cmd, 'inspect']
9
+
10
+ execute(cmd: cmd_arr.join(' '), tpl: tpl)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module PackRb
2
+ class SubCommands
3
+ module Validate
4
+ def validate(opts)
5
+ base_cmd = opts[:base_cmd]
6
+ tpl = opts[:tpl]
7
+
8
+ cmd_arr = [base_cmd, 'validate']
9
+
10
+ execute(cmd: cmd_arr.join(' '), tpl: tpl)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ require 'open3'
2
+
3
+ require 'pack_rb/sub_commands/build'
4
+ require 'pack_rb/sub_commands/inspect'
5
+ require 'pack_rb/sub_commands/validate'
6
+
7
+ module PackRb
8
+ class SubCommands
9
+ include PackRb::SubCommands::Build
10
+ include PackRb::SubCommands::Inspect
11
+ include PackRb::SubCommands::Validate
12
+
13
+ def execute(opts)
14
+ cmd = opts[:cmd]
15
+ tpl = opts[:tpl]
16
+
17
+ out, err, status = Open3.capture3("#{cmd} -", stdin_data: tpl)
18
+
19
+ if status.exitstatus != 0
20
+ puts out if out
21
+ puts err if err
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module PackRb
2
+ VERSION = "0.1.0"
3
+ end
data/lib/pack_rb.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "pack_rb/version"
2
+ require 'pack_rb/packer'
3
+
4
+ module PackRb
5
+ # Your code goes here...
6
+ end
data/pack_rb.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pack_rb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pack_rb"
8
+ spec.version = PackRb::VERSION
9
+ spec.authors = ["Jonathan Niesen"]
10
+ spec.email = ["jon.niesen@gmail.com"]
11
+
12
+ spec.summary = %q{A Packer CLI wrapper.}
13
+ spec.description = ['A gem for driving the Packer command line tool',
14
+ 'from within your Ruby project.'].join(' ')
15
+ spec.homepage = "https://github.com/manheim/pack_rb"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do
19
+ |f| f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "guard", "~> 2.13"
25
+ spec.add_development_dependency "guard-bundler", "~> 2.1"
26
+ spec.add_development_dependency "guard-rspec", "~> 4.6"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pack_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Niesen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: guard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: A gem for driving the Packer command line tool from within your Ruby
84
+ project.
85
+ email:
86
+ - jon.niesen@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Guardfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - circle.yml
102
+ - lib/pack_rb.rb
103
+ - lib/pack_rb/packer.rb
104
+ - lib/pack_rb/sub_commands.rb
105
+ - lib/pack_rb/sub_commands/build.rb
106
+ - lib/pack_rb/sub_commands/error/errors.rb
107
+ - lib/pack_rb/sub_commands/inspect.rb
108
+ - lib/pack_rb/sub_commands/validate.rb
109
+ - lib/pack_rb/version.rb
110
+ - pack_rb.gemspec
111
+ homepage: https://github.com/manheim/pack_rb
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: A Packer CLI wrapper.
135
+ test_files: []