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
58
59
60
# File 'lib/awful/ami.rb', line 52

def delete(id)
  images(options).find do |image|
    image.image_id.match(id)
  end.output do |ami|
    if yes? "Really deregister image #{ami.name} (#{ami.image_id})?", :yellow
      ec2.deregister_image(image_id: ami.image_id)
    end
  end
end

#dump(*ids) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/awful/ami.rb', line 63

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

#last(name = /./) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/awful/ami.rb', line 102

def last(name = /./)
  images(options).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(name = /./) ⇒ Object



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

def ls(name = /./)
  images(options).select do |image|
    image.name.match(name)
  end.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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/awful/ami.rb', line 72

def tags(id, *tags)
  if tags.empty?
    ec2.describe_images(image_ids: [id]).images.first.tags.output do |list|
      print_table list.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