tiller 0.1.5 → 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: 4c3db7fef71f4948a26c6b5eb125302bcff62a19
4
- data.tar.gz: e61b34146b245e680d55a6a6e9bb4c3462c51beb
3
+ metadata.gz: f0724940d4aabb1620ddc521faff0783fa9b5b0f
4
+ data.tar.gz: 379f0c788be67ed8c8176e964226b2ade39afcfe
5
5
  SHA512:
6
- metadata.gz: 61080e310e0b0d7454809269ceb2b9bdf5ef7e61c13e9851b2bf6a7aa343ce741d2d17acf9a92badf6ba81f25eb0b296667ea34004a90df0f8e7d9b5ad065676
7
- data.tar.gz: 383a7379a17f669031b98df9bb2d1804efdd8b4d7ead35168dba1705d30d03195fbb93e0b481a96d994c1240571f93a9987106a612d89e71143a036a45fe5bfc
6
+ metadata.gz: 75d56a1140dd02e6b5ae3ee56a88d639439cc92a1751d129291978ed0f2339ad5899d08cfa6cb39b2064ecbff4265ed6d0a36b988c7fa574306f393498f7dcab
7
+ data.tar.gz: f7836c90b6750fbdcfc0d4a50893c0ef74edba09ddc14c1efb7e024b14240b46888d22e5c0624ae51587e1e2fbec96cbe9382fcf46043f8568647dedcaa00f9f
data/bin/tiller CHANGED
@@ -12,6 +12,9 @@ require 'yaml'
12
12
  require 'fileutils'
13
13
  require 'optparse'
14
14
  require 'pp'
15
+ require 'json'
16
+ require 'socket'
17
+ require 'tiller/api'
15
18
 
16
19
  # This is needed so we can enumerate all the loaded plugins later
17
20
  class Class
@@ -41,6 +44,7 @@ module Tiller
41
44
  # Parse command-line arguments
42
45
  config[:no_exec] = false
43
46
  config[:verbose] = false
47
+ config[:api_enable] = false
44
48
 
45
49
  optparse = OptionParser.new do |opts|
46
50
  opts.on('-n', '--no-exec', 'Do not execute a replacement process') do
@@ -49,6 +53,12 @@ module Tiller
49
53
  opts.on('-v', '--verbose', 'Display verbose output') do
50
54
  config[:verbose] = true
51
55
  end
56
+ opts.on('-a', '--api', 'Enable HTTP API') do
57
+ config['api_enable'] = true
58
+ end
59
+ opts.on('-p', '--api-port [API_PORT]', 'HTTP API port') do |api_port|
60
+ config['api_port'] = api_port
61
+ end
52
62
  opts.on('-b', '--base-dir [BASE_DIR]', 'Override the tiller_base environment variable') do |base_dir|
53
63
  config[:tiller_base] = base_dir
54
64
  end
@@ -79,7 +89,7 @@ module Tiller
79
89
  require 'tiller/datasource.rb'
80
90
 
81
91
  # Load the common YAML configuration file
82
- config[:common_config] = YAML.load(open(File.join(config[:tiller_base], 'common.yaml')))
92
+ config.merge!(YAML.load(open(File.join(config[:tiller_base], 'common.yaml'))))
83
93
 
84
94
  puts "tiller v#{VERSION} (https://github.com/markround/tiller) <github@markround.com>"
85
95
 
@@ -92,8 +102,8 @@ module Tiller
92
102
  # Now load all our plugins
93
103
  template_sources = Array.new
94
104
  data_sources = Array.new
95
- template_sources |= config[:common_config]['template_sources']
96
- data_sources |= config[:common_config]['data_sources']
105
+ template_sources |= config['template_sources']
106
+ data_sources |= config['data_sources']
97
107
  template_sources.each { |t| require "tiller/template/#{t}.rb" }
98
108
  data_sources.each { |t| require "tiller/data/#{t}.rb" }
99
109
 
@@ -127,6 +137,7 @@ module Tiller
127
137
 
128
138
  # Now we go through each template we've identified, and get the
