3551: Remove chmod 755, no longer necessary now that config.rb does that.
[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   output_mode = (File.stat(template_file).mode & 0100) ? 0755 : 0644
53   File.open(output_path, "w", output_mode) do |output|
54     # Set the mode again after opening, to thwart any prevailing umask:
55     output.chmod output_mode
56     File.open(template_file) do |input|
57       input.each_line do |line|
58
59         # This count is used to short-circuit potential
60         # infinite loops of variable substitution.
61         @count = 0
62         while @count < 10
63           @out = line.gsub!(/@@(.*?)@@/) do |var|
64             if config.key?(Regexp.last_match[1])
65               config[Regexp.last_match[1]]
66             else
67               var.gsub!(/@@/, '@_NOT_FOUND_@')
68             end
69           end
70           break if @out.nil?
71           @count += 1
72         end
73
74         output.write(line)
75       end
76     end
77   end
78 end
79
80 # Copy the ssh public key file to base/generated (if a path is given)
81 generated_dir = File.join('base/generated')
82 Dir.mkdir(generated_dir) unless Dir.exists? generated_dir
83 if (!config['PUBLIC_KEY_PATH'].nil? and
84     File.readable? config['PUBLIC_KEY_PATH'])
85   FileUtils.cp(config['PUBLIC_KEY_PATH'],
86                File.join(generated_dir, 'id_rsa.pub'))
87 end