Class: RakeCompilerDock::DockerCheck

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/rake_compiler_dock/docker_check.rb

Constant Summary collapse

COMMANDS =
%w[docker podman]

Constants included from Colors

Colors::ColorMap, Colors::ESC, Colors::NND

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colors

#colored, #disable_colors, #enable_colors

Constructor Details

#initialize(io, pwd, machine_name = "rake-compiler-dock") ⇒ DockerCheck

Returns a new instance of DockerCheck.



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
48
49
50
51
52
53
54
55
# File 'lib/rake_compiler_dock/docker_check.rb', line 14

def initialize(io, pwd, machine_name="rake-compiler-dock")
  @io = io
  @pwd = pwd
  @machine_name = machine_name

  if !io.tty? || (RUBY_PLATFORM=~/mingw|mswin/ && RUBY_VERSION[/^\d+/] < '2')
    disable_colors
  else
    enable_colors
  end

  docker_version

  unless ok?
    doma_version
    if doma_avail?
      io.puts
      io.puts yellow("docker-machine is available, but not ready to use. Trying to start.")

      doma_create
      if doma_create_ok?
        doma_start

        docker_version
      end
    else
      b2d_version

      if b2d_avail?
        io.puts
        io.puts yellow("boot2docker is available, but not ready to use. Trying to start.")

        b2d_init
        if b2d_init_ok?
          b2d_start

          docker_version
        end
      end
    end
  end
end

Instance Attribute Details

#docker_commandObject (readonly)

Returns the value of attribute docker_command.



11
12
13
# File 'lib/rake_compiler_dock/docker_check.rb', line 11

def docker_command
  @docker_command
end

#ioObject (readonly)

Returns the value of attribute io.



9
10
11
# File 'lib/rake_compiler_dock/docker_check.rb', line 9

def io
  @io
end

#machine_nameObject

Returns the value of attribute machine_name.



12
13
14
# File 'lib/rake_compiler_dock/docker_check.rb', line 12

def machine_name
  @machine_name
end

#pwdObject (readonly)

Returns the value of attribute pwd.



10
11
12
# File 'lib/rake_compiler_dock/docker_check.rb', line 10

def pwd
  @pwd
end

Instance Method Details

#add_env_options(options, names) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rake_compiler_dock/docker_check.rb', line 82

def add_env_options(options, names)
  names.each do |name|
    if (v=ENV[name]) && !v.empty?
      options << ["--engine-env", "#{name}=#{ENV[name]}"]
    end
  end
  options
end

#b2d_avail?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rake_compiler_dock/docker_check.rb', line 130

def b2d_avail?
  @b2d_version_status == 0 && @b2d_version_text =~ /version/
end

#b2d_initObject



134
135
136
137
# File 'lib/rake_compiler_dock/docker_check.rb', line 134

def b2d_init
  system("boot2docker init") rescue SystemCallError
  @b2d_init_status = $?.exitstatus
end

#b2d_init_ok?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/rake_compiler_dock/docker_check.rb', line 139

def b2d_init_ok?
  @b2d_init_status == 0
end

#b2d_startObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rake_compiler_dock/docker_check.rb', line 143

def b2d_start
  @b2d_start_text = `boot2docker start` rescue SystemCallError
  @b2d_start_status = $?.exitstatus
  @b2d_start_envset = false

  if @b2d_start_status == 0
    io.puts @b2d_start_text
    if set_env(@b2d_start_text)
      @b2d_start_envset = true
      io.puts yellow("Using above environment variables for starting #{machine_name}.")
    end
  end
end

#b2d_start_has_env?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/rake_compiler_dock/docker_check.rb', line 161

def b2d_start_has_env?
  @b2d_start_envset
end

#b2d_start_ok?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/rake_compiler_dock/docker_check.rb', line 157

def b2d_start_ok?
  @b2d_start_status == 0
end

#b2d_versionObject



125
126
127
128
# File 'lib/rake_compiler_dock/docker_check.rb', line 125

def b2d_version
  @b2d_version_text = `boot2docker version 2>&1` rescue SystemCallError
  @b2d_version_status = $?.exitstatus
end

#docker_client_avail?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rake_compiler_dock/docker_check.rb', line 70

def docker_client_avail?
  @docker_version_text =~ /version/
end

#docker_versionObject



58
59
60
61
62
63
64
# File 'lib/rake_compiler_dock/docker_check.rb', line 58

def docker_version
  COMMANDS.find do |command|
    @docker_version_text, @docker_version_status = run("#{command} version")
    @docker_command = command
    @docker_version_status == 0
  end
