From 5c87ad5d7166c6b8c466f71a406ac94ae1bbaf31 Mon Sep 17 00:00:00 2001 From: Pasha Sadikov Date: Mon, 2 Jun 2014 15:28:55 -0400 Subject: [PATCH 1/3] Added a bunch more command line options --- knife-docker.gemspec | 2 +- lib/chef/knife/docker_create.rb | 137 +++++++++++++++++++++++--------- lib/knife-docker/version.rb | 2 +- 3 files changed, 101 insertions(+), 40 deletions(-) diff --git a/knife-docker.gemspec b/knife-docker.gemspec index 841d0a9..4a05ed5 100644 --- a/knife-docker.gemspec +++ b/knife-docker.gemspec @@ -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" diff --git a/lib/chef/knife/docker_create.rb b/lib/chef/knife/docker_create.rb index a23ed5e..5271d2a 100644 --- a/lib/chef/knife/docker_create.rb +++ b/lib/chef/knife/docker_create.rb @@ -15,6 +15,7 @@ # limitations under the License. # +require 'pry' require 'chef/knife' module ChefDocker @@ -25,60 +26,105 @@ 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" - - def locate_config_value(key) + :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 config[key] || Chef::Config[:knife][key] end @@ -89,9 +135,10 @@ def run show_usage exit 1 end + binding.pry # 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") @@ -109,6 +156,20 @@ def run bootstrap_container(ip, id.rstrip).run end + def build_run_command + cmd = "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] diff --git a/lib/knife-docker/version.rb b/lib/knife-docker/version.rb index 5830582..a7eb3dd 100644 --- a/lib/knife-docker/version.rb +++ b/lib/knife-docker/version.rb @@ -1,6 +1,6 @@ module Knife module Docker - VERSION = "0.0.2" + VERSION = "0.0.3" MAJOR, MINOR, TINY = VERSION.split('.') end end From 867415edcc76e853ec870ee0779e6fae93d01afa Mon Sep 17 00:00:00 2001 From: Pasha Sadikov Date: Mon, 2 Jun 2014 15:52:50 -0400 Subject: [PATCH 2/3] Some bug fixes regarding run_command --- lib/chef/knife/docker_create.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/chef/knife/docker_create.rb b/lib/chef/knife/docker_create.rb index 5271d2a..f529851 100644 --- a/lib/chef/knife/docker_create.rb +++ b/lib/chef/knife/docker_create.rb @@ -124,7 +124,7 @@ class DockerCreate < Chef::Knife :long => "--privileged", :description => "Runs in privileged mode" - def locate_config_value(key) + def locate_config_value(key) key = key.to_sym config[key] || Chef::Config[:knife][key] end @@ -135,7 +135,11 @@ def run show_usage exit 1 end - binding.pry + 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 = `#{build_run_command}` @@ -147,6 +151,7 @@ def run # get container IP container_info = `docker inspect #{id}` + binding.pry ip = container_info.match(/"IPAddress": "([\d\.]+)"/)[1] # containers boot *very* fast, but it might happen that we try to @@ -157,7 +162,7 @@ def run end def build_run_command - cmd = "run -d -p #{config[:ssh_port]}" + 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}"} From 62824ad355c29480d0ffcd6c7cee160f802307d5 Mon Sep 17 00:00:00 2001 From: Pasha Sadikov Date: Mon, 2 Jun 2014 15:55:27 -0400 Subject: [PATCH 3/3] Removing pry --- lib/chef/knife/docker_create.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/chef/knife/docker_create.rb b/lib/chef/knife/docker_create.rb index f529851..d07e7d7 100644 --- a/lib/chef/knife/docker_create.rb +++ b/lib/chef/knife/docker_create.rb @@ -15,7 +15,6 @@ # limitations under the License. # -require 'pry' require 'chef/knife' module ChefDocker @@ -151,7 +150,6 @@ def run # get container IP container_info = `docker inspect #{id}` - binding.pry ip = container_info.match(/"IPAddress": "([\d\.]+)"/)[1] # containers boot *very* fast, but it might happen that we try to