packer-binary 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: 183b48dbb3e3b1a4f896f3395ee13b6aa6c458d7
4
+ data.tar.gz: 33a071f59ebaaafb854644e433da7d1819d84ddc
5
+ SHA512:
6
+ metadata.gz: e598d51851b740d7021adcc64159dce8c6d61930c8f952399a67a15342d5d7184c3fd14688d9f739ae5e89a7c8ab6521701e49da099f26d83cc748895311c93e
7
+ data.tar.gz: 814879243ce9747ff531073bc0cc5156816b0404f84475cfdc45a68e4706a91d185a8a78be55b55173b242e3c337070f7773641470b179346b800e8148934cfe
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.4.1
6
+ before_install: gem install bundler -v 1.15.4
7
+ deploy:
8
+ provider: rubygems
9
+ api_key:
10
+ secure: r43ZO/TYFG+0JKl2422Gv7PGNTP4sQQE6+91GTXAZ27ctkv51sNggeJTHYFVW4Ogju3w3QALz6p1G1hbs1abit6egFokiCLPAffr+ltsJRFdwJeSMpGXWoQIEdVmJM0FkIu9ujhd3RNWGzYC/lS3K8grBd/zyPGqYuayjkuIGL7QURWPLyTdYFIYMreRf+8/fYXDRUbNGCpIy0RlV4SpPyQzj4NKRjr1CBfSVjF3ke/edWW3ktMufHsZ5fIW4kvxSzkkGl9wHJsP2OO+ZaJ6xyLq7bsY49NcqHX5hs3GTKgRrisp8pTWckdAdFEPtoPefZFR5a+kyyuZBR1Z9rDKmJW2Deh/4xN74pfUY+FCrNoVS7uYi3XteGE0U8Fyb+eJcvseQrJcm1xd1FbCTTFiwhOPCjEXyUASzhrjrM2i8CZATxrg/2+8VQuH5UP7ET0z/cpB4dudkGecnjanWiL0nQbmRHc/DnhEn3maA94qscIrwUdz45OwaJyuD4gaxo5UDxX0xK4U1C1tyQisaZaLOAhEaLbHM5JEkoB5RGa+K3r2x+WqZVaeE65t/rXhJfVManzVU82zrhIg2aAm+sMIatpWgt949zCnNXctOSXIhyoOrsCOfuyRLQJB6cQFgDIIsA2JqJLRl/C8VAKYc4tQLJyJzBxmjC/l3SGSJUebXoo=
11
+ on:
12
+ tags: true
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in packer-binary.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nathan Cazell
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,62 @@
1
+ [![Build Status](https://travis-ci.org/NathanTCz/packer-binary.svg?branch=master)](https://travis-ci.org/NathanTCz/packer-binary)
2
+ [![Coverage Status](https://coveralls.io/repos/github/NathanTCz/packer-binary/badge.svg?branch=master)](https://coveralls.io/github/NathanTCz/packer-binary?branch=master)
3
+ [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/NathanTCz/packer-binary/master)
4
+
5
+ # Packer::Binary
6
+
7
+ A gem to install and interface with HashiCorp's Packer utility
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'packer-binary'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install packer-binary
24
+
25
+ ## Usage
26
+ You need to specify which version of [HashiCorp Packer](https://www.packer.io/downloads.html) to use. You can also specify where to save the binary file.
27
+
28
+ ```ruby
29
+ require 'packer/binary'
30
+
31
+ Packer::Binary.configure do |config|
32
+ config.version = '1.0.4'
33
+ config.download_path = '/tmp'
34
+ end
35
+ ```
36
+
37
+ ### Convenience Methods
38
+ {Packer::Binary.Build} - Run the `build` command. Optionally pass any arguments supported by Packer.
39
+
40
+ {Packer::Binary.Fix} - Run the `fix` command. Optionally pass any arguments supported by Packer.
41
+
42
+ {Packer::Binary.Inspect} - Run the `inspect` command. Optionally pass any arguments supported by Packer.
43
+
44
+ {Packer::Binary.Push} - Run the `push` command. Optionally pass any arguments supported by Packer.
45
+
46
+ {Packer::Binary.Validate} - Run the `validate` command. Optionally pass any arguments supported by Packer.
47
+
48
+ {Packer::Binary.Version} - Run the `version` command. Optionally pass any arguments supported by Packer.
49
+
50
+ ## Development
51
+
52
+ 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.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nathantcz/packer-binary.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "packer/binary"
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(__FILE__)
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,57 @@
1
+ require 'os'
2
+
3
+ require "packer/binary/version"
4
+ require 'packer/binary/helpers'
5
+ require 'packer/binary/compressor'
6
+ require 'packer/binary/executable'
7
+ require 'packer/binary/command'
8
+
9
+ module Packer
10
+ module Binary
11
+ module_function
12
+
13
+ attr_writer :config
14
+
15
+ def config
16
+ @config ||= Configuration.new
17
+ end
18
+
19
+ def configure
20
+ yield(config)
21
+ end
22
+
23
+ def Build(opts = '')
24
+ Command.run("build #{opts}")
25
+ end
26
+
27
+ def Fix(opts = '')
28
+ Command.run("fix #{opts}")
29
+ end
30
+
31
+ def Inspect(opts = '')
32
+ Command.run("inspect #{opts}")
33
+ end
34
+
35
+ def Push(opts = '')
36
+ Command.run("push #{opts}")
37
+ end
38
+
39
+ def Validate(opts = '')
40
+ Command.run("validate #{opts}")
41
+ end
42
+
43
+ def Version(opts = '')
44
+ Command.run("version #{opts}")
45
+ end
46
+
47
+ class Configuration
48
+ attr_accessor :version
49
+ attr_accessor :download_path
50
+
51
+ def initialize
52
+ @version = '1.0.4'
53
+ @download_path = '/tmp'
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ module Packer
2
+ module Binary
3
+ module Command
4
+ module_function
5
+
6
+ def run(command)
7
+ system("#{binary} #{command}")
8
+ end
9
+
10
+ def binary
11
+ e = Executable.new
12
+ e.download
13
+ e.binary
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,61 @@
1
+ # This is a simple example which uses rubyzip to
2
+ # recursively generate a zip file from the contents of
3
+ # a specified directory. The directory itself is not
4
+ # included in the archive, rather just its contents.
5
+ #
6
+ # Usage:
7
+ # directoryToZip = "/tmp/input"
8
+ # output_file = "/tmp/out.zip"
9
+ # zf = Zip.new(directoryToZip, output_file)
10
+ # zf.write()
11
+ require 'zip'
12
+
13
+ module Packer
14
+ module Binary
15
+ class Compressor
16
+ class << self
17
+ include Packer::Binary::Helpers
18
+
19
+ # Zip the input directory.
20
+ def write(input_dir, output_file)
21
+ entries = Dir.entries(input_dir); entries.delete("."); entries.delete("..")
22
+ io = Zip::ZipFile.open(output_file, Zip::ZipFile::CREATE);
23
+
24
+ write_entries(entries, "", io, input_dir, output_file)
25
+ io.close();
26
+ end
27
+
28
+ def extract(input_file, output_dir)
29
+ Zip::File.open(input_file) do |zip_file|
30
+ # Handle entries one by one
31
+ zip_file.each do |entry|
32
+ # Extract to file/directory/symlink
33
+ dest_file = "#{output_dir}/#{entry.name}"
34
+ debug("Extracting to #{dest_file}")
35
+ entry.extract(dest_file)
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # A helper method to make the recursion work.
43
+ def write_entries(entries, path, io, input_dir, output_file)
44
+
45
+ entries.each { |e|
46
+ zip_file_path = path == "" ? e : File.join(path, e)
47
+ disk_file_path = File.join(input_dir, zip_file_path)
48
+
49
+ if File.directory?(disk_file_path)
50
+ io.mkdir(zip_file_path)
51
+ subdir =Dir.entries(disk_file_path); subdir.delete("."); subdir.delete("..")
52
+ write_entries(subdir, zip_file_path, io)
53
+ else
54
+ io.get_output_stream(zip_file_path) { |f| f.puts(File.open(disk_file_path, "rb").read())}
55
+ end
56
+ }
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,68 @@
1
+ module Packer
2
+ module Binary
3
+ class Executable
4
+ include Packer::Binary::Helpers
5
+
6
+ def initialize
7
+ @packer_version = Packer::Binary.config.version
8
+ @download_path = "#{Packer::Binary.config.download_path}/packer-binary/#{@packer_version}/bin"
9
+ @download_filename = "#{@packer_version}-packer.zip"
10
+ FileUtils.mkdir_p @download_path
11
+
12
+ raise 'Your OS is not supported' unless supported?
13
+ end
14
+
15
+ def binary
16
+ Dir["#{@download_path}/packer*"][0]
17
+ end
18
+
19
+ def download
20
+ return "#{binary} already exists" if binary_exist?
21
+
22
+ msg("Downloading https://#{download_domain}/#{download_uri}")
23
+
24
+ require 'open-uri'
25
+
26
+ File.open("#{@download_path}/#{@download_filename}", "wb") do |saved_file|
27
+ # the following "open" is provided by open-uri
28
+ open("https://#{download_domain}/#{download_uri}", "rb") do |read_file|
29
+ saved_file.write(read_file.read)
30
+ end
31
+ end
32
+
33
+ extract
34
+ make_exe
35
+ end
36
+
37
+ private
38
+
39
+ def binary_exist?
40
+ !binary.nil?
41
+ end
42
+
43
+ def supported?
44
+ OS.freebsd? || OS.mac? || OS.windows? || OS.linux?
45
+ end
46
+
47
+ def make_exe
48
+ FileUtils.chmod('a+x', binary) if binary_exist?
49
+ end
50
+
51
+ def extract
52
+ Compressor::extract("#{@download_path}/#{@download_filename}", @download_path)
53
+ end
54
+
55
+ def download_uri
56
+ "packer/#{@packer_version}/packer_#{@packer_version}_freebsd_amd64.zip" if OS.freebsd?
57
+ "packer/#{@packer_version}/packer_#{@packer_version}_darwin_amd64.zip" if OS.mac?
58
+ "packer/#{@packer_version}/packer_#{@packer_version}_windows_amd64.zip" if OS.windows?
59
+ "packer/#{@packer_version}/packer_#{@packer_version}_linux_amd64.zip" if OS.linux?
60
+ end
61
+
62
+ def download_domain
63
+ 'releases.hashicorp.com'
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,36 @@
1
+ module Packer
2
+ module Binary
3
+ module Helpers
4
+ module_function
5
+
6
+ #
7
+ # Runs given commands using mixlib-shellout
8
+ #
9
+ def system_command(*command_args)
10
+ cmd = Mixlib::ShellOut.new(*command_args)
11
+ cmd.run_command
12
+ cmd
13
+ end
14
+
15
+ def err(message)
16
+ stderr.print("#{message}\n")
17
+ end
18
+
19
+ def msg(message)
20
+ stdout.print("#{message}\n")
21
+ end
22
+
23
+ def debug(message)
24
+ stdout.print("#{message}\n") if ENV['DEBUG']
25
+ end
26
+
27
+ def stdout
28
+ $stdout
29
+ end
30
+
31
+ def stderr
32
+ $stderr
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module Packer
2
+ module Binary
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "packer/binary/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "packer-binary"
8
+ spec.version = Packer::Binary::VERSION
9
+ spec.authors = ["Nathan Cazell"]
10
+ spec.email = ["nathan.cazell@imageapi.com"]
11
+
12
+ spec.summary = %q{A gem to install and interface with HashiCorp's Packer utility}
13
+ spec.homepage = "https://github.com/nathantcz/packer-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.15"
23
+ spec.add_development_dependency 'pry'
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency 'yard'
27
+ spec.add_development_dependency 'coveralls'
28
+
29
+ spec.add_runtime_dependency 'rubyzip'
30
+ spec.add_runtime_dependency 'os'
31
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: packer-binary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Cazell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-30 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubyzip
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: os
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - nathan.cazell@imageapi.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/packer/binary.rb
142
+ - lib/packer/binary/command.rb
143
+ - lib/packer/binary/compressor.rb
144
+ - lib/packer/binary/executable.rb
145
+ - lib/packer/binary/helpers.rb
146
+ - lib/packer/binary/version.rb
147
+ - packer-binary.gemspec
148
+ homepage: https://github.com/nathantcz/packer-ruby
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.6.11
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: A gem to install and interface with HashiCorp's Packer utility
172
+ test_files: []