hako 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45f04940c4d0ff4ff960ea9619a525085aba342d
4
- data.tar.gz: 67610aa17c6877ed84599ecd1be143c4c39d6a03
3
+ metadata.gz: 3dfc4cedaae2deaccaf4cc32a0f794470f74de82
4
+ data.tar.gz: 46ede129bf2e3c7a89ce46bf62106e13c7e56b38
5
5
  SHA512:
6
- metadata.gz: 92407332bca376bcba4e54248842d685568410cf92c4cb19aa7be30d1353a266e549db55da77b3f12c1cc7b5161a78de6edc0e0fc33a2f322822bb937870dbd2
7
- data.tar.gz: 2599f5133827e410fb4cc2d068fe7fc09e52b654022f140549ab34abc620cade3bc111dae8c8bb861ec60c89768dc9e6e57eb13b89b8dc929dc50e8a0746d7e9
6
+ metadata.gz: 02d2c29d73ea3bd2eced9c916e6de1dffa8f0f3c38fb2f9e3dee672e2b1281f61ed9cf37984058a17361980abffe2045f284087fdc7b21173ee86258fa7aec54
7
+ data.tar.gz: 723a2e83cc6ebf0825a7a4dc08803ed8aef02da28e6c4bb5e35c37885b52fd1060b62a7362e90b831ad527f824fc4d1a6090457949bc0683d0a80c8f3fe814fc
@@ -0,0 +1,46 @@
1
+ scheduler:
2
+ type: ecs
3
+ region: ap-northeast-1
4
+ cluster: eagletmt
5
+ desired_count: 2
6
+ role: ecsServiceRole
7
+ # dynamic_port_mapping is enabled by default with elb_v2
8
+ # dynamic_port_mapping: false
9
+ elb_v2:
10
+ # VPC id where the target group is located
11
+ vpc_id: vpc-WWWWWWWW
12
+ # Health check path of the target group
13
+ health_check_path: /site/sha
14
+ listeners:
15
+ - port: 80
16
+ protocol: HTTP
17
+ - port: 443
18
+ protocol: HTTPS
19
+ certificate_arn: arn:aws:iam::012345678901:server-certificate/hello-lb-v2.example.com
20
+ subnets:
21
+ - subnet-XXXXXXXX
22
+ - subnet-YYYYYYYY
23
+ security_groups:
24
+ - sg-ZZZZZZZZ
25
+ app:
26
+ image: ryotarai/hello-sinatra
27
+ memory: 128
28
+ cpu: 256
29
+ env:
30
+ $providers:
31
+ - type: file
32
+ path: hello.env
33
+ PORT: 3000
34
+ MESSAGE: '#{username}-san'
35
+ additional_containers:
36
+ front:
37
+ image_tag: hako-nginx
38
+ memory: 32
39
+ cpu: 32
40
+ scripts:
41
+ - <<: !include front.yml
42
+ backend_port: 3000
43
+ locations:
44
+ /:
45
+ allow_only_from:
46
+ - 10.0.0.0/24
@@ -4,6 +4,7 @@ scheduler:
4
4
  cluster: eagletmt
5
5
  desired_count: 2
6
6
  role: ecsServiceRole
7
+ # dynamic_port_mapping cannot be enabled with elb
7
8
  elb:
8
9
  listeners:
9
10
  - load_balancer_port: 80
@@ -4,6 +4,8 @@ scheduler:
4
4
  cluster: eagletmt
5
5
  desired_count: 2
6
6
  task_role_arn: arn:aws:iam::012345678901:role/HelloRole
7
+ # dynamic_port_mapping is enabled by default with no ELB option
8
+ # dynamic_port_mapping: false
7
9
  app:
8
10
  image: ryotarai/hello-sinatra
9
11
  memory: 128
@@ -5,6 +5,7 @@ require 'hako/error'
5
5
  require 'hako/scheduler'
6
6
  require 'hako/schedulers/ecs_definition_comparator'
7
7
  require 'hako/schedulers/ecs_elb'
8
+ require 'hako/schedulers/ecs_elb_v2'
8
9
  require 'hako/schedulers/ecs_autoscaling'
9
10
 
10
11
  module Hako
@@ -26,6 +27,11 @@ module Hako
26
27
  @role = options.fetch('role', nil)
27
28
  @task_role_arn = options.fetch('task_role_arn', nil)
28
29
  @ecs_elb_options = options.fetch('elb', nil)
30
+ @ecs_elb_v2_options = options.fetch('elb_v2', nil)
31
+ if @ecs_elb_options && @ecs_elb_v2_options
32
+ validation_error!('Cannot specify both elb and elb_v2')
33
+ end
34
+ @dynamic_port_mapping = options.fetch('dynamic_port_mapping', @ecs_elb_options.nil?)
29
35
  if options.key?('autoscaling')
