singel 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8523164bf81e274258f9fd9df632e69e0073def
4
- data.tar.gz: 591fe73ad0d036a8d0f015f47a142494f7a3a837
3
+ metadata.gz: 1ee381a02993b614f5755e85d1f245423a4b0433
4
+ data.tar.gz: c42b7f3206eef7232508b63031fdbc39feed355f
5
5
  SHA512:
6
- metadata.gz: 7e48ba26e94473f9d535a24d0866314c7e7e162e77ae2e0dc761b513631b2e6acf2ad874426b44008ab6102b45d9ca824ef14e08a0df02b6cd1d8b651f5ca706
7
- data.tar.gz: e34077f5eb6321a36ccf4a8da79f3f32aa461e9cc909b51cdf5a829f5781858e78b9b326be823fb0891f99cf994ca49f7425cbfe998ee765a49d91fa0cfb60dd
6
+ metadata.gz: d0cbeb4027d312aa825eabfd05e1be3d0728b2f65ff2b4cf4c1b7b58bc49ad7c0e3902b77b45a0647a0e7da970152c640a1ef332351878c24545b47e860e9226
7
+ data.tar.gz: 0e54f2c167cb5b7452128995a1ed31254eddd99823d92edd993e1ca4c54363dbe0e2e9794b318c63ceeeb552be328a700f8e31fb4f008cf4c83a51f7be1aaa3d
data/bin/singel CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env ruby
1
2
  # encoding: UTF-8
2
3
  #
3
4
  # Author:: Tim Smith (<tim@cozy.co>)
@@ -16,8 +17,7 @@
16
17
  # See the License for the specific language governing permissions and
17
18
  # limitations under the License.
18
19
 
19
- lib = File.expand_path('../../lib', __FILE__)
20
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
20
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21
21
 
22
22
  begin
23
23
  require 'optparse'
@@ -27,6 +27,7 @@ begin
27
27
  require 'orchestrator.rb'
28
28
  require 'executor.rb'
29
29
  require 'uploader.rb'
30
+ require 'template.rb'
30
31
  rescue LoadError => e
31
32
  raise "Missing gem or lib #{e}"
32
33
  end
@@ -16,52 +16,38 @@
16
16
  # See the License for the specific language governing permissions and
17
17
  # limitations under the License.
18
18
 
19
- # executes packer commands against template files
20
- class SingelExecutor
21
- def initialize(template)
22
- @file_path = template
23
- @packer_dir = File.dirname(template)
24
- @builders = {}
25
- end
26
-
27
- # shell out to packer to validate the config files for correctness
28
- def validates?
29
- Dir.chdir(@packer_dir)
30
- `packer validate #{@file_path} 2>&1`
31
- $CHILD_STATUS.success? ? true : false
32
- end
33
-
34
- # inspect the json file to determine the name and type of the builders
35
- def parse_builders
36
- template_file = File.read(@file_path)
37
- template_json = JSON.parse(template_file)
38
- template_json['builders'].each do |builder|
39
- @builders[builder['name']] = builder['type']
40
- end
41
- rescue Errno::ENOENT
42
- puts "- Could not find the passed template file #{@file_path}".indent.to_red
43
- exit
19
+ # executes packer commands against template object
20
+ class PackerExecutor
21
+ def initialize(template, builders)
22
+ @template = template
23
+ @file_path = template.path
24
+ @template_builders = template.builders
25
+ @specified_builders = builders
44
26
  end
45
27
 
46
28
  # print out the builders for this template
47
29
  def list
48
30
  puts File.basename(@file_path, '.json') + ':'
49
- parse_builders
50
- if @builders.empty?
31
+
32
+ builders = @template.builders_hash
33
+ if builders.empty?
51
34
  puts '- No builders found'.indent.to_red
52
35
  else
53
- @builders.each_pair do |name, type|
36
+ builders.each_pair do |name, type|
54
37
  puts "- #{name} (type: #{type})".indent
55
38
  end
56
39
  end
57
40
  end
58
41
 
42
+ # specify the builders if any were passed
43
+ def build_options
44
+ "-only=#{@specified_builders.join(',')}" unless @specified_builders.empty?
45
+ end
46
+
59
47
  def build
60
48
  puts "Building #{File.basename(@file_path, '.json')}:".to_green
61
- IO.popen("packer build #{@file_path}") do |cmd|
49
+ IO.popen("packer build #{build_options} #{@file_path}") do |cmd|
62
50
  cmd.each { |line| puts line }
63
51
  end
64
-
65
- SingelUploader.new(@file_path).push
66
52
  end
67
53
  end
