ecs_deployer 0.1.3 → 0.1.4

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: 63e2ad46f9cab7f78fddf9952e32b456f798d039
4
- data.tar.gz: 89501da0f01b0ef5a60314771142a18df880cde2
3
+ metadata.gz: 47b8f97fb9482d7736def886976477700742787b
4
+ data.tar.gz: 4d9f5cf0333818a5df1482ece0fc06ead1b9abb0
5
5
  SHA512:
6
- metadata.gz: 3c7628b60938925607c4088061bef7e01b463080271e39b1cc443769860ee12393ff03d0208911d4a8c07940a3a03d528524f13bc02add8aea81463139cbad8a
7
- data.tar.gz: e63fa09d2794e0d25fda1e3c1b9ed0a6ab5ba33204a22eb1cea65949193cdd3797ea5f4310a2b38a2d40366ebb2dc74fe3ea399a91583fab40d7985624a76104
6
+ metadata.gz: 13d158aa62c0e9e9ffa17f19b6a8c1d4b676e3144f2db0294064f69f0e93e684c7dafa79f936a3eec96a82e45de57733b1fe9872af31460155524aca7991416c
7
+ data.tar.gz: 9c6a7a38501ca9709762bfd0689bdcea6baac07896504fdd2700a89427f3f772641328227efd8f36ca6133c6e0514bf7ed032f73568deb5d5e1683fd34d340b0
@@ -6,15 +6,16 @@ module EcsDeployer
6
6
  class Client
7
7
  PAULING_INTERVAL = 20
8
8
 
9
- # @param [String] cluster_name
9
+ # @param [String] cluster
10
10
  # @param [Hash] options
11
11
  # @option options [String] :profile
12
12
  # @option options [String] :region
13
13
  # @return [EcsDeployer::Client]
14
- def initialize(cluster_name, options = {})
15
- @cluster_name = cluster_name
16
- @ecs_command = Commander.new(cluster_name, options)
17
- @family_name = ''
14
+ def initialize(cluster, options = {})
15
+ @cluster = cluster
16
+ @runtime = RuntimeCommand::Builder.new
17
+ @ecs_command = Commander.new(@runtime, options)
18
+ @family = ''
18
19
  @revision = ''
19
20
  @new_task_definition_arn = ''
20
21
  end
@@ -29,42 +30,43 @@ module EcsDeployer
29
30
  # @param [Hash] task_hash
30
31
  # @return [String]
31
32
  def register_task_hash(task_hash)
32
- result = @ecs_command.register_task_definition(
33
- task_hash['family'],
34
- task_hash['containerDefinitions']
35
- )
33
+ result = @ecs_command.register_task_definition(task_hash['family'], task_hash['containerDefinitions'])
36
34
 
37
- @family_name = result['taskDefinition']['family']
35
+ @family = result['taskDefinition']['family']
38
36
  @revision = result['taskDefinition']['revision']
39
37
  @new_task_definition_arn = result['taskDefinition']['taskDefinitionArn']
40
38
  end
41
39
 
42
- # @param [String] service_name
40
+ # @param [String] service
43
41
  # @return [String]
44
- def register_clone_task(service_name)
42
+ def register_clone_task(service)
45
43
  detected_service = false
46
44
 
47
- result = @ecs_command.describe_services(service_name)
48
- result['services'].each do |service|
49
- if service['serviceName'] == service_name
50
- result = @ecs_command.describe_task_definition(service['taskDefinition'])
51
- @new_task_definition_arn = register_task_process(result['taskDefinition'])
45
+ result = @ecs_command.describe_services([service], { 'cluster': @cluster })
46
+ result['services'].each do |svc|
47
+ if svc['serviceName'] == service
48
+ result = @ecs_command.describe_task_definition(svc['taskDefinition'])
49
+ @new_task_definition_arn = register_task_hash(result['taskDefinition'])
52
50
  detected_service = true
53
51
  break
54
52
  end
55
53
  end
56
54
 
57
- raise ServiceNotFoundError.new("'#{service_name}' service is not found.") unless detected_service
55
+ raise ServiceNotFoundError.new("'#{service}' service is not found.") unless detected_service
58
56
 
59
57
  @new_task_definition_arn
60
58
  end
61
59
 
62
- # @param [String] service_name
60
+ # @param [String] service
63
61
  # @param [Fixnum] timeout
64
- def update_service(service_name, wait = true, timeout = 300)
65
- register_clone_task(service_name) if @new_task_definition_arn.empty?
66
- @ecs_command.update_service(service_name, @family_name, @revision)
67
- wait_for_deploy(service_name, timeout) if wait
62
+ def update_service(service, wait = true, timeout = 600)
63
+ register_clone_task(service) if @new_task_definition_arn.empty?
64
+ options = {
65
+ 'cluster': @cluster,
66
+ 'task-definition': @family + ':' + @revision.to_s
67
+ }
68
+ @ecs_command.update_service(service, options)
69
+ wait_for_deploy(service, timeout) if wait
68
70
  end
