Class: KnifePlugins::Ec2AmisUbuntu

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/ec2_amis_ubuntu.rb

Instance Method Summary collapse

Instance Method Details

#build_type(region, arch, root_store, type) ⇒ Object

Makes a nice string for the type of AMI to display, including the region, architecture size and whether the AMI has EBS root store.

Parameters

region<String>

from Ubuntu::Ami#region

arch<String>

from Ubuntu::Ami#arch

root_store<String>

from Ubuntu::Ami#root_store

Returns

String

The region, arch and root_store (if EBS) concatenated

with underscore (_).



99
100
101
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 99

def build_type(region, arch, root_store, type)
  "#{region_fix(region)}_#{size_of(arch)}#{disk_store(root_store)}#{virt_type(type)}"
end

#disk_store(store) ⇒ Object

Indicates whether a particular image has EBS root volume.

Parameters

store<String>

comes from Ubuntu::Ami#root_store

Returns

String

“_ebs” if the #root_store is EBS, _ebs_ssd if ebs-ssd,

_ebs_io1 if provisioned IOPS, etc. Otherwise empty.



65
66
67
68
69
70
71
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 65

def disk_store(store)
  if store =~ /ebs/
    "_#{store.tr('-', '_')}"
  else
    ''
  end
end

#list_amis(distro) ⇒ Object

Iterates over the AMIs available for the specified distro.

Parameters

distro<String>

Release name of the distro to display.

Returns

Hash

Keys are the AMI type, values are the AMI ID.



110
111
112
113
114
115
116
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 110

def list_amis(distro)
  amis = Hash.new
  Ubuntu.release(distro).amis.each do |ami|
    amis[build_type(ami.region, ami.arch, ami.root_store, ami.virtualization_type)] = ami.name
  end
  amis
end

#region_fix(region) ⇒ Object

Substitutes dashes for underscores in region portion of type for nice display. Replaces “1” as implied for most regions.

Parameters

region<String>

comes from Ubuntu::Ami#region

Returns

String

e.g., us_east_small or us_east_small_ebs



42
43
44
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 42

def region_fix(region)
  region.gsub(/-1/,'').gsub(/-/,'_')
end

#runObject

Runs the plugin. If TYPE (name_args) is passed, then select a specified type, based on #build_type, above.



120
121
122
123
124
125
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 120

def run
  distro = name_args[0]
  type = name_args[1]
  ami_list = list_amis(distro)[type] || list_amis(distro)
  output(format_for_display(ami_list))
end

#size_of(arch) ⇒ Object

Indicates whether the architecture type is a large or small AMI.

Parameters

arch<String>

Values can be amd64, x86_64, large, or begin with

“64”. Intended to be from Ubuntu::Ami#arch, but this method move to Ubuntu directly.

Returns

String

For 64 bit architectures (Ubuntu::Ami#arch #=> amd64),

this will return “large”. Otherwise, it returns “small”.



83
84
85
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 83

def size_of(arch)
  String(arch) =~ /(amd64|x86_64|large|^64)/ ? "large" : "small"
end

#virt_type(type) ⇒ Object

Identifies the virtualization type as HVM if image is HVM.

Parameters

type<String>

comes from Ubuntu::Ami#virtualization_type

Returns

String

“_hvm” if the #virtualization_type is HVM, otherwise empty.



53
54
55
# File 'lib/chef/knife/ec2_amis_ubuntu.rb', line 53

def virt_type(type)
  type =~ /hvm/ ? "_hvm" : ''
end