hako 0.21.1 → 0.22.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: 9bb0e925130947e9060b87f35a2b8f05c5e7ba5e
4
- data.tar.gz: 651f08761326f2d6e256adf5a5acdf36536d0a82
3
+ metadata.gz: f7e905ba26a5f96fbc2aa0e452d7802a6253b293
4
+ data.tar.gz: eb043950033d3d9367cbfcc57704238fd9a0f645
5
5
  SHA512:
6
- metadata.gz: b1f0410687bb493c1138be744019ac91b41636d35e451e11f21ad69b3ba142f123cddaadd2c9db722b9b6deceeba9773b21dbd4116dc5a829ef9f30ae1a6292d
7
- data.tar.gz: ed716994d7a904addc260be274d883d5c9df09313f0c664975c2c7337f650509e6e2dfaf903446a0606b6d9a8fe8e77f8037386480a0f0b41652a930797525ef
6
+ metadata.gz: 2b259b71edbdfe3fa4e29894cfcb3d70a4cbbb316943701a511083a065d172398bd29846e1b06d437c315c81a56328631598894bee4abfbfccdf20a4036d544f
7
+ data.tar.gz: 8b7d739da8bbefb071376e82fec854b96e1b093bf4b24acdec4289e1e364a72c5a666467f8917cf886752be77b28bd7988301889d3cc28441cf40c6bb58f82fe
data/.rubocop.yml CHANGED
@@ -4,6 +4,9 @@ AllCops:
4
4
  DisplayCopNames: true
5
5
  TargetRubyVersion: 2.3
6
6
 
7
+ Style/Alias:
8
+ EnforcedStyle: prefer_alias_method
9
+
7
10
  Style/GuardClause:
8
11
  Enabled: false
9
12
 
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,9 @@
1
1
  Metrics/AbcSize:
2
2
  Enabled: false
3
3
 
4
+ Metrics/BlockLength:
5
+ Enabled: false
6
+
4
7
  Metrics/ClassLength:
5
8
  Enabled: false
6
9
 
@@ -0,0 +1 @@
1
+ type: create_aws_cloud_watch_logs_log_group
@@ -14,3 +14,5 @@ app:
14
14
  awslogs-group: my-logs
15
15
  awslogs-region: ap-northeast-1
16
16
  awslogs-stream-prefix: example
17
+ scripts:
18
+ - <<: !include create_aws_cloud_watch_logs_log_group.yml
@@ -22,8 +22,7 @@ module Hako
22
22
  end
23
23
 
24
24
  # @param [Hash] _options
25
- def configure(_options)
26
- end
25
+ def configure(_options); end
27
26
 
28
27
  # @param [Hash<String, Container>] _containers
29
28
  def deploy(_containers)
@@ -3,10 +3,11 @@ require 'aws-sdk'
3
3
  require 'hako'
4
4
  require 'hako/error'
5
5
  require 'hako/scheduler'
6
+ require 'hako/schedulers/ecs_autoscaling'
6
7
  require 'hako/schedulers/ecs_definition_comparator'
7
8
  require 'hako/schedulers/ecs_elb'
8
9
  require 'hako/schedulers/ecs_elb_v2'
9
- require 'hako/schedulers/ecs_autoscaling'
10
+ require 'hako/schedulers/ecs_service_comparator'
10
11
 
11
12
  module Hako
12
13
  module Schedulers
@@ -565,14 +566,7 @@ module Hako
565
566
  # @return [nil]
566
567
  def report_container_instance(container_instance_arn)
567
568
  container_instance = ecs_client.describe_container_instances(cluster: @cluster, container_instances: [container_instance_arn]).container_instances[0]
568
- ec2_client.describe_tags(filters: [{ name: 'resource-id', values: [container_instance.ec2_instance_id] }]).each do |page|
569
- tag = page.tags.find { |t| t.key == 'Name' }
570
- if tag
571
- Hako.logger.info "Container instance is #{container_instance_arn} (#{tag.value} #{container_instance.ec2_instance_id})"
572
- else
573
- Hako.logger.info "Container instance is #{container_instance_arn} (#{container_instance.ec2_instance_id})"
574
- end
575
- end
569
+ Hako.logger.info "Container instance is #{container_instance_arn} (#{container_instance.ec2_instance_id})"
576
570
  end
