awshark 1.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbb6b1c8acc6985b27bae79aceffe7e15898e37a975cdef6eac0678ab3d644f2
4
- data.tar.gz: 141dde43ded62f04ac26d2fea21f38b5b829a6d755ea42c0d0aa0ccfaeb2d316
3
+ metadata.gz: d5706c9a35383e578bac90c8f432f2b4d730857556112222ff122ba7f7fba8e0
4
+ data.tar.gz: 6a3c0ca64b7f017aaea42c7fed255a5e5f44ec33bce1de59a00c875c1023e86e
5
5
  SHA512:
6
- metadata.gz: 8307e5b068979c55a48f7b87b8aab75b32b8b5f0e4e2c4e42fce39b57b78c6dea08441ebcaaca30a8615b2bf56875ca10fe514da61d574f6e71cec52338d9327
7
- data.tar.gz: f8372c0258917758924fadd5aab3f074b17b7dc1ffb2c124af5aba1146dcc560845090d366ce780cfb1f1e4fb6aad4574db0d345a8ed29fb9a6be74a03fd22e8
6
+ metadata.gz: a18d22c21cda523abfa5bd6369df2855b55eb479a2d43a0fa1bd66de0902e055db4e164bab332338f417fcc010094a168b4d2f92a16e4915a840fcb580608584
7
+ data.tar.gz: c4e5e7d9a2105ff44c58ce0f08ae742e48f780d18b4f63459d39ec6f670d372b5636e966f90e081fec071f4c56ad0923b3b1cb94549ef10cf819e064a57575ac
data/.rubocop.yml CHANGED
@@ -7,9 +7,6 @@ AllCops:
7
7
  TargetRubyVersion: 2.6
8
8
  Exclude:
9
9
  - 'bin/*'
10
- - 'node_modules/**/*'
11
- - 'vendor/**/*'
12
- - 'spec/**/*'
13
10
  - 'tmp/*'
14
11
 
15
12
  Layout/BeginEndAlignment:
@@ -17,6 +14,9 @@ Layout/BeginEndAlignment:
17
14
  EnforcedStyleAlignWith: begin
18
15
  Layout/EmptyLinesAroundAttributeAccessor:
19
16
  Enabled: true
17
+ Layout/FirstHashElementIndentation:
18
+ Exclude:
19
+ - 'spec/**/*'
20
20
  Layout/SpaceAroundMethodCallOperator:
21
21
  Enabled: true
22
22
 
@@ -70,6 +70,10 @@ Lint/UselessTimes:
70
70
  Metrics/AbcSize:
71
71
  Enabled: true
72
72
  Max: 20
73
+ Metrics/BlockLength:
74
+ Exclude:
75
+ - '*.gemspec'
76
+ - 'spec/**/*'
73
77
  Metrics/MethodLength:
74
78
  Enabled: false
75
79
  Metrics/ModuleLength:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Changelog
2
2
 
3
+ #### 1.2.0
4
+ - [new] add `awshark ecs list`
5
+ - [break] remove options `--profile` and `--region`; use `AWS_PROFILE` and `AWS_REGION` instead
6
+
3
7
  #### 1.1.0
4
8
  - ssm support added `<%= ssm["/#{stage}/app/application/db_host"] %>`
5
9
 
data/README.md CHANGED
@@ -14,32 +14,41 @@ gem install awshark
14
14
 
15
15
  ### Usage
16
16
 
17
+ Use `AWS_PROFILE=PROFILE` and/or `AWS_REGION=REGION` to configure the internal AWS clients.
18
+
17
19
  #### S3 commands
18
20
 
19
21
  List all S3 buckets
20
22
  ```
21
- awshark s3 list --profile=AWS_PROFILE
23
+ awshark s3 list
22
24
  ```
23
25
 
24
26
  List all objects in a specific S3 bucket
25
27
  ```
26
- awshark s3 objects BUCKET_NAME fonts/ --profile=AWS_PROFILE
28
+ awshark s3 objects BUCKET_NAME fonts/
27
29
  ```
28
30
 
29
31
  #### EC2 commands
30
32
 
31
33
  List all EC2 instances in a region
32
34
  ```
33
- awshark ec2 list --profile=AWS_PROFILE
35
+ awshark ec2 list
36
+ ```
37
+
38
+ #### ECS commands
39
+
40
+ List all EC2 instances in a region
41
+ ```
42
+ awshark ecs list
34
43
  ```
35
44
 
36
45
  #### Cloud Formation commands
37
46
 
38
47
  Update (diff) Cloud Formation stack
39
48
  ```
40
- awshark cf deploy TEMPLATE_PATH --stage=STAGE --bucket=S3_BUCKET.bundesimmo.de --profile=AWS_PROFILE
49
+ awshark cf deploy TEMPLATE_PATH --stage=STAGE --bucket=S3_BUCKET.bundesimmo.de
41
50
 
42
- awshark cf diff TEMPLATE_PATH --stage=STAGE --bucket=S3_BUCKET.bundesimmo.de --profile=AWS_PROFILE
51
+ awshark cf diff TEMPLATE_PATH --stage=STAGE --bucket=S3_BUCKET.bundesimmo.de
43
52
  ```
44
53
 
