Skip to content

Instantly share code, notes, and snippets.

@kareemk
Created April 24, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kareemk/f1af79de76542a5e9040 to your computer and use it in GitHub Desktop.
Save kareemk/f1af79de76542a5e9040 to your computer and use it in GitHub Desktop.
Docker Exec over SSH for Tutum
#!/usr/bin/ruby
require 'net/http'
require 'json'
raise "Invalid arguments: dssh [container-name] [command=/bin/bash]" if ARGV.length < 1
service_name = "#{ARGV[0]}"
ARGV[1] ||= "/bin/bash"
command = ARGV[1..-1].join(' ')
uri = URI.parse("https://dashboard.tutum.co")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = { "Authorization" => "ApiKey koko:#{ENV['TUTUM_API_KEY']}" }
# Get list of containers that match
container = "#{service_name}-1"
response = http.get("/api/v1/container/?name=#{container}", headers)
if response.code == "200"
container_info = JSON.load(response.body)["objects"].first
dns = container_info["public_dns"]
image_name = container_info["image_name"]
exec "ssh -t root@#{dns} docker exec -i -t '$(docker ps | grep \"#{image_name}\" | cut -d\" \" -f1 | head -1) #{command}'"
else
puts "Error accessing tutum: #{response.body}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment