Module: DockerCore

Defined in:
lib/docker_core.rb

Constant Summary collapse

COLOR_CODE =
{ error: 31, info: 32, warn: 33 }

Class Method Summary collapse

Class Method Details

.color(text, code) ⇒ Object

Parameters:

  • text (String)
  • code (Integer)


8
9
10
11
12
13
# File 'lib/docker_core.rb', line 8

def self.color(text, code)
  escape = "\033"
  prefix = "#{escape}[#{code}m"
  suffix = "#{escape}[0m"
  return "#{prefix}#{text}#{suffix}"
end

.echo(text, code = self::COLOR_CODE[:info]) ⇒ Object

Parameters:

  • text (String)
  • code (Integer) (defaults to: self::COLOR_CODE[:info])


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

def self.echo(text, code = self::COLOR_CODE[:info])
  puts(self.color(text, code))
end

.execute(title, run, *argumets) ⇒ Object

Parameters:

  • title (String)
  • run (Method)
  • argumets (Array)


24
25
26
27
28
29
# File 'lib/docker_core.rb', line 24

def self.execute(title, run, *argumets)
  code = self::COLOR_CODE[:warn]
  self.echo(">> #{title}", code)
  run.call(*argumets)
  self.echo("<< #{title}", code)
end

.mkdir(*arguments) ⇒ Object

Parameters:

  • arguments (Array)


60
61
62
63
64
65
# File 'lib/docker_core.rb', line 60

def self.mkdir(*arguments)
  arguments.each do |list|
    FileUtils.mkdir_p(list)
    FileUtils.chown_R('core', 'core', list)
  end
end

.run(command, echo = true, throw = true) ⇒ Object

Parameters:

  • command (String)
  • echo (Boolean) (defaults to: true)
  • throw (Boolean) (defaults to: true)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/docker_core.rb', line 34

def self.run(command, echo = true, throw = true)
  if echo
    self.echo("+ #{command}")
  end

  begin
    print(`#{command}`)
  rescue
    if throw
      raise $!
    end
  end
end

.update_user(uid = 500, gid = 500) ⇒ Object

Parameters:

  • uid (Integer) (defaults to: 500)
  • gid (Integer) (defaults to: 500)


50
51
52
53
54
55
56
57
# File 'lib/docker_core.rb', line 50

def self.update_user(uid = 500, gid = 500)
  uid = ENV.fetch('DOCKER_CORE_UID', uid)
  gid = ENV.fetch('DOCKER_CORE_GID', gid)
  self.run('deluser core')
  self.run('delgroup core')
  self.run("addgroup --system --gid=#{gid} core")
  self.run("adduser --system --disabled-password --no-create-home --ingroup=core --gecos=core --shell=/bin/bash --uid=#{uid} core")
end