Class: Kontena::Cli::Stacks::BuildCommand

Inherits:
Kontena::Command show all
Includes:
Common, Common, Common::StackValuesFromOption, Common::StackValuesToOption
Defined in:
lib/kontena/cli/stacks/build_command.rb

Instance Attribute Summary

Attributes included from Common::StackValuesToOption

#values

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from Common::StackValuesFromOption

included

Methods included from Common::StackValuesToOption

#dump_variables, included

Methods included from Common

#abort_on_validation_errors, #current_dir, #display_notifications, #hint_on_validation_notifications, #loader, #loader_class, #reader, #set_env_variables, #stack, #stack_name, #stacks_client

Methods included from Kontena::Cli::Services::ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #health_status, #health_status_icon, #int_to_filesize, #parse_build_args, #parse_container_name, #parse_deploy_opts, #parse_health_check, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #render_service_deploy_instances, #restart_service, #scale_service, #show_service, #show_service_containers, #show_service_instances, #start_service, #stop_service, #update_service, #wait_for_deploy_to_finish

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#build_docker_image(service) ⇒ Integer

Parameters:

  • service (Hash)

Returns:

  • (Integer)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kontena/cli/stacks/build_command.rb', line 74

def build_docker_image(service)
  dockerfile = dockerfile = service['build']['dockerfile'] || 'Dockerfile'
  build_context = service['build']['context']
  cmd = ['docker', 'build', '-t', service['image']]
  cmd << ['-f', File.join(File.expand_path(build_context), dockerfile)] if dockerfile != "Dockerfile"
  cmd << '--no-cache' if no_cache?
  cmd << '--pull' unless no_pull?
  cmd.unshift('sudo') if sudo?

  args = service['build']['args'] || {}
  args.each do |k, v|
    cmd << "--build-arg=#{k}=#{v}"
  end
  cmd << build_context
  ret = system(*cmd.flatten)
  raise ("Failed to build image #{pastel.cyan(service['image'])}") unless ret
  ret
end

#build_docker_images(services) ⇒ Object

Parameters:

  • services (Hash)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kontena/cli/stacks/build_command.rb', line 46

def build_docker_images(services)
  services.each do |service|
    if service['build']
      dockerfile = service['build']['dockerfile'] || 'Dockerfile'
      abort("'#{service['image']}' is not valid Docker image name") unless valid_image_name?(service['image'])
      abort("'#{service['build']['context']}' does not have #{dockerfile}") unless dockerfile_exist?(service['build']['context'], dockerfile)
      if service['hooks'] && service['hooks']['pre_build']
        puts pastel.cyan("Running pre_build hook")
        run_pre_build_hook(service['hooks']['pre_build'])
      end
      puts "Building image #{pastel.cyan(service['image'])}"
      build_docker_image(service)
    end
  end
end

#dockerfile_exist?(path, dockerfile) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/kontena/cli/stacks/build_command.rb', line 112

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

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kontena/cli/stacks/build_command.rb', line 28

def execute
  set_env_variables(stack_name, current_grid)
  stack # runs validations

  services = stack['services']

  unless service_list.empty?
    services.select! { |service| service_list.include?(service['name']) }
  end

  if services.none?{ |service| service['build'] }
    abort pastel.red("Didn't find any services to build")
  end
  build_docker_images(services)
  push_docker_images(services) unless no_push?
end

#push_docker_image(image) ⇒ Integer

Parameters:

Returns:

  • (Integer)


95
96
97
98
99
100
101
# File 'lib/kontena/cli/stacks/build_command.rb', line 95

def push_docker_image(image)
  cmd = ['docker', 'push', image]
  cmd.unshift('sudo') if sudo?
  ret = system(*cmd)
  raise ("Failed to push image #{pastel.cyan(image)}") unless ret
  ret
end

#push_docker_images(services) ⇒ Object

Parameters:

  • services (Hash)


63
64
65
66
67
68
69
70
# File 'lib/kontena/cli/stacks/build_command.rb', line 63

def push_docker_images(services)
  services.each do |service|
    if service['build']
      puts "Pushing image #{pastel.cyan(service['image'])}"
      push_docker_image(service['image'])
    end
  end
end

#run_pre_build_hook(hook) ⇒ Object

Parameters:

  • hook (Hash)


118
119
120
121
122
123
# File 'lib/kontena/cli/stacks/build_command.rb', line 118

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

#valid_image_name?(name) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


105
106
107
# File 'lib/kontena/cli/stacks/build_command.rb', line 105

def valid_image_name?(name)
  !(/\A[\w.\/\-:]+:?+[\w+.]+\z/ =~ name).nil?
end