class Awful::Ec2
Constants
- COLORS
Public Instance Methods
addresses()
click to toggle source
# File lib/awful/ec2.rb, line 170 def addresses ec2.describe_addresses.map(&:addresses).flatten.map do |ip| [ ip.allocation_id, ip.public_ip, ip.instance_id, ip.domain ] end.output do |list| print_table list end end
allocate()
click to toggle source
# File lib/awful/ec2.rb, line 156 def allocate ec2.allocate_address(domain: 'vpc').first.output do |eip| puts eip.allocation_id, eip.public_ip end end
associate(name, eip)
click to toggle source
# File lib/awful/ec2.rb, line 163 def associate(name, eip) ec2.associate_address(instance_id: find_instance(name), allocation_id: eip).map(&:association_id).output do |id| puts id end end
az()
click to toggle source
# File lib/awful/ec2.rb, line 145 def az ec2.describe_availability_zones.availability_zones.output do |zones| if options[:long] print_table zones.map { |z| [z.zone_name, z.state, z.messages.join(',')] } else puts zones.map(&:zone_name) end end end
color(string)
click to toggle source
# File lib/awful/ec2.rb, line 19 def color(string) set_color(string, COLORS.fetch(string.to_sym, :yellow)) end
create(name)
click to toggle source
# File lib/awful/ec2.rb, line 93 def create(name) opt = load_cfg.merge(symbolize_keys(options)) whitelist = %i[image_id min_count max_count key_name security_group_ids user_data instance_type kernel_id ramdisk_id monitoring subnet_id disable_api_termination instance_initiated_shutdown_behavior additional_info iam_instance_profile ebs_optimized network_interfaces] opt[:min_count] ||= 1 opt[:max_count] ||= 1 opt[:monitoring] = {enabled: opt.fetch(:monitoring, {}).fetch(:state, '') == 'enabled'} ## set subnet from human-readable name, either for network interface, or instance-level if opt[:subnet] subnet = find_subnet(opt[:subnet]) opt[:network_interfaces] ? (opt[:network_interfaces][0][:subnet_id] = subnet) : (opt[:subnet_id] = subnet) end (opt[:tags] = opt.fetch(:tags, [])).find_index { |t| t[:key] == 'Name' }.output do |index| opt[:tags][index || 0] = {key: 'Name', value: name} end ## TODO: block_device_mappings ## TODO: placement opt[:security_group_ids] = opt.fetch(:security_groups, []).map { |sg| sg[:group_id] } opt[:user_data] = Base64.strict_encode64(opt[:user_data]) if opt[:user_data] # scrub unwanted fields from a copied instance dump opt = remove_empty_strings(opt) ## start instance response = ec2.run_instances(only_keys_matching(opt, whitelist)) ids = response.instances.map(&:instance_id) ec2.create_tags(resources: ids, tags: opt[:tags]) # tag instances puts ids # report new instance ids ## wait for instance to enter running state puts 'running instance ...' ec2.wait_until(:instance_running, instance_ids: ids) ## allocate and associate new elastic IPs ids.map { |id| associate(id, allocate.allocation_id) } if opt[:elastic_ip] ## report DNS or IP for instance ec2.describe_instances(instance_ids: ids).map(&:reservations).flatten.map(&:instances).flatten.map do |instance| instance.public_dns_name or instance.public_ip_address or instance.private_ip_address end.output do |list| puts list end end
delete(name)
click to toggle source
# File lib/awful/ec2.rb, line 232 def delete(name) id = if name.match(/^i-[\d[a-f]]{8,17}$/) name else ec2.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.find do |instance| tag_name(instance) == name and not %w[terminated shutting-down].include?(instance.state.name) end.instance_id end if yes? "Really terminate instance #{name} (#{id})?", :yellow ec2.terminate_instances(instance_ids: Array(id)) end end
dns(name)
click to toggle source
# File lib/awful/ec2.rb, line 179 def dns(name) ec2.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.find do |instance| instance.instance_id == name or (n = tag_name(instance) and n.match(name)) end.public_dns_name.output do |dns| puts dns end end
dump(name)
click to toggle source
# File lib/awful/ec2.rb, line 81 def dump(name) ec2.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.find do |instance| instance.instance_id == name or tag_name(instance) == name end.output do |instance| puts YAML.dump(stringify_keys(instance.to_hash)) end end
ls(name = nil)
click to toggle source
# File lib/awful/ec2.rb, line 32 def ls(name = nil) params = {instance_ids: [], filters: []} ## filter by ids options[:ids].each do |id| params[:instance_ids] << id end ## filter by arbitrary tags options[:tags].each do |tag| key, value = tag.split(/[:=]/) params[:filters] << {name: "tag:#{key}", values: [value]} end ## filter shortcuts for stack, resource, autoscaling group params[:filters] << {name: 'tag:aws:cloudformation:stack-name', values: [options[:stack]]} if options[:stack] params[:filters] << {name: 'tag:aws:cloudformation:logical-id', values: [options[:resource]]} if options[:resource] params[:filters] << {name: 'tag:aws:autoscaling:groupName', values: [options[:autoscaling]]} if options[:autoscaling] params[:filters] << {name: 'instance-state-name', values: [options[:state]]} if options[:state] ## get list of instances instances = ec2.describe_instances(params.reject{ |k,v| v.empty? }).reservations.map(&:instances).flatten ## filter by Name tag as a regex instances.select! { |i| tag_name(i, '').match(name) } if name ## output instances.output do |list| if options[:long] print_table list.map { |i| [ tag_name(i, ''), i.instance_id, i.instance_type, i.image_id, i.placement.availability_zone, color(i.state.name), i.security_groups.map(&:group_name).join(',').slice(0..30), i.private_ip_address, i.public_ip_address ] }.sort_by(&:first) else puts list.map(&:instance_id) end end end
start(name)
click to toggle source
# File lib/awful/ec2.rb, line 223 def start(name) ec2.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.find do |instance| instance.instance_id == name or (n = tag_name(instance) and n == name) end.instance_id.output do |id| ec2.start_instances(instance_ids: Array(id)) end end
stop(name)
click to toggle source
# File lib/awful/ec2.rb, line 212 def stop(name) ec2.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.find do |instance| instance.instance_id == name or (n = tag_name(instance) and n == name) end.instance_id.output do |id| if yes? "Really stop instance #{name} (#{id})?", :yellow ec2.stop_instances(instance_ids: Array(id)) end end end
user_data(name)
click to toggle source
# File lib/awful/ec2.rb, line 201 def user_data(name) ec2.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.find do |instance| instance.instance_id == name or tag_name(instance) == name end.output do |instance| ec2.describe_instance_attribute(instance_id: instance.instance_id, attribute: 'userData').user_data.value.output do |user_data| puts Base64.strict_decode64(user_data) end end end