-
-
Save justincampbell/2ac40f316c4a1e1c03f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from keystoneclient.v3 import client | |
OPENSTACK_HOST = os.getenv('OPENSTACK_HOST', '192.168.10.5') | |
AUTH_URL = "http://%s:5000/v3" % OPENSTACK_HOST | |
USER = 'admin' | |
PASS = 'admin' | |
keystone = client.Client( | |
username=USER, | |
password=PASS, | |
auth_url=AUTH_URL) | |
try: | |
if keystone.users.list(name="cloud_admin"): | |
print "User 'cloud_admin' exists" | |
else: | |
print "Creating user 'cloud_admin'" | |
user = keystone.users.create( | |
description="Cloud Administrator", | |
enabled=True, | |
name="cloud_admin", | |
password="password") | |
role = keystone.roles.find(name='admin') | |
domain = keystone.domains.find(name='default') | |
print "Granting '%s' to 'cloud_admin'" % role.name | |
keystone.roles.grant(user=user, role=role, domain=domain) | |
print "Uploading policies to devstack" | |
os.system('./tools/install-policies.sh') | |
except: | |
print "Login as admin failed. Script may have already been run." | |
USER = 'cloud_admin' | |
PASS = 'password' | |
keystone = client.Client( | |
username=USER, | |
password=PASS, | |
domain_name='default', | |
auth_url=AUTH_URL) | |
def make_domain_hierarchy(domain_name, admin_name, project_name, user_name): | |
if keystone.domains.list(name=domain_name): | |
print "Domain '%s' already exists" % domain_name | |
return | |
print "Creating domain '%s'" % domain_name | |
domain = keystone.domains.create(name=domain_name, enabled=True) | |
print "Creating user '%s' in domain '%s'" % (admin_name, domain_name) | |
admin = keystone.users.create( | |
description='Administrator of domain dom1', | |
domain=domain, | |
enabled=True, | |
name=admin_name, | |
password=PASS) | |
role = keystone.roles.find(name='admin') | |
print "Granting '%s' to '%s'" % (role.name, admin_name) | |
keystone.roles.grant(role, user=admin, domain=domain) | |
keystone_as_admin = client.Client( | |
username=admin_name, | |
password=PASS, | |
domain_name=domain_name, | |
auth_url=AUTH_URL, | |
user_domain_name=domain_name) | |
print "Creating project '%s'" % project_name | |
project = keystone_as_admin.projects.create( | |
domain=domain, | |
name=project_name, | |
enabled=True) | |
print "Granting '%s' on '%s' for project '%s'" % (role.name, admin.name, project.name) | |
keystone.roles.grant(role, user=admin, project=project) | |
print "Creating user '%s' for project '%s'" % (user_name, project_name) | |
user = keystone_as_admin.users.create( | |
domain=domain, | |
enabled=True, | |
name=user_name, | |
password=PASS, | |
description="Regular user in %s" % domain_name, | |
default_project=project) | |
member_role = keystone_as_admin.roles.find(name='Member') | |
print "Granting '%s' role to '%s'" % (member_role.name, user.name) | |
keystone_as_admin.roles.grant(member_role, user=user, domain=domain) | |
keystone_as_admin.roles.grant(member_role, user=user, project=project) | |
make_domain_hierarchy('dom1', 'adm1', 'prj1', 'usr1') | |
make_domain_hierarchy('dom2', 'adm2', 'prj2', 'usr2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment