lib/spoon.rb in docker-spoon-0.1.0 vs lib/spoon.rb in docker-spoon-0.2.0
- old
+ new
@@ -1,9 +1,8 @@
require "spoon/version"
require 'docker'
require 'json'
-require 'pp'
require 'uri'
module Spoon
include Methadone::Main
include Methadone::CLILogging
@@ -19,10 +18,12 @@
image_list
elsif options[:build]
image_build
elsif options[:destroy]
instance_destroy(apply_prefix(options[:destroy]))
+ elsif options[:network]
+ instance_network(apply_prefix(options[:network]))
elsif instance
instance_connect(apply_prefix(instance), options[:command])
else
help_now!("You either need to provide an action or an instance to connect to")
end
@@ -33,10 +34,11 @@
# Actions
on("-l", "--list", "List available spoon instances")
on("-d", "--destroy NAME", "Destroy spoon instance with NAME")
on("-b", "--build", "Build image from Dockerfile using name passed to --image")
+ on("-n", "--network NAME", "Display exposed ports using name passed to NAME")
# Configurables
options[:builddir] = '.'
on("--builddir DIR", "Directory containing Dockerfile")
on("--pre-build-commands", "List of commands to run locally before building image")
@@ -74,11 +76,11 @@
def self.image_build
# Run pre-build commands
options["pre-build-commands"].each do |command|
sh command
- end
+ end unless options["pre-build-commands"].nil?
D "pre-build commands complete, building Docker image"
docker_url
build_opts = { 't' => options[:image], 'rm' => true }
Docker::Image.build_from_dir(options[:builddir], build_opts) do |chunk|
@@ -121,26 +123,66 @@
if not instance_exists? name
puts "The `#{name}` container doesn't exist, creating..."
instance_create(name)
end
+ container = get_container(name)
+ unless is_running?(container)
+ instance_start(container)
+ end
+
puts "Connecting to `#{name}`"
instance_ssh(name, command)
end
def self.instance_list
docker_url
puts "List of available spoon containers:"
- container_list = Docker::Container.all
+ container_list = get_all_containers
container_list.each do |container|
name = container.info["Names"].first.to_s
if name.start_with? "/#{options[:prefix]}"
- puts remove_prefix(name)
+ running = is_running?(container) ? "Running" : "Stopped"
+ puts "#{remove_prefix(name)} [ #{running} ]".rjust(40)
end
end
end
+ def self.strip_slash(name)
+ if name.start_with? "/"
+ name[1..-1]
+ else
+ name
+ end
+ end
+
+ def self.is_running?(container)
+ container = Docker::Container.get(container.info["id"])
+ status = container.info["State"]["Running"] || nil
+ unless status.nil?
+ return status
+ else
+ return false
+ end
+ end
+
+ def self.instance_network(name)
+ docker_url
+
+ container = get_container(name)
+
+ if is_running?(container)
+ ports = container.json['NetworkSettings']['Ports']
+ ports.each do |p_name, p_port|
+ tcp_name = p_name.split('/')[0]
+ puts "#{tcp_name} -> #{p_port.first['HostPort']}"
+ end
+ else
+ puts "Container is not running, cannot show ports"
+ end
+ end
+
def self.instance_destroy(name)
docker_url
container = get_container(name)
if container
@@ -179,15 +221,28 @@
else
puts "No container named: #{container.inspect}"
end
end
+ def self.get_all_containers
+ Docker::Container.all(:all => true)
+ end
+
+ def self.get_running_containers
+ Docker::Container.all
+ end
+
+ def self.instance_start(container)
+ container.start!
+ end
+
def self.get_container(name)
docker_url
- container_list = Docker::Container.all
+ container_list = get_all_containers
+ l_name = strip_slash(name)
container_list.each do |container|
- if container.info["Names"].first.to_s == "/#{name}"
+ if container.info["Names"].first.to_s == "/#{l_name}"
return container
end
end
return nil
end