Class: AwsStudentAccounts::App

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/aws_student_accounts/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fog_credentialsObject (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_optionsObject



12
13
14
15
16
17
# File 'lib/aws_student_accounts/app.rb', line 12

def self.common_options
  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_accountsObject



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
  load_and_verify_options
  @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 |, 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(, 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")
  load_and_verify_options
  @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 options[:only]
      File.open("students-fog-api.yml", "w") do |f|
        f << @users_credentials.to_yaml
      end
      say "Stored all user API credentials: #{File.expand_path('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  = @users_passwords[username]
            f << <<-EOS
## #{[:username]}

* Sign-in URL: #{[:url]}
* Username: #{[:username]}
* Password: #{[:password]}

            EOS
          end
        end
        say "Stored all user passwords: #{File.expand_path('students-console-passwords.md')}"
      end
    end
  end
end

#delete_studentsObject



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
  load_and_verify_options
  @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.message, :red
      end
    end
  end
end

#verify_credentialsObject



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
  load_and_verify_options
  @io_semaphore = Mutex.new
  Parallel.each(fog_credentials, in_threads: 10) do |username, credentials|
    begin
       = (credentials)
      server_count = [: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.message, :red
      end
    end
  end
end