Class: Chef::Knife::ContainerDockerInit

Inherits:
Chef::Knife show all
Includes:
Mixin::ShellOut, KnifeContainer::Command
Defined in:
lib/chef/knife/container_docker_init.rb

Instance Method Summary collapse

Methods included from KnifeContainer::Command

#chef_runner, #docker_cookbook_path, #generator_context

Instance Method Details

#chef_client_modeString

Return the mode in which to run: zero or client

Returns:

  • (String)


230
231
232
# File 'lib/chef/knife/container_docker_init.rb', line 230

def chef_client_mode
  config[:local_mode] ? "zero" : "client"
end

#download_and_tag_base_imageObject

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



237
238
239
240
241
242
243
# File 'lib/chef/knife/container_docker_init.rb', line 237

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_systemObject

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



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/chef/knife/container_docker_init.rb', line 248

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_contentString

Generate the JSON object for our first-boot.json

Returns:

  • (String)


219
220
221
222
223
# File 'lib/chef/knife/container_docker_init.rb', line 219

def first_boot_content
  first_boot = {}
  first_boot['run_list'] = config[:run_list]
  JSON.pretty_generate(first_boot)
end

#read_and_validate_paramsObject

Read and validate the parameters



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/chef/knife/container_docker_init.rb', line 130

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

#recipeString

The name of the recipe to use

Returns:

  • (String)


210
211
212
# File 'lib/chef/knife/container_docker_init.rb', line 210

def recipe
  "docker_init"
end

#runObject

Run the plugin



117
118
119
120
121
122
123
124
125
# File 'lib/chef/knife/container_docker_init.rb', line 117

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_defaultsObject

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.


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/chef/knife/container_docker_init.rb', line 154

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] ||= []

  Chef::Config[:knife][:dockerfiles_path] ||= File.join(Chef::Config[:chef_repo_path], "dockerfiles")
  config[:dockerfiles_path] = Chef::Config[:knife][:dockerfiles_path]
end

#setup_contextObject

Setup the generator context



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/chef/knife/container_docker_init.rb', line 185

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