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, #ll, #version

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#delete(id) ⇒ Object



48
49
50
51
52
53
# File 'lib/awful/ami.rb', line 48

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



56
57
58
59
60
61
62
# File 'lib/awful/ami.rb', line 56

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



95
96
97
98
99
100
101
102
103
# File 'lib/awful/ami.rb', line 95

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/awful/ami.rb', line 35

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/awful/ami.rb', line 65

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