cli/lib/rbld_engine.rb in rbld-1.0.0 vs cli/lib/rbld_engine.rb in rbld-1.0.1

- old
+ new

@@ -1,9 +1,10 @@ require 'docker' require 'etc' require 'thread' require 'forwardable' +require 'os' require_relative 'rbld_log' require_relative 'rbld_config' require_relative 'rbld_utils' require_relative 'rbld_print' require_relative 'rbld_registry' @@ -308,10 +309,11 @@ private def with_gzip_writer begin File.open(@filename, 'w') do |f| + f.binmode gz = Zlib::GzipWriter.new(f) begin yield gz ensure gz.close @@ -425,17 +427,57 @@ end private_class_method :new end + class RunSettings + def initialize + @group_name = get_group_name + @user_name = Etc.getlogin + @home = get_home + @pwd = get_pwd + @uid = get_uid + @gid = get_gid + end + + attr_reader :group_name, :home, :pwd, :user_name, :uid, :gid + + private + + def get_group_name + if group_info = Etc.getgrgid(Process.gid) + group_info[:name] + else + Etc.getlogin + end + end + + def get_home + OS.windows? ? "/home/#{Etc.getlogin}" : Dir.home + end + + def get_pwd + Dir.pwd.sub(Dir.home, home) + end + + def get_uid + OS.windows? ? 1000 : Process.uid + end + + def get_gid + OS.windows? ? 1000 : Process.gid + end + end + class API extend Forwardable def initialize(docker_api = Docker, cfg = Rebuild::Config.new) @docker_api, @cfg = docker_api, cfg tweak_excon + tweak_docker_url check_connectivity @cache = PresentEnvironments.new end def remove!(env_name) @@ -608,10 +650,16 @@ # increased Excon.defaults[:write_timeout] = 600 Excon.defaults[:read_timeout] = 600 end + def tweak_docker_url + # Default unix pipe does not work with Docker for Windows + # Use TCP connection instead + Docker.url = 'tcp://127.0.0.1:2375' if OS.windows? + end + def check_connectivity begin @docker_api.validate_version! rescue Docker::Error::VersionError => msg raise UnsupportedDockerService, msg @@ -628,22 +676,40 @@ system( cmdline ) @errno = $?.exitstatus rbld_log.info( "External command returned with code #{@errno}" ) end + def run_user_group_name + if group_info = Etc.getgrgid(Process.gid) + group_info[:name] + else + Etc.getlogin + end + end + + def trace_run_settings + if ENV['RBLD_BOOTSTRAP_TRACE'] && ENV['RBLD_BOOTSTRAP_TRACE'] == '1' + '-e REBUILD_TRACE=1' + else + '' + end + end + def run_settings(env, cmd, opts = {}) - %Q{ -i #{STDIN.tty? ? '-t' : ''} \ - -v #{Dir.home}:#{Dir.home} \ - -e REBUILD_USER_ID=#{Process.uid} \ - -e REBUILD_GROUP_ID=#{Process.gid} \ - -e REBUILD_USER_NAME=#{Etc.getlogin} \ - -e REBUILD_GROUP_NAME=#{Etc.getgrgid(Process.gid)[:name]} \ - -e REBUILD_USER_HOME=#{Dir.home} \ - -e REBUILD_PWD=#{Dir.pwd} \ - --security-opt label:disable \ - #{opts[:rerun] ? env.rerun_img.id : env.img.id} \ - "#{cmd.join(' ')}" \ - } + rs = RunSettings.new + %Q{ -i #{STDIN.tty? ? '-t' : ''} \ + -v #{Dir.home}:#{rs.home} \ + -e REBUILD_USER_ID=#{rs.uid} \ + -e REBUILD_GROUP_ID=#{rs.gid} \ + -e REBUILD_USER_NAME=#{rs.user_name} \ + -e REBUILD_GROUP_NAME=#{rs.group_name} \ + -e REBUILD_USER_HOME=#{rs.home} \ + -e REBUILD_PWD=#{rs.pwd} \ + --security-opt label:disable \ + #{trace_run_settings} \ + #{opts[:rerun] ? env.rerun_img.id : env.img.id} \ + "#{cmd.join(' ')}" \ + } end def run_env_disposable(env, cmd) env.execution_container.remove! if env.execution_container names = NameFactory.new(env)