Skip to content

Commit

Permalink
Merge pull request upserve#118 from swipely/allow-no-creds-for-push
Browse files Browse the repository at this point in the history
Allow no credentials to be passed to #push.
  • Loading branch information
nahiluhmot committed Apr 3, 2014
2 parents fd83ca2 + acc1e06 commit c80ec81
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 55 deletions.
20 changes: 9 additions & 11 deletions lib/docker/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ def run(cmd=nil)

# Push the Image to the Docker registry.
def push(creds = nil, options = {})
repository = self.info['RepoTags'].first.split(/:/)[0] rescue nil

raise ArgumentError, "Image does not have a name to push." unless repository

credentials = creds || Docker.creds
headers = Docker::Util.build_auth_header(credentials)
connection.post(
"/images/#{repository}/push",
options,
:headers => headers
)
repo_tag = info['RepoTags'].first
raise ArgumentError "Image is untagged" if repo_tag.nil?
repo, tag = Docker::Util.parse_repo_tag(repo_tag)
raise ArgumentError, "Image does not have a name to push." if repo.nil?

credentials = creds || Docker.creds || {}
headers = creds.nil? ? {} : Docker::Util.build_auth_header(credentials)
opts = options.merge(:tag => tag)
connection.post("/images/#{repo}/push", opts, :headers => headers)
self
end

Expand Down
8 changes: 8 additions & 0 deletions lib/docker/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def parse_json(body)
raise UnexpectedResponseError, ex.message
end

def parse_repo_tag(str)
if match = str.match(/\A(.*):([^:]*)\z/)
match.captures
else
[str, '']
end
end

def fix_json(body)
parse_json("[#{body.gsub(/}\s*{/, '},{')}]")
end
Expand Down
15 changes: 15 additions & 0 deletions spec/docker/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@
it 'pushes the Image', :vcr do
new_image.push(credentials)
end

context 'when there are no credentials' do
let(:credentials) { nil }
let(:image) {
Docker::Image.create('fromImage' => 'registry', 'tag' => 'latest')
}

before do
image.tag('repo' => 'localhost:5000/registry', 'tag' => 'test')
end

it 'still pushes', :vcr do
expect { image.push }.to_not raise_error
end
end
end

describe '#tag' do
Expand Down
Loading

0 comments on commit c80ec81

Please sign in to comment.