Class: Hako::Schedulers::Ecs
Constant Summary
collapse
- DEFAULT_CLUSTER =
'default'
- DEFAULT_FRONT_PORT =
10000
Instance Attribute Summary collapse
Instance Method Summary
collapse
#initialize, #upload_front_config
Instance Attribute Details
#task ⇒ Object
Returns the value of attribute task.
14
15
16
|
# File 'lib/hako/schedulers/ecs.rb', line 14
def task
@task
end
|
Instance Method Details
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/hako/schedulers/ecs.rb', line 16
def configure(options)
@cluster = options.fetch('cluster', DEFAULT_CLUSTER)
@desired_count = options.fetch('desired_count') { validation_error!('desired_count must be set') }
region = options.fetch('region') { validation_error!('region must be set') }
@role = options.fetch('role', nil)
@ecs = Aws::ECS::Client.new(region: region)
@elb = EcsElb.new(@app_id, Aws::ElasticLoadBalancing::Client.new(region: region), options.fetch('elb', nil))
@ec2 = Aws::EC2::Client.new(region: region)
@started_at = nil
@container_instance_arn = nil
end
|
#deploy(containers) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/hako/schedulers/ecs.rb', line 28
def deploy(containers)
app = containers.fetch('app')
front = containers.fetch('front')
front_port = determine_front_port
definitions = create_definitions(containers, front_port)
if @dry_run
definitions.each do |d|
Hako.logger.info "Add container #{d}"
end
else
task_definition = register_task_definition(definitions)
if task_definition == :noop
Hako.logger.info "Task definition isn't changed"
task_definition = @ecs.describe_task_definition(task_definition: @app_id).task_definition
else
Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}"
upload_front_config(@app_id, front, app.port)
Hako.logger.debug "Uploaded front configuration to s3://#{front.s3.bucket}/#{front.s3.key(@app_id)}"
end
service = create_or_update_service(task_definition.task_definition_arn, front_port)
if service == :noop
Hako.logger.info "Service isn't changed"
else
Hako.logger.info "Updated service: #{service.service_arn}"
wait_for_ready(service)
end
Hako.logger.info 'Deployment completed'
end
end
|
#oneshot(containers, commands, env) ⇒ Object
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
|
# File 'lib/hako/schedulers/ecs.rb', line 59
def oneshot(containers, commands, env)
definitions = create_definitions(containers, -1)
definitions.each do |definition|
definition.delete(:essential)
end
if @dry_run
definitions.each do |d|
Hako.logger.info "Add container #{d}"
end
env.each do |k, v|
Hako.logger.info "Add environment #{k}=#{v}"
end
Hako.logger.info "Execute command #{commands}"
0
else
task_definition = register_task_definition_for_oneshot(definitions)
if task_definition == :noop
Hako.logger.info "Task definition isn't changed"
task_definition = @ecs.describe_task_definition(task_definition: "#{@app_id}-oneshot").task_definition
else
Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}"
end
@task = run_task(task_definition, commands, env)
Hako.logger.info "Started task: #{@task.task_arn}"
@scripts.each { |script| script.oneshot_started(self) }
wait_for_oneshot_finish
end
end
|
#remove ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/hako/schedulers/ecs.rb', line 155
def remove
service = describe_service
if service
@ecs.delete_service(cluster: @cluster, service: @app_id)
Hako.logger.info "#{service.service_arn} is deleted"
else
puts "Service #{@app_id} doesn't exist"
end
@elb.destroy
end
|
#status ⇒ Object
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
148
149
150
151
152
153
|
# File 'lib/hako/schedulers/ecs.rb', line 97
def status
service = describe_service
unless service
puts 'Unavailable'
exit 1
end
unless service.load_balancers.empty?
lb = service.load_balancers[0]
lb_detail = @elb.describe_load_balancer
puts 'Load balancer:'
lb_detail.listener_descriptions.each do |ld|
l = ld.listener
puts " #{lb_detail.dns_name}:#{l.load_balancer_port} -> #{lb.container_name}:#{lb.container_port}"
end
end
puts 'Deployments:'
service.deployments.each do |d|
abbrev_task_definition = d.task_definition.slice(%r{task-definition/(.+)\z}, 1)
puts " [#{d.status}] #{abbrev_task_definition} desired_count=#{d.desired_count}, pending_count=#{d.pending_count}, running_count=#{d.running_count}"
end
puts 'Tasks:'
@ecs.list_tasks(cluster: @cluster, service_name: service.service_arn).each do |page|
unless page.task_arns.empty?
tasks = @ecs.describe_tasks(cluster: @cluster, tasks: page.task_arns).tasks
container_instances = {}
@ecs.describe_container_instances(cluster: @cluster, container_instances: tasks.map(&:container_instance_arn)).container_instances.each do |ci|
container_instances[ci.container_instance_arn] = ci
end
ec2_instances = {}
@ec2.describe_instances(instance_ids: container_instances.values.map(&:ec2_instance_id)).reservations.each do |r|
r.instances.each do |i|
ec2_instances[i.instance_id] = i
end
end
tasks.each do |task|
ci = container_instances[task.container_instance_arn]
instance = ec2_instances[ci.ec2_instance_id]
print " [#{task.last_status}]: #{ci.ec2_instance_id}"
if instance
name_tag = instance.tags.find { |t| t.key == 'Name' }
if name_tag
print " (#{name_tag.value})"
end
end
puts
end
end
end
puts 'Events:'
service.events.first(10).each do |e|
puts " #{e.created_at}: #{e.message}"
end
end
|
#stop_oneshot ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/hako/schedulers/ecs.rb', line 89
def stop_oneshot
if @task
Hako.logger.warn "Stopping #{@task.task_arn}"
@ecs.stop_task(cluster: @cluster, task: @task.task_arn, reason: 'Stopped by hako stop_oneshot')
wait_for_oneshot_finish
end
end
|