129
139
  # values for each one.
140
+ all_templates = Hash.new
130
141
  templates.each do |template, content|
131
142
  values = Hash.new
132
143
  target_values = Hash.new
@@ -170,6 +181,12 @@ module Tiller
170
181
  target.puts(parsed_template)
171
182
  target.close
172
183
 
184
+ # config is redundant in target_values, remove it for the final status hash.
185
+ all_templates[template]={
186
+ 'merged_values' => tiller,
187
+ 'target_values' => target_values.reject{|k,v| k=='config'}
188
+ }
189
+
173
190
  # Set permissions if we are running as root
174
191
  if Process::Sys.geteuid == 0
175
192
  puts "Setting ownership/permissions on #{target_values['target']}" if config[:verbose]
@@ -189,11 +206,25 @@ module Tiller
189
206
 
190
207
  puts 'Template generation completed'
191
208
 
192
- if config[:no_exec] == false && config[:common_config].key?('exec')
193
- # All templates created, so let's handover to the replacement process then
194
- # it's home in time for tea and medals.
195
- puts "Executing #{config[:common_config]['exec']}..."
196
- exec(config[:common_config]['exec'])
209
+ # Final status structure for API
210
+ tiller_api_hash={'config' => config, 'global_values' => global_values, 'templates' => all_templates}
211
+
212
+ if config['api_enable']
213
+ Thread.start{ tiller_api(tiller_api_hash) }
214
+ end
215
+
216
+ if config[:no_exec] == false && config.key?('exec')
217
+ # All templates created, so let's start the replacement process
218
+ puts "Executing #{config['exec']}..."
219
+
220
+ # Fork and wait so API can continue to run
221
+ fork do
222
+ system(config['exec'])
223
+ end
224
+
225
+ puts "Child process forked." if config[:verbose]
226
+ Process.wait
227
+ puts "Child process finished, Tiller is stopping." if config[:verbose]
197
228
  end
198
229
 
199
230
 
@@ -3,3 +3,6 @@ data_sources:
3
3
  - file
4
4
  template_sources:
5
5
  - file
