Class: EcsCmd::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_cmd/service.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(cluster, name, region) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
15
# File 'lib/ecs_cmd/service.rb', line 8

def initialize(cluster, name, region)
  @client = Aws::ECS::Client.new(region: region)
  @ec2_client = Aws::EC2::Client.new(region: region)
  @cluster = cluster
  @name = name
  @service_stats = @client.describe_services(cluster: cluster, services: [name])[0]
  raise 'service does not appear to exist' if @service_stats.empty?
end

Instance Method Details

#arnObject



17
18
19
# File 'lib/ecs_cmd/service.rb', line 17

def arn
  @service_stats[0]['service_arn']
end

#container_instance(task_arn) ⇒ Object

return container instance arn for given task id



80
81
82
83
84
85
86
# File 'lib/ecs_cmd/service.rb', line 80

def container_instance(task_arn)
instance = []
@client.describe_tasks(cluster: @cluster, tasks: [task_arn])[0].each do |e|
  instance << e[:container_instance_arn]
end
instance[0]
end

#container_instance_id(arn) ⇒ Object



88
89
90
91
# File 'lib/ecs_cmd/service.rb', line 88

def container_instance_id(arn)
  instance = [arn.to_s]
  @client.describe_container_instances(cluster: @cluster, container_instances: instance)[0][0][:ec2_instance_id]
end

#container_instance_idsObject



93
94
95
96
97
98
99
# File 'lib/ecs_cmd/service.rb', line 93

def container_instance_ids
  ids = []
  @client.describe_container_instances(cluster: @cluster, container_instances: container_instances)[0].each do |e|
    ids << e[:ec2_instance_id]
  end
  ids.uniq
end

#container_instance_ip(instance_id) ⇒ Object



101
102
103
104
# File 'lib/ecs_cmd/service.rb', line 101

def container_instance_ip(instance_id)
  id = [instance_id]
  @ec2_client.describe_instances(instance_ids: id)[:reservations][0][:instances][0][:private_ip_address]
end

#container_instance_ipsObject



106
107
108
109
110
111
112
# File 'lib/ecs_cmd/service.rb', line 106

def container_instance_ips
  ips = []
  @ec2_client.describe_instances(instance_ids: container_instance_ids)[:reservations][0][:instances].each do |e|
    ips << e[:private_ip_address]
  end
  ips
end

#container_instancesObject

list all container instance arns for given service’s tasks



72
73
74
75
76
77
78
# File 'lib/ecs_cmd/service.rb', line 72

def container_instances
  instances = []
  @client.describe_tasks(cluster: @cluster, tasks: tasks)[0].each do |e|
    instances << e[:container_instance_arn]
  end
  instances
end

#deployment_configurationObject



42
43
44
# File 'lib/ecs_cmd/service.rb', line 42

def deployment_configuration
  @service_stats[0]['deployment_configuration']
end

#deploymentsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ecs_cmd/service.rb', line 25

def deployments
  t = []
  @service_stats[0]['deployments'].each do |e|
    t << ['id', e['id']]
    t << ['status', e['status']]
    t << ['task definition', e['task_definition']]
    t << ['desired count', e['desired_count']]
    t << ['pending count', e['pending_count']]
    t << ['running count', e['running_count']]
    t << ['created at', e['created_at']]
    t << ['updated at', e['updated_at']]
    t << ["\n"]
  end
  table = Terminal::Table.new headings: ['DEPLOYMENTS', ''], rows: t
  table
end

#desired_countObject



46
47
48
# File 'lib/ecs_cmd/service.rb', line 46

def desired_count
  @service_stats[0]['desired_count']
end

#eventsObject



118
119
120
121
122
# File 'lib/ecs_cmd/service.rb', line 118

def events
  @service_stats[0]['events'].each do |e|
    puts "#{e['created_at']}: #{e['message']}"
  end
end

#health_check_grace_periodObject



114
115
116
# File 'lib/ecs_cmd/service.rb', line 114

def health_check_grace_period
  @service_stats[0]['health_check_grace_period_seconds']
end

#launch_typeObject



128
129
130
# File 'lib/ecs_cmd/service.rb', line 128

def launch_type
  @service_stats[0]['deployments'][0]['launch_type']
end

#list_serviceObject



166
167
168
169
170
171
# File 'lib/ecs_cmd/service.rb', line 166

def list_service
  puts overview_table
  puts task_def_table
  puts deployments
  puts tasks_table
end

#nameObject



124
125
126
# File 'lib/ecs_cmd/service.rb', line 124

def name
  @service_stats[0]['service_name']
end

#overview_tableObject



149
150
151
152
153
154
155
156
157
# File 'lib/ecs_cmd/service.rb', line 149

def overview_table
  row1 = []
  row1 << [name, status, running_count, desired_count, pending_count,
           deployment_configuration['maximum_percent'], deployment_configuration['minimum_healthy_percent']]
  table1 = Terminal::Table.new headings: ['NAME', 'STATUS', 'RUNNING COUNT',
                                          'DESIRED COUNT', 'PENDING COUNT',
                                          'MAX HEALTHY', 'MIN HEALTHY'], rows: row1
  table1
end

#pending_countObject



54
55
56
# File 'lib/ecs_cmd/service.rb', line 54

def pending_count
  @service_stats[0]['pending_count']
end

#running_countObject



50
51
52
# File 'lib/ecs_cmd/service.rb', line 50

def running_count
  @service_stats[0]['running_count']
end

#statusObject



21
22
23
# File 'lib/ecs_cmd/service.rb', line 21

def status
  @service_stats[0]['status']
end

#task_def_tableObject



159
160
161
162
163
164
# File 'lib/ecs_cmd/service.rb', line 159

def task_def_table
  row2 = []
  row2 << [task_definition]
  table2 = Terminal::Table.new headings: ['TASK DEFINITION'], rows: row2
  table2
end

#task_definitionObject



58
59
60
# File 'lib/ecs_cmd/service.rb', line 58

def task_definition
  @service_stats[0]['task_definition']
end

#task_familyObject



62
63
64
65
66
# File 'lib/ecs_cmd/service.rb', line 62

def task_family
  # known issue, won't work with / in task family names
  # TODO: improve this later
  @service_stats[0]['task_definition'].split('/')[1].split(':')[0]
end

#tasksObject

list task arns for a service



68
69
70
# File 'lib/ecs_cmd/service.rb', line 68

def tasks
  @client.list_tasks(cluster: @cluster, service_name: @name)[0]
end

#tasks_tableObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ecs_cmd/service.rb', line 132

def tasks_table
  t = []
  if launch_type == 'FARGATE'
    tasks.each do |e|
      t << [e]
    end
    table = Terminal::Table.new headings: ['TASK_ID'], rows: t
  else
    tasks.each do |e|
      t << [e, container_instance_id(container_instance(e)),
            container_instance_ip(container_instance_id(container_instance(e)))]
    end
    table = Terminal::Table.new headings: ['TASK_ID', 'INSTANCE_ID', 'IP'], rows: t
  end
  table
end