-
Notifications
You must be signed in to change notification settings - Fork 1
/
iam-user-import
executable file
·58 lines (44 loc) · 1.6 KB
/
iam-user-import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#! /usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "fog"
require "fileutils"
require 'trollop'
require 'yaml'
require 'csv'
def set_environment(environment)
environments[environment]
end
def environments
YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'config.yml'))
end
def send_email(email, connection, variables)
message= {'Subject' =>{}, 'Body' =>{'Text'=>{},'Html'=>{}}}
message['Subject']['Data'] = email
message['Body']['Text']['Data'] = email
message['Body']['Html']['Data'] = email
puts message
connection.send_email("[email protected]",{'ToAddresses' => ["[email protected]"]}, message)
end
opts = Trollop::options do
opt :environment, "", :type => :string
opt :file, "user file", :type => :string
opt :groups, "user groups", :type => :strings
end
Trollop::die :environment, "needs an environment. Valid values(#{environments.keys.join(',')})" unless environments.keys.include? opts[:environment]
Trollop::die :file, "must exist" if opts[:file].nil? || !File.exist?(opts[:file])
Trollop::die :groups, "must exist" if opts[:groups].length == 0
settings = set_environment(opts[:environment])
settings.delete("region")
dataset = CSV.read(opts[:file])
@iam = Fog::AWS::IAM.new(settings)
@ses = Fog::AWS::SES.new(settings)
dataset.each do |row|
if row.length>0
user_name = row[0]
email = row[1]
@iam.create_user(user_name) if @iam.users.get(user_name).nil?
opts[:groups].each {|group| @iam.add_user_to_group(group, user_name) }
send_email(email, @ses, {:password => "", :user_name => user_name, :url => ""})
end
end