Class: Awful::Ecs
  
  
  
  
    
      Constant Summary
      collapse
    
    
      
        - COLORS =
          
        
 
        {
  ACTIVE:   :green,
  INACTIVE: :red,
  true:     :green,
  false:    :red,
  RUNNING:  :green,
  STOPPED:  :red,
} 
      
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods inherited from Cli
  #initialize, #ll, #version
  
  Constructor Details
  
    This class inherits a constructor from Awful::Cli
  
 
  
    Instance Method Details
    
      
  
  
    #create(name)  ⇒ Object 
  
  
  
  
    
      
53
54
55
56
57 
     | 
    
      # File 'lib/awful/ecs.rb', line 53
def create(name)
  ecs.create_cluster(cluster_name: name).cluster.output do |cluster|
    puts YAML.dump(stringify_keys(cluster.to_h))
  end
end
     | 
  
 
    
      
  
  
    #definitions(family = nil)  ⇒ Object 
  
  
  
  
    
      
100
101
102
103
104
105
106
107
108 
     | 
    
      # File 'lib/awful/ecs.rb', line 100
def definitions(family = nil)
  params = {family_prefix: family, status: options[:inactive] ? 'INACTIVE' : 'ACTIVE'}.reject{|_,v| v.nil?}
  arns = ecs.list_task_definitions(params).task_definition_arns
  if options[:arns]
    arns
  else
    arns.map{|a| a.split('/').last}
  end.output(&method(:puts))
end
     | 
  
 
    
      
  
  
    #delete(name)  ⇒ Object 
  
  
  
  
    
      
60
61
62
63
64
65
66 
     | 
    
      # File 'lib/awful/ecs.rb', line 60
def delete(name)
  if yes?("Really delete stack #{name}?", :yellow)
    ecs.delete_cluster(cluster: name).cluster.output do |cluster|
      puts YAML.dump(stringify_keys(cluster.to_h))
    end
  end
end
     | 
  
 
    
      
  
  
    #deregister(task)  ⇒ Object 
  
  
  
  
    
      
136
137
138 
     | 
    
      # File 'lib/awful/ecs.rb', line 136
def deregister(task)
  ecs.deregister_task_definition(task_definition: task)
end 
     | 
  
 
    
      
  
  
    #dump(task)  ⇒ Object 
  
  
  
  
    
      
142
143
144
145
146
147
148
149
150 
     | 
    
      # File 'lib/awful/ecs.rb', line 142
def dump(task)
  ecs.describe_task_definition(task_definition: task).task_definition.to_h.output do |hash|
    if options[:json]
      puts JSON.pretty_generate(hash)
    else
      puts YAML.dump(stringify_keys(hash))
    end
  end
end
     | 
  
 
    
      
  
  
    #events(cluster, service)  ⇒ Object 
  
  
  
  
    
      
219
220
221
222
223 
     | 
    
      # File 'lib/awful/ecs.rb', line 219
def events(cluster, service)
  ecs.describe_services(cluster: cluster, services: [service]).services.first.events.output do |events|
    print_table events.map { |e| [e.created_at, e.id, e.message] }
  end
end
     | 
  
 
    
      
  
  
    #families(prefix = nil)  ⇒ Object 
  
  
  
  
    
      
111
112
113
114
115
116
117
118
119
120
121 
     | 
    
      # File 'lib/awful/ecs.rb', line 111
def families(prefix = nil)
  next_token = nil
  families = []
  loop do
    response = ecs.list_task_definition_families(family_prefix: prefix, next_token: next_token)
    families += response.families
    next_token = response.next_token
    break unless next_token
  end
  families.output(&method(:puts))
end
     | 
  
 
    
      
  
  
    #instances(cluster)  ⇒ Object 
  
  
  
  
    
      
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 
     | 
    
      # File 'lib/awful/ecs.rb', line 70
def instances(cluster)
  arns = ecs.list_container_instances(cluster: cluster).container_instance_arns
  if options[:long]
    container_instances = ecs.describe_container_instances(cluster: cluster, container_instances: arns).container_instances
        tags = ec2.describe_instances(instance_ids: container_instances.map(&:ec2_instance_id)).
             map(&:reservations).flatten.
             map(&:instances).flatten.
             each_with_object({}) do |i,h|
      h[i.instance_id] = tag_name(i, '--')
    end
    print_table container_instances.each_with_index.map { |ins, i|
      [
        tags[ins.ec2_instance_id],
        ins.container_instance_arn.split('/').last,
        ins.ec2_instance_id,
        "agent:#{color(ins.agent_connected.to_s)}",
        color(ins.status),
      ]
    }.sort
  else
    puts arns
  end
end
     | 
  
 
    
      
  
  
    #ls(name = '.')  ⇒ Object 
  
  
  
  
    
      
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 
     | 
    
      # File 'lib/awful/ecs.rb', line 29
