77c721c61f9ea1cca45302e58bad9c33f9e465de
[arvados.git] / docker / build_tools / config.rb
1 #! /usr/bin/env ruby
2
3 require 'yaml'
4 require 'fileutils'
5 require 'digest'
6
7 abort 'Error: Ruby >= 1.9.3 required.' if RUBY_VERSION < '1.9.3'
8
9 # Initialize config settings from config.yml
10 config = YAML.load_file('config.yml')
11
12 # ============================================================
13 # Add dynamically chosen config settings. These settings should
14 # be suitable for any installation.
15
16 # Any _PW/_SECRET config settings represent passwords/secrets. If they
17 # are blank, choose a password. Make sure the generated password
18 # doesn't change if config.yml doesn't change. Otherwise, keys won't
19 # match any more if (say) keep's files get regenerated but apiserver's
20 # don't.
21 config.sort.map do |var,val|
22   if (var.end_with?('_PW') || var.end_with?('_SECRET')) && (config[var].nil? || config[var].empty?)
23     config[var] = Digest::SHA1.hexdigest(`hostname` + var + config.to_yaml)
24   end
25 end
26
27 # ============================================================
28 # For each *.in file in the docker directories, substitute any
29 # @@variables@@ found in the file with the appropriate config
30 # variable. Support up to 10 levels of nesting.
31 #
32 # TODO(twp): add the *.in files directory to the source tree, and
33 # when expanding them, add them to the "generated" directory with
34 # the same tree structure as in the original source. Then all
35 # the files can be added to the docker container with a single ADD.
36
37 if ARGV[0] and ARGV[0].length > 0
38   globdir = ARGV[0]
39 else
40   globdir = '*'
41 end
42
43 Dir.glob(globdir + '/generated/*') do |stale_file|
44   File.delete(stale_file)
45 end
46
47 File.umask(022)
48 Dir.glob(globdir + '/*.in') do |template_file|
49   generated_dir = File.join(File.dirname(template_file), 'generated')
50   Dir.mkdir(generated_dir) unless Dir.exists? generated_dir
51   output_path = File.join(generated_dir, File.basename(template_file, '.in'))
52   File.open(output_path, "w") do |output|
53     File.open(template_file) do |input|
54       input.each_line do |line|
55
56         # This count is used to short-circuit potential
57         # infinite loops of variable substitution.
58         @count = 0
59         while @count < 10
60           @out = line.gsub!(/@@(.*?)@@/) do |var|
61             if config.key?(Regexp.last_match[1])
62               config[Regexp.last_match[1]]
63             else
64               var.gsub!(/@@/, '@_NOT_FOUND_@')
65             end
66           end
67           break if @out.nil?
68           @count += 1
69         end
70
71         output.write(line)
72       end
73     end
74   end
75 end
76
77 # Copy the ssh public key file to base/generated (if a path is given)
78 generated_dir = File.join('base/generated')
79 Dir.mkdir(generated_dir) unless Dir.exists? generated_dir
80 if (!config['PUBLIC_KEY_PATH'].nil? and
81     File.readable? config['PUBLIC_KEY_PATH'])
82   FileUtils.cp(config['PUBLIC_KEY_PATH'],
83                File.join(generated_dir, 'id_rsa.pub'))
84 end