firespring_dev_commands 1.4.4 → 1.5.0.pre.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35c3ded1d9447a6d6e2867a9ab430f75329391ce29e634b92d22202216898cee
4
- data.tar.gz: cab9bd35fbc64bca310156f6b91ab9c4500c3aca29453a40064768b094aa68f0
3
+ metadata.gz: ea3652c102b7a6ec3395c7b524db518655b574a6050d8f740f2c20daedf63aed
4
+ data.tar.gz: 162e4aa151f14b901b7fcfea1c2b8e3cb1b20ab3de2d9102d00a35b0754e5773
5
5
  SHA512:
6
- metadata.gz: 18b2a90783750ba4417c78d9da0e6b1830585791bbbb2f0709d1c0542d64db5a5be08122ee1f2742acbb87c5fed8c28f70dd40f9d3b7f1f2999ed0e9bf40bd77
7
- data.tar.gz: a2f4eed55a1c3c79a595a30fa496e0679e2924f7b3f40244c206bac4120175408bf3d75f55496f73f5f4eee35bd070abcc0bf794298fdaaac05c0ed8664a2035
6
+ metadata.gz: 724282fb3154de07595863a899f29b1ffeea0e5fda1d3d2aa5fb31fd3e17f57bfbc0e9baaf534c4a0e259b8b95b051ba05b6d7072d1cdd4bc544799f665e519a
7
+ data.tar.gz: b60bf002f396a052f0814838940dcb441d95c263a4001bdbdcd18878d0d1a8ee0b7f1a8b5b4026ce43664a92666ab93b23a27e0d9d765669f827171c377d4d3d
@@ -3,14 +3,13 @@ module Dev
3
3
  # Class containing useful methods for interacting with the Aws account
4
4
  class Account
5
5
  # Config object for setting top level Aws account config options
6
- # TODO: registry is deprecated and should be removed on the next major release
7
- Config = Struct.new(:root, :children, :default, :registry, :ecr_registry_ids, :login_to_account_ecr_registry)
6
+ Config = Struct.new(:root, :children, :default, :registry, :default_login_role_name)
8
7
 
9
8
  # Instantiates a new top level config object if one hasn't already been created
10
9
  # Yields that config object to any given block
11
10
  # Returns the resulting config object
12
11
  def self.config
13
- @config ||= Config.new
12
+ @config ||= Config.new(default_login_role_name: Dev::Aws::DEFAULT_LOGIN_ROLE_NAME)
14
13
  yield(@config) if block_given?
15
14
  @config
16
15
  end
@@ -23,8 +22,7 @@ module Dev
23
22
  # The name of the file containing the Aws settings
24
23
  CONFIG_FILE = "#{Dev::Aws::CONFIG_DIR}/config".freeze
25
24
 
26
- # TODO: registry is deprecated and should be removed on the next major release
27
- attr_accessor :root, :children, :default, :registry, :ecr_registry_ids
25
+ attr_accessor :root, :children, :default, :registry
28
26
 
29
27
  # Instantiate an account object
30
28
  # Requires that root account and at least one child account have been configured
@@ -37,13 +35,7 @@ module Dev
37
35
  @root = self.class.config.root
38
36
  @children = self.class.config.children
39
37
  @default = self.class.config.default
40
-
41
- # Create the ecr registry list based off several possible configuration values
42
- @ecr_registry_ids = [self.class.config.registry]
43
- @ecr_registry_ids << Dev::Aws::Profile.new.current if self.class.config.login_to_account_ecr_registry
44
- @ecr_registry_ids.concat(Array(self.class.config.ecr_registry_ids))
45
- @ecr_registry_ids = @ecr_registry_ids.flatten.compact.reject(&:empty?).uniq
46
- @registry = @ecr_registry_ids.first
38
+ @registry = self.class.config.registry
47
39
  end
48
40
 
49
41
  # Returns all configured account information objects
@@ -63,7 +55,7 @@ module Dev
63
55
 
64
56
  # Look up the account name for the given account id
65
57
  def name_by_account(account)