def ls(name = '.')
  arns = ecs.list_clusters.cluster_arns.select do |arn|
    arn.split('/').last.match(/#{name}/i)
  end
  ecs.describe_clusters(clusters: arns).clusters.output do |clusters|
    if options[:arns]
      puts arns
    elsif options[:long]
      print_table clusters.map { |c|
        [
          c.cluster_name,
          color(c.status),
          "instances:#{c.registered_container_instances_count}",
          "pending:#{c.pending_tasks_count}",
          "running:#{c.running_tasks_count}",
        ]
      }
    else
      puts clusters.map(&:cluster_name).sort
    end
  end
end
     | 
  
 
    
      
  
  
    #register(family)  ⇒ Object 
  
  
  
  
    
      
124
125
126
127
128
129
130
131
132
133 
     | 
    
      # File 'lib/awful/ecs.rb', line 124
def register(family)
  taskdef = ecs.describe_task_definition(task_definition: family).task_definition   ecs.register_task_definition(
    family: family,
    container_definitions: taskdef.container_definitions,
    volumes: taskdef.volumes
  ).task_definition.output do |td|
    puts td.task_definition_arn
  end
end
     | 
  
 
    
      
  
  
    #run_task(cluster, task)  ⇒ Object 
  
  
  
  
    
      
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 
     | 
    
      # File 'lib/awful/ecs.rb', line 227
def run_task(cluster, task)
  container_overrides = {}
  if options[:command]
    name, command = options[:command].split(':', 2)
    container_overrides.merge!(name: name, command: command.split(','))
  end
  params = {
    cluster: cluster,
    task_definition: task,
    overrides: container_overrides.empty? ? {} : {container_overrides: [container_overrides]}
  }
  ecs.run_task(params).output do |response|
    puts YAML.dump(stringify_keys(response.to_h))
  end
end
     | 
  
 
    
      
  
  
    #services(cluster)  ⇒ Object 
  
  
  
  
    
      
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 
     | 
    
      # File 'lib/awful/ecs.rb', line 188
def services(cluster)
  arns = ecs.list_services(cluster: cluster).service_arns
  if options[:long]
    print_table ecs.describe_services(cluster: cluster, services: arns).services.map { |svc|
      [
        svc.service_name,
        color(svc.status),
        svc.task_definition.split('/').last,
        "#{svc.running_count}/#{svc.desired_count}",
      ]
    }
  else
    arns.output(&method(:puts))
  end
end
     | 
  
 
    
      
  
  
    #status(cluster, *tasks)  ⇒ Object 
  
  
  
  
    
      
178
179
180
181
182
183
184 
     | 
    
      # File 'lib/awful/ecs.rb', line 178
def status(cluster, *tasks)
  ecs.describe_tasks(cluster: cluster, tasks: tasks).tasks.output do |responses|
    responses.each do |response|
      puts YAML.dump(stringify_keys(response.to_h))
    end
  end
end
     | 
  
 
    
      
  
  
    #stop_task(cluster, id)  ⇒ Object 
  
  
  
  
    
      
246
247
248
249
250 
     | 
    
      # File 'lib/awful/ecs.rb', line 246
def stop_task(cluster, id)
  ecs.stop_task(cluster: cluster, task: id).task.output do |response|
    puts YAML.dump(stringify_keys(response.to_h))
  end
end
     | 
  
 
    
      
  
  
    #tasks(cluster)  ⇒ Object 
  
  
  
  
    
      
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 
     | 
    
      # File 'lib/awful/ecs.rb', line 155
def tasks(cluster)
  status = %w[running pending stopped].find{ |s| s.match(/^#{options[:status]}/i) }
  arns = ecs.list_tasks(cluster: cluster, desired_status: status.upcase).task_arns
  if arns.empty?
    []
  elsif options[:long]
    ecs.describe_tasks(cluster: cluster, tasks: arns).tasks.output do |tasks|
      print_table tasks.map { |task|
        [
          task.task_arn.split('/').last,
          task.task_definition_arn.split('/').last,
          task.container_instance_arn.split('/').last,
          "#{color(task.last_status)} (#{task.desired_status})",
          task.started_by,
        ]
      }
    end
  else
    arns.output(&method(:puts))
  end
end
     | 
  
 
    
      
  
  
    #update(cluster, service)  ⇒ Object 
  
  
  
  
    
      
207
208
209
210
211
212
213
214
215
216 
     | 
    
      # File 'lib/awful/ecs.rb', line 207
def update(cluster, service)
  params = {
    cluster:         cluster,
    service:         service,
    desired_count:   options[:desired_count],
    task_definition: options[:task_definition],
  }.reject { |k,v| v.nil? }
  ecs.update_service(params).service
end
     |