cfoundry_helper 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/.gitmodules +3 -0
- data/CHANGELOG.textile +10 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +95 -0
- data/README.textile +55 -0
- data/Rakefile +24 -0
- data/VERSION +1 -0
- data/bin/add_users_to_org +94 -0
- data/bin/create_space_for_org +79 -0
- data/bin/delete_orgs_and_users +73 -0
- data/cfoundry_helper.gemspec +39 -0
- data/config/services.yml.example +22 -0
- data/lib/cfoundry_helper.rb +18 -0
- data/lib/cfoundry_helper/helpers.rb +6 -0
- data/lib/cfoundry_helper/helpers/client_helper.rb +106 -0
- data/lib/cfoundry_helper/helpers/organization_helper.rb +148 -0
- data/lib/cfoundry_helper/helpers/space_helper.rb +125 -0
- data/lib/cfoundry_helper/helpers/user_helper.rb +148 -0
- data/lib/cfoundry_helper/models.rb +4 -0
- data/lib/cfoundry_helper/models/organization_role.rb +18 -0
- data/lib/cfoundry_helper/models/space_role.rb +18 -0
- data/lib/cfoundry_helper/version.rb +3 -0
- data/spec/integration_test.rb +11 -0
- data/spec/spec_helper.rb +10 -0
- metadata +273 -0
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/CHANGELOG.textile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
h1. v0.1.0
|
2
|
+
* OrganizationHelper implementation
|
3
|
+
* ClientHelper implementation
|
4
|
+
* UserHelper implementation
|
5
|
+
|
6
|
+
h2. v0.2.0
|
7
|
+
* add users to org script
|
8
|
+
|
9
|
+
h2. v0.2.1
|
10
|
+
* Bugfix: Don't cache the cloud controller client since the access token expires in long running apps.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cfoundry_helper (0.2.1)
|
5
|
+
activesupport
|
6
|
+
cfoundry
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (4.0.0)
|
12
|
+
activesupport (= 4.0.0)
|
13
|
+
builder (~> 3.1.0)
|
14
|
+
activesupport (4.0.0)
|
15
|
+
i18n (~> 0.6, >= 0.6.4)
|
16
|
+
minitest (~> 4.2)
|
17
|
+
multi_json (~> 1.3)
|
18
|
+
thread_safe (~> 0.1)
|
19
|
+
tzinfo (~> 0.3.37)
|
20
|
+
atomic (1.1.13)
|
21
|
+
builder (3.1.4)
|
22
|
+
cf-uaa-lib (2.0.0)
|
23
|
+
multi_json
|
24
|
+
cfoundry (4.3.6)
|
25
|
+
activemodel (>= 3.2.13, < 5.0.0)
|
26
|
+
cf-uaa-lib (~> 2.0.0)
|
27
|
+
multi_json (~> 1.7)
|
28
|
+
multipart-post (~> 1.1)
|
29
|
+
rubyzip (~> 0.9)
|
30
|
+
coderay (1.0.9)
|
31
|
+
columnize (0.3.6)
|
32
|
+
daemons (1.1.9)
|
33
|
+
debugger (1.6.1)
|
34
|
+
columnize (>= 0.3.1)
|
35
|
+
debugger-linecache (~> 1.2.0)
|
36
|
+
debugger-ruby_core_source (~> 1.2.3)
|
37
|
+
debugger-linecache (1.2.0)
|
38
|
+
debugger-ruby_core_source (1.2.3)
|
39
|
+
diff-lcs (1.2.4)
|
40
|
+
eventmachine (0.12.10)
|
41
|
+
httpclient (2.3.4.1)
|
42
|
+
i18n (0.6.5)
|
43
|
+
json_pure (1.8.0)
|
44
|
+
method_source (0.8.2)
|
45
|
+
minitest (4.7.5)
|
46
|
+
multi_json (1.7.9)
|
47
|
+
multipart-post (1.2.0)
|
48
|
+
nats (0.4.28)
|
49
|
+
daemons (>= 1.1.5)
|
50
|
+
eventmachine (= 0.12.10)
|
51
|
+
json_pure (>= 1.7.3)
|
52
|
+
thin (>= 1.4.1)
|
53
|
+
pry (0.9.12.2)
|
54
|
+
coderay (~> 1.0.5)
|
55
|
+
method_source (~> 0.8)
|
56
|
+
slop (~> 3.4)
|
57
|
+
pry-debugger (0.2.2)
|
58
|
+
debugger (~> 1.3)
|
59
|
+
pry (~> 0.9.10)
|
60
|
+
rack (1.5.2)
|
61
|
+
rake (10.1.0)
|
62
|
+
rspec (2.14.1)
|
63
|
+
rspec-core (~> 2.14.0)
|
64
|
+
rspec-expectations (~> 2.14.0)
|
65
|
+
rspec-mocks (~> 2.14.0)
|
66
|
+
rspec-core (2.14.5)
|
67
|
+
rspec-expectations (2.14.2)
|
68
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
69
|
+
rspec-mocks (2.14.3)
|
70
|
+
rubyzip (0.9.9)
|
71
|
+
slop (3.4.6)
|
72
|
+
thin (1.5.1)
|
73
|
+
daemons (>= 1.0.9)
|
74
|
+
eventmachine (>= 0.12.6)
|
75
|
+
rack (>= 1.0.0)
|
76
|
+
thread_safe (0.1.2)
|
77
|
+
atomic
|
78
|
+
tzinfo (0.3.37)
|
79
|
+
yajl-ruby (1.1.0)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
activesupport
|
86
|
+
bundler (~> 1.3)
|
87
|
+
cf-uaa-lib
|
88
|
+
cfoundry_helper!
|
89
|
+
httpclient
|
90
|
+
nats
|
91
|
+
pry
|
92
|
+
pry-debugger
|
93
|
+
rake
|
94
|
+
rspec
|
95
|
+
yajl-ruby
|
data/README.textile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
h1. CFoundry Helper
|
2
|
+
|
3
|
+
This collection of scripts provides helper methods to the cfoundry gem's
|
4
|
+
API. It implements common use cases when administrating a Cloud Foundry installation.
|
5
|
+
|
6
|
+
h2. Organization management
|
7
|
+
|
8
|
+
The CFoundryHelper::Helpers::OrganizationHelper modules' methods provide helper methods for the following use cases:
|
9
|
+
|
10
|
+
* create organizations
|
11
|
+
* add/remove Users from organizations
|
12
|
+
* administer User roles within organizations
|
13
|
+
|
14
|
+
h2. User management
|
15
|
+
|
16
|
+
The CFoundryHelper::Helpers::UserHelper module provides methods for managing users within the system:
|
17
|
+
|
18
|
+
* get user by email
|
19
|
+
* create a user
|
20
|
+
* delete a user
|
21
|
+
* change a user's password
|
22
|
+
|
23
|
+
h2. Space management
|
24
|
+
|
25
|
+
The CFoundryHelper::Helpers::OrganizationHelper modules' methods provide helper methods for the following use cases:
|
26
|
+
* create a space within an organization
|
27
|
+
* add/remove Users from spaces
|
28
|
+
* administer User roles within a space
|
29
|
+
* empty a space (remove all apps, service_bindings, service_instances, routes and domains from the space)
|
30
|
+
* delete a space
|
31
|
+
|
32
|
+
h2. Client Helper
|
33
|
+
|
34
|
+
The CFoundryHelper::Helpers::ClientHelper is a factory for uaa and cloud controller clients.
|
35
|
+
|
36
|
+
|
37
|
+
h2. Scripts
|
38
|
+
|
39
|
+
* bin/add_users_to_org : adds users to an organization
|
40
|
+
** usage : RAILS_ENV=<test / production / development> bundle exec bin/add_users_to_org <org name> <users_email_list_file_location> <services_config_file_location>
|
41
|
+
* bin/create_space_for_org : creates a space to an organization and assigns all organization members to it
|
42
|
+
** usage : RAILS_ENV=<test / production / development> bundle exec bin/create_space_for_org <org name> <space name> <services_config_file_location>
|
43
|
+
|
44
|
+
h2. Important environment variables
|
45
|
+
|
46
|
+
* RAILS_ENV : The helper is reading the target environment on which to execute commands using the RAILS_ENV environment variable. (e.g. production, test)
|
47
|
+
|
48
|
+
* CFOUNDRY_HELPER_CONFIG This variable has to point to your services.yml file.
|
49
|
+
|
50
|
+
h2. Console Usage
|
51
|
+
|
52
|
+
Use the following command to start the console with the associated target:
|
53
|
+
RAILS_ENV=<target_env> CFOUNDRY_HELPER_CONFIG=<services config file> rake c
|
54
|
+
|
55
|
+
e.g.: RAILS_ENV=production CFOUNDRY_HELPER_CONFIG=config/services.yml rake c
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
if ["test", "development"].include?(ENV["RAILS_ENV"])
|
4
|
+
require "pry"
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
task :default => :spec
|
10
|
+
else
|
11
|
+
task :default => :console
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
desc 'starts a interactive shell'
|
16
|
+
task :console do
|
17
|
+
if ENV["RAILS_ENV"] == "production"
|
18
|
+
exec('irb -r ./lib/cfoundry_helper.rb')
|
19
|
+
else
|
20
|
+
exec('pry -r ./lib/cfoundry_helper.rb')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
task c: :console
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "cfoundry_helper"
|
5
|
+
|
6
|
+
#require_relative '../lib/cfoundry_helper'
|
7
|
+
|
8
|
+
# function definitions ---------------------------------------------
|
9
|
+
def print_help
|
10
|
+
puts "You have entered a wrong number of arguments!"
|
11
|
+
puts "Usage: add_users_to_org <organization name> <user list file location> <config file path>"
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_user_to_org(org, email)
|
15
|
+
user = CFoundryHelper::Helpers::UserHelper.get_user_by_email email
|
16
|
+
CFoundryHelper::Helpers::OrganizationHelper.add_user org, user
|
17
|
+
CFoundryHelper::Helpers::OrganizationHelper.add_roles org, user,
|
18
|
+
[CFoundryHelper::Models::OrganizationRole::AUDITOR, CFoundryHelper::Models::OrganizationRole::MANAGER]
|
19
|
+
end
|
20
|
+
|
21
|
+
def check_users_present(file_loc)
|
22
|
+
all_existing = true
|
23
|
+
File.open(file_loc).each do |email|
|
24
|
+
email = email.strip
|
25
|
+
puts "Checking email=#{email} ...."
|
26
|
+
unless CFoundryHelper::Helpers::UserHelper.email_exists? email
|
27
|
+
puts "The user email=#{email} doesn't exist in the system!"
|
28
|
+
all_existing = false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
if all_existing
|
32
|
+
puts "All emails are present in the system!"
|
33
|
+
else
|
34
|
+
puts "Some emails are not present in the system!"
|
35
|
+
end
|
36
|
+
all_existing
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_users(org_name, file_loc)
|
40
|
+
org = CFoundryHelper::Helpers::OrganizationHelper.get_organization_by_name org_name
|
41
|
+
if org.nil?
|
42
|
+
puts "No organization with the name=#{org_name} could be found!"
|
43
|
+
exit(-1)
|
44
|
+
end
|
45
|
+
puts "Adding users to the #{org_name} organization!"
|
46
|
+
|
47
|
+
File.open(file_loc).each do |email|
|
48
|
+
email = email.strip
|
49
|
+
puts "Adding user with email=#{email}"
|
50
|
+
add_user_to_org org, email
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "Finished adding users!"
|
54
|
+
end
|
55
|
+
|
56
|
+
def print_rails_env
|
57
|
+
puts "RAILS_ENV=#{ENV["RAILS_ENV"]}"
|
58
|
+
end
|
59
|
+
|
60
|
+
# execute script ----------------------------------------------
|
61
|
+
|
62
|
+
if ARGV.count < 3
|
63
|
+
print_help
|
64
|
+
exit(-1)
|
65
|
+
end
|
66
|
+
|
67
|
+
puts "add users to organization"
|
68
|
+
print_rails_env
|
69
|
+
|
70
|
+
organization_name = ARGV[0]
|
71
|
+
file_loc = ARGV[1]
|
72
|
+
config_file_path = ARGV[2]
|
73
|
+
|
74
|
+
CFoundryHelper::Helpers::ClientHelper.set_config_file_path config_file_path
|
75
|
+
|
76
|
+
puts "Organization: #{organization_name}"
|
77
|
+
puts "Cloud Controller target url: #{CFoundryHelper::Helpers::ClientHelper.get_cc_target_url}"
|
78
|
+
|
79
|
+
unless check_users_present(file_loc)
|
80
|
+
puts "Canceling operation!"
|
81
|
+
exit(-1)
|
82
|
+
end
|
83
|
+
|
84
|
+
add_users(organization_name, file_loc)
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "cfoundry_helper"
|
5
|
+
|
6
|
+
# function definitions ---------------------------------------------
|
7
|
+
def print_help
|
8
|
+
puts "This script adds a new space with the given name to the organization with the given name."
|
9
|
+
puts "If the given space already exists, it just assigns the roles to the organization's users."
|
10
|
+
puts "Members of the given organization are registered as Space Developers and Space Auditors."
|
11
|
+
puts "You have entered a wrong number of arguments!"
|
12
|
+
puts "Usage: create_spaces_for_org <organization name> <space name> <config file path>"
|
13
|
+
end
|
14
|
+
|
15
|
+
def print_rails_env
|
16
|
+
puts "RAILS_ENV=#{ENV["RAILS_ENV"]}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_org(org_name)
|
20
|
+
org = CFoundryHelper::Helpers::OrganizationHelper.get_organization_by_name org_name
|
21
|
+
if org.nil?
|
22
|
+
print "No organization with the given name could be found!"
|
23
|
+
exit -1
|
24
|
+
end
|
25
|
+
org
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_or_get_space(org, space_name)
|
29
|
+
puts "Creating space with name=#{space_name} ..."
|
30
|
+
space = nil
|
31
|
+
begin
|
32
|
+
space = CFoundryHelper::Helpers::SpaceHelper.create_space org, space_name
|
33
|
+
puts "Created space successfully!"
|
34
|
+
rescue
|
35
|
+
puts "The space #{space_name} already exists."
|
36
|
+
puts "Using this space to assign user roles now."
|
37
|
+
space = CFoundryHelper::Helpers::SpaceHelper.get_space org, space_name
|
38
|
+
end
|
39
|
+
space
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_user_roles(space, user)
|
43
|
+
CFoundryHelper::Helpers::SpaceHelper.add_roles space, user,
|
44
|
+
[CFoundryHelper::Models::SpaceRole::AUDITOR,
|
45
|
+
CFoundryHelper::Models::SpaceRole::DEVELOPER]
|
46
|
+
end
|
47
|
+
|
48
|
+
def set_org_users_space_roles(org, space)
|
49
|
+
puts "Setting roles for organization users..."
|
50
|
+
org.users.each do |u|
|
51
|
+
puts "Setting roles for user with guid=#{u.guid} ...."
|
52
|
+
set_user_roles space, u
|
53
|
+
end
|
54
|
+
puts "Finished setting roles!"
|
55
|
+
end
|
56
|
+
|
57
|
+
# execute script ----------------------------------------------
|
58
|
+
|
59
|
+
if ARGV.count < 3
|
60
|
+
print_help
|
61
|
+
exit(-1)
|
62
|
+
end
|
63
|
+
|
64
|
+
puts "create space for organization"
|
65
|
+
print_rails_env
|
66
|
+
|
67
|
+
organization_name = ARGV[0]
|
68
|
+
space_name = ARGV[1]
|
69
|
+
config_file_path = ARGV[2]
|
70
|
+
|
71
|
+
CFoundryHelper::Helpers::ClientHelper.set_config_file_path config_file_path
|
72
|
+
|
73
|
+
puts "Organization: #{organization_name}"
|
74
|
+
puts "Space name: #{space_name}"
|
75
|
+
puts "Cloud Controller target url: #{CFoundryHelper::Helpers::ClientHelper.get_cc_target_url}"
|
76
|
+
|
77
|
+
org = get_org organization_name
|
78
|
+
space = create_or_get_space org, space_name
|
79
|
+
set_org_users_space_roles org, space
|
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "cfoundry_helper"
|
5
|
+
|
6
|
+
# function definitions ---------------------------------------------
|
7
|
+
def print_help
|
8
|
+
puts "This script delete a list of users including their default organization."
|
9
|
+
puts "You have entered a wrong number of arguments!"
|
10
|
+
puts "Usage: delete_orgs_and_users <user list file path> <config file path>"
|
11
|
+
end
|
12
|
+
|
13
|
+
def print_rails_env
|
14
|
+
puts "RAILS_ENV=#{ENV["RAILS_ENV"]}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_standard_org_name_for_email(email)
|
18
|
+
email.gsub(/[\.|@]/, "_")
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete_user(email)
|
22
|
+
begin
|
23
|
+
user = CFoundryHelper::Helpers::UserHelper.get_user_by_email email
|
24
|
+
rescue
|
25
|
+
puts "Could not find user with email=#{email}"
|
26
|
+
return
|
27
|
+
end
|
28
|
+
|
29
|
+
puts "Deleting user with email=#{email}"
|
30
|
+
CFoundryHelper::Helpers::UserHelper.delete_user user
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete_org(org_name)
|
34
|
+
org = CFoundryHelper::Helpers::OrganizationHelper.get_organization_by_name org_name
|
35
|
+
if org.nil?
|
36
|
+
puts "Could not find organization with name=#{org_name}"
|
37
|
+
return
|
38
|
+
end
|
39
|
+
|
40
|
+
puts "Deleting organization with name=#{org_name}"
|
41
|
+
CFoundryHelper::Helpers::OrganizationHelper.delete_organization_recursive org
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def delete_users(file_loc)
|
46
|
+
File.open(file_loc).each do |email|
|
47
|
+
email = email.strip
|
48
|
+
delete_user email
|
49
|
+
delete_org get_standard_org_name_for_email(email)
|
50
|
+
end
|
51
|
+
|
52
|
+
puts "Finished deleting users!"
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
# execute script ----------------------------------------------
|
57
|
+
|
58
|
+
if ARGV.count < 2
|
59
|
+
print_help
|
60
|
+
exit(-1)
|
61
|
+
end
|
62
|
+
|
63
|
+
file_loc = ARGV[0]
|
64
|
+
config_file_path = ARGV[1]
|
65
|
+
|
66
|
+
print_rails_env
|
67
|
+
puts "removing users and organizations described in #{file_loc}"
|
68
|
+
|
69
|
+
CFoundryHelper::Helpers::ClientHelper.set_config_file_path config_file_path
|
70
|
+
|
71
|
+
puts "Cloud Controller target url: #{CFoundryHelper::Helpers::ClientHelper.get_cc_target_url}"
|
72
|
+
|
73
|
+
delete_users file_loc
|