30
36
  @autoscaling = EcsAutoscaling.new(options.fetch('autoscaling'), dry_run: @dry_run)
31
37
  end
@@ -154,13 +160,8 @@ module Hako
154
160
  end
155
161
 
156
162
  unless service.load_balancers.empty?
157
- lb = service.load_balancers[0]
158
- lb_detail = ecs_elb_client.describe_load_balancer
159
163
  puts 'Load balancer:'
160
- lb_detail.listener_descriptions.each do |ld|
161
- l = ld.listener
162
- puts " #{lb_detail.dns_name}:#{l.load_balancer_port} -> #{lb.container_name}:#{lb.container_port}"
163
- end
164
+ ecs_elb_client.show_status(service.load_balancers[0])
164
165
  end
165
166
 
166
167
  puts 'Deployments:'
@@ -245,9 +246,14 @@ module Hako
245
246
  @ec2_client ||= Aws::EC2::Client.new(region: @region)
246
247
  end
247
248
 
248
- # @return [EcsElb]
249
+ # @return [EcsElb, EcsElbV2]
249
250
  def ecs_elb_client
250
- @ecs_elb_client ||= EcsElb.new(@app_id, Aws::ElasticLoadBalancing::Client.new(region: @region), @ecs_elb_options, dry_run: @dry_run)
251
+ @ecs_elb_client ||=
252
+ if @ecs_elb_options
253
+ EcsElb.new(@app_id, @region, @ecs_elb_options, dry_run: @dry_run)
254
+ else
255
+ EcsElbV2.new(@app_id, @region, @ecs_elb_v2_options, dry_run: @dry_run)
256
+ end
251
257
  end
252
258
 
253
259
  # @return [Aws::ECS::Types::Service, nil]
@@ -260,6 +266,9 @@ module Hako
260
266
 
261
267
  # @return [Fixnum]
262
268
  def determine_front_port
269
+ if @dynamic_port_mapping
270
+ return 0
271
+ end
263
272
  if @dry_run
264
273
  return DEFAULT_FRONT_PORT
265
274
  end
@@ -555,14 +564,9 @@ module Hako
555
564
  desired_count: @desired_count,
556
565
  role: @role,
557
566
  }
558
- name = ecs_elb_client.find_or_create_load_balancer(front_port)
559
- if name
567
+ if ecs_elb_client.find_or_create_load_balancer(front_port)
560
568
  params[:load_balancers] = [
561
- {
562
- load_balancer_name: name,
563
- container_name: 'front',
564
- container_port: 80,
565
- },
569
+ @ecs_elb_client.load_balancer_params_for_service.merge(container_name: 'front', container_port: 80),
566
570
  ]
567
571
  end
568
572
  ecs_client.create_service(params).service
@@ -6,47 +6,59 @@ module Hako
6
6
  module Schedulers
7
7
  class EcsElb
8
8
  # @param [String] app_id
9
- # @param [Aws::ElasticLoadBalancing::Client] elb
9
+ # @param [String] region
10
10
  # @param [Hash] elb_config
11
11
  # @param [Boolean] dry_run
12
- def initialize(app_id, elb, elb_config, dry_run:)
12
+ def initialize(app_id, region, elb_config, dry_run:)
13
13
  @app_id = app_id
14
- @elb = elb
14
+ @elb = Aws::ElasticLoadBalancing::Client.new(region: region)
15
15
  @elb_config = elb_config
16
16
  @dry_run = dry_run
17
17
  end
18
18
 
19
+ # @param [Aws::ECS::Types::LoadBalancer] ecs_lb
20
+ # @return [nil]
21
+ def show_status(ecs_lb)
22
+ lb = describe_load_balancer
23
+ lb.listener_descriptions.each do |ld|
24
+ l = ld.listener
25
+ puts " #{lb.dns_name}:#{l.load_balancer_port} -> #{ecs_lb.container_name}:#{ecs_lb.container_port}"
26
+ end
27
+ end
28
+
19
29
  # @return [Aws::ElasticLoadBalancing::Types::LoadBalancerDescription]
20
30
  def describe_load_balancer
21
31
  @elb.describe_load_balancers(load_balancer_names: [name]).load_balancer_descriptions[0]
22
32
  end
23
33
 
24
34
  # @param [Fixnum] front_port
25
- # @return [nil]
35
+ # @return [Boolean]
26
36
  def find_or_create_load_balancer(front_port)
