本文整理汇总了Python中service.accounts.openstack.AccountDriver类的典型用法代码示例。如果您正苦于以下问题:Python AccountDriver类的具体用法?Python AccountDriver怎么用?Python AccountDriver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AccountDriver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
"""
TODO: Add argparse, --delete : Deletes existing users in openstack (Never use in PROD)
"""
openstack = Provider.objects.filter(
type__name__iexact="openstack").order_by("id")
if not openstack:
raise Provider.DoesNotExist("No OpenStack Provider Found")
openstack = openstack[0]
os_driver = OSAccountDriver(openstack)
found = 0
create = 0
usernames = os_driver.list_usergroup_names()
quota_dict = {
'cpu': 10,
'memory': 20,
'storage': 10,
'storage_count': 10
}
higher_quota = Quota.objects.get_or_create(**quota_dict)[0]
for user in usernames:
# Openstack account exists, but we need the identity.
ident = os_driver.create_account(user)
if is_staff(ident):
im = ident.identity_membership.all()[0]
# Disable time allocation
im.allocation = None
# Raise everybody's quota
im.quota = higher_quota
im.save()
print "Total users added to atmosphere:%s" % len(usernames)
示例2: main
def main():
"""
TODO: Add argparse, --delete : Deletes existing users in eucalyptus (Never use in PROD)
"""
euca = Provider.objects.get(location="Eucalyptus (retiring March 4)")
euca_driver = EucaAccountDriver(euca)
openstack = Provider.objects.get(location="iPlant Cloud - Tucson")
os_driver = OSAccountDriver(openstack)
all_users = euca_driver.list_users()
# Sort by users
all_values = sorted(all_users.values(), key=lambda user: user["username"])
total = 0
for user_dict in all_values:
id_exists = Identity.objects.filter(created_by__username=user_dict["username"], provider=euca)
if not id_exists:
euca_driver.create_account(user_dict)
total += 1
print "Added to Eucalyptus: %s" % user_dict["username"]
print "Total users added:%s" % total
if include_openstack:
print "Adding all eucalyptus users to openstack"
total = 0
for user_dict in all_values:
id_exists = Identity.objects.filter(created_by__username=user_dict["username"], provider=openstack)
if not id_exists:
os_driver.create_account(user_dict["username"])
total += 1
print "Added to Openstack: %s" % user_dict["username"]
print "Total users added to openstack:%s" % total
示例3: security_group_init
def security_group_init(core_identity):
os_driver = OSAccountDriver(core_identity.provider)
creds = core_identity.get_credentials()
security_group = os_driver.init_security_group(
creds["key"], creds["secret"], creds["ex_tenant_name"], creds["ex_tenant_name"], os_driver.MASTER_RULES_LIST
)
return security_group
示例4: main
def main():
"""
Using the keyname and public_key defined in settings
Ensure that the keypair has been distributed to every identity on the
provider.
It is essential that all users carry the same keypair to allow Deployment
access
"""
keyname = settings.ATMOSPHERE_KEYPAIR_NAME
with open(settings.ATMOSPHERE_KEYPAIR_FILE, 'r') as pub_key_file:
public_key = pub_key_file.read()
print "Adding keypair: %s Contents: %s" % (keyname, public_key)
os_providers = Provider.objects.filter(type__name="OpenStack")
for prov in os_providers:
count = 0
identities = Identity.objects.filter(provider=prov)
os_accounts = OSAccountDriver(prov)
for ident in identities:
creds = os_accounts.parse_identity(ident)
try:
(keypair, created) = os_accounts.get_or_create_keypair(
creds['username'], creds['password'], creds['tenant_name'],
keyname, public_key)
except Unauthorized, exc:
print "Could not create keypair for %s. Error message: %s"\
% (creds['username'], exc.message)
if created:
print "Created keypair %s for user %s"\
% (keypair, creds['username'])
count += 1
print 'Keypairs added for %s accounts on %s' % (count, prov)
示例5: clear_empty_ips
def clear_empty_ips():
logger.debug("clear_empty_ips task started at %s." % datetime.now())
from service import instance as instance_service
from rtwo.driver import OSDriver
from api import get_esh_driver
from service.accounts.openstack import AccountDriver as OSAccountDriver
identities = Identity.objects.filter(provider__type__name__iexact="openstack", provider__active=True)
key_sorter = lambda ident: attrgetter(ident.provider.type.name, ident.created_by.username)
identities = sorted(identities, key=key_sorter)
os_acct_driver = None
total = len(identities)
for idx, core_identity in enumerate(identities):
try:
# Initialize the drivers
driver = get_esh_driver(core_identity)
if not isinstance(driver, OSDriver):
continue
if not os_acct_driver or os_acct_driver.core_provider != core_identity.provider:
os_acct_driver = OSAccountDriver(core_identity.provider)
logger.info("Initialized account driver")
# Get useful info
creds = core_identity.get_credentials()
tenant_name = creds["ex_tenant_name"]
logger.info("Checking Identity %s/%s - %s" % (idx + 1, total, tenant_name))
# Attempt to clean floating IPs
num_ips_removed = driver._clean_floating_ip()
if num_ips_removed:
logger.debug("Removed %s ips from OpenStack Tenant %s" % (num_ips_removed, tenant_name))
# Test for active/inactive instances
instances = driver.list_instances()
active = any(driver._is_active_instance(inst) for inst in instances)
inactive = all(driver._is_inactive_instance(inst) for inst in instances)
for instance in instances:
if driver._is_inactive_instance(instance) and instance.ip:
# If an inactive instance has floating/fixed IPs.. Remove them!
instance_service.remove_ips(driver, instance)
if active and not inactive:
# User has >1 active instances AND not all instances inactive
pass
elif os_acct_driver.network_manager.get_network_id(
os_acct_driver.network_manager.neutron, "%s-net" % tenant_name
):
# User has 0 active instances OR all instances are inactive
# Network exists, attempt to dismantle as much as possible
remove_network = not inactive
logger.info("Removing project network %s for %s" % (remove_network, tenant_name))
if remove_network:
# Sec. group can't be deleted if instances are suspended
# when instances are suspended we pass remove_network=False
os_acct_driver.delete_security_group(core_identity)
os_acct_driver.delete_network(core_identity, remove_network=remove_network)
else:
# logger.info("No Network found. Skipping %s" % tenant_name)
pass
except Exception as exc:
logger.exception(exc)
logger.debug("clear_empty_ips task finished at %s." % datetime.now())
示例6: network_init
def network_init(core_identity):
provider_creds = core_identity.provider.get_credentials()
if 'router_name' not in provider_creds.keys():
logger.warn("ProviderCredential 'router_name' missing:"
"cannot create virtual network")
return
os_driver = OSAccountDriver(core_identity.provider)
(network, subnet) = os_driver.create_network(core_identity)
return (network, subnet)
示例7: main
def main():
"""
TODO: Add argparse, --delete : Deletes existing users in openstack (Never use in PROD)
"""
openstack = Provider.objects.get(location='iPlant Cloud - Tucson')
os_driver = OSAccountDriver(openstack)
found = 0
create = 0
quota_dict = {
'cpu':16,
'memory': 128,
'storage': 10,
'storage_count': 10
}
higher_quota = Quota.objects.get_or_create(**quota_dict)[0]
usernames = os_driver.list_usergroup_names()
staff = get_staff_users()
staff_users = sorted(list(set(staff) & set(usernames)))
non_staff = sorted(list(set(usernames) - set(staff)))
for user in non_staff:
#Raise everybody's quota
#try:
im_list = IdentityMembership.objects.filter(identity__created_by__username=user, identity__provider=openstack)
if not im_list:
print "Missing user:%s" % user
continue
im = im_list[0]
if not im.allocation:
print "User missing Allocation: %s" % user
im.allocation = Allocation.default_allocation()
im.save()
#Ignore the quota set if you are above it..
if im.quota.cpu >= quota_dict["cpu"] \
or im.quota.memory >= quota_dict["memory"]:
continue
print "Existing Quota CPU:%s should be %s" % (im.quota.cpu, quota_dict["cpu"])
im.quota = higher_quota
im.save()
print 'Found non-staff user:%s -- Update quota and add allocation' % user
#for user in staff_users:
# # Openstack account exists, but we need the identity.
# im = IdentityMembership.objects.filter(identity__created_by__username=user, identity__provider=openstack)
# if not im:
# print "Missing user:%s" % user
# continue
# im = im[0]
# if im.quota.cpu == quota_dict["cpu"]:
# continue
# #Disable time allocation
# im.allocation = None
# im.quota = higher_quota
# im.save()
# print 'Found staff user:%s -- Update quota and no allocation' % user
print "Total users added to atmosphere:%s" % len(usernames)
示例8: add_os_project_network
def add_os_project_network(core_identity, *args, **kwargs):
try:
logger.debug("add_os_project_network task started at %s." % datetime.now())
from rtwo.accounts.openstack import AccountDriver as OSAccountDriver
account_driver = OSAccountDriver(core_identity.provider)
account_driver.create_network(core_identity)
logger.debug("add_os_project_network task finished at %s." % datetime.now())
except Exception as exc:
add_os_project_network.retry(exc=exc)
示例9: keypair_init
def keypair_init(core_identity):
os_driver = OSAccountDriver(core_identity.provider)
creds = core_identity.get_credentials()
with open(settings.ATMOSPHERE_KEYPAIR_FILE, 'r') as pub_key_file:
public_key = pub_key_file.read()
keypair, created = os_driver.get_or_create_keypair(
creds['key'], creds['secret'], creds['ex_tenant_name'],
settings.ATMOSPHERE_KEYPAIR_NAME, public_key)
if created:
logger.info("Created keypair for %s" % creds['key'])
return keypair
示例10: main
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--provider", type=int,
help="Atmosphere provider ID"
" to use when importing users.")
parser.add_argument("--users",
help="LDAP usernames to import. (comma separated)")
parser.add_argument("--admin", action="store_true",
help="Users addded as admin and staff users.")
args = parser.parse_args()
users = None
added = 0
if args.provider:
provider = Provider.objects.get(id=args.provider)
else:
provider = Provider.objects.get(location='iPlant Cloud - Tucson')
print "Using Provider: %s" % provider
type_name = provider.type.name.lower()
if type_name == 'openstack':
acct_driver = OSAccountDriver(provider)
elif type_name == 'eucalyptus':
acct_driver = EucaAccountDriver(provider)
else:
raise Exception("Could not find an account driver for Provider with"
" type:%s" % type_name)
if not args.users:
print "Retrieving all 'atmo-user' members in LDAP."
users = get_members('atmo-user')
else:
users = args.users.split(",")
for user in users:
# Then add the Openstack Identity
try:
id_exists = Identity.objects.filter(
created_by__username__iexact=user,
provider=provider)
if id_exists:
continue
acct_driver.create_account(user, max_quota=args.admin)
added += 1
if args.admin:
make_admin(user)
print "%s added as admin." % (user)
else:
print "%s added." % (user)
except Exception as e:
print "Problem adding %s." % (user)
print e.message
print "Total users added:%s" % (added)
示例11: _create_and_attach_port
def _create_and_attach_port(provider, driver, instance, core_identity):
accounts = OSAccountDriver(core_identity.provider)
tenant_id = instance.extra['tenantId']
network_resources = accounts.network_manager.find_tenant_resources(tenant_id)
network = network_resources['networks']
if not network:
network, subnet = accounts.create_network(core_identity)
else:
network = network[0]
subnet = network_resources['subnets'][0]
#new_fixed_ip = _get_next_fixed_ip(network_resources['ports'])
#admin = accounts.admin_driver
#port = accounts.network_manager.create_port(
# instance.id, network['id'], subnet['id'], new_fixed_ip, tenant_id)
attached_intf = driver._connection.ex_attach_interface(instance.id, network['id'])
return attached_intf
示例12: remove_empty_network
def remove_empty_network(
driverCls, provider, identity,
core_identity_id,
*args, **kwargs):
try:
#For testing ONLY.. Test cases ignore countdown..
if app.conf.CELERY_ALWAYS_EAGER:
logger.debug("Eager task waiting 1 minute")
time.sleep(60)
logger.debug("remove_empty_network task started at %s." %
datetime.now())
logger.debug("CoreIdentity(id=%s)" % core_identity_id)
core_identity = Identity.objects.get(id=core_identity_id)
driver = get_driver(driverCls, provider, identity)
instances = driver.list_instances()
active_instances = False
#TODO: Replace with any()
for instance in instances:
if driver._is_active_instance(instance):
active_instances = True
break
if not active_instances:
inactive_instances = all(driver._is_inactive_instance(
instance) for instance in instances)
#Inactive instances, True: Remove network, False
remove_network = not inactive_instances
#Check for project network
from service.accounts.openstack import AccountDriver as\
OSAccountDriver
os_acct_driver = OSAccountDriver(core_identity.provider)
logger.info("No active instances. Removing project network"
"from %s" % core_identity)
os_acct_driver.delete_network(core_identity,
remove_network=remove_network)
if remove_network:
#Sec. group can't be deleted if instances are suspended
# when instances are suspended we pass remove_network=False
os_acct_driver.delete_security_group(core_identity)
return True
logger.debug("remove_empty_network task finished at %s." %
datetime.now())
return False
except Exception as exc:
logger.exception("Failed to check if project network is empty")
remove_empty_network.retry(exc=exc)
示例13: main
def main():
"""
TODO: Add argparse, --delete : Deletes existing users in openstack
(Never use in PROD)
"""
euca_driver = EucaAccountDriver(
Provider.objects.get(location='EUCALYPTUS'))
os_driver = OSAccountDriver(Provider.objects.get(location='OpenStack-Tucson (BETA)'))
found = 0
create = 0
core_services = ['estevetest03', ] # get_core_services()
for user in core_services:
euca_driver.create_account(user, max_quota=True)
# Then add the Openstack Identity
os_driver.create_account(user, max_quota=True)
make_admin(user)
print "Total core-service/admins added:%s" % len(core_services)
示例14: main
def main():
"""
Generate openstack users then add them to the DB
"""
driver = AccountDriver(secrets.OPENSTACK_ARGS)
#Build the admin driver for openstack first.
driver.create_identity(secrets.OPENSTACK_ADMIN_KEY,
secrets.OPENSTACK_ADMIN_SECRET, secrets.OPENSTACK_ADMIN_TENANT,
True)
success = 1
#Add the others
core_services = ['atmo_test']#'sgregory', 'jmatt', 'edwins', 'cjlarose','mlent']
for username in core_services:
try:
password = driver.hashpass(username)
user = driver.get_user(username)
if not user:
user = driver.create_user(username, usergroup=True)
print 'New OStack User - %s Pass - %s' % (user.name,password)
else:
print 'Found OStack User - %s Pass - %s' % (user.name,password)
#ASSERT: User exists on openstack, create an identity for them.
ident = driver.create_identity(user.name, password, project_name=username)
success += 1
print 'New OStack Identity - %s:%s' % (ident.id, ident)
except Exception as e:
print "Problem adding username: %s" % username
print e
raise
print "Total users created:%s/%s" % (success,len(core_services))
示例15: main
def main():
"""
TODO: Add argparse, --delete : Deletes existing users in openstack
(Never use in PROD)
"""
euca_driver = EucaAccountDriver(
Provider.objects.get(location='EUCALYPTUS'))
os_driver = OSAccountDriver(Provider.objects.get(location='iPlant Cloud - Tucson'))
found = 0
create = 0
raise Exception("Must manually set core_services!! See Line 30.")
core_services = ['jchansen', ] # get_core_services() # ['estevetest03', ]
for user in core_services:
euca_driver.create_account(user, max_quota=True)
# Then add the Openstack Identity
os_driver.create_account(user, max_quota=True)
make_admin(user)
print "Total core-service/admins added:%s" % len(core_services)