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)


228
229
230
# File 'lib/chef/knife/container_docker_init.rb', line 228

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



235
236
237
238
239
240
241
# 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_systemObject

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



246
247
248
249
250
251
252
253
254
255
256
257
# 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_contentString

Generate the JSON object for our first-boot.json

Returns:

  • (String)


217
218
219
220
221
# 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_paramsObject

Read and validate the parameters



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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

#recipeString

The name of the recipe to use

Returns:

  • (String)


208
209
210
# File 'lib/chef/knife/container_docker_init.rb', line 208

def recipe
  "docker_init"
end

#runObject

Run the plugin



116
117
118
119
120
121
122
123
124
# 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_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.


153
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
# 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_contextObject

Setup the generator context



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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