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