Class: Sle2Docker::Builder
- Inherits:
-
Object
- Object
- Sle2Docker::Builder
- Defined in:
- lib/sle2docker/builder.rb
Instance Method Summary collapse
-
#create(template_dir) ⇒ String
Creates the actual Docker image using kiwi.
-
#find_template_file(template_dir) ⇒ String
Looks for either config.xml or config.xml.erb inside of the template directory.
-
#initialize(options) ⇒ Builder
constructor
A new instance of Builder.
-
#render_template(template_file) ⇒ String
Performs the rendering of config.xml.erb.
Constructor Details
#initialize(options) ⇒ Builder
Returns a new instance of Builder.
4 5 6 |
# File 'lib/sle2docker/builder.rb', line 4 def initialize() = end |
Instance Method Details
#create(template_dir) ⇒ String
Creates the actual Docker image using kiwi
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sle2docker/builder.rb', line 12 def create(template_dir) tmp_dir = Dir.mktmpdir("sle2docker") tmp_template_dir = File.join(tmp_dir, "template") result_dir = File.join(tmp_dir, "result") FileUtils.cp_r(File.join(template_dir, "."), tmp_template_dir) FileUtils.mkdir_p(result_dir) template_file = find_template_file(tmp_template_dir) if template_file.end_with?('.erb') template = render_template(template_file) File.open(File.join(tmp_template_dir, "config.xml"), "w") do |file| file.write(template) end end docker_cmd = "docker run " # ensure kiwi cache is persistent docker_cmd += "-v /var/cache/kiwi:/var/cache/kiwi " # share build dir docker_cmd += "-v #{tmp_dir}:/#{tmp_dir} " # required because kiwi needs to bind mount /proc while creating the image docker_cmd += "--privileged " # the image to use docker_cmd += "opensuse/kiwi " # kiwi directives docker_cmd += "--build #{tmp_template_dir} --type docker -d #{result_dir}" begin puts "Starting build process inside of Docker container" if !system(docker_cmd) $stderr.printf("Something wrong happened during the build process\n") exit(1) end end Dir[File.join(result_dir, "*.tbz")].first end |
#find_template_file(template_dir) ⇒ String
Looks for either config.xml or config.xml.erb inside of the template directory.
Exits with an error if no file is found.
56 57 58 59 60 61 62 63 64 |
# File 'lib/sle2docker/builder.rb', line 56 def find_template_file(template_dir) template_file = File.join(template_dir, 'config.xml.erb') if !File.exist?(template_file) raise ConfigNotFoundError.new("Cannot find config.xml.erb file inside of #{template_dir}") end template_file end |
#render_template(template_file) ⇒ String
Performs the rendering of config.xml.erb
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sle2docker/builder.rb', line 70 def render_template(template_file) host = if [:smt_host] [:smt_host] else "nu.novell.com" end username = [:username] if !username && (![:password].empty? || ![:smt_host]) puts "Enter #{@options[:smt_host] ? '' : 'NCC '}username:" username = $stdin.gets.chomp end password = [:password] if (username || ![:smt_host]) && password.empty? puts "Enter #{@options[:smt_host] ? '' : 'NCC '}password:" password = $stdin.noecho(&:gets).chomp end credentials = "" if username || !password.empty? credentials = "username='#{username}' password='#{password}'" end use_ncc = ![:smt_host] enable_https = ![:disable_https] ERB.new(File.read(template_file)).result(binding) end |