Module: RakeCompilerDock

Defined in:
lib/rake_compiler_dock.rb,
lib/rake_compiler_dock/version.rb

Defined Under Namespace

Classes: DockerIsNotAvailable

Constant Summary collapse

VERSION =
"0.3.0"
@@docker_checked =
false

Class Method Summary collapse

Class Method Details

.check_dockerObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rake_compiler_dock.rb', line 103

def check_docker
  return if @@docker_checked

  version_text = `docker version` rescue SystemCallError
  if $?.exitstatus == 0 && version_text.to_s =~ /version/
    @@docker_checked = true
  else
    case RUBY_PLATFORM
    when /mingw|mswin/
      $stderr.puts "Docker is not available. Please download and install boot2docker:"
      $stderr.puts "   https://github.com/boot2docker/windows-installer/releases"
      $stderr.puts
      $stderr.puts "Then execute 'boot2docker start' and follow the instuctions"
    when /linux/
      $stderr.puts "Docker is not available."
      $stderr.puts
      $stderr.puts "Install on Ubuntu/Debian:"
      $stderr.puts "   sudo apt-get install docker.io"
      $stderr.puts
      $stderr.puts "Install on Fedora/Centos/RHEL"
      $stderr.puts "   sudo yum install docker"
      $stderr.puts "   sudo systemctl start docker"
      $stderr.puts
      $stderr.puts "Install on SuSE"
      $stderr.puts "   sudo zypper install docker"
      $stderr.puts "   sudo systemctl start docker"
    when /darwin/
      $stderr.puts "Docker is not available. Please download and install boot2docker:"
      $stderr.puts "   https://github.com/boot2docker/osx-installer/releases"
    else
      $stderr.puts "Docker is not available."
    end
    raise DockerIsNotAvailable, "Docker is not available"
  end
end

.exec(*args) ⇒ Object

Run the command cmd within a fresh rake-compiler-dock container. The command is run directly, without the shell.

If a block is given, upon command completion the block is called with an OK flag (true on a zero exit status) and a Process::Status object. Without a block a RuntimeError is raised when the command exits non-zero.

Option :verbose can be set to enable printing of the command line. If not set, the rake verbose_flag is used. Option :check_docker can be set to false to disable the docker check.

Examples:

RakeCompilerDock.exec 'bash', '-c', 'echo $RUBY_CC_VERSION'


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
85
86
87
88
89
90
91
92
93
# File 'lib/rake_compiler_dock.rb', line 50

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

  check_docker if options.fetch(:check_docker){ true }

  if RUBY_PLATFORM =~ /mingw|mswin/
    # Change Path from "C:\Path" to "/c/Path" as used by boot2docker
    pwd = Dir.pwd.gsub(/^([a-z]):/i){ "/#{$1.downcase}" }
    uid = 1000
    gid = 1000
  else
    pwd = Dir.pwd
    uid = Process.uid
    gid = Process.gid
  end
  user = `id -nu`.chomp
  group = `id -ng`.chomp

  cmd = ["docker", "run", "--rm", "-i", "-t",
        "-v", "#{pwd}:#{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']}",
        "-w", pwd,
        image_name,
        "sigfw", "runas", *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



33
34
35
# File 'lib/rake_compiler_dock.rb', line 33

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

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

Run the command cmd within a fresh rake-compiler-dock container and within a shell.

If a block is given, upon command completion the block is called with an OK flag (true on a zero exit status) and a Process::Status object. Without a block a RuntimeError is raised when the command exits non-zero.

Option :verbose can be set to enable printing of the command line. If not set, the rake verbose_flag is used.

Examples:

RakeCompilerDock.sh 'bundle && rake cross native gem'

# check exit status after command runs
sh %{bundle && rake cross native gem}, verbose: true do |ok, res|
  if ! ok
    puts "windows cross build failed (status = #{res.exitstatus})"
  end
end


26
27
28
29
30
31
# File 'lib/rake_compiler_dock.rb', line 26

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



95
96
97
98
99
# File 'lib/rake_compiler_dock.rb', line 95

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