Class: EcsCmd::Service

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

Instance Method Summary collapse

Constructor Details

#initialize(cluster, name, region) ⇒ Service

Returns a new instance of Service.



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

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



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

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

#container_instance_id(arn) ⇒ Object



79
80
81
82
# File 'lib/ecs_cmd/service.rb', line 79

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



84
85
86
87
88
89
90
# File 'lib/ecs_cmd/service.rb', line 84

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



92
93
94
95
# File 'lib/ecs_cmd/service.rb', line 92

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



97
98
99
100
101
102
103
# File 'lib/ecs_cmd/service.rb', line 97

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



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

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



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

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

#deploymentsObject



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

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



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

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

#eventsObject



109
110
111
112
113
# File 'lib/ecs_cmd/service.rb', line 109

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

#health_check_grace_periodObject



105
106
107
# File 'lib/ecs_cmd/service.rb', line 105

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

#list_container_instancesObject



119
120
121
122
123
124
125
126
# File 'lib/ecs_cmd/service.rb', line 119

def list_container_instances
  t = []
  container_instances.each do |e|
    t << [container_instance_id(e), container_instance_ip(container_instance_id(e))]
  end
  table = Terminal::Table.new headings: ['INSTANCE ID', 'IP'], rows: t
  table
end

#list_serviceObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ecs_cmd/service.rb', line 128

def list_service
  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
  row2 = []
  row2 << [task_definition]
  table2 = Terminal::Table.new headings: ['TASK DEFINITION'], rows: row2
  puts table1
  puts table2
  puts list_container_instances
  puts deployments
end

#nameObject



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

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

#pending_countObject



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

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

#running_countObject



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

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

#statusObject



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

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

#task_definitionObject



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

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

#task_familyObject



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

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



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

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