27
- if @elb_config
28
- unless exist?
29
- listeners = @elb_config.fetch('listeners').map do |l|
30
- {
31
- protocol: l.fetch('protocol'),
32
- load_balancer_port: l.fetch('load_balancer_port'),
33
- instance_port: front_port,
34
- ssl_certificate_id: l.fetch('ssl_certificate_id', nil),
35
- }
36
- end
37
- tags = @elb_config.fetch('tags', {}).map { |k, v| { key: k, value: v.to_s } }
38
- lb = @elb.create_load_balancer(
39
- load_balancer_name: name,
40
- listeners: listeners,
41
- subnets: @elb_config.fetch('subnets'),
42
- security_groups: @elb_config.fetch('security_groups'),
43
- scheme: @elb_config.fetch('scheme', nil),
44
- tags: tags.empty? ? nil : tags,
45
- )
46
- Hako.logger.info "Created ELB #{lb.dns_name} with instance_port=#{front_port}"
37
+ unless @elb_config
38
+ return false
39
+ end
40
+
41
+ unless exist?
42
+ listeners = @elb_config.fetch('listeners').map do |l|
43
+ {
44
+ protocol: l.fetch('protocol'),
45
+ load_balancer_port: l.fetch('load_balancer_port'),
46
+ instance_port: front_port,
47
+ ssl_certificate_id: l.fetch('ssl_certificate_id', nil),
48
+ }
47
49
  end
48
- name
50
+ tags = @elb_config.fetch('tags', {}).map { |k, v| { key: k, value: v.to_s } }
51
+ lb = @elb.create_load_balancer(
52
+ load_balancer_name: name,
53
+ listeners: listeners,
54
+ subnets: @elb_config.fetch('subnets'),
55
+ security_groups: @elb_config.fetch('security_groups'),
56
+ scheme: @elb_config.fetch('scheme', nil),
57
+ tags: tags.empty? ? nil : tags,
58
+ )
59
+ Hako.logger.info "Created ELB #{lb.dns_name} with instance_port=#{front_port}"
49
60
  end
61
+ true
50
62
  end
51
63
 
52
64
  # @return [nil]
@@ -75,6 +87,11 @@ module Hako
75
87
  def name
76
88
  "hako-#{@app_id}"
77
89
  end
90
+
91
+ # @return [Hash]
92
+ def load_balancer_params_for_service
93
+ { load_balancer_name: name }
94
+ end
78
95
  end
79
96
  end
80
97
  end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+ require 'aws-sdk'
