Module: Builderator::Util

Defined in:
lib/builderator/util.rb,
lib/builderator/util/aws_exception.rb,
lib/builderator/util/task_exception.rb,
lib/builderator/util/limit_exception.rb

Overview

Shared helper methods

Defined Under Namespace

Classes: AwsException, LimitException, TaskException

Constant Summary collapse

GEM_PATH =
Pathname.new(__FILE__).join('../../..').expand_path
WORKSPACE =
'.builderator'.freeze
VENDOR =
'vendor'.freeze

Class Method Summary collapse

Class Method Details

.asg(region = Config.aws.region) ⇒ Object



78
79
80
# 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) ⇒ Object

AWS Clients



63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
# 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 = {}) ⇒ Object

Set-filter helpers



46
47
48
49
50
# File 'lib/builderator/util.rb', line 46

def filter(resources, filters = {})
  resources.select do |_, r|
    _filter_reduce(r, filters)
  end
end

.filter!(resources, filters = {}) ⇒ Object



52
53
54
55
56
57
58
# 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) ⇒ Object



20
21
22
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/builderator/util.rb', line 96

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
  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)
  puts "Created SecurityGroup #{group_id}"
  group_id
end

.relative_path(*relative) ⇒ Object

Relative path from working directory



27
28
29
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# 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)
  resp = ec2.delete_security_group(group_id: group_id)
  puts "Deleted SecurityGroup #{group_id}"
end

.source_path(*relative) ⇒ Object



39
40
41
# 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) ⇒ Object

Transform helpers



16
17
18
# File 'lib/builderator/util.rb', line 16

def to_array(arg)
  arg.is_a?(Array) ? arg : [arg]
end

.vendor(*relative) ⇒ Object



35
36
37
# File 'lib/builderator/util.rb', line 35

def vendor(*relative)
  workspace(VENDOR, relative)
end

.workspace(*relative) ⇒ Object



31
32
33
# File 'lib/builderator/util.rb', line 31

def workspace(*relative)
  relative_path(WORKSPACE, relative)
end