66
- all.find { |it| it.id == account }&.name
58
+ all.find { |it| it.id == account }.name
67
59
  end
68
60
 
69
61
  # Setup base Aws settings
@@ -81,8 +73,13 @@ module Dev
81
73
  region_default = defaultini['region'] || ENV['AWS_DEFAULT_REGION'] || Dev::Aws::DEFAULT_REGION
82
74
  defaultini['region'] = Dev::Common.new.ask('Default region name', region_default)
83
75
 
84
- mfa_default = defaultini['mfa_serial'] || ENV['AWS_MFA_ARN'] || "arn:aws:iam::#{root.id}:mfa/#{ENV.fetch('USERNAME', nil)}"
85
- defaultini['mfa_serial'] = Dev::Common.new.ask('Default mfa arn', mfa_default)
76
+ # NOTE: We had an old config for "mfa_serial" which included the entire arn. We deprecated that config since
77
+ # it made it much more difficult to switch between different root accounts.
78
+ mfa_name_default = defaultini['mfa_serial']&.split(%r{mfa/})&.last || ENV['AWS_MFA_ARN']&.split(%r{mfa/})&.last || ENV.fetch('USERNAME', nil)
79
+ defaultini['mfa_serial_name'] = Dev::Common.new.ask('Default mfa name', mfa_name_default)
80
+ # TODO: Eventually, we should delete the mfa_serial entry from the config. Leaving it for now because some projects
81
+ # may be using older versions of the dev_commands library
82
+ # defaultini.delete('mfa_serial')
86
83
 
87
84
  session_name_default = defaultini['role_session_name'] || "#{ENV.fetch('USERNAME', nil)}_cli"
88
85
  defaultini['role_session_name'] = Dev::Common.new.ask('Default session name', session_name_default)
@@ -119,8 +116,13 @@ module Dev
119
116
  region_default = profileini['region'] || defaultini['region'] || ENV['AWS_DEFAULT_REGION'] || Dev::Aws::DEFAULT_REGION
120
117
  profileini['region'] = Dev::Common.new.ask('Default region name', region_default)
121
118
 
122
- role_default = profileini['role_arn'] || "arn:aws:iam::#{account}:role/ReadonlyAccessRole"
123
- profileini['role_arn'] = Dev::Common.new.ask('Default role arn', role_default)
119
+ # NOTE: We had an old config for "role_arn" which included the entire arn. We deprecated that config since
120
+ # it made it much more difficult to switch between different accounts.
121
+ role_name_default = profileini['role_name'] || profileini['role_arn']&.split(%r{role/})&.last || self.class.config.default_login_role_name
122
+ profileini['role_name'] = Dev::Common.new.ask('Default role name', role_name_default)
123
+ # TODO: Eventually, we should delete the role_arn entry from the config. Leaving it for now because some projects
124
+ # may be using older versions of the dev_commands library
125
+ # profileini.delete('role_arn')
124
126
 
125
127
  cfgini.write
126
128
  end
@@ -16,16 +16,6 @@ module Dev
16
16
  ::Aws::STS::Client.new.get_caller_identity.account
17
17
  end
18
18
 
19
- # The arn of the currently logged in identity
20
- def logged_in_arn
21
- ::Aws::STS::Client.new.get_caller_identity.arn
22
- end
23
-
24
- # The role the current identity is using
25
- def logged_in_role
26
- logged_in_arn.split(%r{/})[1]
27
- end
28
-
29
19
  # The region associated with the current login
30
20
  def logged_in_region
31
21
  ::Aws::STS::Client.new.send(:config).region
@@ -75,6 +65,8 @@ module Dev
75
65
  credini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/credentials", default: 'default')
76
66
  defaultini = credini['default']
77
67
 
68
+ # TODO: Should we allow for multiple sets of base credentials? How do I use this for both FDP and SBF?
69
+
78
70
  access_key_default = defaultini['aws_access_key_id']
79
71
  defaultini['aws_access_key_id'] = Dev::Common.new.ask('AWS Access Key ID', access_key_default)
