forked from cloudfoundry/cloud_controller_ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_yeti
executable file
·190 lines (166 loc) · 6.42 KB
/
setup_yeti
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! /usr/bin/env ruby
require "yaml"
require "yajl"
module YetiSetup
def self.setup(uaa_url, uaa_cc_secret, cc_url, cc_admin_email, cc_admin_password, num_users, email, password, org_name, space_name)
check_uaac_version
create_user(uaa_url, uaa_cc_secret, cc_url, cc_admin_email, cc_admin_password, email, password, org_name, space_name)
info = create_users(uaa_url, uaa_cc_secret, cc_url, cc_admin_email, cc_admin_password, num_users, email, password, org_name, space_name)
puts YAML.dump("parallel" => info)
end
def self.create_users(uaa_url, uaa_cc_secret, cc_url, cc_admin_email, cc_admin_password, num_users, email, password, org_name, space_name)
(1..num_users.to_i).map do |i|
(user, domain) = email.split("@")
new_user_email = "#{user}+#{i}@#{domain}"
new_org_name = "#{org_name}-#{i}"
create_user(uaa_url, uaa_cc_secret, cc_url, cc_admin_email, cc_admin_password, new_user_email, password, new_org_name, space_name)
{ "email" => new_user_email, "passwd" => password }
end
end
def self.create_user(uaa_url, uaa_cc_secret, cc_url, cc_admin_email, cc_admin_password, email, password, org_name, space_name)
with_uaa_target(uaa_url) do
user_guid = create_uaa_user(uaa_cc_secret, email, password)
token = auth_token(cc_admin_email, cc_admin_password)
quota_guid = get_yeti_quota_guid(cc_url, token)
create_cc_user(cc_url, token, user_guid)
org_guid = create_cc_org(cc_url, token, org_name, user_guid, quota_guid)
space_guid = create_cc_space(cc_url, token, space_name, org_guid, user_guid)
end
puts "SUCCESS"
end
def self.create_uaa_user(uaa_cc_secret, email, password)
with_cc_uaa_client(uaa_cc_secret) do
output = run "uaac user add #{email} --email #{email} --given_name yeti --family_name testuser -p #{password}"
error unless output =~ /added/
uaa_uid(email)
end
end
def self.create_cc_user(cc_url, token, guid)
puts "# adding user to cc"
cmd = "curl -v -X POST -H 'Content-Type: application/json' " +
" -H 'Authorization: #{token}' " +
" -d '{\"guid\":\"#{guid}\"}' " +
" #{cc_url}/v2/users"
output = run cmd
cc_guid = output[/"guid": "([^"]*)/, 1]
error "could not extract user guid" unless guid
error "cc guid did not match uaa guid" unless cc_guid == guid
guid
end
def self.get_yeti_quota_guid(cc_url, token)
puts "# fetching yeti quota guid"
cmd = "curl -H 'Content-Type: application/json' " +
" -H 'Authorization: #{token}' " +
" #{cc_url}/v2/quota_definitions"
output = run cmd
output_hash = Yajl::Parser.parse(output)
resource = output_hash["resources"].select do |r|
r["entity"]["name"] == "yeti"
end
error "could not find yeti quota" unless resource
resource.first["metadata"]["guid"]
end
def self.create_cc_org(cc_url, token, name, user_guid, quota_guid)
puts "# creating cc org"
cmd = "curl -v -X POST -H 'Content-Type: application/json' " +
" -H 'Authorization: #{token}' " +
" -d '{" +
"\"name\":\"#{name}\", " +
"\"user_guids\": [\"#{user_guid}\"], " +
"\"manager_guids\": [\"#{user_guid}\"], " +
"\"quota_definition_guid\": \"#{quota_guid}\" " +
"}'" +
" #{cc_url}/v2/organizations"
output = run cmd
guid = output[/"guid": "([^"]*)/, 1]
error "could not extract org guid" unless guid
enable_billing(cc_url, token, guid)
guid
end
def self.enable_billing(cc_url, token, guid)
puts "# setting billing_enabled: true"
cmd = "curl -v -X PUT -H 'Content-Type: application/json' " +
" -H 'Authorization: #{token}' " +
" -d '{\"billing_enabled\":true}'" +
" #{cc_url}/v2/organizations/#{guid}"
run cmd
end
def self.create_cc_space(cc_url, token, name, org_guid, user_guid)
puts "# creating cc space"
cmd = "curl -v -X POST -H 'Content-Type: application/json' " +
" -H 'Authorization: #{token}' " +
" -d '{" +
"\"name\":\"#{name}\", " +
"\"organization_guid\":\"#{org_guid}\", " +
"\"manager_guids\": [\"#{user_guid}\"], " +
"\"developer_guids\": [\"#{user_guid}\"] " +
"}'" +
" #{cc_url}/v2/spaces"
output = run cmd
guid = output[/"guid": "([^"]*)/, 1]
error "could not extract space guid" unless guid
guid
end
def self.run(cmd)
puts "#{cmd}"
output = `#{cmd}`
output.split("\n").each { |l| puts " #{l}" }
puts
output
end
def self.error(str = nil)
STDERR.puts "#{str}" if str
exit -1
end
def self.with_uaa_target(uaa_url)
puts "# saving original uaa target"
output = run "uaac target"
orig_target = output[/Target: ([^,]*)?/, 1]
output = run "uaac target #{uaa_url}"
error unless output =~ /Target: #{uaa_url}/
begin
yield if block_given?
ensure
if orig_target
puts "# restoring original uaa target"
run "uaac target #{orig_target}"
end
end
end
def self.with_cc_uaa_client(uaa_cc_secret)
puts "# switching to cloud_controller context"
output = run "uaac token client get cloud_controller -s #{uaa_cc_secret}"
error if output =~ /error/
begin
yield if block_given?
ensure
puts "# removing cloud_controller context"
run "uaac token delete cloud_controller"
end
end
def self.uaa_uid(email)
# cmdline copied from a previous bash script, hence the grep/sed
puts "# fetching uaa uid for user"
output = run "uaac user get #{email} | grep ' id: ' | sed 's/ *id: //'"
output.chomp
end
def self.auth_token(email, password)
output = run "uaac token get #{email} #{password}"
error if output =~ /failed/
output = run "uaac context | grep access_token | sed 's/ *access_token: //'"
"bearer #{output.chomp}"
end
def self.check_uaac_version
output = run "uaac --version"
ver = output.split.last
current_uaac_version = Gem::Version.new(ver)
minimum_version = "1.3.3"
minimum_uaac_version = Gem::Version.new(minimum_version)
abort "Error: uaac minimum version #{minimum_version} required; please 'gem update cf-uaac'" unless current_uaac_version >= minimum_uaac_version
end
end
unless ARGV.size == 10
STDERR.puts "usage: setup_yeti <uaa_url> <uaa_cc_secret> <cc_url> <admin_email> <admin_password> <num_parallel_users> <user_email> <user_password> <org_name> <space_name>"
exit -1
end
YetiSetup.setup(*ARGV)