Class: App::AWSProfile
- Inherits:
-
Object
- Object
- App::AWSProfile
- Defined in:
- lib/aws/aws_profile.rb
Constant Summary collapse
- FILE_AWS_CONFIG =
File.('~/.aws/config')
- FILE_AWS_CREDENTIALS =
File.('~/.aws/credentials')
- PROFILE_DEFAULT =
'DefaultProfile'
- PROFILE =
'Profile'
- PROFILES =
'Profiles'
- CLOUDFORMATION =
'CloudFormation'
- SSH_KEYS =
'SSHKeys'
- PROJECTS =
'Projects'
- STACKS =
'Stacks'
- @@profiles =
nil
- @@profile =
nil
- @@credentials =
nil
- @@ssh_users =
nil
Class Method Summary collapse
-
.download_s3_ssh_users(use_cache = true) ⇒ Object
Gets Users from S3.
-
.get_credentials ⇒ Object
Gets AWS credentials from ~/.aws directory for given profile.
-
.get_profile ⇒ Object
Gets the active profile.
-
.get_profile_name ⇒ Object
Convenience method to just get the profile name.
-
.get_profile_names ⇒ Object
Returns an Array of all available profile names (not the entire profile).
-
.get_ssh_users ⇒ Object
Gets configured SSH users.
-
.init(config_data) ⇒ Object
Reads the config data and decides what profile to use.
Class Method Details
.download_s3_ssh_users(use_cache = true) ⇒ Object
Gets Users from S3. Can be called multiple times (which you might do if you want to invalidate the cache).
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/aws/aws_profile.rb', line 140 def self.download_s3_ssh_users(use_cache = true) if @@profile.has_key?(SSH_KEYS) s3 = @@profile[SSH_KEYS]['S3Bucket'] tmp_path = Blufin::AWS::download_s3_data(s3['Name'], s3['Path'], profile: @@profile[PROFILE], region: s3['Region'], use_cache: use_cache) tmp_path_files = Blufin::Files::get_files_in_dir(tmp_path) users = [] # Gets a unique list of users (since every user has 2 files, private and public key). if Blufin::Files::path_exists(tmp_path) && tmp_path_files.is_a?(Array) && tmp_path_files.any? tmp_path_files.each do |file| users << Blufin::Files::extract_file_name(file, false).gsub(/\.pub$/i, '') end end @@ssh_users = {} unless use_cache users.uniq! users.sort! users.each do |user| pub_key = "#{tmp_path}/#{user}.pub" @@ssh_users[user] = Blufin::Files::file_exists(pub_key) ? pub_key : nil end tmp_path end end |
.get_credentials ⇒ Object
Gets AWS credentials from ~/.aws directory for given profile. If credentials don’t exist (or are missing information) – nil is returned.
123 124 125 |
# File 'lib/aws/aws_profile.rb', line 123 def self.get_credentials @@credentials end |
.get_profile ⇒ Object
Gets the active profile.
103 104 105 |
# File 'lib/aws/aws_profile.rb', line 103 def self.get_profile @@profile end |
.get_profile_name ⇒ Object
Convenience method to just get the profile name.
109 110 111 112 |
# File 'lib/aws/aws_profile.rb', line 109 def self.get_profile_name return nil if @@credentials.nil? @@profile[PROFILE] end |
.get_profile_names ⇒ Object
Returns an Array of all available profile names (not the entire profile).
116 117 118 |
# File 'lib/aws/aws_profile.rb', line 116 def self.get_profile_names @@profiles.keys end |
.get_ssh_users ⇒ Object
Gets configured SSH users. Must be configured in YML and S3.
129 130 131 132 133 134 135 |
# File 'lib/aws/aws_profile.rb', line 129 def self.get_ssh_users @@ssh_users.each do |user, pub_key| Blufin::Terminal::error("Public key not found for user: #{Blufin::Terminal::format_invalid(user)}", "Expected file to exist: #{Blufin::Terminal::format_directory("#{user}.pub", false)}", true) if pub_key.nil? end raise RuntimeError, 'SSHUser Hash is empty. Atleast one user is required.' unless @@ssh_users.is_a?(Hash) && @@ssh_users.any? @@ssh_users end |
.init(config_data) ⇒ Object
Reads the config data and decides what profile to use.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/aws/aws_profile.rb', line 22 def self.init(config_data) raise RuntimeError, 'Cannot run App::AWSProfile::init more than once.' unless @@profiles.nil? && @@profile.nil? && @@credentials.nil? && @@ssh_users.nil? @@profiles = {} @@profile = {} @@ssh_users = {} first_key = nil # Put all the profiles in a global Hash. config_data[PROFILES].each do |profile| first_key = profile[PROFILE] if first_key.nil? @@profiles[profile[PROFILE]] = profile end if @@profiles.length == 1 @@profile = @@profiles[first_key] elsif @@profiles.length > 1 if config_data[PROFILE_DEFAULT].nil? Blufin::Terminal::error('No default profile found.', "When more than 1 profile is specified, you must also specify a #{Blufin::Terminal::format_highlight('DefaultProfile:')}", true) elsif !@@profiles.keys.include?(config_data[PROFILE_DEFAULT]) Blufin::Terminal::error("Invalid profile: #{Blufin::Terminal::format_invalid(config_data[PROFILE_DEFAULT])}. Available profiles are:", @@profiles.keys, true) else @@profile = @@profiles[config_data[PROFILE_DEFAULT]] end else raise RuntimeError, 'No profiles found.' end errors = [] # Validate CloudFormation data (if exist). if @@profile.has_key?(CLOUDFORMATION) if @@profile[CLOUDFORMATION]['Templates'].has_key?('Local') cloudformation_template_path = @@profile[CLOUDFORMATION]['Templates']['Local']['Path'] errors << "Path not found: #{Blufin::Terminal::format_invalid(cloudformation_template_path)}" unless Blufin::Files::path_exists(cloudformation_template_path) end s3_region = @@profile[CLOUDFORMATION]['Uploads']['S3Bucket']['Region'] default_regions = @@profile[CLOUDFORMATION]['Defaults']['Regions'] errors << "Invalid region: #{Blufin::Terminal::format_invalid(s3_region)}" unless App::AWS::VALID_REGIONS.include?(s3_region) errors << "Need atleast 1 default region for: #{Blufin::Terminal::format_invalid('Profiles[].CloudFormation.Defaults.Regions')}" if default_regions.nil? || !default_regions.any? default_regions.each do |default_region| errors << "Invalid region: #{Blufin::Terminal::format_invalid(default_region)}" unless App::AWS::VALID_REGIONS.include?(default_region) end end # Validate SSHKeys (if exist). download_s3_ssh_users(true) # Check the credentials exist. if Blufin::Files::file_exists(FILE_AWS_CREDENTIALS) @@credentials = App::AWSCredentials.new profile = @@profile[PROFILE] config = Blufin::Files::file_exists(FILE_AWS_CONFIG) ? ParseConfig.new(FILE_AWS_CONFIG) : nil credentials = ParseConfig.new(FILE_AWS_CREDENTIALS) unless credentials.params[profile].nil? # Currently not used/required (but here just in case). unless config.nil? || config.params[profile].nil? @@credentials.region = config.params[profile]['region'] unless config.params[profile]['region'].nil? @@credentials.output = config.params[profile]['output'] unless config.params[profile]['output'].nil? end @@credentials.aws_key = credentials.params[profile]['aws_access_key_id'] unless credentials.params[profile]['aws_access_key_id'].nil? @@credentials.aws_secret = credentials.params[profile]['aws_secret_access_key'] unless credentials.params[profile]['aws_secret_access_key'].nil? end errors << "aws-cli error. Cannot find #{profile}: #{Blufin::Terminal::format_invalid('aws_access_key_id')} in: #{Blufin::Terminal::format_directory(FILE_AWS_CREDENTIALS)}" if @@credentials.aws_key.nil? errors << "aws-cli error. Cannot find #{profile}: #{Blufin::Terminal::format_invalid('aws_secret_access_key')} in: #{Blufin::Terminal::format_directory(FILE_AWS_CREDENTIALS)}" if @@credentials.aws_secret.nil? else # Returns 'yes' if running on EC2 instance, 'no' if not. return if `#{App::Opt::get_base_path}/#{App::Opt::OPT_PATH}/shell/ec2-check`.to_s.gsub("\n", '') =~ /yes/i errors << "aws-cli error. Cannot find file: #{Blufin::Terminal::format_invalid(FILE_AWS_CREDENTIALS)}" end # If anything is wrong, output error(s). Blufin::Config::invalid_configuration(App::GEM_NAME, errors) if errors.any? end |