80
72
 
@@ -8,7 +8,7 @@ module Dev
8
8
  class Login
9
9
  # Main interface for logging in to an AWS account
10
10
  # If an account is not specified the user is given an account selection menu
11
- # If account registries have been configured, the user is also logged in to the docker registries
11
+ # If an account registry has been configured, the user is also logged in to the docker registry
12
12
  def login!(account = nil)
13
13
  # If more than one child account has been configured, have the user select the account they want to log in to
14
14
  account ||= Dev::Aws::Account.new.select
@@ -22,8 +22,8 @@ module Dev
22
22
  # Load credentials into the ENV for subprocesses
23
23
  Dev::Aws::Credentials.new.export!
24
24
 
25
- # Login in to all configured docker registries
26
- registry_logins!
25
+ # Login in to the docker registry if the user has configured one
26
+ registry_login! if Dev::Aws::Account.new.registry
27
27
  end
28
28
 
29
29
  # Authorize your local credentials
@@ -31,28 +31,32 @@ module Dev
31
31
  # Temporary credentials are written back to the credentials file
32
32
  def authorize!(account)
33
33
  # Make sure the account has been set up
34
- cfgini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/config", default: 'default')
35
- unless cfgini.has_section?("profile #{account}")
36
- Dev::Aws::Account.new.write!(account)
37
- cfgini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/config", default: 'default')
38
- end
34
+ cfgini = setup_cfgini
39
35
 
40
36
  defaultini = cfgini['default']
41
37
  profileini = cfgini["profile #{account}"]
42
38
 
43
- serial = profileini['mfa_serial'] || defaultini['mfa_serial']
44
- role = profileini['role_arn'] || defaultini['role_arn']
39
+ region = profileini['region'] || defaultini['region'] || Dev::Aws::DEFAULT_REGION
40
+
41
+ serial = profileini['mfa_serial_name'] || defaultini['mfa_serial_name']
42
+ serial = "arn:aws:iam::#{Dev::Aws::Account.new.roo.id}:mfa/#{serial}" if serial
43
+ serial ||= profileini['mfa_serial'] || defaultini['mfa_serial']
44
+
45
+ role = profileini['role_name'] || defaultini['role_name']
46
+ role = "arn:aws:iam::#{account}:role/#{role}" if role
47
+ role ||= profileini['role_arn'] || defaultini['role_arn']
48
+
45
49
  session_name = profileini['role_session_name'] || defaultini['role_session_name']
46
50
  session_duration = profileini['session_duration'] || defaultini['session_duration']
47
51
 
48
52
  puts
49
- puts " Logging in to #{account} as #{role}".light_yellow
53
+ puts " Logging in to #{account} in #{region} as #{role}".light_yellow
50
54
  puts
51
55
 
52
56
  code = ENV['AWS_TOKEN_CODE'] || Dev::Common.new.ask("Enter the MFA code for the #{ENV.fetch('USERNAME', '')} user serial #{serial}")
53
57
  raise 'MFA is required' unless code.to_s.strip
54
58
 
