Class: AwsStudentAccounts::App
- Inherits:
-
Thor
- Object
- Thor
- AwsStudentAccounts::App
- Includes:
- Thor::Actions
- Defined in:
- lib/aws_student_accounts/app.rb
Instance Attribute Summary collapse
-
#fog_credentials ⇒ Object
readonly
Returns the value of attribute fog_credentials.
Class Method Summary collapse
Instance Method Summary collapse
- #clean_accounts ⇒ Object
- #create_students(path_to_student_folders = "students") ⇒ Object
- #delete_students ⇒ Object
- #verify_credentials ⇒ Object
Instance Attribute Details
#fog_credentials ⇒ Object (readonly)
Returns the value of attribute fog_credentials.
10 11 12 |
# File 'lib/aws_student_accounts/app.rb', line 10 def fog_credentials @fog_credentials end |
Class Method Details
.common_options ⇒ Object
12 13 14 15 16 17 |
# File 'lib/aws_student_accounts/app.rb', line 12 def self. method_option :fog_file, desc: "Path to YAML file of fog credentials", type: :string, aliases: "-C", required: true method_option :only, desc: "Restrict to comma-separated list of fog keys", type: :string, aliases: "-o" end |
Instance Method Details
#clean_accounts ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/aws_student_accounts/app.rb', line 113 def clean_accounts @io_semaphore = Mutex.new # Double check before unleashing devastation unless yes?("Do you really want to terminate all instances, disk, IPs, networks etc?", :red) say "Phew!", :green exit 1 end Parallel.each(fog_credentials, in_threads: fog_credentials.size) do |account, credentials| all_regions = aws_regions(false) Parallel.each(all_regions, in_threads: all_regions.size) do |aws_region| compute = Fog::Compute::AWS.new(credentials.merge(region: aws_region)) destroy_everything(account, aws_region, compute) end end end |
#create_students(path_to_student_folders = "students") ⇒ Object
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 |
# File 'lib/aws_student_accounts/app.rb', line 44 def create_students(path_to_student_folders="students") @io_semaphore = Mutex.new @users_credentials = ThreadSafe::Hash.new @users_passwords = ThreadSafe::Hash.new FileUtils.mkdir_p(path_to_student_folders) FileUtils.chdir(path_to_student_folders) do Parallel.each(fog_credentials, in_threads: fog_credentials.size) do |username, credentials| create_student_user(username, credentials) end # don't rewrite the shared file if only re-creating credentials for one person # FIXME: update these files rather than rewriting? # TODO: make backups of shared files before rewriting unless [:only] File.open("students-fog-api.yml", "w") do |f| f << @users_credentials.to_yaml end say "Stored all user API credentials: #{File.('students-fog-api.yml')}" File.open("students-console-passwords.md", "w") do |f| f << "# Student AWS logins\n\n" fog_credentials.each do |username, credentials| if user_login = @users_passwords[username] f << <<-EOS ## #{user_login[:username]} * Sign-in URL: #{user_login[:url]} * Username: #{user_login[:username]} * Password: #{user_login[:password]} EOS end end say "Stored all user passwords: #{File.('students-console-passwords.md')}" end end end end |
#delete_students ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/aws_student_accounts/app.rb', line 88 def delete_students @io_semaphore = Mutex.new Parallel.each(fog_credentials, in_threads: fog_credentials.size) do |username, credentials| begin iam = Fog::AWS::IAM.new(credentials) delete_user(iam, username) @io_semaphore.synchronize do user_say username, "Deleted user", :green end rescue Fog::AWS::IAM::NotFound @io_semaphore.synchronize do user_say username, "Does not exist", :red end rescue => e @io_semaphore.synchronize do user_say username, e., :red end end end end |
#verify_credentials ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/aws_student_accounts/app.rb', line 21 def verify_credentials @io_semaphore = Mutex.new Parallel.each(fog_credentials, in_threads: 10) do |username, credentials| begin account = account_summary(credentials) server_count = account[:servers] @io_semaphore.synchronize do say "#{username}: " say "OK ", :green say "(#{server_count} vm)" end rescue => e @io_semaphore.synchronize do say "#{username}: " say e., :red end end end end |