6
+ exec: sleep 600
7
+
8
+
data/lib/tiller/api.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'json'
2
+ require 'socket'
3
+ require 'tiller/api/handlers/404'
4
+ require 'tiller/api/handlers/ping'
5
+ require 'tiller/api/handlers/config'
6
+ require 'tiller/api/handlers/globals'
7
+ require 'tiller/api/handlers/templates'
8
+ require 'tiller/api/handlers/template'
9
+
10
+
11
+ API_VERSION=1
12
+ API_PORT=6275
13
+
14
+ # The following is a VERY simple HTTP API, used for querying the status of Tiller
15
+ # after it has generated templates and forked a child process.
16
+
17
+ def tiller_api(tiller_api_hash)
18
+ api_port = tiller_api_hash['config']['api_port'] ?
19
+ tiller_api_hash['config']['api_port'] : API_PORT
20
+
21
+ puts "Tiller API starting on port #{api_port}" if tiller_api_hash['config'][:verbose]
22
+
23
+ server = TCPServer.new('127.0.0.1', api_port)
24
+
25
+ loop do
26
+ socket = server.accept
27
+ request = socket.gets
28
+ (method, uri, http_version) = request.split
29
+
30
+ if uri =~ /^\/v([0-9]+)\//
31
+ api_version = uri.split('/')[1]
32
+ end
33
+
34
+ # Defaults
35
+ response = handle_404
36
+
37
+ # Routing
38
+ case method
39
+ when 'GET'
40
+ case uri
41
+ when '/ping'
42
+ response = handle_ping
43
+ when /^\/v([0-9]+)\/config/
44
+ response = handle_config(api_version, tiller_api_hash)
45
+ when /^\/v([0-9]+)\/globals/
46
+ response = handle_globals(api_version, tiller_api_hash)
47
+ when /^\/v([0-9]+)\/templates/
48
+ response = handle_templates(api_version, tiller_api_hash)
49
+ when /^\/v([0-9]+)\/template\//
50
+ template = uri.split('/')[3]
51
+ response = handle_template(api_version, tiller_api_hash, template)
52
+ end
53
+ end
54
+
55
+ # Response
56
+ socket.print "HTTP/1.1 #{response[:status]}\r\n" +
57
+ "Content-Type: application/json\r\n" +
58
+ "Server: Tiller #{VERSION} / API v#{API_VERSION}\r\n"
59
+ "Content-Length: #{response[:content].bytesize}\r\n" +
60
+ "Connection: close\r\n"
61
+ socket.print "\r\n"
62
+ socket.print response[:content]
63
+ socket.close
64
+ end
65
+ end
66
+
67
+
68
+
69
+
@@ -0,0 +1,6 @@
1
+ def handle_404
2
+ {
3
+ :content => '{ "error" : "not implemented" }',
4
+ :status => '404 Not Found'
5
+ }
6
+ end
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+ require 'tiller/api/handlers/404'
3
+
4
+ def handle_config(api_version, tiller_api_hash)
5
+ case api_version
6
+ when 'v1'
7
+ {
8
+ :content => tiller_api_hash['config'].to_json,
9
+ :status => '200 OK'
10
+ }
11
+ else
12
+ handle_404
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+ require 'tiller/api/handlers/404'
3
+
4
+ def handle_globals(api_version, tiller_api_hash)
5
+ case api_version
6
+ when 'v1'
7
+ {
8
+ :content => tiller_api_hash['global_values'].to_json,
9
+ :status => '200 OK'
10
+ }
11
+ else
12
+ handle_404
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ def handle_ping
2
+ {
3
+ :content => "{ \"ping\": \"Tiller API v#{API_VERSION} OK\" }",
4
+ :status => '200 OK'
5
+ }
6
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+ require 'tiller/api/handlers/404'
3
+
4
+ def handle_template(api_version, tiller_api_hash, template)
5
+ case api_version
6
+ when 'v1'
7
+ if tiller_api_hash['templates'].has_key?(template)
8
+ {
9
+ :content => tiller_api_hash['templates'][template].to_json,
10
+ :status => '200 OK'
11
+ }
12
+ else
13
+ {
14
+ :content => '{ "error" : "template not found" }',
15
+ :status => '404 Not Found'
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+ require 'tiller/api/handlers/404'
3
+
4
+ def handle_templates(api_version, tiller_api_hash)
5
+ case api_version
6
+ when 'v1'
7
+ {
8
+ :content => tiller_api_hash['templates'].keys.to_json,
9
+ :status => '200 OK'
10
+ }
11
+ else
12
+ handle_404
13
+ end
14
+ end
@@ -18,6 +18,7 @@ class FileDataSource < Tiller::DataSource
18
18
  end
19
19
 
20
20
  def target_values(template_name)
21
+ # The config element is redundant (not a target value)
21
22
  @config_hash.key?(template_name) ? @config_hash[template_name] : Hash.new
22
23
  end
23
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Round
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-17 00:00:00.000000000 Z
11
+ date: 2014-10-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A tool to create configuration files in Docker containers from a variety
14
14
  of sources. See https://github.com/markround/tiller for examples and documentation.
@@ -31,6 +31,13 @@ files:
31
31
  - examples/plugins/lib/tiller/data/dummy.rb
32
32
  - examples/plugins/lib/tiller/data/network.rb
33
33
  - examples/plugins/lib/tiller/template/dummy.rb
34
+ - lib/tiller/api.rb
35
+ - lib/tiller/api/handlers/404.rb
36
+ - lib/tiller/api/handlers/config.rb
37
+ - lib/tiller/api/handlers/globals.rb
38
+ - lib/tiller/api/handlers/ping.rb
39
+ - lib/tiller/api/handlers/template.rb
40
+ - lib/tiller/api/handlers/templates.rb
34
41
  - lib/tiller/data/environment.rb
35
42
  - lib/tiller/data/environment_json.rb
36
43
  - lib/tiller/data/file.rb