Module: Inf::AWS

Defined in:
lib/aws.rb

Class Method Summary collapse

Class Method Details

.bootstrap_script(app_name, state_bucket, fleet_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aws.rb', line 22

def self.bootstrap_script(app_name, state_bucket, fleet_name)
  <<BOOTSTRAP_SCRIPT
#!/bin/bash

cd /tmp

cat > bootstrap-env <<EOF
export APP_NAME=#{app_name}
export STATE_BUCKET=#{state_bucket}
export FLEET_NAME=#{fleet_name}
export AWS_REGION=#{ENV['AWS_REGION']}
export JENKINS_PUBLIC_KEY='#{ENV['JENKINS_PUBLIC_KEY']}'
EOF

. ./bootstrap-env

aws s3 cp s3://$STATE_BUCKET/apps/$APP_NAME/bootstrap-instance.sh ./bootstrap.sh
chmod +x bootstrap.sh

( setsid ./bootstrap.sh 2>&1 | setsid logger -t bootstrap ) & disown

BOOTSTRAP_SCRIPT
end

.ec2Object



6
7
8
# File 'lib/aws.rb', line 6

def self.ec2
  @ec2 ||= Aws::EC2::Client.new
end

.elbObject



10
11
12
# File 'lib/aws.rb', line 10

def self.elb
  @elb ||= Aws::ElasticLoadBalancing::Client.new
end

.launch_load_balancerObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/aws.rb', line 46

def self.launch_load_balancer
  lb_name = "app-lb-#{app_name}"
  cert = ENV['CERT_ARN']

  listeners = [{
    instance_port: 5000,
    instance_protocol: 'HTTP',
    load_balancer_port: 80,
    protocol: 'HTTP'
  }]

  cert && listeners.push(
    instance_port: 5000,
    instance_protocol: 'HTTP',
    load_balancer_port: 443,
    protocol: 'HTTPS',
    ssl_certificate_id: cert
  )

  lb_dns = elb.create_load_balancer(
    load_balancer_name: lb_name,
    subnets: ENV['SUBNET_IDS'].split(','),
    security_groups: [ENV['ELB_SG']],
    scheme: 'internet-facing',
    listeners: listeners
  ).dns_name

  elb.configure_health_check(
    load_balancer_name: lb_name,
    health_check: {
      target: 'HTTP:5000/health_check',
      interval: 10, # seconds
      timeout: 5, # seconds to respond
      unhealthy_threshold: 2, # two consecutive failures
      healthy_threshold: 2
    }
  )

  put_state("apps/#{app_name}/lb-name", lb_name)
  put_state("apps/#{app_name}/lb-dns", lb_dns)

  puts 'Launched load balancer (or it was running)'.blue
end

.method_missing(m, *args, &block) ⇒ Object



2
3
4
# File 'lib/aws.rb', line 2

def self.method_missing(m, *args, &block)
  Inf.send(m, *args, &block)
end

.request_spot_fleet(fleet_name = 'default') ⇒ Object



90
91
92
93
94
95
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/aws.rb', line 90

def self.request_spot_fleet(fleet_name='default')
  launch_specifications = ENV['SUBNET_IDS'].split(',').map do |subnet_id|
    ENV['INSTANCE_TYPES'].split(',').map do |type|
      {
        image_id: ENV['AMI'],
        key_name: ENV['KEY_NAME'],
        instance_type: type,
        monitoring: {
          enabled: false,
        },
        network_interfaces: [{
          associate_public_ip_address: true,
          device_index: 0,
          groups: [ENV['SG']],
          subnet_id: subnet_id
        }],
        iam_instance_profile: {
          name: ENV['INSTANCE_PROFILE_NAME'],
        },
        # ebs_optimized: type == 'm1.small' ? false : false,
        weighted_capacity: 1.0,
        user_data: Base64.strict_encode64(bootstrap_script(app_name, state_bucket, fleet_name))
      }
    end
  end.flatten

  tag_specifications = [{
    tags: [{
      key: 'app_name',
      value: app_name
    }, {
      key: 'fleet_name',
      value: fleet_name
    }]
  }]

  resp = ec2.request_spot_fleet(
    spot_fleet_request_config: {
      spot_price: ENV['SPOT_PRICE'],
      target_capacity: ENV['TARGET_CAPACITY'],
      valid_from: Time.now,
      valid_until: Time.parse('1/1/2050'),
      terminate_instances_with_expiration: ENV['TERMINATE_INSTANCES'],
      iam_fleet_role: ENV['FLEET_ROLE'],
      launch_specifications: launch_specifications,
      # tag_specifications: tag_specifications,
      excess_capacity_termination_policy: 'default', # accepts noTermination, default
      allocation_strategy: 'diversified', # accepts lowestPrice, diversified
      type: 'maintain', # accepts request, maintain
      replace_unhealthy_instances: true,
    },
  )

  resp.spot_fleet_request_id.tap do |sfr|
    puts "Launched spot fleet #{fleet_name} (#{sfr})".blue
    put_state("apps/#{app_name}/fleets/#{fleet_name}/sfr", sfr)
  end
end

.route53Object



18
19
20
# File 'lib/aws.rb', line 18

def self.route53
  @route53 ||= Aws::Route53::Client.new
end

.s3Object



14
15
16
# File 'lib/aws.rb', line 14

def self.s3
  @s3 ||= Aws::S3::Client.new
end