577
571
 
578
572
  # @param [String] task_definition_arn
@@ -615,23 +609,11 @@ module Hako
615
609
  end
616
610
  end
617
611
 
618
- SERVICE_KEYS = %i[desired_count task_definition].freeze
619
-
620
612
  # @param [Aws::ECS::Types::Service] service
621
613
  # @param [Hash] params
622
614
  # @return [Boolean]
623
615
  def service_changed?(service, params)
624
- SERVICE_KEYS.each do |key|
625
- if service.public_send(key) != params[key]
626
- return true
627
- end
628
- end
629
- params[:deployment_configuration].each do |key, val|
630
- if val && val != service.deployment_configuration.public_send(key)
631
- return true
632
- end
633
- end
634
- false
616
+ EcsServiceComparator.new(params).different?(service)
635
617
  end
636
618
 
637
619
  # @param [Aws::ECS::Types::Service] service
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require 'hako/schema'
3
+
4
+ module Hako
5
+ module Schedulers
6
+ class EcsServiceComparator
7
+ def initialize(expected_service)
8
+ @expected_service = expected_service
9
+ @schema = service_schema
10
+ end
11
+
12
+ # @param [Aws::ECS::Types::Service] actual_service
13
+ # @return [Boolean]
14
+ def different?(actual_service)
15
+ !@schema.same?(actual_service.to_h, @expected_service)
16
+ end
17
+
18
+ private
19
+
20
+ def service_schema
21
+ Schema::Structure.new.tap do |struct|
22
+ struct.member(:desired_count, Schema::Integer.new)
23
+ struct.member(:task_definition, Schema::String.new)
24
+ struct.member(:deployment_configuration, Schema::WithDefault.new(deployment_configuration_schema, default_configuration))
25
+ end
26
+ end
27
+
28
+ def deployment_configuration_schema
29
+ Schema::Structure.new.tap do |struct|
30
+ struct.member(:maximum_percent, Schema::Integer.new)
31
+ struct.member(:minimum_healthy_percent, Schema::Integer.new)
32
+ end
33
+ end
34
+
35
+ def default_configuration
36
+ {
37
+ maximum_percent: 200,
38
+ minimum_healthy_percent: 100,
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ module Hako
3
+ module Schema
4
+ class WithDefault
5
+ def initialize(schema, default)
6
+ @schema = schema
7
+ @default = default
8
+ end
9
+
10
+ def valid?(object)
11
+ object.nil? || @schema.valid?(object)
12
+ end
13
+
14
+ def same?(x, y)
15
+ @schema.same?(wrap(x), wrap(y))
16
+ end
17
+
18
+ private
19
+
20
+ def wrap(x)
21
+ x.nil? ? @default : x
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/hako/schema.rb CHANGED
@@ -7,3 +7,4 @@ require 'hako/schema/string'
7
7
  require 'hako/schema/structure'
8
8
  require 'hako/schema/table'
9
9
  require 'hako/schema/unordered_array'
10
+ require 'hako/schema/with_default'
data/lib/hako/script.rb CHANGED
@@ -13,37 +13,29 @@ module Hako
13
13
  end
14
14
 
15
15
  # @param [Hash<String, Container>] _containers
16
- def deploy_starting(_containers)
17
- end
16
+ def deploy_starting(_containers); end
18
17
 
19
18
  # @param [Hash<String, Container>] _containers
20
19
  # @param [Fixnum] _front_port
21
- def deploy_started(_containers, _front_port)
22
- end
20
+ def deploy_started(_containers, _front_port); end
23
21
 
24
22
  # @param [Hash<String, Container>] _containers
25
- def deploy_finished(_containers)
26
- end
23
+ def deploy_finished(_containers); end
27
24
 
28
25
  # @param [Hash<String, Container>] _containers
29
- def oneshot_starting(_containers)
30
- end
26
+ def oneshot_starting(_containers); end
31
27
 
32
28
  # @param [Scheduler] _scheduler
33
- def oneshot_started(_scheduler)
34
- end
29
+ def oneshot_started(_scheduler); end
35
30
 
36
31
  # @param [Hash<String, Container>] _containers
37
- def oneshot_finished(_containers)
38
- end
32
+ def oneshot_finished(_containers); end
39
33
 
40
- def after_remove
41
- end
34
+ def after_remove; end
42
35
 
43
36
  private
44
37
 
45
38
  # @param [Hash] _options
46
- def configure(_options)
47
- end
39
+ def configure(_options); end
48
40
  end
49
41
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ require 'aws-sdk'
3
+ require 'hako'
4
+ require 'hako/script'
5
+
6
+ module Hako
7
+ module Scripts
8
+ class CreateAwsCloudWatchLogsLogGroup < Script
9
+ # @param [Hash<String, Container>] containers
10
+ # @return [nil]
11
+ def deploy_starting(containers)
12
+ containers.each_value do |container|
13
+ log_configuration = container.log_configuration
14
+ unless log_configuration
15
+ next
16
+ end
17
+
18
+ if log_configuration[:log_driver] == 'awslogs'
19
+ create_log_group_if_not_exist(log_configuration.fetch(:options))
20
+ end
21
+ end
22
+ end
23
+
24
+ alias_method :oneshot_starting, :deploy_starting
25
+
26
+ private
27
+
28
+ # @param [Hash] options
29
+ # @return [nil]
30
+ def create_log_group_if_not_exist(options)
31
+ group = options.fetch('awslogs-group')
32
+ region = options.fetch('awslogs-region')
33
+
34
+ unless log_group_exist?(group, region: region)
35
+ cloudwatch_logs(region).create_log_group(log_group_name: group)
36
+ Hako.logger.info "Created CloudWatch log group #{group} in #{region}"
37
+ end
38
+ end
39
+
40
+ # @param [String] region
41
+ # @return [Aws::CloudWatchLogs::Client]
42
+ def cloudwatch_logs(region)
43
+ @cloudwatch_logs ||= {}
44
+ @cloudwatch_logs[region] ||= Aws::CloudWatchLogs::Client.new(region: region)
45
+ end
46
+
47
+ # @param [String] group
48
+ # @param [String] region
49
+ # @return [Boolean]
50
+ def log_group_exist?(group, region:)
51
+ cloudwatch_logs(region).describe_log_groups(log_group_name_prefix: group).any? do |page|
52
+ page.log_groups.any? { |log_group| log_group.log_group_name == group }
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/hako/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Hako
3
- VERSION = '0.21.1'
3
+ VERSION = '0.22.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.21.1
4
+ version: 0.22.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-10-11 00:00:00.000000000 Z
11
+ date: 2016-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -113,6 +113,7 @@ files:
113
113
  - Rakefile
114
114
  - bin/console
115
115
  - bin/setup
116
+ - examples/create_aws_cloud_watch_logs_log_group.yml
116
117
  - examples/front.yml
117
118
  - examples/hello-autoscaling-group.yml
118
119
  - examples/hello-autoscaling.yml
@@ -145,6 +146,7 @@ files:
145
146
  - lib/hako/schedulers/ecs_definition_comparator.rb
146
147
  - lib/hako/schedulers/ecs_elb.rb
147
148
  - lib/hako/schedulers/ecs_elb_v2.rb
149
+ - lib/hako/schedulers/ecs_service_comparator.rb
148
150
  - lib/hako/schema.rb
149
151
  - lib/hako/schema/boolean.rb
150
152
  - lib/hako/schema/integer.rb
@@ -154,8 +156,10 @@ files:
154
156
  - lib/hako/schema/structure.rb
155
157
  - lib/hako/schema/table.rb
156
158
  - lib/hako/schema/unordered_array.rb
159
+ - lib/hako/schema/with_default.rb
157
160
  - lib/hako/script.rb
158
161
  - lib/hako/scripts.rb
162
+ - lib/hako/scripts/create_aws_cloud_watch_logs_log_group.rb
159
163
  - lib/hako/scripts/nginx_front.rb
160
164
  - lib/hako/templates/nginx.conf.erb
161
165
  - lib/hako/templates/nginx.location.conf.erb