hashipack 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +79 -0
- data/README.md +88 -0
- data/Rakefile +10 -0
- data/lib/hashipack/artifact.rb +18 -0
- data/lib/hashipack/client.rb +43 -0
- data/lib/hashipack/message.rb +63 -0
- data/lib/hashipack/version.rb +5 -0
- data/lib/hashipack.rb +7 -0
- data/sig/hashipack.rbs +4 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a6c25005946f4906333f98699391a281e3b183b862149261dc904e8edc4ca1ea
|
4
|
+
data.tar.gz: a65eba6ce8350861b28269fab10be83951f03c568339dc303123caa809198260
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d2cfe192adc690bcb79782dc9b6e1fb784cffaa3958017e578106fb30b31ebd6dbdeaa04123e74b4f3ad36bee5cb2b883f0ee5be23831138896212b71e5b7e04
|
7
|
+
data.tar.gz: 305121b087a7c79ff89e048c60451b7573608f84c2df6508ea12368a5022c075771b3b7e4b1dc8be638f6a00e78a41a282eba5a9cff27bf25872e854b2c0e2cb
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hashipack (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
base64 (0.1.1)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
json (2.6.3)
|
13
|
+
language_server-protocol (3.17.0.3)
|
14
|
+
lint_roller (1.1.0)
|
15
|
+
parallel (1.23.0)
|
16
|
+
parser (3.2.2.4)
|
17
|
+
ast (~> 2.4.1)
|
18
|
+
racc
|
19
|
+
racc (1.7.1)
|
20
|
+
rainbow (3.1.1)
|
21
|
+
rake (13.0.6)
|
22
|
+
regexp_parser (2.8.2)
|
23
|
+
rexml (3.2.6)
|
24
|
+
rspec (3.12.0)
|
25
|
+
rspec-core (~> 3.12.0)
|
26
|
+
rspec-expectations (~> 3.12.0)
|
27
|
+
rspec-mocks (~> 3.12.0)
|
28
|
+
rspec-core (3.12.2)
|
29
|
+
rspec-support (~> 3.12.0)
|
30
|
+
rspec-expectations (3.12.3)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.12.0)
|
33
|
+
rspec-mocks (3.12.6)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.12.0)
|
36
|
+
rspec-support (3.12.1)
|
37
|
+
rubocop (1.56.4)
|
38
|
+
base64 (~> 0.1.1)
|
39
|
+
json (~> 2.3)
|
40
|
+
language_server-protocol (>= 3.17.0)
|
41
|
+
parallel (~> 1.10)
|
42
|
+
parser (>= 3.2.2.3)
|
43
|
+
rainbow (>= 2.2.2, < 4.0)
|
44
|
+
regexp_parser (>= 1.8, < 3.0)
|
45
|
+
rexml (>= 3.2.5, < 4.0)
|
46
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
47
|
+
ruby-progressbar (~> 1.7)
|
48
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
49
|
+
rubocop-ast (1.29.0)
|
50
|
+
parser (>= 3.2.1.0)
|
51
|
+
rubocop-performance (1.19.1)
|
52
|
+
rubocop (>= 1.7.0, < 2.0)
|
53
|
+
rubocop-ast (>= 0.4.0)
|
54
|
+
ruby-progressbar (1.13.0)
|
55
|
+
standard (1.31.2)
|
56
|
+
language_server-protocol (~> 3.17.0.2)
|
57
|
+
lint_roller (~> 1.0)
|
58
|
+
rubocop (~> 1.56.4)
|
59
|
+
standard-custom (~> 1.0.0)
|
60
|
+
standard-performance (~> 1.2)
|
61
|
+
standard-custom (1.0.2)
|
62
|
+
lint_roller (~> 1.0)
|
63
|
+
rubocop (~> 1.50)
|
64
|
+
standard-performance (1.2.1)
|
65
|
+
lint_roller (~> 1.1)
|
66
|
+
rubocop-performance (~> 1.19.1)
|
67
|
+
unicode-display_width (2.5.0)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
x86_64-linux
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
hashipack!
|
74
|
+
rake (~> 13.0)
|
75
|
+
rspec (~> 3.0)
|
76
|
+
standard (~> 1.3)
|
77
|
+
|
78
|
+
BUNDLED WITH
|
79
|
+
2.4.10
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Hashipack
|
2
|
+
|
3
|
+
Hashipack is a Ruby client for Hashicorp's Packer.
|
4
|
+
|
5
|
+
It allows building images and provides live status updates about the progress, as well as the resulting artifacts.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
bundle add hashipack
|
13
|
+
```
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
gem install hashipack
|
19
|
+
```
|
20
|
+
|
21
|
+
And require it in your program:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'hashipack'
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Create a client:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
client = Hashipack::Client.new
|
33
|
+
```
|
34
|
+
|
35
|
+
Build an image by providing the template:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
template = File.read('myimage.pkr.hcl')
|
39
|
+
result = client.build(template)
|
40
|
+
```
|
41
|
+
|
42
|
+
Subscribe to log messages and print them as they arrive:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
print_message = -> (message) { puts message }
|
46
|
+
result = client.build(template, on_output: print_message)
|
47
|
+
|
48
|
+
# This would print something like
|
49
|
+
# ...
|
50
|
+
# ==> myvm.hcloud.example: Shutting down server...
|
51
|
+
# ==> myvm.hcloud.example: Creating snapshot ...
|
52
|
+
# ==> myvm.hcloud.example: This can take some time
|
53
|
+
# ...
|
54
|
+
```
|
55
|
+
|
56
|
+
In addition to the template, you can also provide a lambda that is called when the builder progresses. You
|
57
|
+
need to define an estimated build duration in seconds so hashipack can calculate the progress.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
print_message = -> (message) { puts message }
|
61
|
+
print_progress = -> (progress) { puts progress }
|
62
|
+
result = client.build(template, on_output: print_message, on_progress: print_progress, estimated_duration: 300)
|
63
|
+
```
|
64
|
+
|
65
|
+
The result contains an array of artifacts that were generated during the build:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
print result
|
69
|
+
# => [#<hashipack::Artifact @builder_id="hcloud.builder", @id="131872234", @string="A snapshot was created: 'packer-1697561712'">]
|
70
|
+
```
|
71
|
+
|
72
|
+
This usually includes the following fields:
|
73
|
+
|
74
|
+
| Field | Value | Example |
|
75
|
+
|---|---|---|
|
76
|
+
| builder_id | The plugin name | `"hcloud.builder"` |
|
77
|
+
| id | The platforms native artifact id | `"131872234"`, which is the ID of the image on Hetzner Cloud |
|
78
|
+
| string | The completion message by Packer | `"A snapshot was created: 'packer-1697561712'"` |
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
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.
|
83
|
+
|
84
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/code-fabrik/hashipack.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Hashipack
|
2
|
+
class Artifact
|
3
|
+
attr_reader :builder_id, :id, :string, :files_count
|
4
|
+
|
5
|
+
def append_info(key, value)
|
6
|
+
case key.to_s
|
7
|
+
when 'builder-id'
|
8
|
+
@builder_id = value
|
9
|
+
when 'id'
|
10
|
+
@id = value
|
11
|
+
when 'string'
|
12
|
+
@string = value
|
13
|
+
when 'files_count'
|
14
|
+
@files_count = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module Hashipack
|
4
|
+
class Client
|
5
|
+
def build(content, on_output: lambda{}, on_progress: lambda{}, estimated_duration: 300)
|
6
|
+
template = Tempfile.new(["template", ".pkr.hcl"])
|
7
|
+
template.write(content)
|
8
|
+
template.close
|
9
|
+
command = "packer -machine-readable build #{template.path}"
|
10
|
+
|
11
|
+
initial_timestamp = 99999999999
|
12
|
+
last_timestamp = 0
|
13
|
+
|
14
|
+
artifacts = []
|
15
|
+
|
16
|
+
IO.popen(command).each do |line|
|
17
|
+
|
18
|
+
message = Message.parse(line)
|
19
|
+
|
20
|
+
if message.type == :ui
|
21
|
+
initial_timestamp = message.timestamp if message.timestamp < initial_timestamp
|
22
|
+
|
23
|
+
if message.timestamp > last_timestamp
|
24
|
+
last_timestamp = message.timestamp
|
25
|
+
running_duration = message.timestamp - initial_timestamp
|
26
|
+
progress = running_duration / estimated_duration.to_f
|
27
|
+
on_progress.call(progress)
|
28
|
+
end
|
29
|
+
|
30
|
+
on_output.call(message.text)
|
31
|
+
|
32
|
+
elsif message.type == :artifact
|
33
|
+
|
34
|
+
artifacts[message.number] ||= Artifact.new
|
35
|
+
artifacts[message.number].append_info(message.key, message.value)
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
return artifacts
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Hashipack
|
2
|
+
class Message
|
3
|
+
def self.parse(line)
|
4
|
+
msg = Message.new(line)
|
5
|
+
if msg.ui?
|
6
|
+
return UiMessage.new(line)
|
7
|
+
elsif msg.artifact?
|
8
|
+
return ArtifactMessage.new(line)
|
9
|
+
else
|
10
|
+
puts "Ignoring unknown message type #{msg.type}"
|
11
|
+
return Message.new(line)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(line)
|
16
|
+
@parts = line.split(",")
|
17
|
+
end
|
18
|
+
|
19
|
+
def timestamp
|
20
|
+
@parts.first.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def type
|
24
|
+
@parts[2]
|
25
|
+
end
|
26
|
+
|
27
|
+
def ui?
|
28
|
+
type == 'ui'
|
29
|
+
end
|
30
|
+
|
31
|
+
def artifact?
|
32
|
+
type == 'artifact'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class UiMessage < Message
|
37
|
+
def type
|
38
|
+
:ui
|
39
|
+
end
|
40
|
+
|
41
|
+
def text
|
42
|
+
@parts[4]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class ArtifactMessage < Message
|
47
|
+
def type
|
48
|
+
:artifact
|
49
|
+
end
|
50
|
+
|
51
|
+
def number
|
52
|
+
@parts[3].to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
def key
|
56
|
+
@parts[4]
|
57
|
+
end
|
58
|
+
|
59
|
+
def value
|
60
|
+
@parts[5]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/hashipack.rb
ADDED
data/sig/hashipack.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashipack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lukas_Skywalker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: None.
|
14
|
+
email:
|
15
|
+
- LukasSkywalker@users.noreply.github.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".standard.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/hashipack.rb
|
28
|
+
- lib/hashipack/artifact.rb
|
29
|
+
- lib/hashipack/client.rb
|
30
|
+
- lib/hashipack/message.rb
|
31
|
+
- lib/hashipack/version.rb
|
32
|
+
- sig/hashipack.rbs
|
33
|
+
homepage: https://code-fabrik.ch
|
34
|
+
licenses: []
|
35
|
+
metadata:
|
36
|
+
allowed_push_host: https://rubygems.org
|
37
|
+
homepage_uri: https://code-fabrik.ch
|
38
|
+
source_code_uri: https://github.com/code-fabrik/hashipack-ruby
|
39
|
+
changelog_uri: https://github.com/code-fabrik/hashipack-ruby/blob/master/CHANGELOG.md
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.6.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.4.10
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Ruby client for Hashicorps Packer
|
59
|
+
test_files: []
|