Module: Central::Cli::Apps::DockerHelper

Included in:
BuildCommand, DeployCommand
Defined in:
lib/central/cli/apps/docker_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_docker_image(name, path, dockerfile, no_cache = false) ⇒ Integer

Parameters:

  • name (String)
  • path (String)
  • dockerfile (String)
  • no_cache (Boolean) (defaults to: false)

Returns:

  • (Integer)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/central/cli/apps/docker_helper.rb', line 43

def build_docker_image(name, path, dockerfile, no_cache = false)
  cmd = ["docker build -t #{name}"]
  if dockerfile != 'Dockerfile'
    cmd << "-f #{File.join(File.expand_path(path), dockerfile)}"
  end
  cmd << '--no-cache' if no_cache
  cmd << path
  ret = system(cmd.join(' '))
  abort("Failed to build image #{name.colorize(:cyan)}") unless ret
  ret
end

#dockerfile_exist?(path, dockerfile) ⇒ Boolean

Parameters:

  • path (String)
  • dockerfile (String)

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/central/cli/apps/docker_helper.rb', line 72

def dockerfile_exist?(path, dockerfile)
  file = File.join(File.expand_path(path), dockerfile)
  File.exist?(file)
end

#image_exist?(image) ⇒ Boolean

Parameters:

  • image (String)

Returns:

  • (Boolean)


65
66
67
# File 'lib/central/cli/apps/docker_helper.rb', line 65

def image_exist?(image)
  `docker history #{image} 2>&1`; $CHILD_STATUS.success?
end

#process_docker_images(services, force_build = false, no_cache = false) ⇒ Object

Parameters:

  • services (Hash)
  • force_build (Boolean) (defaults to: false)
  • no_cache (Boolean) (defaults to: false)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/central/cli/apps/docker_helper.rb', line 6

def process_docker_images(services, force_build = false, no_cache = false)
  if services.none? { |_name, service| service['build'] }
    puts 'Not found any service with build option'
    return
  end

  services.each do |_name, service|
    next unless service['build'] && (!image_exist?(service['image']) || force_build)
    dockerfile = service['dockerfile'] || 'Dockerfile'
    unless validate_image_name(service['image'])
      abort("'#{service['image']}' is not valid Docker image name")
    end
    unless dockerfile_exist?(service['build'], dockerfile)
      abort("'#{service['build']}' does not have #{dockerfile}")
    end
    if service['hooks'] && service['hooks']['pre_build']
      puts 'Running pre_build hook'.colorize(:cyan)
      run_pre_build_hook(service['hooks']['pre_build'])
    end
    puts "Building image #{service['image'].colorize(:cyan)}"
    build_docker_image(service['image'], service['build'], dockerfile, no_cache)
    puts "Pushing image #{service['image'].colorize(:cyan)} to registry"
    push_docker_image(service['image'])
  end
end

#push_docker_image(image) ⇒ Integer

Parameters:

  • image (String)

Returns:

  • (Integer)


57
58
59
60
61
# File 'lib/central/cli/apps/docker_helper.rb', line 57

def push_docker_image(image)
  ret = system("docker push #{image}")
  abort("Failed to push image #{image.colorize(:cyan)}") unless ret
  ret
end

#run_pre_build_hook(hook) ⇒ Object

Parameters:

  • hook (Hash)


78
79
80
81
82
83
# File 'lib/central/cli/apps/docker_helper.rb', line 78

def run_pre_build_hook(hook)
  hook.each do |h|
    ret = system(h['cmd'])
    abort("Failed to run pre_build hook: #{h['name']}!".colorize(:red)) unless ret
  end
end

#validate_image_name(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


34
35
36
# File 'lib/central/cli/apps/docker_helper.rb', line 34

def validate_image_name(name)
  !(/^[\w.\/\-:]+:?+[\w+.]+$/ =~ name).nil?
end