class Awful::Cli

Public Class Methods

new(args = [], local_options = {}, config = {}) click to toggle source
Calls superclass method
# File lib/awful.rb, line 24
def initialize(args = [], local_options = {}, config = {})
  super
  $awful_quiet = options[:quiet]                           # turn off output
  ENV['AWS_REGION'] = options[:region] if options[:region] #cmdline override for region
end

Public Instance Methods

api_gateway() click to toggle source
# File lib/awful/api_gateway.rb, line 10
def api_gateway
  @api_gateway ||= Aws::APIGateway::Client.new
end
autoscaling() click to toggle source
# File lib/awful.rb, line 40
def autoscaling
  @autoscaling ||= Aws::AutoScaling::Client.new
end
cf() click to toggle source
# File lib/awful.rb, line 79
def cf
  @cf ||= Aws::CloudFormation::Client.new
end
datapipeline() click to toggle source
# File lib/awful.rb, line 52
def datapipeline
  @datapipeline ||= Aws::DataPipeline::Client.new
end
dynamodb() click to toggle source

to use dynamodb-local, set DYNAMO_ENDPOINT or DYNAMODB_ENDPOINT to e.g. localhost:8000

# File lib/awful.rb, line 57
def dynamodb
  options = {endpoint: ENV['DYNAMO_ENDPOINT'] || ENV['DYNAMODB_ENDPOINT']}.reject { |_,v| v.to_s.empty? }
  @dynamodb ||= Aws::DynamoDB::Client.new(options)
end
dynamodb_simple() click to toggle source

dynamodb client with parameter conversion turned off, for getting result as Aws::Plugins::Protocols::JsonRpc; see docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataFormat.html; client method calls need simple attributes in requests also (strings not symbols, etc)

# File lib/awful.rb, line 66
def dynamodb_simple
  options = {
    simple_attributes: false,
    simple_json: true,
    endpoint: ENV['DYNAMO_ENDPOINT'] || ENV['DYNAMODB_ENDPOINT'],
  }.reject { |_,v| v.nil? }
  @dynamodb_simple ||= Aws::DynamoDB::Client.new(options)
end
ec2() click to toggle source
# File lib/awful.rb, line 36
def ec2
  @ec2 ||= Aws::EC2::Client.new
end
elb() click to toggle source
# File lib/awful.rb, line 44
def elb
  @elb ||= Aws::ElasticLoadBalancing::Client.new
end
file_or_stdin(file) click to toggle source

returns contents of named file, or stdin if file = nil, or false if no stdin

# File lib/awful.rb, line 116
def file_or_stdin(file)
  (file and File.read(file)) || ((not $stdin.tty?) and $stdin.read)
end
find_instance(name) click to toggle source

return id for instance by name

# File lib/awful.rb, line 147
def find_instance(name)
  if name .nil?
    nil?
  elsif 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
    end.instance_id
  end
end
find_sg(name) click to toggle source

return id for security group by name

# File lib/awful.rb, line 171
def find_sg(name)
  if name.match(/^sg-[\d[a-f]]{8}$/)
    name
  else
    ec2.describe_security_groups.map(&:security_groups).flatten.find do |sg|
      tag_name(sg) == name
    end.group_id
  end
end
find_subnet(name) click to toggle source

return id for subnet by name

# File lib/awful.rb, line 160
def find_subnet(name)
  if name.match(/^subnet-[\d[a-f]]{8}$/)
    name
  else
    ec2.describe_subnets.map(&:subnets).flatten.find do |subnet|
      tag_name(subnet) == name
    end.subnet_id
  end
end
lambda() click to toggle source
# File lib/awful.rb, line 83
def lambda
  @lambda ||= Aws::Lambda::Client.new
end
load_cfg(options = {}, file = nil) click to toggle source

merge options with config from erb-parsed yaml stdin or file

# File lib/awful.rb, line 121
def load_cfg(options = {}, file = nil)
  Dotenv.overload(options[:env]) if options[:env]
  src = (file and File.read(file)) || ((not $stdin.tty?) and $stdin.read)
  cfg = src ? YAML.load(::ERB.new(src).result(binding)) : {}
  symbolize_keys(cfg).merge(symbolize_keys(options.reject{ |_,v| v.nil? }))
end
only_keys_matching(hash, keylist) click to toggle source

return a copy of hash, but with only listed keys

# File lib/awful.rb, line 129
def only_keys_matching(hash, keylist)
  hash.select do |key,_|
    keylist.include?(key)
  end
end
paginate(thing) { |token| ... } click to toggle source

universal paginator for aws-sdk calls

# File lib/awful.rb, line 182
def paginate(thing)
  token = nil
  things = []
  loop do
    resp = yield(token)
    things += resp.send(thing)
    ## some apis use marker, some use token
    token = resp.respond_to?(:next_marker) ? resp.next_marker : resp.next_token
    break if token.nil?
  end
  things
end
rds() click to toggle source
# File lib/awful.rb, line 48
def rds
  @rds ||= Aws::RDS::Client.new
end
remove_empty_strings(hash) click to toggle source
# File lib/awful.rb, line 135
def remove_empty_strings(hash)
  hash.reject do |_,value|
    value.respond_to?(:empty?) and value.empty?
  end
end
s3() click to toggle source
# File lib/awful.rb, line 75
def s3
  @s3 ||= Aws::S3::Client.new
end
stringify_keys(thing) click to toggle source
# File lib/awful.rb, line 105
def stringify_keys(thing)
  if thing.is_a?(Hash)
    Hash[ thing.map { |k,v| [ k.to_s, stringify_keys(v) ] } ]
  elsif thing.respond_to?(:map)
    thing.map { |v| stringify_keys(v) }
  else
    thing
  end
end
support() click to toggle source
# File lib/awful.rb, line 87
def support
  @support ||= Aws::Support::Client.new
end
symbolize_keys(thing) click to toggle source
# File lib/awful.rb, line 95
def symbolize_keys(thing)
  if thing.is_a?(Hash)
    Hash[ thing.map { |k,v| [ k.to_sym, symbolize_keys(v) ] } ]
  elsif thing.respond_to?(:map)
    thing.map { |v| symbolize_keys(v) }
  else
    thing
  end
end
tag_name(thing, default = nil) click to toggle source
# File lib/awful.rb, line 141
def tag_name(thing, default = nil)
  tn = thing.tags.find { |tag| tag.key == 'Name' }
  tn ? tn.value : default
end
yes?(statement, color = nil) click to toggle source

override thor yes? method to handle our yes option

Calls superclass method
# File lib/awful.rb, line 32
def yes?(statement, color = nil)
  options[:yes] ? say("#{statement} yes", color) : super(statement, color)
end