diff --git a/bin/canga b/bin/canga index ee86cd3..49edfc3 100755 --- a/bin/canga +++ b/bin/canga @@ -13,7 +13,7 @@ require "tempfile" require "pp" -$config = Cangallo::Config.new +$cangallo = Cangallo.new class Canga < Thor class_option :repo, :desc => 'repository to use' @@ -29,7 +29,7 @@ class Canga < Thor option :parent, :desc => "id of the parent image" option :tag, :desc => "tag name of the new image" def add(file) - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) sha256 = repo.add_image(file) repo.add_tag(options[:tag], sha256) if options[:tag] @@ -37,57 +37,35 @@ class Canga < Thor desc "tag TAGNAME IMAGE", "add a tag name to an existing image" def tag(tag, sha256) - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) repo.add_tag(tag, sha256) end desc "list", "list images" def list() - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) repo_name = repo.name + images = $cangallo.get_images + format = "%-30.30s %11.11s %-30.30s" puts format % %w{NAME SIZE DESCRIPTION} - reverse_tags = repo.tags.invert - - repo.images.each do |sha256, info| - tag = reverse_tags[sha256] - - if tag - name = "#{repo_name}:#{tag}" - else - name = "#{repo_name}:#{sha256[0..15]}" - end - - parent_sha256 = repo.get(sha256)["parent"] - parent = repo.get(parent_sha256) if parent_sha256 - - if parent_sha256 - tag = reverse_tags[parent_sha256] - - if tag - parent_name = "#{repo_name}:#{tag}" - else - parent_name = "#{repo_name}:#{parent_sha256[0..15]}" - end - name = "*#{name}" - else - parent_name = "" - name = " #{name}" - end + images.each do |image| + parent = image["parent"] ? "*" : " " + name = "#{parent}#{image["name"]}" - size = info["actual-size"].to_f / (1024 * 1024) # Mb + size = image["size"].to_f / (1024 * 1024) # Mb size = size.round(1) - puts format % [name, "#{size} Mb", info["description"]] + puts format % [name, "#{size} Mb", image["description"]] end end desc "show IMAGE", "show information about an image" def show(name) - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) image = repo.get(name) if image @@ -100,7 +78,7 @@ class Canga < Thor desc "overlay IMAGE FILE", "create a new image based on another one" def overlay(sha256, file) - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) image = repo.find(sha256) @@ -116,7 +94,7 @@ class Canga < Thor desc "build CANGAFILE", "create a new image using a Cangafile" def build(file) - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) cangafile = Cangafile.new(file) puts cangafile.libguestfs_commands @@ -159,26 +137,26 @@ class Canga < Thor desc "fetch", "download the index of the repository" def fetch - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) repo.fetch end desc "sign", "sign the index file with keybase" def sign - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) repo.sign end desc "verify", "verify index signature with keybase" def verify - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) repo.verify end desc "pull NAME", "downloads an image from a remote repository" def pull(name) - repo = $config.repo(options[:repo]) + repo = $cangallo.repo(options[:repo]) repo.pull(name) end diff --git a/lib/cangallo.rb b/lib/cangallo.rb index 152ff5f..76e4496 100644 --- a/lib/cangallo.rb +++ b/lib/cangallo.rb @@ -1,4 +1,6 @@ +# vim:ts=2:sw=2 + require 'cangallo/qcow2' require 'cangallo/config' require 'cangallo/repo' @@ -6,3 +8,62 @@ require 'cangallo/libguestfs' require 'cangallo/keybase' +class Cangallo + def initialize + @config = Cangallo::Config.new + end + + def repo(name = nil) + @config.repo(name) + end + + def get_images + info = [] + + @config.repos.each do |r| + repo = self.repo(r) + + repo.images.each do |sha256, image| + name = repo.short_name(sha256) + + info << { + "repo" => r, + "sha256" => sha256, + "name" => "#{r}:#{name}", + "size" => image["actual-size"], + "parent" => short_name(image["parent"], r), + "description" => image["description"] + } + end + end + + info + end + + def parse_name(name) + slices = name.split(':') + + repo = nil + name = name + + if slices.length > 1 + repo = slices[0] + name = slices[1] + end + + return repo, name + end + + def short_name(string, repo = nil) + return nil if !string + + img_repo, img_name = parse_name(string) + img_repo ||= repo + + image = self.repo(img_repo).find(img_name) + name = self.repo(img_repo).short_name(image) + + "#{img_repo}:#{name}" + end +end + diff --git a/lib/cangallo/config.rb b/lib/cangallo/config.rb index 713bf9a..85f1540 100644 --- a/lib/cangallo/config.rb +++ b/lib/cangallo/config.rb @@ -1,4 +1,6 @@ +# vim:ts=2:sw=2 + require 'fileutils' require 'yaml' @@ -71,5 +73,9 @@ def config_dir def config_file File.join(config_dir, CONFIG_FILE) end + + def repos + @conf["repos"].keys + end end end diff --git a/lib/cangallo/repo.rb b/lib/cangallo/repo.rb index 207ed53..70fd3fb 100644 --- a/lib/cangallo/repo.rb +++ b/lib/cangallo/repo.rb @@ -51,8 +51,9 @@ def read_index(index = nil) data = YAML.load(index) end - @images = data["images"] - @tags = data["tags"] + @images = data["images"] + @tags = data["tags"] + @reverse_tags = @tags.invert end def write_index @@ -222,6 +223,18 @@ def pull(name) system(cmd) end + + def short_name(sha256) + tag = @reverse_tags[sha256] + + if tag + name = "#{tag}" + else + name = "#{sha256[0..15]}" + end + + name + end end end