end

#doma_avail?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rake_compiler_dock/docker_check.rb', line 78

def doma_avail?
  @doma_version_status == 0 && @doma_version_text =~ /version/
end

#doma_createObject



91
92
93
94
95
# File 'lib/rake_compiler_dock/docker_check.rb', line 91

def doma_create
  options = add_env_options([], %w[ftp_proxy http_proxy https_proxy])
  driver = ENV['MACHINE_DRIVER'] || 'virtualbox'
  @doma_create_text, @doma_create_status = run("docker-machine create --driver #{driver.inspect} #{options.join(" ")} #{machine_name}", cmd: :visible, output: :visible)
end

#doma_create_ok?Boolean

Returns:

  • (Boolean)


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

def doma_create_ok?
  @doma_create_status == 0 || @doma_create_text =~ /already exists/
end

#doma_env_ok?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/rake_compiler_dock/docker_check.rb', line 117

def doma_env_ok?
  @doma_env_status == 0
end

#doma_has_env?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rake_compiler_dock/docker_check.rb', line 121

def doma_has_env?
  @doma_env_set
end

#doma_pwd_ok?Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
# File 'lib/rake_compiler_dock/docker_check.rb', line 169

def doma_pwd_ok?
  case host_os
  when /mingw|mswin/
    pwd =~ /^\/c\/users/i
  when /linux/
    true
  when /darwin/
    pwd =~ /^\/users/i
  end
end

#doma_startObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rake_compiler_dock/docker_check.rb', line 101

def doma_start
  @doma_start_text, @doma_start_status = run("docker-machine start #{machine_name}", cmd: :visible, output: :visible)
  @doma_env_set = false

  if doma_start_ok?
    @doma_env_text, @doma_env_status = run("docker-machine env #{machine_name} --shell bash --no-proxy")
    if @doma_env_status == 0 && set_env(@doma_env_text)
      @doma_env_set = true
    end
  end
end

#doma_start_ok?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/rake_compiler_dock/docker_check.rb', line 113

def doma_start_ok?
  @doma_start_status == 0 || @doma_start_text =~ /already running/
end

#doma_versionObject



74
75
76
# File 'lib/rake_compiler_dock/docker_check.rb', line 74

def doma_version
  @doma_version_text, @doma_version_status = run("docker-machine --version")
end

#help_textObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rake_compiler_dock/docker_check.rb', line 193