69
71
 
70
72
  # @return [String]
@@ -73,60 +75,66 @@ module EcsDeployer
73
75
  end
74
76
 
75
77
  private
76
- def wait_for_deploy(service_name, timeout)
78
+ def wait_for_deploy(service, timeout)
77
79
  detected_service = false
78
80
 
79
- result = @ecs_command.describe_services(service_name)
80
- result['services'].each do |service|
81
- next unless service['serviceName'] == service_name
81
+ result = @ecs_command.describe_services([service], { 'cluster': @cluster })
82
+ result['services'].each do |svc|
83
+ next unless svc['serviceName'] == service
82
84
  detected_service = true
83
85
 
84
- result = @ecs_command.describe_task_definition(service['taskDefinition'])
86
+ result = @ecs_command.describe_task_definition(svc['taskDefinition'])
85
87
 
86
- if service['desiredCount'] > 0
88
+ if svc['desiredCount'] > 0
87
89
  running_new_task = false
88
90
  wait_time = 0
89
- puts 'Start deploing...'
91
+ @runtime.puts 'Start deploing...'
90
92
 
91
93
  begin
92
94
  sleep(PAULING_INTERVAL)
93
95
  wait_time += PAULING_INTERVAL
94
96
 
95
97
  # Get current tasks
96
- result = @ecs_command.list_tasks(service_name)
98
+ options = {
99
+ 'cluster': @cluster,
100
+ 'service-name': service,
101
+ 'desired-status': 'RUNNING'
102
+ }
103
+ result = @ecs_command.list_tasks(options)
97
104
 
98
- if result['taskArns'].size > 0
99
- success_count = 0
105
+ raise TaskNotFoundError.new('Desired count is 0.') if result['taskArns'].size == 0
100
106
 
101
- result = @ecs_command.describe_tasks(result['taskArns'])
102
- result['tasks'].each do |task|
103
- success_count += 1 if @new_task_definition_arn == task['taskDefinitionArn']
104
- end
105
-
106
- if result['tasks'].size == success_count
107
- puts 'Service update succeeded.'
108
- puts "New task definition: #{@new_task_definition_arn}"
107
+ new_running_count = 0
108
+ result = @ecs_command.describe_tasks(result['taskArns'], { 'cluster': @cluster })
109
109
 
110
- running_new_task = true
111
- end
112
- else
113
- raise TaskNotFoundError.new('Desired count is 0.')
110
+ result['tasks'].each do |task|
111
+ new_running_count += 1 if @new_task_definition_arn == task['taskDefinitionArn']
114
112
  end
115
113
 
116
- if wait_time > timeout
117
- puts "New task definition: #{@new_task_definition_arn}"
118
- raise DeployTimeoutError.new('Service is being updating, but process is timed out.')
119
- end
114
+ current_running_count = result['tasks'].size
120
115
 
121
- puts "Deploying... (#{wait_time} seconds elapsed)"
116
+ if current_running_count == new_running_count
117
+ @runtime.puts "Service update succeeded. [#{new_running_count}/#{current_running_count}]"
118
+ @runtime.puts "New task definition: #{@new_task_definition_arn}"
122
119
 
120
+ running_new_task = true
121
+
122
+ else
123
+ @runtime.puts "Deploying... [#{new_running_count}/#{current_running_count}] (#{wait_time} seconds elapsed)"
124
+ @runtime.puts 'You can stop process with Ctrl+C. Deployment will continue.'
125
+
126
+ if wait_time > timeout
127
+ @runtime.puts "New task definition: #{@new_task_definition_arn}"
128
+ raise DeployTimeoutError.new('Service is being updating, but process is timed out.')
129
+ end
130
+ end
123
131
  end while !running_new_task
124
132
  end
125
133
 
126
134
  break
127
135
  end
128
136
 
129
- raise ServiceNotFoundError.new("'#{service_name}' service is not found.") unless detected_service
137
+ raise ServiceNotFoundError.new("'#{service}' service is not found.") unless detected_service
130
138
  end
131
139
  end
132
140
  end
@@ -1,80 +1,61 @@
1
1
  require 'oj'
2
- require 'runtime_command'
3
2
 
4
3
  module EcsDeployer
5
4
  class Commander
6
- # @param [String] cluster_name
5
+ # @param [RuntimeCommand::Builder] runtime
7
6
  # @param [Hash] options
8
7
  # @return EcsDeployer::Commander
9
- def initialize(cluster_name, options = {})
10
- @runtime = RuntimeCommand::Builder.new
8
+ def initialize(runtime, options = {})
9
+ @runtime = runtime
11
10
  @options = options
12
- @cluster_name = cluster_name
13
11
  end
14
12
 