55
- sts = ::Aws::STS::Client.new(profile: 'default')
59
+ sts = ::Aws::STS::Client.new(profile: 'default', region: region)
56
60
  creds = sts.assume_role(
57
61
  serial_number: serial,
58
62
  role_arn: role,
@@ -65,19 +69,21 @@ module Dev
65
69
  Dev::Aws::Credentials.new.write!(account, creds)
66
70
  end
67
71
 
68
- # Authroizes the docker cli to pull/push images from the Aws container registry (e.g. if docker compose needs to pull an image)
69
- # Authroizes the docker ruby library to pull/push images from the Aws container registry
70
- def registry_logins!(registry_ids: Dev::Aws::Account.new.ecr_registry_ids, region: Dev::Aws::DEFAULT_REGION)
71
- return if registry_ids.empty?
72
-
73
- puts
74
- registry_ids.each { |id| registry_login!(registry_id: id, region: region) }
75
- puts
72
+ # Returns the config ini file
73
+ # Runs the setup for our current account if it's not already setup
74
+ def setup_cfgini
75
+ cfgini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/config", default: 'default')
76
+ unless cfgini.has_section?("profile #{account}")
77
+ Dev::Aws::Account.new.write!(account)
78
+ cfgini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/config", default: 'default')
79
+ end
80
+ cfgini
76
81
  end
77
82
 
78
83
  # Authroizes the docker cli to pull/push images from the Aws container registry (e.g. if docker compose needs to pull an image)
79
84
  # Authroizes the docker ruby library to pull/push images from the Aws container registry
80
- def registry_login!(registry_id: Dev::Aws::Account.new.ecr_registry_ids.first, region: Dev::Aws::DEFAULT_REGION)
85
+ def registry_login!(registry_id: Dev::Aws::Account.new.registry, region: nil)
86
+ region ||= Dev::Aws::Credentials.new.logged_in_region || Dev::Aws::DEFAULT_REGION
81
87
  raise 'registry_id is required' if registry_id.to_s.strip.empty?
82
88
  raise 'region is required' if region.to_s.strip.empty?
83
89
 
@@ -92,25 +98,27 @@ module Dev
92
98
  # Authroizes the docker cli to pull/push images from the Aws container registry
93
99
  # (e.g. if docker compose needs to pull an image)
94
100
  # @deprecated Please use {Dev::Aws::Login#registry_login!} instead
95
- def docker_login!(registry_id: Dev::Aws::Account.new.ecr_registry_ids.first, region: Dev::Aws::DEFAULT_REGION)
101
+ def docker_login!(registry_id: Dev::Aws::Account.new.registry, region: nil)
102
+ region ||= Dev::Aws::Credentials.new.logged_in_region || Dev::Aws::DEFAULT_REGION
96
103
  warn '[DEPRECATION] `Dev::Aws::Login#docker_login!` is deprecated. Please use `Dev::Aws::Login#registry_login!` instead.'
97
104
  docker_cli_login!(registry: "#{registry_id}.dkr.ecr.#{region}.amazonaws.com", region: region)
98
- puts
99
105
  end
100
106
 
101
107
  # Authroizes the docker cli to pull/push images from the Aws container registry
102
108
  # (e.g. if docker compose needs to pull an image)
103
109
  private def docker_cli_login!(registry:, region:)
104
- print(" Logging in to #{registry} in docker... ")
110
+ print(' Logging in to ECR in docker... ')
105
111
  login_cmd = "aws --profile=#{Dev::Aws::Profile.new.current} ecr --region=#{region} get-login-password"
106
112
  login_cmd << ' | '
107
113
  login_cmd << "docker login --password-stdin --username AWS #{registry}"
108
114
  Dev::Common.new.run_command([login_cmd])
115
+ puts
109
116
  end
110
117
 
111
118
  # Authroizes the docker ruby library to pull/push images from the Aws container registry
112
119
  # @deprecated Please use {Dev::Aws::Login#registry_login!} instead
113
- def ecr_login!(registry_id: Dev::Aws::Account.new.ecr_registry_ids.first, region: Dev::Aws::DEFAULT_REGION)
120
+ def ecr_login!(registry_id: Dev::Aws::Account.new.registry, region: nil)
121
+ region ||= Dev::Aws::Credentials.new.logged_in_region || Dev::Aws::DEFAULT_REGION
114
122
  warn '[DEPRECATION] `Dev::Aws::Login#ecr_login!` is deprecated. Please use `Dev::Aws::Login#registry_login!` instead.'
115
123
  docker_lib_login!(registry_id: registry_id, region: region)
116
124
  end
@@ -33,10 +33,8 @@ module Dev
33
33
  # Print the profile info for the current account
34
34
  def info
35
35
  Dev::Aws::Credentials.new.export!
36
- current_role = Dev::Aws::Credentials.new.logged_in_role
37
- current_account_name = Dev::Aws::Account.new.name_by_account(current)
38
36
  puts
39
- puts " Currently logged in as #{current_role} in the #{current_account_name} (#{current}) account".light_yellow
37
+ puts " Currently logged in to the #{Dev::Aws::Account.new.name_by_account(current)} (#{current})".light_yellow
40
38
  puts
41
39
  puts ' To use this profile in your local aws cli, you must either pass the profile as a command line argument ' \
42
40
  'or export the corresponding aws variable:'.light_white
@@ -4,7 +4,10 @@ module Dev
4
4
  # The config dir for the user's AWS settings
5
5
  CONFIG_DIR = "#{Dir.home}/.aws".freeze
6
6
 
7
- # The default region used if none have been configured in the AWS settings
7
+ # The default region used if none has been configured in the AWS settings
8
8
  DEFAULT_REGION = 'us-east-1'.freeze
9
+
10
+ # The default role name used if none has been configured when logging in
11
+ DEFAULT_LOGIN_ROLE_NAME = 'ReadonlyAccessRole'.freeze
9
12
  end
10
13
  end
@@ -70,11 +70,8 @@ module Dev
70
70
  end
71
71
 
72
72
  # Prunes/removes all unused volumes
73
- # Specify ALL_VOLUMES=false in your environment to only clean anonymous volumes (docker version 23.x+)
74
73
  def prune_volumes
75
- opts = {}
76
- opts[:filters] = {all: ['true']}.to_json if Dev::Common.new.version_greater_than('22.9999.0', self.class.version) && ENV['ALL_VOLUMES'].to_s.strip != 'false'
77
- _prune('volumes', opts: opts)
74
+ _prune('volumes')
78
75
  end
79
76
 
80
77
  # Prunes/removes all unused images
@@ -83,8 +80,8 @@ module Dev
83
80
  end
84
81
 
85
82
  # Private method which actually calls the prune endpoint on the docker api connection
86
- private def _prune(type, opts: {})
87
- response = ::Docker.connection.post("/#{type.downcase}/prune", opts)
83
+ private def _prune(type)
84
+ response = ::Docker.connection.post("/#{type.downcase}/prune", {})
88
85
  format_prune(type, response)
89
86
  rescue ::Docker::Error::ServerError => e
90
87
  # Specifically check for 'prune already running' error and retry if found
@@ -137,7 +137,7 @@ module Dev
137
137
  DEV_COMMANDS_TOP_LEVEL.instance_eval do
138
138
  namespace application do
139
139
  namespace :php do
140
- desc 'Install all composer packages'
140
+ desc 'Install all npm packages'
141
141
  task install: %w(init_docker up_no_deps) do
142
142
  Dev::Docker::Compose.new(services: application).exec(*php.install_command)
143
143
  end
@@ -93,7 +93,7 @@ module Dev
93
93
  DEV_COMMANDS_TOP_LEVEL.instance_eval do
94
94
  namespace application do
95
95
  namespace :ruby do
96
- desc 'Install all bundled gems'
96
+ desc 'Install all npm packages'
97
97
  task install: %w(init_docker up_no_deps) do
98
98
  Dev::Docker::Compose.new(services: application).exec(*ruby.install_command)
99
99
  end
@@ -6,6 +6,6 @@ module Dev
6
6
  # Use 'v.v.v.pre.alpha.v' for pre-release vesions
7
7
  # Use 'v.v.v.beta.v for beta versions
8
8
  # Use semantic versioning for any releases (https://semver.org/)
9
- VERSION = '1.4.4'.freeze
9
+ VERSION = '1.5.0.pre.alpha.1'.freeze
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firespring_dev_commands
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.5.0.pre.alpha.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Firespring
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-17 00:00:00.000000000 Z
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -453,9 +453,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
453
453
  version: '2.7'
454
454
  required_rubygems_version: !ruby/object:Gem::Requirement
455
455
  requirements:
456
- - - ">="
456
+ - - ">"
457
457
  - !ruby/object:Gem::Version
458
- version: '0'
458
+ version: 1.3.1
459
459
  requirements: []
460
460
  rubygems_version: 3.1.6
461
461
  signing_key: