ecs_cmd 0.1.0

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.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Daniel Schaaff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # Ecs-Cmd
2
+
3
+ [![Build Status](https://travis-ci.org/dschaaff/ecs-cmd.svg?branch=master)](https://travis-ci.org/dschaaff/ecs-cmd)
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ecs_cmd`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ TODO: Delete this and the text above, and describe your gem
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ecs_cmd'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ecs_cmd
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ecs_cmd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
42
+
43
+ ## Code of Conduct
44
+
45
+ Everyone interacting in the EcsCmd project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ecs_cmd/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ecs_cmd"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start(__FILE__)
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+ require 'ecs_cmd'
4
+ include GLI::App
5
+ include EcsCmd
6
+ program_desc 'Command utility for interacting with AWS ECS'
7
+
8
+ version EcsCmd::VERSION
9
+
10
+ subcommand_option_handling :normal
11
+ arguments :strict
12
+
13
+ desc 'Describe some switch here'
14
+ switch [:s,:switch]
15
+
16
+ desc 'Set the aws region'
17
+ default_value 'us-east-1'
18
+ arg_name 'region'
19
+ flag [:r,:region]
20
+
21
+ desc 'Get Info '
22
+ command :get do |c|
23
+ c.command :clusters do |clusters|
24
+ clusters.desc 'list ecs clusters'
25
+ clusters.action do |global_options,options,args|
26
+ clust = EcsCmd::Clusters.new(global_options[:region])
27
+ clust.list_clusters
28
+ end
29
+ end
30
+
31
+ c.arg_name '<cluster>'
32
+ c.command :services do |services|
33
+ services.desc 'list services in a given ecs cluster'
34
+ services.action do |global_options, options, args|
35
+ serv = EcsCmd::Services.new(global_options[:region])
36
+ serv.list_services(args[0])
37
+ end
38
+ end
39
+
40
+ c.arg_name '<service> <cluster'
41
+ c.command :service do |service|
42
+ service.default_desc 'get info about an ecs service'
43
+ service.action do |global_options, options, args|
44
+ EcsCmd::Service.new(args[1],args[0], global_options[:region]).list_service
45
+ end
46
+ service.arg_name '<service> <cluster>'
47
+ service.desc 'get event stream for ecs service'
48
+ service.command :events do |events|
49
+ events.desc 'list events for service'
50
+ events.action do |global_options, options, args|
51
+ EcsCmd::Service.new(args[1],args[0], global_options[:region]).events
52
+ end
53
+ end
54
+ service.arg_name '<service> <cluster> task_definition'
55
+ service.desc 'get json task definition for service'
56
+ service.command :task_definition do |task_definition|
57
+ task_definition.desc 'get the current task definition for the service'
58
+ task_definition.action do |global_options, options, args|
59
+ EcsCmd::TaskDefinition.new(EcsCmd::Service.new(args[1],args[0], global_options[:region]).task_definition).print_json
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ # desc 'Add description here'
66
+ # arg_name 'Describe arguments here'
67
+ # command :get do |c|
68
+ # c.action do |global_options, options, args|
69
+ # if args[0] == 'task_definition'
70
+ # task = EcsCmd::TaskDefinition.new(args[1])
71
+ # puts JSON.pretty_generate(JSON[task.json])
72
+ # elsif
73
+ # puts "not implemented"
74
+ # end
75
+ # end
76
+ # end
77
+
78
+ desc 'Describe add here'
79
+ arg_name 'Describe arguments to add here'
80
+ command :add do |c|
81
+ c.action do |global_options,options,args|
82
+ puts "add command ran"
83
+ end
84
+ end
85
+
86
+ # desc 'Describe complete here'
87
+ # arg_name 'Describe arguments to complete here'
88
+ # command :complete do |c|
89
+ # c.action do |global_options,options,args|
90
+ # puts "complete command ran"
91
+ # end
92
+ # end
93
+
94
+ pre do |global,command,options,args|
95
+ # Pre logic here
96
+ # Return true to proceed; false to abort and not call the
97
+ # chosen command
98
+ # Use skips_pre before a command to skip this block
99
+ # on that command only
100
+ true
101
+ end
102
+
103
+ post do |global,command,options,args|
104
+ # Post logic here
105
+ # Use skips_post before a command to skip this
106
+ # block on that command only
107
+ end
108
+
109
+ on_error do |exception|
110
+ # Error logic here
111
+ # return false to skip default error handling
112
+ true
113
+ end
114
+
115
+ exit run(ARGV)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'ecs_cmd/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'ecs_cmd'
10
+ spec.version = EcsCmd::VERSION
11
+ spec.authors = ['Daniel Schaaff']
12
+ spec.email = ['dbschaaff@gmail.com']
13
+
14
+ spec.summary = 'AWS ECS CLI Utility'
15
+ spec.description = 'AWS ECS CLI Utility'
16
+ spec.homepage = 'https://danielschaaff.com'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ # if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "rubyge'"
23
+ # else
24
+ # raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ # 'public gem pushes.'
26
+ # end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.bindir = 'bin'
32
+ spec.executables << 'ecs-cmd'
33
+ spec.require_paths = ['lib']
34
+ spec.add_runtime_dependency 'aws-sdk'
35
+ spec.add_runtime_dependency 'gli', '2.17.1'
36
+ spec.add_runtime_dependency 'terminal-table'
37
+
38
+ spec.add_development_dependency 'bundler', '~> 1.16'
39
+ spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.0'
41
+ spec.add_development_dependency 'pry'
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'ecs_cmd/version'
2
+ require 'ecs_cmd/clusters.rb'
3
+ require 'ecs_cmd/services.rb'
4
+ require 'ecs_cmd/service.rb'
5
+ require 'ecs_cmd/task_definition.rb'
6
+
7
+ module EcsCmd
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,47 @@
1
+ require 'aws-sdk'
2
+ require 'terminal-table'
3
+
4
+ module EcsCmd
5
+ class Clusters
6
+ def initialize(region)
7
+ @client = Aws::ECS::Client.new(region: region)
8
+ end
9
+
10
+ def get_clusters
11
+ @cluster_arns = @client.list_clusters
12
+ end
13
+
14
+ def get_container_instance_count(stats)
15
+ stats['registered_container_instances_count']
16
+ end
17
+
18
+ def get_service_count(stats)
19
+ stats['active_services_count']
20
+ end
21
+
22
+ def get_running_task_count(stats)
23
+ stats['running_tasks_count']
24
+ end
25
+
26
+ def get_pending_task_count(stats)
27
+ stats['pending_tasks_count']
28
+ end
29
+
30
+ def cluster_names
31
+ get_clusters[0]
32
+ end
33
+
34
+ def list_clusters
35
+ rows = []
36
+ cluster_names.map do |c|
37
+ cluster_name = c.split('/')[1]
38
+ stats = @client.describe_clusters(clusters: [cluster_name])[0][0]
39
+ rows << [cluster_name, get_container_instance_count(stats), get_service_count(stats),
40
+ get_running_task_count(stats), get_pending_task_count(stats)]
41
+ end
42
+ table = Terminal::Table.new headings: ['CLUSTER NAME', 'CONTAINER_INSTANCE_COUNT',
43
+ 'SERVICES', 'RUNNING_TASKS', 'PENDING_TASKS'], rows: rows
44
+ puts table
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,86 @@
1
+ require 'aws-sdk'
2
+ require 'terminal-table'
3
+
4
+ module EcsCmd
5
+ class Service
6
+ def initialize(cluster, name, region)
7
+ @client = Aws::ECS::Client.new(region: region)
8
+ @service_stats = @client.describe_services(cluster: cluster, services: [name])[0]
9
+ raise 'service does not appear to exist' if @service_stats.empty?
10
+ end
11
+
12
+ def arn
13
+ @service_stats[0]['service_arn']
14
+ end
15
+
16
+ def status
17
+ @service_stats[0]['status']
18
+ end
19
+
20
+ def deployments
21
+ t = []
22
+ @service_stats[0]['deployments'].each do |e|
23
+ t << ['id', e['id']]
24
+ t << ['status', e['status']]
25
+ t << ['task definition', e['task_definition']]
26
+ t << ['desired count', e['desired_count']]
27
+ t << ['pending count', e['pending_count']]
28
+ t << ['running count', e['running_count']]
29
+ t << ['created at', e['created_at']]
30
+ t << ['updated at', e['updated_at']]
31
+ t << ["\n"]
32
+ end
33
+ table = Terminal::Table.new headings: ['DEPLOYMENTS'], rows: t
34
+ table
35
+ end
36
+
37
+ def deployment_configuration
38
+ @service_stats[0]['deployment_configuration']
39
+ end
40
+
41
+ def desired_count
42
+ @service_stats[0]['desired_count']
43
+ end
44
+
45
+ def running_count
46
+ @service_stats[0]['running_count']
47
+ end
48
+
49
+ def pending_count
50
+ @service_stats[0]['pending_count']
51
+ end
52
+
53
+ def task_definition
54
+ @service_stats[0]['task_definition']
55
+ end
56
+
57
+ def health_check_grace_period
58
+ @service_stats[0]['health_check_grace_period_seconds']
59
+ end
60
+
61
+ def events
62
+ @service_stats[0]['events'].each do |e|
63
+ puts "#{e['created_at']}: #{e['message']}"
64
+ end
65
+ end
66
+
67
+ def name
68
+ @service_stats[0]['service_name']
69
+ end
70
+
71
+ def list_service
72
+ row1 = []
73
+ row1 << [name, status, running_count, desired_count, pending_count,
74
+ deployment_configuration['maximum_percent'], deployment_configuration['minimum_healthy_percent']]
75
+ table1 = Terminal::Table.new headings: ['NAME', 'STATUS', 'RUNNING COUNT',
76
+ 'DESIRED COUNT', 'PENDING COUNT',
77
+ 'MAX HEALTHY', 'MIN HEALTHY'], rows: row1
78
+ row2 = []
79
+ row2 << [task_definition]
80
+ table2 = Terminal::Table.new headings: ['TASK DEFINITION'], rows: row2
81
+ puts table1
82
+ puts table2
83
+ puts deployments
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,33 @@
1
+ require 'aws-sdk'
2
+ require 'terminal-table'
3
+
4
+ module EcsCmd
5
+ class Services
6
+ def initialize(region)
7
+ @client = Aws::ECS::Client.new(region: region)
8
+ @reg = region
9
+ end
10
+
11
+ def get_services(cluster)
12
+ @service_arns = []
13
+ @client.list_services(cluster: cluster).each do |r|
14
+ @service_arns << r[0]
15
+ @service_arns.flatten!
16
+ end
17
+ @service_arns
18
+ end
19
+
20
+ def list_services(cluster, reg = @reg)
21
+ @service_list = get_services(cluster)
22
+ rows = []
23
+ @service_list.map do |service|
24
+ service_name = service.split('/')[1]
25
+ serv = EcsCmd::Service.new(cluster, service_name, reg)
26
+ rows << [service_name, serv.desired_count, serv.running_count, serv.pending_count]
27
+ end
28
+ table = Terminal::Table.new headings: ['SERVICE NAME', 'DESIRED_COUNT',
29
+ 'RUNNING_COUNT', 'PENDING_COUNT'], rows: rows
30
+ puts table
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ require 'aws-sdk'
2
+ require 'terminal-table'
3
+
4
+ module EcsCmd
5
+ class TaskDefinition
6
+
7
+ def initialize(task_definition)
8
+ @client = Aws::ECS::Client.new(region: 'us-east-1')
9
+ @task_def = @client.describe_task_definition(task_definition: task_definition)[0]
10
+ end
11
+
12
+
13
+ def container_defintions
14
+ @task_def['container_definitions']
15
+ end
16
+
17
+ def hash
18
+ @task_def.to_hash
19
+ end
20
+
21
+ def json
22
+ hash.to_json
23
+ end
24
+
25
+ def print_json
26
+ puts JSON.pretty_generate(JSON[json])
27
+
28
+ end
29
+
30
+ end
31
+ end