Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Commit

Permalink
Use name of image for managing services
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinv committed Nov 10, 2013
1 parent dc0428c commit 8f83bdd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
13 changes: 6 additions & 7 deletions lib/dockistrano/docker.rb
Expand Up @@ -104,11 +104,10 @@ def self.clean
Dockistrano::CommandLine.command_with_stream("#{docker_command} rm $(#{docker_command} ps -a -q)")
end

def self.running_container_id(full_image_name)
request(["containers", "json"]).each do |container|
return container["Id"] if container["Image"] == full_image_name
end
nil
def self.running?(image_name)
request(["containers", image_name, "json"])["State"]["Running"]
rescue ResourceNotFound
false
end

# Returns the id of the last container with an error
Expand All @@ -132,8 +131,8 @@ def self.inspect_image(full_image_name)
raise ImageNotFound.new(e.message)
end

def self.inspect_container(id)
request(["containers", id, "json"])
def self.inspect_container(name)
request(["containers", name, "json"])
end

def self.stop_all_containers_from_image(full_image_name)
Expand Down
4 changes: 2 additions & 2 deletions lib/dockistrano/service.rb
Expand Up @@ -128,7 +128,7 @@ def stop

# Returns if this service is running
def running?
Docker.running_container_id(full_image_name)
Docker.running?(image_name)
end

# Pulls backing services for this service
Expand Down Expand Up @@ -355,7 +355,7 @@ def reset
private

def container_settings
@container_settings ||= Docker.inspect_container(Docker.running_container_id(full_image_name))
@container_settings ||= Docker.inspect_container(image_name)
end

class EnvironmentVariablesMissing < StandardError
Expand Down
12 changes: 6 additions & 6 deletions spec/dockistrano/docker_spec.rb
Expand Up @@ -106,13 +106,13 @@
end
end

context ".running_container_id" do
it "returns the first id of a running container matching with the image name" do
stub_request(:get, "http://127.0.0.1:4243/containers/json").to_return(status: 200, body: '[{"Id":"8e319853f561ba2c22de2ec9ff2584f99d091dd20cc5b393ab874631c6993a36","Image":"registry.dev.provider.net/provider-app-2.0:develop","Command":"bin/provider webserver","Created":1382449380,"Status":"Up 24 hours","Ports":[{"PrivatePort":3000,"PublicPort":49225,"Type":"tcp"}],"SizeRw":0,"SizeRootFs":0},
{"Id":"107fc12c1c4b8e42091bd46e97c985d42c9e0d06506327e84f28fef585f9865a","Image":"registry.dev.provider.net/provider-app-2.0:develop","Command":"bin/provider worker","Created":1382449380,"Status":"Up 24 hours","Ports":[{"PrivatePort":3000,"PublicPort":49224,"Type":"tcp"}],"SizeRw":0,"SizeRootFs":0},
{"Id":"066474c539231e445c636dd6ef2879e6a3e304cead10f78bd79e385b161cbdf5","Image":"registry.dev.provider.net/hipache:develop","Command":"supervisord -n","Created":1382447352,"Status":"Up 24 hours","Ports":[{"PrivatePort":80,"PublicPort":80,"Type":"tcp"},{"PrivatePort":6379,"PublicPort":16379,"Type":"tcp"}],"SizeRw":0,"SizeRootFs":0}]')
context ".running?" do
it "returns if the container is running" do
stub_request(:get, "http://127.0.0.1:4243/containers/provider_app_20/json").to_return(status: 200, body: '{
"ID":"a2ad211eb274ca5f8db962f571b07021ab77386a747a358135b30c3231b73844","State":{"Running":true,"Pid":3749,"ExitCode":0,"StartedAt":"2013-11-10T15:04:54.127812749+01:00","FinishedAt":"0001-01-01T00:00:00Z","Ghost":false}
}')

expect(subject.running_container_id("registry.dev.provider.net/provider-app-2.0:develop")).to eq("8e319853f561ba2c22de2ec9ff2584f99d091dd20cc5b393ab874631c6993a36")
expect(subject.running?("provider_app_20")).to be_true
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/dockistrano/service_spec.rb
Expand Up @@ -191,17 +191,17 @@

context "#running?" do
before do
allow(subject).to receive(:full_image_name).and_return("image_name:tag")
allow(subject).to receive(:image_name).and_return("image_name")
end

it "returns true when the service is running" do
expect(Dockistrano::Docker).to receive(:running_container_id).with("image_name:tag").and_return("423c138040f1")
expect(subject.running?).to eq("423c138040f1")
expect(Dockistrano::Docker).to receive(:running?).with("image_name").and_return(true)
expect(subject.running?).to be_true
end

it "returns false when the service is not running" do
expect(Dockistrano::Docker).to receive(:running_container_id).with("image_name:tag").and_return(nil)
expect(subject.running?).to eq(nil)
expect(Dockistrano::Docker).to receive(:running?).with("image_name").and_return(false)
expect(subject.running?).to be_false
end
end

Expand Down

0 comments on commit 8f83bdd

Please sign in to comment.