45
54
  For a further information visit the [Wiki](https://github.com/jdahlke/awshark/wiki).
@@ -50,12 +59,6 @@ For a further information visit the [Wiki](https://github.com/jdahlke/awshark/wi
50
59
  After checking out the repo, run `bin/setup` to install dependencies.
51
60
  You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
61
 
53
- To install this gem onto your local machine, run
54
-
55
- gem build awshark.gemspec
56
- gem install --local awshark-1.1.0.gem
57
- rm awshark-1.1.0.gem
58
-
59
62
 
60
63
  ### Contributing
61
64
 
data/awshark.gemspec CHANGED
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_dependency 'aws-sdk-cloudwatch'
35
35
  spec.add_dependency 'aws-sdk-ec2'
36
36
  spec.add_dependency 'aws-sdk-ecr'
37
+ spec.add_dependency 'aws-sdk-ecs'
37
38
  spec.add_dependency 'aws-sdk-rds'
38
39
  spec.add_dependency 'aws-sdk-s3'
39
40
  spec.add_dependency 'aws-sdk-ssm', '~> 1.117.0'
data/lib/awshark/cli.rb CHANGED
@@ -16,8 +16,6 @@ module Awshark
16
16
  map '-v' => :version
17
17
 
18
18
  class_option :help, type: :boolean, desc: 'Prints this help'
19
- class_option :profile, type: :string, desc: 'Load the AWS credentials profile named PROFILE.'
20
- class_option :region, type: :string, desc: 'Sets the AWS region name REGION.'
21
19
 
22
20
  desc 'cf COMMAND', 'Run CloudFormation command'
23
21
  subcommand 'cf', Awshark::Subcommands::CloudFormation
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ module CloudFormation
5
+ class Configuration
6
+ attr_reader :event_polling
7
+
8
+ def initialize
9
+ @event_polling = 3
10
+ end
11
+
12
+ def client
13
+ @client ||= begin
14
+ region = Aws.config[:region]
15
+ Aws::CloudFormation::Client.new(region: region)
16
+ end
17
+ end
18
+
19
+ attr_writer :client
20
+ end
21
+ end
22
+ end
@@ -69,10 +69,7 @@ module Awshark
69
69
  private
70
70
 
71
71
  def client
72
- return Awshark.config.cloud_formation.client if Awshark.config.cloud_formation.client
73
-
74
- region = Aws.config[:region]
75
- @client ||= Aws::CloudFormation::Client.new(region: region)
72
+ Awshark.config.cloud_formation.client
76
73
  end
77
74
 
78
75
  def get_stack(stack_name)
@@ -35,7 +35,7 @@ module Awshark
35
35
 
36
36
  {
37
37
  context: RecursiveOpenStruct.new(context),
38
- aws_account_id: Awshark.config.aws_account_id,
38
+ aws_account_id: Awshark.config.sts.aws_account_id,
39
39
  stage: stage,
40
40
  ssm: ssm
41
41
  }
@@ -69,13 +69,11 @@ module Awshark
69
69
  end
70
70
 
71
71
  def region
72
- Aws.config[:region] || 'eu-central-1'
72
+ Awshark.config.s3.region
73
73
  end
74
74
 
75
75
  def s3
76
- return Awshark.config.s3.client if Awshark.config.s3.client
77
-
78
- @s3 ||= Aws::S3::Client.new(region: region, signature_version: 'v4')
76
+ Awshark.config.s3.client
79
77
  end
80
78
 
81
79
  def s3_key
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ module Ecs
5
+ class Cluster
6
+ attr_reader :arn
7
+
8
+ def initialize(arn)
9
+ @arn = arn
10
+ end
11
+
12
+ def name
13
+ arn.split('/').last
14
+ end
15
+
16
+ def tasks
17
+ @tasks ||= begin
18
+ response = client.list_tasks(cluster: arn)
19
+ response = client.describe_tasks(cluster: arn, tasks: response.task_arns)
20
+ response.tasks
21
+ end
22
+ end
23
+
24
+ def services
25
+ @services ||= begin
26
+ response = client.list_services(cluster: arn)
27
+ response = client.describe_services(cluster: arn, services: response.service_arns)
28
+ response.services
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def client
35
+ Awshark.config.ecs.client
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ module Ecs
5
+ class Configuration
6
+ def client
7
+ @client ||= begin
8
+ region = Aws.config[:region]
9
+ Aws::ECS::Client.new(region: region)
10
+ end
11
+ end
12
+
13
+ attr_writer :client
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ module Ecs
5
+ class Manager
6
+ def clusters
7
+ response = client.list_clusters
8
+ response.cluster_arns.map { |arn| Ecs::Cluster.new(arn) }
9
+ end
10
+
11
+ def inspect_cluster(cluster)
12
+ args = { name: cluster.name, services: cluster.services.size, tasks: cluster.tasks.size }
13
+ format("Cluster: %-20<name>s %<services>s services %<tasks>s tasks\n", args)
14
+ end
15
+
16
+ # https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/ECS/Types/Container.html
17
+ #
18
+ # @param task [Aws::ECS::Types::Container]
19
+ def inspect_container(container)
20
+ args = { name: container.name, health: container.health_status, status: container.last_status }
21
+ format("- %-15<name>s status: %<status>s\n", args)
22
+ end
23
+
24
+ # https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/ECS/Types/Service.html
25
+ #
26
+ # @param task [Aws::ECS::Types::Service]
27
+ def inspect_service(service)
28
+ task = task_definition(service.task_definition)
29
+ lines = []
30
+
31
+ args = {
32
+ name: service.service_name,
33
+ status: service.status,
34
+ running: service.running_count,
35
+ cpu: "#{task.cpu}MB",
36
+ memory: "#{task.memory}MB"
37
+ }
38
+ lines << format('Service: %-30<name>s status: %-8<status>s running: %-4<running>s ' \
39
+ "cpu: %-8<cpu>s mem: %<memory>s\n", args)
40
+ task.container_definitions.each do |c|
41
+ lines << format("- %-15<name>s\n", name: c.name)
42
+ end
43
+
44
+ lines.join
45
+ end
46
+
47
+ # https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/ECS/Types/Task.html
48
+ #
49
+ # @param task [Aws::ECS::Types::Task]
50
+ def inspect_task(task)
51
+ lines = []
52
+
53
+ args = { type: task.launch_type, cpu: "#{task.cpu} MB", memory: "#{task.memory} MB" }
54
+ lines << format("Task: %-20<type>s cpu: %-10<cpu>s memory: %<memory>s\n", args)
55
+
56
+ task.containers.each { |c| lines << inspect_container(c) }
57
+
58
+ lines.join
59
+ end
60
+
61
+ def task_definition(family)
62
+ response = client.describe_task_definition(task_definition: family)
63
+ response.task_definition
64
+ end
65
+
66
+ private
67
+
68
+ def client
69
+ Awshark.config.ecs.client
70
+ end
71
+ end
72
+ end
73
+ end
@@ -2,15 +2,13 @@
2
2
 
3
3
  module Awshark
4
4
  class ProfileResolver
5
- attr_reader :region
6
-
7
5
  def initialize(options)
8
6
  @profile = options[:profile] || ENV['AWS_PROFILE']
9
7
  @shared_config = ::Aws::SharedConfig.new(
10
8
  profile_name: @profile,
11
9
  config_enabled: true
12
10
  )
13
- @region = options[:region] || @shared_config.region || 'eu-central-1'
11
+ @region = options[:region] || ENV['REGION']
14
12
  end
15
13
 
16
14
  def credentials
@@ -32,6 +30,13 @@ module Awshark
32
30
  @shared_config.credentials
33
31
  end
34
32
 
33
+ # Returns Aws region from option, env or .aws/config
34
+ #
35
+ # @returns [String]
36
+ def region
37
+ @region || @shared_config.region || 'eu-central-1'
38
+ end
39
+
35
40
  # Returns Aws credentials for configuration with
36
41
  # [profile]
37
42
  # role_arn = ROLE_ARN
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ module S3
5
+ class Configuration
6
+ def client
7
+ @client ||= Aws::S3::Client.new(region: region, signature_version: 'v4')
8
+ end
9
+
10
+ def region
11
+ Aws.config[:region] || 'eu-central-1'
12
+ end
13
+
14
+ attr_writer :client
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ module Sts
5
+ class Configuration
6
+ def aws_account_id
7
+ @aws_account_id ||= begin
8
+ response = client.get_caller_identity
9
+ response.account
10
+ end
11
+ end
12
+
13
+ def client
14
+ @client ||= Aws::STS::Client.new
15
+ end
16
+
17
+ attr_writer :client
18
+ end
19
+ end
20
+ end
@@ -1,12 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'aws-sdk-ecr'
4
+ require 'aws-sdk-ecs'
5
+
6
+ require 'awshark/ecs/cluster'
7
+ require 'awshark/ecs/manager'
4
8
 
5
9
  module Awshark
6
10
  module Subcommands
7
11
  class Ecs < Thor
8
12
  include Awshark::Subcommands::ClassOptions
9
13
 
14
+ desc 'list', 'list ECS tasks'
15
+ long_desc <<-LONGDESC
16
+ List all running ECS tasks and services.
17
+
18
+ Example: `awshark ecs list`
19
+ LONGDESC
20
+ def list
21
+ process_class_options
22
+
23
+ manager = Awshark::Ecs::Manager.new
24
+ manager.clusters.each do |cluster|
25
+ puts "\n"
26
+ puts manager.inspect_cluster(cluster)
27
+ cluster.services.each do |service|
28
+ puts manager.inspect_service(service)
29
+ end
30
+ end
31
+ end
32
+
10
33
  desc 'login', 'docker login to AWS ECR'
11
34
  long_desc <<-LONGDESC
12
35
  Use docker login with AWS credentials to Elastic Container Registry
@@ -19,7 +42,7 @@ module Awshark
19
42
 
20
43
  user_name = token.split(':').first
21
44
  password = token.split(':').last
22
- url = "https://#{Awshark.config.aws_account_id}.dkr.ecr.eu-central-1.amazonaws.com"
45
+ url = "https://#{Awshark.config.sts.aws_account_id}.dkr.ecr.eu-central-1.amazonaws.com"
23
46
 
24
47
  `docker login -u #{user_name} -p #{password} #{url}`
25
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Awshark
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
data/lib/awshark.rb CHANGED
@@ -6,13 +6,28 @@ require 'thor'
6
6
  require 'yaml'
7
7
 
8
8
  require 'awshark/version'
9
- require 'awshark/configuration'
9
+ require 'awshark/cloud_formation/configuration'
10
+ require 'awshark/ecs/configuration'
11
+ require 'awshark/s3/configuration'
12
+ require 'awshark/sts/configuration'
10
13
 
11
14
  module Awshark
12
15
  class GracefulFail < StandardError; end
13
16
 
14
17
  def self.config
15
- @config ||= Configuration.new
18
+ @config ||= begin
19
+ cf = CloudFormation::Configuration.new
20
+ ecs = Ecs::Configuration.new
21
+ s3 = S3::Configuration.new
22
+ sts = Sts::Configuration.new
23
+
24
+ OpenStruct.new(
25
+ cloud_formation: cf,
26
+ ecs: ecs,
27
+ s3: s3,
28
+ sts: sts
29
+ )
30
+ end
16
31
  end
17
32
 
18
33
  def self.configure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awshark
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joergen Dahlke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-26 00:00:00.000000000 Z
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: aws-sdk-ecs
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: aws-sdk-rds
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -239,6 +253,7 @@ files:
239
253
  - gems.rb
240
254
  - lib/awshark.rb
241
255
  - lib/awshark/cli.rb
256
+ - lib/awshark/cloud_formation/configuration.rb
242
257
  - lib/awshark/cloud_formation/file_loading.rb
243
258
  - lib/awshark/cloud_formation/inferrer.rb
244
259
  - lib/awshark/cloud_formation/manager.rb
@@ -246,15 +261,19 @@ files:
246
261
  - lib/awshark/cloud_formation/stack.rb
247
262
  - lib/awshark/cloud_formation/stack_events.rb
248
263
  - lib/awshark/cloud_formation/template.rb
249
- - lib/awshark/configuration.rb
250
264
  - lib/awshark/ec2/instance.rb
251
265
  - lib/awshark/ec2/manager.rb
266
+ - lib/awshark/ecs/cluster.rb
267
+ - lib/awshark/ecs/configuration.rb
268
+ - lib/awshark/ecs/manager.rb
252
269
  - lib/awshark/profile_resolver.rb
253
270
  - lib/awshark/rds/check_reservations.rb
254
271
  - lib/awshark/rds/manager.rb
255
272
  - lib/awshark/s3/artifact.rb
256
273
  - lib/awshark/s3/bucket.rb
274
+ - lib/awshark/s3/configuration.rb
257
275
  - lib/awshark/s3/manager.rb
276
+ - lib/awshark/sts/configuration.rb
258
277
  - lib/awshark/subcommands/class_options.rb
259
278
  - lib/awshark/subcommands/cloud_formation.rb
260
279
  - lib/awshark/subcommands/ec2.rb
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Awshark
4
- class Configuration
5
- attr_accessor :cloud_formation, :s3
6
-
7
- def initialize
8
- @cloud_formation = OpenStruct.new(
9
- client: nil,
10
- event_polling: 3
11
- )
12
-
13
- @s3 = OpenStruct.new(
14
- client: nil
15
- )
16
- end
17
-
18
- def aws_account_id
19
- return @aws_account_id if defined?(@aws_account_id)
20
-
21
- response = sts_client.get_caller_identity
22
-
23
- @aws_account_id = response.account
24
- end
25
-
26
- def sts_client
27
- @sts_client ||= Aws::STS::Client.new
28
- end
29
-
30
- private
31
-
32
- attr_writer :sts_client
33
- end
34
- end