Class: Awful::Ami

Inherits:
Cli show all
Defined in:
lib/awful/ami.rb

Constant Summary collapse

COLORS =
{
  available: :green,
  pending:   :yellow,
  failed:    :red,
}

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#delete(id) ⇒ Object



52
53
54
55
56
57
# File 'lib/awful/ami.rb', line 52

def delete(id)
  ami = images(id).first
  if yes? "Really deregister image #{ami.name} (#{ami.image_id})?", :yellow
    ec2.deregister_image(image_id: ami.image_id)
  end
end

#dump(*ids) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/awful/ami.rb', line 60

def dump(*ids)
  images(*ids).output do |images|
    images.each do |image|
      puts YAML.dump(stringify_keys(image.to_hash))
    end
  end
end

#last(name = /./) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/awful/ami.rb', line 99

def last(name = /./)
  images.select do |image|
    image.name.match(name)
  end.sort_by(&:creation_date).last(options[:count]).map do |image|
    image.image_id
  end.output do |list|
    puts list
  end
end

#ls(*ids) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/awful/ami.rb', line 39

def ls(*ids)
  images(*ids).output do |list|
    if options[:long]
      print_table list.map { |i|
        [ i.name, i.image_id, i.root_device_type, color(i.state), i.creation_date, i.tags.map{ |t| "#{t.key}=#{t.value}" }.sort.join(',') ]
      }.sort
    else
      puts list.map(&:name).sort
    end
  end
end

#tags(id, *tags) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/awful/ami.rb', line 69

def tags(id, *tags)
  if tags.empty?
    images(id).first.tags.output do |tags|
      print_table tags.map { |t| [t.key, t.value] }
    end
  else
    ec2.create_tags(
      resources: [id],
      tags: tags.map do |t|
        key, value = t.split(/[:=]/)
        {key: key, value: value}
      end
    )
  end
end