yutani 0.1.18 → 0.1.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb2d4ad0c04c2786185ef8c2fc5dee90be6460a3
4
- data.tar.gz: f178131e875565618b1ce5d80bec675d2139e2cd
3
+ metadata.gz: 19392ae5e18da8ace5f0da1b75df60195b02344e
4
+ data.tar.gz: 58d4d3605b5364954891c45c7bd6b4db756e248d
5
5
  SHA512:
6
- metadata.gz: 680b9f75c7ac957e049625be0ed4008f4e4ee00806f3877deca9333770692d2d5db3efffade73d7cb945387fc7835facaff6a81704b985f6a15ca2c207aaa99e
7
- data.tar.gz: ba161b43a65b0f9269964e9f4dc093630762b69986223ac7f50433493c177958193290862f6aeac51c5dac07adb810b280b81616ffc922ae663a742fa92221e1
6
+ metadata.gz: 192cea6e367fbb41488479646ae933eb7931825566adb7ec9b654e4f40f0c045f6093f3068d597cd4ba4bd7b8949c0b691f5d0aaf2b56e4ef1dd2b4c287269a4
7
+ data.tar.gz: 8154a43329154a3466949852de0ba4d7bc20f6ad3fe79f7a248d34989ac5a889bd1789fe638299a5523fb9dc1f5b6802bc49b1c5b29983148cdc925dcce176ef
data/lib/yutani/cli.rb CHANGED
@@ -50,9 +50,14 @@ module Yutani
50
50
  Yutani.logger.info "Re-build triggered: #{a} added" unless a.empty?
51
51
  Yutani.logger.info "Re-build triggered: #{d} deleted" unless d.empty?
52
52
 
53
- build
54
-
55
- Yutani.logger.info "Re-build finished"
53
+ begin
54
+ build
55
+ rescue Exception => e
56
+ Yutani.logger.error "#{e.class.name} #{e.message}"
57
+ #Yutani.logger.error Yutani::Cli.format_backtrace(e.backtrace) unless e.backtrace.empty?
58
+ else
59
+ Yutani.logger.info "Re-build finished successfully"
60
+ end
56
61
  end.start
57
62
 
58
63
  # exit cleanly upon Ctrl-C
@@ -70,42 +75,26 @@ module Yutani
70
75
  puts Yutani::VERSION
71
76
  end
72
77
 
73
- desc 'target', 'Generate list of Terraform targets'
74
- def target(stack_dir, *args)
75
- files = Dir.glob(File.join(stack_dir, '*.tf.json'))
76
- if files.empty?
77
- raise "Could not find *.tf.json files in #{stack_dir}"
78
- end
79
-
80
- if args.empty?
81
- raise "No targets specified"
82
- end
83
-
84
- contents = files.inject({}) do |h, f|
85
- h.merge!(JSON.parse(File.read(f)))
86
- h
87
- end
88
-
89
- targets = contents['resource'].inject({}) do |h,(k,v)|
90
- h[k] = v.select do |k,v|
91
- (args - k.split('_')).empty?
78
+ %w(plan apply destroy).each do |tf_cmd|
79
+ desc tf_cmd, "Run terraform #{tf_cmd} with wildcard targets"
80
+ define_method tf_cmd do |*args|
81
+ # we only support json files
82
+ files = Dir.glob('*.tf.json')
83
+ if files.empty?
84
+ raise "Could not find any *.tf.json files"
92
85
  end
93
- h
94
- end.reject{|k,v| v.empty? }
95
86
 
96
- target_flags = targets.inject([]) do |flags, (k,v)|
97
- flags << v.keys.map do |resource_name|
98
- "-target " + ["resource", k, resource_name].join('.')
87
+ # merge contents of *.tf.json files into one hash
88
+ contents = files.inject({}) do |h, f|
89
+ h.merge!(JSON.parse(File.read(f)))
90
+ h
99
91
  end
100
- flags
101
- end.flatten
102
92
 
103
- puts target_flags.join(" ")
104
- end
93
+ new_args = []
94
+ expand_target_wildcard_args(new_args, args, contents)
105
95
 
106
- # Invoke Terraform CLI command
107
- def method_missing(name, *args, &block)
108
- %x/terraform #{name} #{args}/
96
+ Yutani::Utils.run_tf_command(tf_cmd, new_args)
97
+ end
109
98
  end
110
99
 
111
100
  desc 'init', 'Initialize with a basic setup'
@@ -147,6 +136,55 @@ module Yutani
147
136
 
148
137
  private
149
138
 
139
+ # horror show, in dire need of a succint algorithm
140
+ def expand_target_wildcard_args(new_args, args, contents)
141
+ flag = args.shift
142
+
143
+ if flag == '-target'
144
+ target_val = args.shift
145
+
146
+ if target_val.nil?
147
+ raise "-target arg missing corresponding target resource"
148
+ end
149
+
150
+ # if no wildcard found, then pass through unaltered
151
+ if target_val !~ /\*/
152
+ new_args << '-target'
153
+ new_args << target_val
154
+ expand_target_wildcard_args(new_args, args, contents)
155
+ end
156
+
157
+ target_regex = Regexp.new('^' + target_val.gsub(/\*/, '.*') + '$')
158
+
159
+ tf_targets = []
160
+ contents['resource'].each do |resource_type,v|
161
+ v.each do |resource_name,_|
162
+ if "#{resource_type}.#{resource_name}" =~ target_regex
163
+ tf_targets << "#{resource_type}.#{resource_name}"
164
+ end
165
+ end
166
+ end
167
+
168
+ if tf_targets.empty?
169
+ # we didn't find any matches, so pass through unaltered and
170
+ # let TF throw error
171
+ new_args << '-target'
172
+ new_args << target_val
173
+ else
174
+ tf_targets.each do |target|
175
+ new_args << '-target'
176
+ new_args << target
177
+ end
178
+ end
179
+ elsif flag.nil?
180
+ return
181
+ else
182
+ new_args.push flag
183
+ end
184
+
185
+ expand_target_wildcard_args(new_args, args, contents)
186
+ end
187
+
150
188
  def self.format_backtrace(bt)
151
189
  "Backtrace: #{bt.join("\n from ")}"
152
190
  end
data/lib/yutani/utils.rb CHANGED
@@ -7,17 +7,23 @@ class Array
7
7
  end
8
8
 
9
9
  module Yutani
10
- class IndifferentHash < Hash
10
+ class IndifferentHash < Hash
11
11
  include Hashie::Extensions::MergeInitializer
12
- include Hashie::Extensions::IndifferentAccess
13
- end
12
+ include Hashie::Extensions::IndifferentAccess
13
+ end
14
14
 
15
- class DeepMergeHash < Hash
16
- include Hashie::Extensions::DeepMerge
17
- end
15
+ class DeepMergeHash < Hash
16
+ include Hashie::Extensions::DeepMerge
17
+ end
18
18
 
19
19
  module Utils
20
20
  class << self
21
+ def run_tf_command(cmd, *args)
22
+ command = ENV.fetch('TERRAFORM', 'terraform') + ' ' + cmd + ' ' + args.join(' ')
23
+ Yutani.logger.info command
24
+ exec(command)
25
+ end
26
+
21
27
  def convert_symbols_to_strings_in_flat_hash(h)
22
28
  h.inject({}) do |h, (k,v)|
23
29
  k = k.is_a?(Symbol) ? k.to_s : k
@@ -1,3 +1,3 @@
1
1
  module Yutani
2
- VERSION = '0.1.18'
2
+ VERSION = '0.1.19'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yutani
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Louis Garman