Skip to content

Commit

Permalink
gh-4: list images from all repositories
Browse files Browse the repository at this point in the history
Also moved some functionality from the command to libraries
  • Loading branch information
jfontan committed Mar 19, 2016
1 parent 6dc3587 commit b62bb32
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 42 deletions.
58 changes: 18 additions & 40 deletions bin/canga
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -29,65 +29,43 @@ 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]
end

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
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
61 changes: 61 additions & 0 deletions lib/cangallo.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,69 @@

# vim:ts=2:sw=2

require 'cangallo/qcow2'
require 'cangallo/config'
require 'cangallo/repo'
require 'cangallo/cangafile'
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

6 changes: 6 additions & 0 deletions lib/cangallo/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

# vim:ts=2:sw=2

require 'fileutils'
require 'yaml'

Expand Down Expand Up @@ -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
17 changes: 15 additions & 2 deletions lib/cangallo/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b62bb32

Please sign in to comment.