def main(): parser = argparse.ArgumentParser() parser.add_argument("--provider", type=int, help="Atmosphere provider ID" " to use.") parser.add_argument("image_ids", help="Image ID(s) to be repaired. (Comma-Separated)") args = parser.parse_args() if not args.provider: provider = Provider.objects.get(location='iPlant Cloud - Tucson') else: provider = Provider.objects.get(id=args.provider) images = args.image_ids.split(",") accounts = OSAccountDriver(provider) for image_id in images: mr = MachineRequest.objects.get( new_machine__instance_source__identifier=image_id) glance_image = accounts.get_image(image_id) if hasattr(glance_image, 'properties'): glance_image_properties = glance_image.properties else: glance_image_properties = dict(glance_image.items()) if 'kernel_id' not in glance_image_properties\ or 'ramdisk_id' not in glance_image_properties: print "Image %s (%s) is missing kernel and/or ramdisk ..." % ( image_id, glance_image.name), fix_image(accounts, glance_image, mr)
def get_account_driver(provider, raise_exception=False): """ Create an account driver for a given provider. """ try: if type(provider) == uuid.UUID: provider = CoreProvider.objects.get(uuid=provider) type_name = provider.get_type_name().lower() if 'openstack' in type_name: from service.accounts.openstack_manager import AccountDriver as\ OSAccountDriver return OSAccountDriver(provider) elif 'eucalyptus' in type_name: from service.accounts.eucalyptus import AccountDriver as\ EucaAccountDriver return EucaAccountDriver(provider) except: if type(provider) == uuid.UUID: provider_str = "Provider with UUID %s" % provider else: provider_str = "Provider %s" % provider.location logger.exception("Account driver for provider %s not found." % (provider_str)) if raise_exception: raise return None
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)
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 KeystoneUnauthorized as 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)
def update_password_for(prov, identities, dry_run=False, rebuild=False): count = 0 accounts = OSAccountDriver(prov) for ident in identities: creds = accounts.parse_identity(ident) username = creds['username'] password = creds['password'] # Represents the *SAVED* password. new_password = accounts.hashpass(username, strategy='salt_hashpass') if skip_change_password(accounts, username, password, new_password, dry_run=dry_run, rebuild=rebuild): print "Skipping user %s" % (username, ) continue # ASSERT: Saved Password is 'old' print "Changing password: %s (OLD:%s -> NEW:%s)" \ % (username, password, new_password), if dry_run: print "OK" count += 1 continue kwargs = {} if rebuild: old_password = get_old_password(accounts, username) kwargs.update({'old_password': old_password}) success = accounts.change_password(ident, new_password, **kwargs) if success: print "OK" count += 1 else: print "FAILED" print 'Changed passwords for %s accounts on %s' % (count, prov)
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
def main(): parser = argparse.ArgumentParser() parser.add_argument("--fixed-ip", help="Fixed IP address to use " " (This overrides any attempt to 'guess' " "the next IP address to use.") parser.add_argument("--port-id", help="Atmosphere port ID (Override)" " to use.") parser.add_argument("--provider", type=int, help="Atmosphere provider ID" " to use.") parser.add_argument("instance", help="Instance to repair") parser.add_argument("--admin", action="store_true", help="Users addded as admin and staff users.") parser.add_argument("--suspend-loop", action="store_true", help="Repair an instance that is in suspended loop") parser.add_argument("--suspend-release", action="store_true", help="Release the bridge-port for this instance") args = parser.parse_args() users = None added = 0 provider_id = args.provider instance_id = args.instance new_fixed_ip = args.fixed_ip if not provider_id: provider_id = 4 if not instance_id: raise Exception("Instance ID is required") provider = Provider.objects.get(id=provider_id) accounts = OSAccountDriver(Provider.objects.get(id=provider_id)) admin = accounts.admin_driver instance = admin.get_instance(instance_id) if not instance: raise Exception( "Instance %s does not exist on provider %s" % instance_id, provider_id) if args.suspend_release: suspended_release_instance(accounts, admin, instance, provider, args.port_id) elif args.suspend_loop: suspended_repair_instance(accounts, admin, instance, provider) print 'Resuming instance: %s' % instance.id admin.resume_instance(instance) print 'Waiting 5 minutes to allow instance to resume (Ctrl+C to cancel): %s' % instance.id time.sleep(5 * 60) print 'Rebuilding instance network and adding port: %s' % instance.id repair_instance(accounts, admin, instance, provider, new_fixed_ip) else: repair_instance(accounts, admin, instance, provider, new_fixed_ip)
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)
def get_account_driver(provider): """ Create an account driver for a given provider. """ try: type_name = provider.get_type_name().lower() if 'openstack' in type_name: from service.accounts.openstack_manager import AccountDriver as\ OSAccountDriver return OSAccountDriver(provider) elif 'eucalyptus' in type_name: from service.accounts.eucalyptus import AccountDriver as\ EucaAccountDriver return EucaAccountDriver(provider) except: logger.exception("Account driver for provider %s not found." % (provider.location)) return None
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 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. # continue # continue # #Disable time allocation print "Total users added to atmosphere:%s" % len(usernames)
def start(images): print 'Initializing account drivers' euca_accounts = EucaAccountDriver(Provider.objects.get(id=1)) euca_img_class = euca_accounts.image_manager.__class__ euca_img_creds = euca_accounts.image_creds os_accounts = OSAccountDriver(Provider.objects.get(id=4)) os_img_class = os_accounts.image_manager.__class__ os_img_creds = os_accounts.image_creds migrate_args = { 'download_dir': "/Storage", 'image_id': None, 'xen_to_kvm': True, } print 'Account drivers initialized' for mach_to_migrate in images: migrate_args['image_id'] = mach_to_migrate pm = ProviderMachine.objects.get(identifier=mach_to_migrate) migrate_args['image_name'] = pm.application.name print 'Migrating %s..' % mach_to_migrate # Lookup machine, set nme migrate_image(euca_img_class, euca_img_creds, os_img_class, os_img_creds, **migrate_args)