15
- # @param [String] service_name
16
- # @param [String] family_name
17
- # @param [Fixnum] revision
13
+ # @param [String] service
14
+ # @param [Hash] options
18
15
  # @return [Hash]
19
- def update_service(service_name, family_name, revision)
20
- args = {
21
- 'cluster': @cluster_name,
22
- 'service': service_name,
23
- 'task-definition': family_name + ':' + revision.to_s
24
- }
25
-
26
- exec('update-service', args)
16
+ def update_service(service, options = {})
17
+ options['service'] = service
18
+ exec('update-service', options)
27
19
  end
28
20
 
29
- # @param [String] service_name
21
+ # @param [Hash] options
30
22
  # @return [Hash]
31
- def list_tasks(service_name)
32
- args = {
33
- 'cluster': @cluster_name,
34
- 'service-name': service_name,
35
- 'desired-status': 'RUNNING'
36
- }
37
- exec('list-tasks', args)
23
+ def list_tasks(options = {})
24
+ exec('list-tasks', options)
38
25
  end
39
26
 
40
27
  # @param [Array] tasks
28
+ # @param [Hash] options
41
29
  # @return [Hash]
42
- def describe_tasks(tasks)
43
- args = {
44
- 'cluster': @cluster_name,
45
- 'tasks': tasks.join(' ')
46
- }
47
- exec('describe-tasks', args)
30
+ def describe_tasks(tasks, options = {})
31
+ options['tasks'] = tasks.join(' ')
32
+ exec('describe-tasks', options)
48
33
  end
49
34
 
50
35
  # @param [String] task_definition
36
+ # @param [Hash] options
51
37
  # @return [Hash]
52
- def describe_task_definition(task_definition)
53
- args = {
54
- 'task-definition': task_definition
55
- }
56
- exec('describe-task-definition', args)
38
+ def describe_task_definition(task_definition, options = {})
39
+ options['task-definition'] = task_definition
40
+ exec('describe-task-definition', options)
57
41
  end
58
42
 
59
- # @param [String] service_name
43
+ # @param [Array] services
44
+ # @param [Hash] options
60
45
  # @return [Hash]
61
- def describe_services(service_name)
62
- args = {
63
- 'cluster': @cluster_name,
64
- 'services': service_name
65
- }
66
- exec('describe-services', args)
46
+ def describe_services(services, options = {})
47
+ options['services'] = services.join(' ')
48
+ exec('describe-services', options)
67
49
  end
68
50
 
69
- # @param [String] family_name
51
+ # @param [String] family
70
52
  # @param [Hash] container_definitions
53
+ # @param [Hash] options
71
54
  # @return [Hash]
72
- def register_task_definition(family_name, container_definitions)
73
- args = {
74
- 'family': family_name,
75
- 'container-definitions': '"' + Oj.dump(container_definitions).gsub('"', '\\"') + '"'
76
- }
77
- exec('register-task-definition', args)
55
+ def register_task_definition(family, container_definitions, options = {})
56
+ options['family'] = family
57
+ options['container-definitions'] = '"' + Oj.dump(container_definitions).gsub('"', '\\"') + '"'
58
+ exec('register-task-definition', options)
78
59
  end
79
60
 
80
61
  # @return [String]
@@ -84,24 +65,23 @@ module EcsDeployer
84
65
 
85
66
  private
86
67
  # @param [String] command
87
- # @param [Hash] args
68
+ # @param [Hash] params
88
69
  # @return [Hash]
89
- def exec(command, args)
70
+ def exec(command, params)
90
71
  arg = ''
91
- args.each do |name, value|
72
+ params.each do |name, value|
92
73
  arg << "--#{name} #{value} "
93
74
  end
94
75
 
95
- arg << "--profile #{@options[:profile]} " if @options.has_key?(:profile)
96
- arg << "--region #{@options[:region]} " if @options.has_key?(:region)
76
+ arg << "--profile #{params[:profile]} " if params.has_key?(:profile)
77
+ arg << "--region #{params[:region]} " if params.has_key?(:region)
97
78
 
98
79
  command = "aws ecs #{command} #{arg}"
99
80
  result = @runtime.exec(command)
100
81
 
101
82
  raise CommandError.new unless result.buffered_stderr.empty?
102
83
 
103
- result = result.buffered_stdout
104
- Oj.load(result)
84
+ Oj.load(result.buffered_stdout)
105
85
  end
106
86
  end
107
87
  end
@@ -1,3 +1,3 @@
1
1
  module EcsDeployer
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecs_deployer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - naomichi-y
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-27 00:00:00.000000000 Z
11
+ date: 2017-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: runtime_command
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  requirements: []
124
124
  rubyforge_project:
125
- rubygems_version: 2.5.1
125
+ rubygems_version: 2.6.10
126
126
  signing_key:
127
127
  specification_version: 4
128
128
  summary: Deploy application to ECS.