3
+ require 'hako'
4
+
5
+ module Hako
6
+ module Schedulers
7
+ class EcsElbV2
8
+ # @param [String] app_id
9
+ # @param [String] region
10
+ # @param [Hash] elb_v2_config
11
+ # @param [Boolean] dry_run
12
+ def initialize(app_id, region, elb_v2_config, dry_run:)
13
+ @app_id = app_id
14
+ @elb_v2 = Aws::ElasticLoadBalancingV2::Client.new(region: region)
15
+ @elb_v2_config = elb_v2_config
16
+ @dry_run = dry_run
17
+ end
18
+
19
+ # @param [Aws::ECS::Types::LoadBalancer] ecs_lb
20
+ # @return [nil]
21
+ def show_status(ecs_lb)
22
+ lb = describe_load_balancer
23
+ @elb_v2.describe_listeners(load_balancer_arn: lb.load_balancer_arn).each do |page|
24
+ page.listeners.each do |listener|
25
+ puts " #{lb.dns_name}:#{listener.port} -> #{ecs_lb.container_name}:#{ecs_lb.container_port}"
26
+ end
27
+ end
28
+ end
29
+
30
+ # @return [Aws::ElasticLoadBalancingV2::Types::LoadBalancer]
31
+ def describe_load_balancer
32
+ @elb_v2.describe_load_balancers(names: [name]).load_balancers[0]
33
+ rescue Aws::ElasticLoadBalancingV2::Errors::LoadBalancerNotFound
34
+ nil
35
+ end
36
+
37
+ # @return [Aws::ElasticLoadBalancingV2::Types::TargetGroup]
38
+ def describe_target_group
39
+ @elb_v2.describe_target_groups(names: [name]).target_groups[0]
40
+ rescue Aws::ElasticLoadBalancingV2::Errors::TargetGroupNotFound
41
+ nil
42
+ end
43
+
44
+ # @param [Fixnum] front_port
45
+ # @return [nil]
46
+ def find_or_create_load_balancer(_front_port)
47
+ unless @elb_v2_config
48
+ return false
49
+ end
50
+
51
+ load_balancer = describe_load_balancer
52
+ unless load_balancer
53
+ tags = @elb_v2_config.fetch('tags', {}).map { |k, v| { key: k, value: v.to_s } }
54
+ load_balancer = @elb_v2.create_load_balancer(
55
+ name: name,
56
+ subnets: @elb_v2_config.fetch('subnets'),
57
+ security_groups: @elb_v2_config.fetch('security_groups'),
58
+ scheme: @elb_v2_config.fetch('scheme', nil),
59
+ tags: tags.empty? ? nil : tags,
60
+ ).load_balancers[0]
61
+ Hako.logger.info "Created ELBv2 #{load_balancer.dns_name}"
62
+ end
63
+
64
+ target_group = describe_target_group
65
+ unless target_group
66
+ target_group = @elb_v2.create_target_group(
67
+ name: name,
68
+ port: 80,
69
+ protocol: 'HTTP',
70
+ vpc_id: @elb_v2_config.fetch('vpc_id'),
71
+ health_check_path: @elb_v2_config.fetch('health_check_path', nil),
72
+ ).target_groups[0]
73
+ Hako.logger.info "Created target group #{target_group.target_group_arn}"
74
+ end
75
+
76
+ listener_ports = @elb_v2.describe_listeners(load_balancer_arn: load_balancer.load_balancer_arn).flat_map { |page| page.listeners.map(&:port) }
77
+ @elb_v2_config.fetch('listeners').each do |l|
78
+ params = {
79
+ load_balancer_arn: load_balancer.load_balancer_arn,
80
+ protocol: l.fetch('protocol'),
81
+ port: l.fetch('port'),
82
+ default_actions: [{ type: 'forward', target_group_arn: target_group.target_group_arn }],
83
+ }
84
+ certificate_arn = l.fetch('certificate_arn', nil)
85
+ if certificate_arn
86
+ params[:certificates] = [{ certificate_arn: certificate_arn }]
87
+ end
88
+
89
+ unless listener_ports.include?(params[:port])
90
+ listener = @elb_v2.create_listener(params).listeners[0]
91
+ Hako.logger.info("Created listener #{listener.listener_arn}")
92
+ end
93
+ end
94
+
95
+ true
96
+ end
97
+
98
+ # @return [nil]
99
+ def destroy
100
+ load_balancer = describe_load_balancer
101
+ if load_balancer
102
+ if @dry_run
103
+ Hako.logger.info("@elb_v2.delete_load_balancer(load_balancer_arn: #{load_balancer.load_balancer_arn})")
104
+ else
105
+ @elb_v2.delete_load_balancer(load_balancer_arn: load_balancer.load_balancer_arn)
106
+ Hako.logger.info "Deleted ELBv2 #{load_balancer.load_balancer_arn}"
107
+ end
108
+ else
109
+ Hako.logger.info "ELBv2 #{name} doesn't exist"
110
+ end
111
+
112
+ target_group = describe_target_group
113
+ if target_group
114
+ if @dry_run
115
+ Hako.logger.info("@elb_v2.delete_target_group(target_group_arn: #{target_group.target_group_arn})")
116
+ else
117
+ @elb_v2.delete_target_group(target_group_arn: target_group.target_group_arn)
118
+ Hako.logger.info "Deleted target group #{target_group.target_group_arn}"
119
+ end
120
+ end
121
+ end
122
+
123
+ # @return [String]
124
+ def name
125
+ "hako-#{@app_id}"
126
+ end
127
+
128
+ # @return [Hash]
129
+ def load_balancer_params_for_service
130
+ { target_group_arn: describe_target_group.target_group_arn }
131
+ end
132
+ end
133
+ end
134
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Hako
3
- VERSION = '0.19.0'
3
+ VERSION = '0.20.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hako
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-17 00:00:00.000000000 Z
11
+ date: 2016-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -116,6 +116,7 @@ files:
116
116
  - examples/front.yml
117
117
  - examples/hello-autoscaling-group.yml
118
118
  - examples/hello-autoscaling.yml
119
+ - examples/hello-lb-v2.yml
119
120
  - examples/hello-lb.yml
120
121
  - examples/hello.env
121
122
  - examples/hello.yml
@@ -140,6 +141,7 @@ files:
140
141
  - lib/hako/schedulers/ecs_autoscaling.rb
141
142
  - lib/hako/schedulers/ecs_definition_comparator.rb
142
143
  - lib/hako/schedulers/ecs_elb.rb
144
+ - lib/hako/schedulers/ecs_elb_v2.rb
143
145
  - lib/hako/script.rb
144
146
  - lib/hako/scripts.rb
145
147
  - lib/hako/scripts/nginx_front.rb