def help_text
  help = []
  if !ok? && docker_client_avail? && !doma_avail? && !b2d_avail?
    help << red("Docker client tools work, but connection to the local docker server failed.")
    case host_os
    when /linux/
      help << yellow("Please make sure the docker daemon is running.")
      help << ""
      help << yellow("On Ubuntu/Debian:")
      help << "   sudo service docker start"
      help << yellow("or")
      help << "   sudo service docker.io start"
      help << ""
      help << yellow("On Fedora/Centos/RHEL")
      help << "   sudo systemctl start docker"
      help << ""
      help << yellow("On SuSE")
      help << "   sudo systemctl start docker"
      help << ""
      help << yellow("Then re-check with '") + white("docker version") + yellow("'")
      help << yellow("or have a look at our FAQs: http://git.io/vm8AL")
    else
      help << yellow("    Please check why '") + white("docker version") + yellow("' fails")
      help << yellow("    or have a look at our FAQs: http://git.io/vm8AL")
    end
  elsif !ok? && !doma_avail? && !b2d_avail?
    case host_os
    when /mingw|mswin/
      help << red("Docker is not available.")
      help << yellow("Please download and install the docker-toolbox:")
      help << yellow("    https://www.docker.com/docker-toolbox")
    when /linux/
      help << red("Neither Docker nor Podman is available.")
      help << ""
      help << yellow("Install Docker on Ubuntu/Debian:")
      help << "    sudo apt-get install docker.io"
      help << ""
      help << yellow("Install Docker on Fedora/Centos/RHEL")
      help << "    sudo yum install docker"
      help << "    sudo systemctl start docker"
      help << ""
      help << yellow("Install Docker on SuSE")
      help << "    sudo zypper install docker"
      help << "    sudo systemctl start docker"
    when /darwin/
      help << red("Docker is not available.")
      help << yellow("Please install docker-machine per homebrew:")
      help << "    brew cask install virtualbox"
      help << "    brew install docker"
      help << "    brew install docker-machine"
      help << ""
      help << yellow("or download and install the official docker-toolbox:")
      help << yellow("    https://www.docker.com/docker-toolbox")
    else
      help << red("Docker is not available.")
    end
  elsif doma_avail?
    if !ok? && !doma_create_ok?
      help << red("docker-machine is installed but machine couldn't be created.")
      help << ""
      help << yellow("    Please check why '") + white("docker-machine create") + yellow("' fails")
      help << yellow("    or have a look at our FAQs: http://git.io/vRzIg")
    elsif !ok? && !doma_start_ok?
      help << red("docker-machine is installed but couldn't be started.")
      help << ""
      help << yellow("    Please check why '") + white("docker-machine start") + yellow("' fails.")
      help << yellow("    You might need to re-init with '") + white("docker-machine rm") + yellow("'")
      help << yellow("    or have a look at our FAQs: http://git.io/vRzIg")
    elsif !ok? && !doma_env_ok?
        help << red("docker-machine is installed and started, but 'docker-machine env' failed.")
        help << ""
        help << yellow("You might try to regenerate TLS certificates with:")
        help << "    docker-machine regenerate-certs #{machine_name}"
    elsif !ok? && !doma_pwd_ok?
      help << red("docker-machine can not mount the current working directory.")
      help << ""
      case host_os
      when /mingw|mswin/
        help << yellow("    Please move to a diretory below C:\\Users")
      when /darwin/
        help << yellow("    Please move to a diretory below /Users")
      end
    elsif !ok?
      help << red("docker-machine is installed and started, but 'docker version' failed.")
      help << ""

      if doma_has_env?
        help << yellow("    Please copy and paste following environment variables to your terminal")
        help += @doma_env_text.each_line.reject{|l| l=~/\s*#/ }.map{|l| "        #{l.chomp}" }
        help << yellow("    and check why '") + white("docker version") + yellow("' fails.")
      else
        help << yellow("    Please check why '") + white("docker version") + yellow("' fails.")
      end
      help << yellow("    You might also have a look at our FAQs: http://git.io/vRzIg")
    end
  elsif b2d_avail?
    if !ok? && !b2d_init_ok?
      help << red("boot2docker is installed but couldn't be initialized.")
      help << ""
      help << yellow("    Please check why '") + white("boot2docker init") + yellow("' fails")
      help << yellow("    or have a look at our FAQs: http://git.io/vm8Nr")
    elsif !ok? && !b2d_start_ok?
      help << red("boot2docker is installed but couldn't be started.")
      help << ""
      help << yellow("    Please check why '") + white("boot2docker start") + yellow("' fails.")
      help << yellow("    You might need to re-init with '") + white("boot2docker delete") + yellow("'")
      help << yellow("    or have a look at our FAQs: http://git.io/vm8Nr")
    elsif !ok? && !doma_pwd_ok?
      help << red("boot2docker can not mount the current working directory.")
      help << ""
      case host_os
      when /mingw|mswin/
        help << yellow("    Please move to a diretory below C:\\Users")
      when /darwin/
        help << yellow("    Please move to a diretory below /Users")
      end
    elsif !ok? && b2d_start_ok?
      help << red("boot2docker is installed and started, but 'docker version' failed.")
      help << ""
      if b2d_start_has_env?
        help << yellow("    Please copy and paste above environment variables to your terminal")
        help << yellow("    and check why '") + white("docker version") + yellow("' fails.")
      else
        help << yellow("    Please check why '") + white("docker version") + yellow("' fails.")
      end
      help << yellow("    You might need to re-init with '") + white("boot2docker delete") + yellow("'")
      help << yellow("    or have a look at our FAQs: http://git.io/vm8Nr")
    end
  end

  help.join("\n")
end

#host_osObject



165
166
167
# File 'lib/rake_compiler_dock/docker_check.rb', line 165

def host_os
  RbConfig::CONFIG['host_os']
end

#ok?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rake_compiler_dock/docker_check.rb', line 66

def ok?
  @docker_version_status == 0 && @docker_version_text =~ /version/i && doma_pwd_ok?
end


326
327
328
# File 'lib/rake_compiler_dock/docker_check.rb', line 326

def print_help_text
  io.puts(help_text)
end

#set_env(text) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rake_compiler_dock/docker_check.rb', line 180

def set_env(text)
  set = false
  text.scan(/(unset |Remove-Item Env:\\)(.+?)$/) do |_, key|
    ENV.delete(key)
    set = true
  end
  text.scan(/(export |\$Env:)(.+?)(="|=| = ")(.*?)(|\")$/) do |_, key, _, val, _|
    ENV[key] = val
    set = true
  end
  set
end