Class: ChefDocker::DockerCreate

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/docker_create.rb

Instance Method Summary collapse

Instance Method Details

#bootstrap_container(fqdn, id) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/knife/docker_create.rb', line 112

def bootstrap_container(fqdn, id)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [fqdn]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
  bootstrap.config[:ssh_password] = locate_config_value(:ssh_password)
  bootstrap.config[:ssh_port] = config[:ssh_port]
  bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:chef_node_name] = config[:chef_node_name] || id
  bootstrap.config[:bootstrap_version] = config[:bootstrap_version]
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
  bootstrap.config[:use_sudo_password] = true if bootstrap.config[:use_sudo]
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = locate_config_value(:environment)

  bootstrap
end

#locate_config_value(key) ⇒ Object



81
82
83
84
# File 'lib/chef/knife/docker_create.rb', line 81

def locate_config_value(key)
  key = key.to_sym
  config[key] || Chef::Config[:knife][key]
end

#runObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/knife/docker_create.rb', line 86

def run
  unless config[:_image]
    ui.error("Please provide a valid Docker container image (-I)")
    show_usage
    exit 1
  end

  # start a new container with sshd
  id = `docker run -d -p #{config[:ssh_port]} #{config[:_image]} /usr/sbin/sshd -D`

  unless $?.exitstatus == 0
    ui.error("Cannot create container")
    exit 1
  end

  # get container IP
  container_info = `docker inspect #{id}`
  ip = container_info.match(/"IPAddress": "([\d\.]+)"/)[1]

  # containers boot *very* fast, but it might happen that we try to
  # bootstrap before SSH is up. Let's wait a second.
  sleep 1

  bootstrap_container(ip, id.rstrip).run
end