class Chef::Knife::ContainerDockerInit

Public Instance Methods

chef_client_mode() click to toggle source

Return the mode in which to run: zero or client

@return [String]

# File lib/chef/knife/container_docker_init.rb, line 228
def chef_client_mode
  config[:local_mode] ? "zero" : "client"
end
download_and_tag_base_image() click to toggle source

Download the base Docker image and tag it with the image name

# File lib/chef/knife/container_docker_init.rb, line 235
def download_and_tag_base_image
  ui.info("Downloading base image: #{config[:base_image]}. This process may take awhile...")
  shell_out("docker pull #{config[:base_image]}")
  image_name = config[:base_image].split(':')[0]
  ui.info("Tagging base image #{image_name} as #{@name_args[0]}")
  shell_out("docker tag #{image_name} #{@name_args[0]}")
end
eval_current_system() click to toggle source

Run some evaluations on the system to make sure it is in the state we need.

# File lib/chef/knife/container_docker_init.rb, line 246
def eval_current_system
  # Check to see if the Docker context already exists.
  if File.exist?(File.join(config[:dockerfiles_path], @name_args[0]))
    if config[:force]
      FileUtils.rm_rf(File.join(config[:dockerfiles_path], @name_args[0]))
    else
      show_usage
      ui.fatal("The Docker Context you are trying to create already exists. Please use the --force flag if you would like to re-create this context.")
      exit 1
    end
  end
end
first_boot_content() click to toggle source

Generate the JSON object for our first-boot.json

@return [String]

# File lib/chef/knife/container_docker_init.rb, line 217
def first_boot_content
  first_boot = {}
  first_boot['run_list'] = config[:run_list]
  JSON.pretty_generate(first_boot)
end
read_and_validate_params() click to toggle source

Read and validate the parameters

# File lib/chef/knife/container_docker_init.rb, line 129
def read_and_validate_params
  if @name_args.length < 1
    show_usage
    ui.fatal("You must specify a Dockerfile name")
    exit 1
  end

  if config[:generate_berksfile]
    begin
      require 'berkshelf'
    rescue LoadError
      show_usage
      ui.fatal("You must have the Berkshelf gem installed to use the Berksfile flag.")
      exit 1
    end
  end
end
recipe() click to toggle source

The name of the recipe to use

@return [String]

# File lib/chef/knife/container_docker_init.rb, line 208
def recipe
  "docker_init"
end
run() click to toggle source

Run the plugin

# File lib/chef/knife/container_docker_init.rb, line 116
def run
  read_and_validate_params
  set_config_defaults
  eval_current_system
  setup_context
  chef_runner.converge
  download_and_tag_base_image
  ui.info("\n#{ui.color("Context Created: #{config[:dockerfiles_path]}/#{@name_args[0]}", :magenta)}")
end
set_config_defaults() click to toggle source

Set default configuration values

We do this here and not in the option syntax because the Chef::Config
is not available to us at that point. It also gives us a space to set
other defaults.
# File lib/chef/knife/container_docker_init.rb, line 153
def set_config_defaults
  %w(
    chef_server_url
    cookbook_path
    node_path
    role_path
    environment_path
    validation_key
    validation_client_name
    trusted_certs_dir
    encrypted_data_bag_secret
  ).each do |var|
    config[:"#{var}"] ||= Chef::Config[:"#{var}"]
  end

  config[:base_image] ||= "chef/ubuntu-12.04:latest"

  # if no tag is specified, use latest
  unless config[:base_image] =~ /[a-zA-Z0-9\/]+:[a-zA-Z0-9.\-]+/
    config[:base_image] = "#{config[:base_image]}:latest"
  end

  config[:run_list] ||= []

  config[:dockerfiles_path] ||= Chef::Config[:knife][:dockerfiles_path] || File.join(Chef::Config[:chef_repo_path], 'dockerfiles')
end
setup_context() click to toggle source

Setup the generator context

# File lib/chef/knife/container_docker_init.rb, line 183
def setup_context
  generator_context.dockerfile_name = @name_args[0]
  generator_context.dockerfiles_path = config[:dockerfiles_path]
  generator_context.base_image = config[:base_image]
  generator_context.chef_client_mode = chef_client_mode
  generator_context.run_list = config[:run_list]
  generator_context.cookbook_path = config[:cookbook_path]
  generator_context.role_path = config[:role_path]
  generator_context.node_path = config[:node_path]
  generator_context.environment_path = config[:environment_path]
  generator_context.chef_server_url = config[:chef_server_url]
  generator_context.validation_key = config[:validation_key]
  generator_context.validation_client_name = config[:validation_client_name]
  generator_context.trusted_certs_dir = config[:trusted_certs_dir]
  generator_context.encrypted_data_bag_secret = config[:encrypted_data_bag_secret]
  generator_context.first_boot = first_boot_content
  generator_context.generate_berksfile = config[:generate_berksfile]
  generator_context.include_credentials = config[:include_credentials]
end