Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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 FileUtils.rm_r Dir.glob(globdir + '/generated/*')
44
45 File.umask(022)
46 Dir.glob(globdir + '/*.in') do |template_file|
47   generated_dir = File.join(File.dirname(template_file), 'generated')
48   Dir.mkdir(generated_dir) unless Dir.exists? generated_dir
49   output_path = File.join(generated_dir, File.basename(template_file, '.in'))
50   output_mode = (File.stat(template_file).mode & 0100) ? 0755 : 0644
51   File.open(output_path, "w", output_mode) do |output|
52     File.open(template_file) do |input|
53       input.each_line do |line|
54
55         # This count is used to short-circuit potential
56         # infinite loops of variable substitution.
57         @count = 0
58         while @count < 10
59           @out = line.gsub!(/@@(.*?)@@/) do |var|
60             if config.key?(Regexp.last_match[1])
61               config[Regexp.last_match[1]]
62             else
63               var.gsub!(/@@/, '@_NOT_FOUND_@')
64             end
65           end
66           break if @out.nil?
67           @count += 1
68         end
69
70         output.write(line)
71       end
72     end
73   end
74 end