module Builderator::Util

Shared helper methods

Constants

GEM_PATH
VENDOR
WORKSPACE

Public Class Methods

asg(region = Config.aws.region) click to toggle source
# File lib/builderator/util.rb, line 78
def asg(region = Config.aws.region)
  clients["asg-#{region}"] ||= Aws::AutoScaling::Client.new(:region => region)
end
ec2(region = Config.aws.region, credentials=nil) click to toggle source

AWS Clients

# File lib/builderator/util.rb, line 63
def ec2(region = Config.aws.region, credentials=nil)
  options = { :region => region }

  # Don't memoize if supplying explicit credentials as it could be an assumed role for a remote account
  if credentials.nil?
    clients["ec2-#{region}"] ||= Aws::EC2::Client.new(options)
  else
    Aws::EC2::Client.new options.merge(credentials)
  end
end
ecr(region = Config.aws.region) click to toggle source
# File lib/builderator/util.rb, line 74
def ecr(region = Config.aws.region)
  clients["ecr-#{region}"] ||= Aws::ECR::Client.new(:region => region)
end
filter(resources, filters = {}) click to toggle source

Set-filter helpers

# File lib/builderator/util.rb, line 46
def filter(resources, filters = {})
  resources.select do |_, r|
    _filter_reduce(r, filters)
  end
end
filter!(resources, filters = {}) click to toggle source
# File lib/builderator/util.rb, line 52
def filter!(resources, filters = {})
  resources.select! do |_, r|
    _filter_reduce(r, filters)
  end

  resources
end
from_tags(aws_tags) click to toggle source
# File lib/builderator/util.rb, line 20
def from_tags(aws_tags)
  {}.tap { |tt| aws_tags.each { |t| tt[t.key.to_s] = t.value } }
end
get_security_group_id(region = Config.aws.region) click to toggle source
# File lib/builderator/util.rb, line 98
def get_security_group_id(region = Config.aws.region)
  group_id = nil
  if region.nil?
    group_id = 'sg-DRYRUNSG'
    puts "Dry-run; skipping create and returning #{group_id}"
    return group_id
  end
  ec2 = ec2(region)
  group = nil
  require 'open-uri'
  external_ip = open('http://checkip.amazonaws.com').read.strip
  cidr_ip = external_ip + '/32'

  # Create a security group with microsecond timestamp (to avoid collisions when using seconds)
  ts_usec = (Time.now.to_f*1000000).to_i
  Retryable.retryable(:sleep => lambda { |n| 4**n }, :tries => 4, :on => [Aws::EC2::Errors::ServiceError, Aws::EC2::Errors::InternalError]) do |retries, _|
    resp = ec2.create_security_group(group_name: "BuilderatorSecurityGroupSSHOnly-#{ts_usec}",
                                     description: "Created by Builderator at #{Time.now}")
    group_id = resp[:group_id]

    resp = ec2.describe_security_groups(group_ids: [group_id])
    groups = resp[:security_groups]
    group = groups.first

    # Ensure the group_id has the right permissions
    resp = ec2.authorize_security_group_ingress(group_id: group_id,
                                                ip_protocol: 'tcp',
                                                from_port: 22,
                                                to_port: 22,
                                                cidr_ip: cidr_ip)
  end
  puts "Created SecurityGroup #{group_id}"
  group_id
end
relative_path(*relative) click to toggle source

Relative path from working directory

# File lib/builderator/util.rb, line 27
def relative_path(*relative)
  Pathname.pwd.join(*(relative.flatten.map(&:to_s))).expand_path
end
remove_security_group(region = Config.aws.region, group_id = nil) click to toggle source
# File lib/builderator/util.rb, line 82
def remove_security_group(region = Config.aws.region, group_id = nil)
  if region.nil?
    puts "Dry-run; skipping delete of group_id #{group_id}"
    return
  end
  if group_id.nil?
    puts "Not removing security group"
    return
  end
  ec2 = ec2(region)
  Retryable.retryable(:sleep => lambda { |n| 4**n }, :tries => 4, :on => [Aws::EC2::Errors::ServiceError, Aws::EC2::Errors::InternalError]) do |retries, _|
    resp = ec2.delete_security_group(group_id: group_id)
  end
  puts "Deleted SecurityGroup #{group_id}"
end
source_path(*relative) click to toggle source
# File lib/builderator/util.rb, line 39
def source_path(*relative)
  GEM_PATH.join(*(relative.flatten.map(&:to_s))).expand_path
end
to_array(arg) click to toggle source

Transform helpers

# File lib/builderator/util.rb, line 16
def to_array(arg)
  arg.is_a?(Array) ? arg : [arg]
end
vendor(*relative) click to toggle source
# File lib/builderator/util.rb, line 35
def vendor(*relative)
  workspace(VENDOR, relative)
end
workspace(*relative) click to toggle source
# File lib/builderator/util.rb, line 31
def workspace(*relative)
  relative_path(WORKSPACE, relative)
end

Private Class Methods

_filter_reduce(resource, filters) click to toggle source
# File lib/builderator/util.rb, line 139
def _filter_reduce(resource, filters)
  filters.reduce(true) do |memo, (k, v)|
    ## Allow for negation with a leading `~`
    if v[0] == '~'
      memo && (!resource[:properties].include?(k.to_s) || resource[:properties][k.to_s] != v[1..-1])
    else
      memo && resource[:properties].include?(k.to_s) && resource[:properties][k.to_s] == v
    end
  end
end
clients() click to toggle source
# File lib/builderator/util.rb, line 135
def clients
  @clients ||= {}
end