@@ -38,7 +38,11 @@ class SingelOrchestrator
38
38
 
39
39
  # check to make sure the packer dir exists
40
40
  def check_dirs
41
- (puts "#{@options[:packer_dir]} not present. Cannot continue".to_red && exit) unless Dir.exist?(@options[:packer_dir])
41
+ unless Dir.exist?(@options[:packer_dir])
42
+ puts "#{@options[:packer_dir]} not present.".to_red
43
+ puts "See help for information on specifying an alternate dir.\n".to_red
44
+ exit!
45
+ end
42
46
  end
43
47
 
44
48
  # make a test connection using the AWS keys to determine if they're valid
@@ -47,7 +51,7 @@ class SingelOrchestrator
47
51
  ec2.describe_instance_status
48
52
  rescue
49
53
  puts 'Could not connect to EC2. Check your local AWS credentials before continuing.'.to_red
50
- exit
54
+ exit!
51
55
  end
52
56
 
53
57
  # check to make sure the prereq binaries present
@@ -56,39 +60,42 @@ class SingelOrchestrator
56
60
  `which #{binary} 2>&1`
57
61
  unless $CHILD_STATUS.success?
58
62
  puts "Could not find #{name} binary #{binary}. You must install #{name} before continuing.".to_red unless $CHILD_STATUS
59
- exit
63
+ exit!
60
64
  end
61
65
  end
62
66
  end
63
67
 
64
68
  # find the available packer templates on the host
65
69
  def find_templates
70
+ # find packer templates on disk since none were passed via args
66
71
  if @options[:templates].empty?
67
72
  templates = []
68
73
  Dir.foreach(@options[:packer_dir]) do |item|
69
74
  templates << File.join(@options[:packer_dir], item) if File.extname(item).downcase == '.json'
70
75
  end
71
76
 
77
+ # throw and error and exist if we still dont find any templates
72
78
  if templates.empty?
73
79
  puts "No packer templates found in the 'packer' dir. Cannot continue.".to_red
74
- exit
80
+ exit!
75
81
  end
76
82
  templates
77
- else
83
+ else # parse the arg provided template list
78
84
  @options[:templates].map { |x| File.join(@options[:packer_dir], x) }
79
85
  end
80
86
  end
81
87
 
82
88
  # run the passed command per packer template
83
89
  def execute_command(cmd)
84
- @templates.each do |template|
85
- packer = SingelExecutor.new(template)
86
- puts "Packer template validation for #{template} failed.\n".to_red unless packer.validates?
90
+ @templates.each do |t|
91
+ template = PackerTemplate.new(t)
92
+ executor = PackerExecutor.new(template, @options[:builders])
93
+ puts "Packer template validation for #{template.path} failed.\n".to_red unless template.validates?
87
94
  begin
88
- packer.send(cmd)
95
+ executor.send(cmd)
89
96
  rescue NoMethodError
90
97
  puts "Action \"#{cmd}\" not found. Cannot continue".to_red
91
- exit
98
+ exit!
92
99
  end
93
100
  end
94
101
  end
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'json'
20
+
21
+ # instance of a single packer template with methods to validate and extract data
22
+ class PackerTemplate
23
+ attr_accessor :path, :name
24
+
25
+ def initialize(path)
26
+ @path = File.expand_path(path)
27
+ @name = File.basename(@path, '.json')
28
+ @file = File.read(path)
29
+ end
30
+
31
+ def json
32
+ @parsed || @parsed = JSON.parse(@file)
33
+ end
34
+
35
+ def builders
36
+ json['builders'].map { |b| b['name'] }
37
+ end
38
+
39
+ def builders_hash
40
+ builders = {}
41
+ json['builders'].each do |builder|
42
+ builders[builder['name']] = builder['type']
43
+ end
44
+ builders
45
+ end
46
+
47
+ # shell out to packer to validate the config files for correctness
48
+ def validates?
49
+ Dir.chdir(File.dirname(@path))
50
+ `packer validate #{@path} 2>&1`
51
+ $CHILD_STATUS.success? ? true : false
52
+ end
53
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'singel'
3
- s.version = '0.1.1'
3
+ s.version = '0.2.0'
4
4
  s.date = Date.today.to_s
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.extra_rdoc_files = ['README.md', 'LICENSE']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: singel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-01 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -70,6 +70,7 @@ files:
70
70
  - bin/singel
71
71
  - lib/executor.rb
72
72
  - lib/orchestrator.rb
73
+ - lib/template.rb
73
74
  - lib/uploader.rb
74
75
  - singel.gemspec
75
76
  homepage: http://www.github.com/tas50/singel