Class: RakeCompilerDock::Starter

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_compiler_dock/starter.rb

Constant Summary collapse

@@docker_checked =
nil

Class Method Summary collapse

Class Method Details

.check_dockerObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rake_compiler_dock/starter.rb', line 117

def check_docker
  return if @@docker_checked

  check = DockerCheck.new($stderr)
  unless check.ok?
    at_exit do
      check.print_help_text
    end
    raise DockerIsNotAvailable, "Docker is not available"
  end

  @@docker_checked = check
end

.exec(*args) ⇒ Object



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rake_compiler_dock/starter.rb', line 21

def exec(*args)
  options = (Hash === args.last) ? args.pop : {}
  runargs = args.dup

  check_docker if options.fetch(:check_docker){ true }
  runargs.unshift("sigfw") if options.fetch(:sigfw){ true }
  runargs.unshift("runas") if options.fetch(:runas){ true }
  docker_opts = options.fetch(:options) do
    opts = ["--rm", "-i"]
    opts << "-t" if $stdin.tty?
    opts
  end

  case RUBY_PLATFORM
  when /mingw|mswin/
    # Change Path from "C:\Path" to "/c/Path" as used by boot2docker
    pwd = Dir.pwd.gsub(/^([a-z]):/i){ "/#{$1.downcase}" }
    # Virtualbox shared folders don't care about file permissions, so we use generic ids.
    uid = 1000
    gid = 1000
  when /darwin/
    pwd = Dir.pwd
    uid = 1000
    gid = 1000
  else
    pwd = Dir.pwd
    # Docker mounted volumes also share file uid/gid and permissions with the host.
    # Therefore we use the same attributes inside and outside the container.
    uid = Process.uid
    gid = Process.gid
  end
  user = make_valid_user_name(`id -nu`.chomp)
  group = make_valid_group_name(`id -ng`.chomp)

  drun_args = docker_opts + [image_name] + runargs

  cmd = ["docker", "run",
      "-v", "#{pwd}:#{make_valid_path(pwd)}",
      "-e", "UID=#{uid}",
      "-e", "GID=#{gid}",
      "-e", "USER=#{user}",
      "-e", "GROUP=#{group}",
      "-e", "ftp_proxy=#{ENV['ftp_proxy']}",
      "-e", "http_proxy=#{ENV['http_proxy']}",
      "-e", "https_proxy=#{ENV['https_proxy']}",
      "-e", "RCD_HOST_RUBY_PLATFORM=#{RUBY_PLATFORM}",
      "-e", "RCD_HOST_RUBY_VERSION=#{RUBY_VERSION}",
      "-e", "RCD_IMAGE=#{image_name}",
      "-w", make_valid_path(pwd),
      *drun_args]

  cmdline = Shellwords.join(cmd)
  if verbose_flag(options) == true
    $stderr.puts cmdline
  end

  ok = system(*cmd)
  if block_given?
    yield(ok, $?)
  elsif !ok
    fail "Command failed with status (#{$?.exitstatus}): " +
    "[#{cmdline}]"
  end
end

.image_nameObject



17
18
19
# File 'lib/rake_compiler_dock/starter.rb', line 17

def image_name
  ENV['RCD_IMAGE'] || ENV['RAKE_COMPILER_DOCK_IMAGE'] || "larskanis/rake-compiler-dock:#{IMAGE_VERSION}"
end

.make_valid_group_name(name) ⇒ Object



105
106
107
108
# File 'lib/rake_compiler_dock/starter.rb', line 105

def make_valid_group_name(name)
  name = make_valid_name(name)
  PredefinedGroups.include?(name) ? make_valid_name("_#{name}") : name
end

.make_valid_name(name) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/rake_compiler_dock/starter.rb', line 92

def make_valid_name(name)
  name = name.downcase
  # Convert disallowed characters
  name = name[0..0].gsub(/[^a-z_]/, "_") + name[1..-2].gsub(/[^a-z0-9_-]/, "_") + name[-1..-1].gsub(/[^a-z0-9_$-]/, "_")
  # Limit to 32 characters
  name.sub( /^(.{16}).{2,}(.{15})$/ ){ $1+"-"+$2 }
end

.make_valid_path(name) ⇒ Object



110
111
112
113
# File 'lib/rake_compiler_dock/starter.rb', line 110

def make_valid_path(name)
  # Convert problematic characters
  name = name.gsub(/[ ]/i, "_")
end

.make_valid_user_name(name) ⇒ Object



100
101
102
103
# File 'lib/rake_compiler_dock/starter.rb', line 100

def make_valid_user_name(name)
  name = make_valid_name(name)
  PredefinedUsers.include?(name) ? make_valid_name("_#{name}") : name
end

.sh(cmd, options = {}, &block) ⇒ Object



10
11
12
13
14
15
# File 'lib/rake_compiler_dock/starter.rb', line 10

def sh(cmd, options={}, &block)
  if verbose_flag(options)
    $stderr.puts "rake-compiler-dock bash -c #{ cmd.inspect }"
  end
  exec('bash', '-c', cmd, options, &block)
end

.verbose_flag(options) ⇒ Object



86
87
88
89
90
# File 'lib/rake_compiler_dock/starter.rb', line 86

def verbose_flag(options)
  options.fetch(:verbose) do
    Object.const_defined?(:Rake) && Rake.const_defined?(:FileUtilsExt) ? Rake::FileUtilsExt.verbose_flag : false
  end
end