Last active
June 26, 2016 21:39
-
-
Save sbower/36ec3454a134ad83d68b to your computer and use it in GitHub Desktop.
Script to auto create DTR repository before pushing an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'net/http' | |
require 'net/https' | |
def rest_call (registry, path, verb = "get", post_data = "") | |
http = Net::HTTP.new(registry, 443) | |
http.use_ssl = true | |
path = "/api/v0/" + path | |
path = URI.escape(path) | |
http.start do |req| | |
response = (verb.eql?(:post) ? req.post(path, post_data, @headers) : req.request_get(path, @headers)) | |
response.body | |
end | |
end | |
#### MAIN #### | |
full_image_name = ARGV[0] | |
(registry, org, image) = full_image_name.split('/') | |
image_name = image.split(':')[0] | |
config_hash = JSON.parse(File.read("#{Dir.home}/.docker/config.json")) | |
dtr_auth = config_hash["auths"][registry]["auth"] | |
raise ArgumentError, "Unable to find auth token for #{registry}" unless dtr_auth | |
@headers = {"Authorization" => "Basic #{dtr_auth}", "Content-Type" => "application/json"} | |
repo_info = JSON.parse(rest_call(registry, "/repositories/#{org}/#{image_name}")) | |
if repo_info["errors"] and repo_info["errors"][0]["code"].eql?("NO_SUCH_REPOSITORY") then | |
puts "no repo, let's create it" | |
repo_create_info = JSON.parse(rest_call(registry, "/repositories/#{org}", :post, "{\"name\": \"#{image_name}\", \"visibility\": \"public\"}")) | |
raise RuntimeError, "Unable to create repository. #{repo_create_info.inspect}" if repo_create_info["errors"] | |
end | |
puts "pushing image" | |
puts `docker push #{full_image_name}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment