ecs_cmd 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3ed292f74e7e2f9b9b155dbe9db89e0718cb901a
4
- data.tar.gz: 2f62f4c6195d6c439a94a9a2525334522c1fb660
2
+ SHA256:
3
+ metadata.gz: f215ab372a26cb580e3cc8c6ba5e1a2dc6f3fe946f4543652f7aa2ecf3ea0298
4
+ data.tar.gz: 473a4ccf2769d52faf13fc5787d82805ef6fb719bd1ddf5611755825ba685e72
5
5
  SHA512:
6
- metadata.gz: 7bcf5c0fed809384394df6d7219c97ce0c5558fcb7d619d244999b51f93fc47a8e73bf3b3fa44b0d87979a06e2fdc93b07a129e8ff7947c8d258a210f9d8fc99
7
- data.tar.gz: 72a546275e4e2608d82acaef6365a638224d4c0c538d4f4cf6ae71686a33d2fa0ed7ee3c25dc4aa5ff0e4963d88fffb91941a55b32e0ad231ef567f0ecd82ff9
6
+ metadata.gz: c36537549d61e3aa8c7474b0a25509fb90e41e825cef8d94d80227e8a5eb355ed4062bcbe1f28ef60f0090e4f93cf4526084df078af50c64395621f01786ec52
7
+ data.tar.gz: a3b25ffbf9a11d7018ef537b54361d4c1ede8c8f6eaf4294bad1db7fc417a02969f5c7de8b7604cb0c51d7ca35e6bb7ef4f0301044e7549416c24398f9e97344
@@ -1,5 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
+ env:
4
+ - GLI_DEBUG=true
3
5
  rvm:
4
6
  - 2.4.3
5
7
  - 2.5.1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ecs_cmd (0.1.0)
4
+ ecs_cmd (0.1.1)
5
5
  aws-sdk
6
6
  gli (= 2.17.1)
7
7
  terminal-table
@@ -609,4 +609,4 @@ DEPENDENCIES
609
609
  rspec (~> 3.0)
610
610
 
611
611
  BUNDLED WITH
612
- 1.16.1
612
+ 1.16.3
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ecs-Cmd
2
2
 
3
- [![Build Status](https://travis-ci.org/dschaaff/ecs-cmd.svg?branch=master)](https://travis-ci.org/dschaaff/ecs-cmd)
3
+ [![Build Status](https://travis-ci.org/dschaaff/ecs-cmd.svg?branch=master)](https://travis-ci.org/dschaaff/ecs-cmd) [![Gem Version](https://badge.fury.io/rb/ecs_cmd.svg)](https://badge.fury.io/rb/ecs_cmd)
4
4
 
5
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
6
 
@@ -10,76 +10,68 @@ version EcsCmd::VERSION
10
10
  subcommand_option_handling :normal
11
11
  arguments :strict
12
12
 
13
- desc 'Describe some switch here'
14
- switch [:s,:switch]
15
-
16
13
  desc 'Set the aws region'
17
14
  default_value 'us-east-1'
18
15
  arg_name 'region'
19
- flag [:r,:region]
16
+ flag %i[r region]
20
17
 
21
- desc 'Get Info '
18
+ desc 'Get Info on Clusters and Services'
22
19
  command :get do |c|
23
20
  c.command :clusters do |clusters|
24
21
  clusters.desc 'list ecs clusters'
25
- clusters.action do |global_options,options,args|
22
+ clusters.action do |global_options, _options, _args|
26
23
  clust = EcsCmd::Clusters.new(global_options[:region])
27
24
  clust.list_clusters
28
25
  end
29
26
  end
30
27
 
31
- c.arg_name '<cluster>'
32
28
  c.command :services do |services|
29
+ services.flag %i[c cluster], desc: 'cluster name', arg_name: 'cluster', required: true
33
30
  services.desc 'list services in a given ecs cluster'
34
- services.action do |global_options, options, args|
31
+ services.action do |global_options, options, _args|
35
32
  serv = EcsCmd::Services.new(global_options[:region])
36
- serv.list_services(args[0])
33
+ serv.list_services(options[:cluster])
37
34
  end
38
35
  end
39
36
 
40
- c.arg_name '<service> <cluster'
37
+ c.arg_name '<service>'
41
38
  c.command :service do |service|
39
+ service.flag %i[c cluster], desc: 'cluster name', arg_name: 'cluster', required: true
40
+ service.flag %i[s service], desc: 'service name', arg_name: 'service', required: true
42
41
  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
42
+ service.switch %i[e events], desc: 'get ecs events', default_value: false
43
+ service.switch %i[t task_definition], desc: 'get current task definition for service', default_value: false
44
+ service.action do |global_options, options|
45
+ if options[:events] == true
46
+ EcsCmd::Service.new(options[:cluster], options[:service], global_options[:region]).events
47
+ elsif options[:task_definition] == true
48
+ task_def = EcsCmd::Service.new(options[:cluster], options[:service], global_options[:region]).task_definition
49
+ EcsCmd::TaskDefinition.new(task_def, global_options[:region]).print_json
50
+ else
51
+ EcsCmd::Service.new(options[:cluster], options[:service], global_options[:region]).list_service
60
52
  end
61
53
  end
62
54
  end
63
55
  end
64
56
 
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'
57
+ desc 'Run a One Off Task On an ECS Cluster'
79
58
  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"
59
+ command :'run-task' do |c|
60
+ c.flag %i[c cluster], desc: 'cluster name', arg_name: 'cluster', required: true
61
+ c.flag %i[n container-name], desc: 'container name', arg_name: 'container name', required: false
62
+ c.flag %i[t task-definition], desc: 'the task definition to use for task', required: true
63
+ c.flag %i[i image], desc: 'docker image to use for task', arg_name: 'image', required: false
64
+ c.flag %i[d command], desc: 'override task definition command', arg_name: 'command'
65
+ c.action do |global_options, options, _args|
66
+ command = options[:d].tokenize
67
+ if options[:i]
68
+ puts "generating new task definition with image #{options[:i]}"
69
+ new_task = EcsCmd::TaskDefinition.new(options[:t], global_options[:region]).update_image(options[:i])
70
+ puts "registered task definition #{new_task}"
71
+ EcsCmd::RunTask.new(global_options[:region], options[:c], new_task, options[:n], command).run
72
+ else
73
+ EcsCmd::RunTask.new(global_options[:region], options[:c], options[:t], options[:n], command).run
74
+ end
83
75
  end
84
76
  end
85
77
 
@@ -91,7 +83,7 @@ end
91
83
  # end
92
84
  # end
93
85
 
94
- pre do |global,command,options,args|
86
+ pre do |_global, _command, _options, _args|
95
87
  # Pre logic here
96
88
  # Return true to proceed; false to abort and not call the
97
89
  # chosen command
@@ -100,13 +92,13 @@ pre do |global,command,options,args|
100
92
  true
101
93
  end
102
94
 
103
- post do |global,command,options,args|
95
+ post do |global, command, options, args|
104
96
  # Post logic here
105
97
  # Use skips_post before a command to skip this
106
98
  # block on that command only
107
99
  end
108
100
 
109
- on_error do |exception|
101
+ on_error do |_exception|
110
102
  # Error logic here
111
103
  # return false to skip default error handling
112
104
  true
@@ -1,9 +1,12 @@
1
1
  require 'ecs_cmd/version'
2
2
  require 'ecs_cmd/clusters.rb'
3
+ require 'ecs_cmd/run_task.rb'
3
4
  require 'ecs_cmd/services.rb'
4
5
  require 'ecs_cmd/service.rb'
5
6
  require 'ecs_cmd/task_definition.rb'
7
+ require 'ecs_cmd/utils.rb'
6
8
 
7
- module EcsCmd
8
- # Your code goes here...
9
- end
9
+ # module EcsCmd
10
+ # # Your code goes here..
11
+
12
+ # end
@@ -0,0 +1,39 @@
1
+ require 'aws-sdk'
2
+ require 'terminal-table'
3
+
4
+ module EcsCmd
5
+ class RunTask
6
+ def initialize(region, cluster, task_def, container_name = nil, command = [])
7
+ @client = Aws::ECS::Client.new(region: region)
8
+ @cluster = cluster
9
+ @container_name = container_name
10
+ @task_def = task_def
11
+ @command = command
12
+ end
13
+
14
+ # simply run the task
15
+ def run
16
+ puts 'running task...'
17
+ resp = if @container_name
18
+ @client.run_task(cluster: @cluster, task_definition: @task_def, overrides: {
19
+ container_overrides: [{
20
+ name: @container_name, command: @command
21
+ }]
22
+ })
23
+ else
24
+ @client.run_task(cluster: @cluster, task_definition: @task_def)
25
+ end
26
+ task_arn = resp[0][0]['task_arn']
27
+ result =
28
+ begin
29
+ puts 'waiting for task to complete...'
30
+ @client.wait_until(:tasks_stopped, cluster: @cluster, tasks: [task_arn])
31
+ rescue Aws::Waiters::Errors::WaiterFailed => error
32
+ puts "failed waiting for task to run: #{error.message}"
33
+ end
34
+ puts "task ended with exit code #{result[0][0]['containers'][0]['exit_code']}"
35
+ # return exit code
36
+ raise 'task appears to have failed, check container logs' if result[0][0]['containers'][0]['exit_code'] != 0
37
+ end
38
+ end
39
+ end
@@ -1,18 +1,36 @@
1
1
  require 'aws-sdk'
2
2
  require 'terminal-table'
3
+ require 'ecs_cmd/utils'
3
4
 
4
5
  module EcsCmd
5
6
  class TaskDefinition
6
-
7
- def initialize(task_definition)
8
- @client = Aws::ECS::Client.new(region: 'us-east-1')
7
+ def initialize(task_definition, region)
8
+ @client = Aws::ECS::Client.new(region: region)
9
9
  @task_def = @client.describe_task_definition(task_definition: task_definition)[0]
10
10
  end
11
11
 
12
12
 
13
- def container_defintions
13
+ def container_definitions
14
14
  @task_def['container_definitions']
15
15
  end
16
+
17
+ def images
18
+ self.container_definitions.each do |e|
19
+ puts e['image']
20
+ end
21
+ end
22
+
23
+ def family
24
+ @task_def['family']
25
+ end
26
+
27
+ def revision
28
+ @task_def['revision']
29
+ end
30
+
31
+ def volumes
32
+ @task_def['volumes']
33
+ end
16
34
 
17
35
  def hash
18
36
  @task_def.to_hash
@@ -21,10 +39,29 @@ module EcsCmd
21
39
  def json
22
40
  hash.to_json
23
41
  end
42
+
43
+ def task_role_arn
44
+ @task_def['task_role_arn']
45
+ end
46
+
47
+ def update_image(image)
48
+ @new_task_def = @task_def.to_hash
49
+ @new_task_def[:container_definitions].each do |e, i=image|
50
+ if Utils.parse_image_name(e[:image]) == Utils.parse_image_name(i)
51
+ e[:image] = image
52
+ end
53
+ end
54
+ @new_task_def
55
+ resp = register_task_definition(@new_task_def[:family], @new_task_def[:container_definitions], @new_task_def[:volumes], @new_task_def[:task_role_arn])
56
+ resp.task_definition.task_definition_arn
57
+ end
58
+
59
+ def register_task_definition(family, container_definitions, volumes, task_role_arn)
60
+ @client.register_task_definition(family: family, container_definitions: container_definitions, volumes: volumes, task_role_arn: task_role_arn)
61
+ end
24
62
 
25
63
  def print_json
26
64
  puts JSON.pretty_generate(JSON[json])
27
-
28
65
  end
29
66
 
30
67
  end
@@ -0,0 +1,24 @@
1
+ module EcsCmd
2
+ module Utils
3
+ def self.parse_image_name(image)
4
+ regex = /^([a-zA-Z0-9\.\-]+):?([0-9]+)?\/([a-zA-Z0-9\._\-]+)(\/[\/a-zA-Z0-9\._\-]+)?:?([a-zA-Z0-9\._\-]+)?$/
5
+ raise 'invalid image supplied, please verify correct image format' unless regex.match(image)
6
+ if regex.match(image)[4].nil? || regex.match(image)[4] == false
7
+ regex.match(image)[3]
8
+ else
9
+ regex.match(image)[4].gsub(/\//, '')
10
+ end
11
+ end
12
+
13
+ def self.parse_image_tag(image); end
14
+ end
15
+ end
16
+
17
+ class String
18
+ def tokenize
19
+ self.
20
+ split(/\s(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/).
21
+ select {|s| not s.empty? }.
22
+ map {|s| s.gsub(/(^ +)|( +$)|(^["']+)|(["']+$)/,'')}
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module EcsCmd
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecs_cmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schaaff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-30 00:00:00.000000000 Z
11
+ date: 2018-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -132,9 +132,11 @@ files:
132
132
  - ecs_cmd.gemspec
133
133
  - lib/ecs_cmd.rb
134
134
  - lib/ecs_cmd/clusters.rb
135
+ - lib/ecs_cmd/run_task.rb
135
136
  - lib/ecs_cmd/service.rb
136
137
  - lib/ecs_cmd/services.rb
137
138
  - lib/ecs_cmd/task_definition.rb
139
+ - lib/ecs_cmd/utils.rb
138
140
  - lib/ecs_cmd/version.rb
139
141
  homepage: https://danielschaaff.com
140
142
  licenses:
@@ -156,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
158
  version: '0'
157
159
  requirements: []
158
160
  rubyforge_project:
159
- rubygems_version: 2.6.14
161
+ rubygems_version: 2.7.6
160
162
  signing_key:
161
163
  specification_version: 4
162
164
  summary: AWS ECS CLI Utility