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