Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion knife-docker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.version = Knife::Docker::VERSION

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Emanuele Rocca"]
s.authors = ["Emanuele Rocca", "Pasha Sadikov"]
s.summary = "Chef's Knife plugin to create and bootstrap Docker containers"
s.description = s.summary
s.email = "ema@linux.it"
Expand Down
136 changes: 100 additions & 36 deletions lib/chef/knife/docker_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,103 @@ class DockerCreate < Chef::Knife
Chef::Knife::Bootstrap.load_deps
end

banner "knife docker create (options)"
banner "knife docker create RUN_COMMAND (options)"

option :_image,
:short => "-I IMAGE",
:long => "--image IMAGE",
:description => "The Docker container image to use"
:short => "-I IMAGE",
:long => "--image IMAGE",
:description => "The Docker container image to use"

option :chef_node_name,
:short => "-N NAME",
:long => "--node-name NAME",
:description => "The Chef node name for your new node"
:short => "-N NAME",
:long => "--node-name NAME",
:description => "The Chef node name for your new node"

option :ssh_user,
:short => "-x USERNAME",
:long => "--ssh-user USERNAME",
:description => "The ssh username",
:default => 'root'
:short => "-x USERNAME",
:long => "--ssh-user USERNAME",
:description => "The ssh username",
:default => 'root'

option :ssh_password,
:short => "-P PASSWORD",
:long => "--ssh-password PASSWORD",
:description => "The ssh password"
:short => "-P PASSWORD",
:long => "--ssh-password PASSWORD",
:description => "The ssh password"

option :ssh_port,
:long => "--ssh-port PORT",
:description => "The ssh port. Default is 22",
:default => '22'
:long => "--ssh-port PORT",
:description => "The ssh port. Default is 22",
:default => '22'

option :distro,
:short => "-d DISTRO",
:long => "--distro DISTRO",
:description => "Bootstrap a distro using a template",
:proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
:default => "chef-full"
:short => "-d DISTRO",
:long => "--distro DISTRO",
:description => "Bootstrap a distro using a template",
:proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
:default => "chef-full"

option :template_file,
:long => "--template-file TEMPLATE",
:description => "Full path to location of template to use",
:proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
:default => false
:long => "--template-file TEMPLATE",
:description => "Full path to location of template to use",
:proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
:default => false

option :run_list,
:short => "-r RUN_LIST",
:long => "--run-list RUN_LIST",
:description => "Comma separated list of roles/recipes to apply",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []
:short => "-r RUN_LIST",
:long => "--run-list RUN_LIST",
:description => "Comma separated list of roles/recipes to apply",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []

option :identity_file,
:short => "-i IDENTITY_FILE",
:long => "--identity IDENTITY_FILE",
:description => "SSH identity file for authentication"
:short => "-i IDENTITY_FILE",
:long => "--identity IDENTITY_FILE",
:description => "SSH identity file for authentication"

option :port_forward,
:short => "-p PORT[,PORT...]",
:long => "--forward PORT[,PORT...]",
:description => "Ports to forward using docker syntax",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []

option :dns_servers,
:long => "--dns HOST[,HOST...]",
:description => "DNS servers",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []

option :dns_search,
:long => "--search DOMAIN[,DOMAIN...]",
:description => "DNS search suffixes",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []

option :volumes,
:long => "--volumes VOLUME[,VOLUME...]",
:description => "Volumes to mount, see docker syntax",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []

option :hostname,
:short => "-h HOSTNAME",
:long => "--hostname HOSTNAME",
:description => "Hostname to assign to the container"

option :memory,
:short => "-m MEMORY",
:long => "--memory MEMORY",
:description => "How much RAM to allocate/permit"

option :cpu,
:short => "-C CPU",
:long => "--cpu CPU",
:description => "How much CPU to allocate/permit"

option :privileged,
:short => "-G",
:long => "--privileged",
:description => "Runs in privileged mode"

def locate_config_value(key)
key = key.to_sym
Expand All @@ -89,9 +134,14 @@ def run
show_usage
exit 1
end
if name_args.size > 0
config[:run_command] = name_args * ' '
else
config[:run_command] = "/usr/sbin/sshd -D -o UseDNS=no -o UsePAM=no"
end

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

unless $?.exitstatus == 0
ui.error("Cannot create container")
Expand All @@ -109,6 +159,20 @@ def run
bootstrap_container(ip, id.rstrip).run
end

def build_run_command
cmd = "docker run -d -p #{config[:ssh_port]}"
Array(config[:port_forward]).each {|port| cmd << " -p #{port}"}
Array(config[:dns_servers]).each {|dns| cmd << " --dns #{dns}"}
Array(config[:dns_search]).each {|search| cmd << " --dns-search #{search}"}
Array(config[:volumes]).each {|volume| cmd << " -v #{volume}"}
cmd << " -h #{config[:hostname]}" if config[:hostname]
cmd << " -m #{config[:memory]}" if config[:memory]
cmd << " -c #{config[:cpu]}" if config[:cpu]
cmd << " -privileged" if config[:privileged]
cmd << " #{config[:_image]} #{config[:run_command]}"
cmd
end

def bootstrap_container(fqdn, id)
bootstrap = Chef::Knife::Bootstrap.new
bootstrap.name_args = [fqdn]
Expand Down
2 changes: 1 addition & 1 deletion lib/knife-docker/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Knife
module Docker
VERSION = "0.0.2"
VERSION = "0.0.3"
MAJOR, MINOR, TINY = VERSION.split('.')
end
end