Fixed Makefile dependencies, added docker_build script to work around
[arvados.git] / docker / config.rb
1 #! /usr/bin/env ruby
2
3 require 'yaml'
4
5 # Initialize config settings from config.yml
6 config = YAML.load_file('config.yml')
7
8 # ============================================================
9 # Add dynamically chosen config settings. These settings should
10 # be suitable for any installation.
11
12 # Any _PW/_SECRET config settings represent passwords/secrets. If they
13 # are blank, choose a password randomly.
14 config.each_key do |var|
15   if (var.end_with?('_PW') || var.end_with?('_SECRET')) && (config[var].nil? || config[var].empty?)
16     config[var] = rand(2**256).to_s(36)
17   end
18 end
19
20 # ============================================================
21 # For each *.in file in the docker directories, substitute any
22 # @@variables@@ found in the file with the appropriate config
23 # variable. Support up to 10 levels of nesting.
24
25 # TODO(twp): add the *.in files directory to the source tree, and
26 # when expanding them, add them to the "generated" directory with
27 # the same tree structure as in the original source. Then all
28 # the files can be added to the docker container with a single ADD.
29
30 Dir.glob('*/generated/*') do |stale_file|
31   File.delete(stale_file)
32 end
33
34 Dir.glob('*/*.in') do |template_file|
35   generated_dir = File.join(File.dirname(template_file), 'generated')
36   Dir.mkdir(generated_dir) unless Dir.exists? generated_dir
37   output_path = File.join(generated_dir, File.basename(template_file, '.in'))
38   output = File.open(output_path, "w")
39   File.open(template_file) do |input|
40     input.each_line do |line|
41
42       @count = 0
43       while @count < 10
44         @out = line.gsub!(/@@(.*?)@@/) do |var|
45           if config.key?(Regexp.last_match[1])
46             config[Regexp.last_match[1]]
47           else
48             var.gsub!(/@@/, '@_NOT_FOUND_@')
49           end
50         end
51         break if @out.nil?
52         @count += 1
53       end
54
55       output.write(line)
56     end
57   end
58   output.close
59 end