def add_system(): # We accept JSON or form-encoded for convenience if request.json: if 'fqdn' not in request.json: raise BadRequest400('Missing fqdn key') new_fqdn = request.json['fqdn'] elif request.form: if 'fqdn' not in request.form: raise BadRequest400('Missing fqdn parameter') new_fqdn = request.form['fqdn'] else: raise UnsupportedMediaType415 with convert_internal_errors(): if System.query.filter(System.fqdn == new_fqdn).count() != 0: raise Conflict409('System with fqdn %r already exists' % new_fqdn) system = System(fqdn=new_fqdn, owner=identity.current.user) session.add(system) # new systems are visible to everybody by default system.custom_access_policy = SystemAccessPolicy() system.custom_access_policy.add_rule(SystemPermission.view, everybody=True) # XXX this should be 201 with Location: /systems/FQDN/ but 302 is more # convenient because it lets us use a traditional browser form without AJAX # handling, and for now we're redirecting to /view/FQDN until that is moved # to /systems/FQDN/ return flask_redirect(url(u'/view/%s#essentials' % system.fqdn))
def create_system(arch=u'i386', type=SystemType.machine, status=None, owner=None, fqdn=None, shared=True, exclude_osmajor=[], exclude_osversion=[], hypervisor=None, kernel_type=None, date_added=None, return_existing=False, private=False, with_power=True, lab_controller=None, **kw): if owner is None: owner = create_user() if fqdn is None: name = get_test_name() fqdn = unique_name(u'system%s.' + name.replace('_', '.')) if status is None: status = SystemStatus.automated if lab_controller is not None else SystemStatus.manual if System.query.filter(System.fqdn == fqdn).count(): if return_existing: system = System.query.filter(System.fqdn == fqdn).first() for property, value in kw.iteritems(): setattr(system, property, value) else: raise ValueError('Attempted to create duplicate system %s' % fqdn) else: system = System(fqdn=fqdn,type=type, owner=owner, status=status, lab_controller=lab_controller, **kw) session.add(system) if date_added is not None: system.date_added = date_added system.custom_access_policy = SystemAccessPolicy() if not private: system.custom_access_policy.add_rule(SystemPermission.view, everybody=True) if shared: system.custom_access_policy.add_rule( permission=SystemPermission.reserve, everybody=True) if isinstance(arch, list): for a in arch: system.arch.append(Arch.by_name(a)) system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(a), osmajor=osmajor) for osmajor in exclude_osmajor) system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(a), osversion=osversion) for osversion in exclude_osversion) else: system.arch.append(Arch.by_name(arch)) system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(arch), osmajor=osmajor) for osmajor in exclude_osmajor) system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(arch), osversion=osversion) for osversion in exclude_osversion) if with_power: configure_system_power(system) if hypervisor: system.hypervisor = Hypervisor.by_name(hypervisor) if kernel_type: system.kernel_type = KernelType.by_name(kernel_type) system.date_modified = datetime.datetime.utcnow() log.debug('Created system %r', system) return system
def create_system(arch=u'i386', type=SystemType.machine, status=SystemStatus.automated, owner=None, fqdn=None, shared=True, exclude_osmajor=[], exclude_osversion=[], hypervisor=None, kernel_type=None, date_added=None, return_existing=False, private=False, with_power=True, **kw): if owner is None: owner = create_user() if fqdn is None: fqdn = unique_name(u'system%s.testdata') if System.query.filter(System.fqdn == fqdn).count(): if return_existing: system = System.query.filter(System.fqdn == fqdn).first() for property, value in kw.iteritems(): setattr(system, property, value) else: raise ValueError('Attempted to create duplicate system %s' % fqdn) else: system = System(fqdn=fqdn,type=type, owner=owner, status=status, **kw) session.add(system) if date_added is not None: system.date_added = date_added system.custom_access_policy = SystemAccessPolicy() if not private: system.custom_access_policy.add_rule(SystemPermission.view, everybody=True) if shared: system.custom_access_policy.add_rule( permission=SystemPermission.reserve, everybody=True) if isinstance(arch, list): for a in arch: system.arch.append(Arch.by_name(a)) system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(a), osmajor=osmajor) for osmajor in exclude_osmajor) system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(a), osversion=osversion) for osversion in exclude_osversion) else: system.arch.append(Arch.by_name(arch)) system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(arch), osmajor=osmajor) for osmajor in exclude_osmajor) system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(arch), osversion=osversion) for osversion in exclude_osversion) if with_power: configure_system_power(system) if hypervisor: system.hypervisor = Hypervisor.by_name(hypervisor) if kernel_type: system.kernel_type = KernelType.by_name(kernel_type) system.date_modified = datetime.datetime.utcnow() log.debug('Created system %r', system) return system
def _import_row(self, data, log): if data['csv_type'] in system_types and ('fqdn' in data or 'id' in data): if data.get('id', None): try: system = System.query.filter(System.id == data['id']).one() except InvalidRequestError as e: raise ValueError('Non-existent system id') else: try: system = System.query.filter( System.fqdn == data['fqdn']).one() except InvalidRequestError: # Create new system with some defaults # Assume the system is broken until proven otherwise. # Also assumes its a machine. we have to pick something system = System(fqdn=data['fqdn'], owner=identity.current.user, type=SystemType.machine, status=SystemStatus.broken) session.add(system) # new systems are visible to everybody by default system.custom_access_policy = SystemAccessPolicy() system.custom_access_policy.add_rule(SystemPermission.view, everybody=True) if not system.can_edit(identity.current.user): raise ValueError('You are not the owner of %s' % system.fqdn) # we change the FQDN only when a valid system id is supplied if not data.get('id', None): data.pop('fqdn') self.from_csv(system, data, log) elif data['csv_type'] == 'user_group' and 'user' in data: user = User.by_user_name(data['user']) if user is None: raise ValueError('%s is not a valid user' % data['user']) CSV_GroupUser.from_csv(user, data, log) else: raise ValueError('Invalid csv_type %s or missing required fields' % data['csv_type'])
def _import_row(self, data, log): if data['csv_type'] in system_types and ('fqdn' in data or 'id' in data): if data.get('id', None): try: system = System.query.filter(System.id == data['id']).one() except InvalidRequestError as e: raise ValueError('Non-existent system id') else: try: system = System.query.filter(System.fqdn == data['fqdn']).one() except InvalidRequestError: # Create new system with some defaults # Assume the system is broken until proven otherwise. # Also assumes its a machine. we have to pick something system = System(fqdn=data['fqdn'], owner=identity.current.user, type=SystemType.machine, status=SystemStatus.broken) session.add(system) # new systems are visible to everybody by default system.custom_access_policy = SystemAccessPolicy() system.custom_access_policy.add_rule( SystemPermission.view, everybody=True) if not system.can_edit(identity.current.user): raise ValueError('You are not the owner of %s' % system.fqdn) # we change the FQDN only when a valid system id is supplied if not data.get('id', None): data.pop('fqdn') self.from_csv(system, data, log) elif data['csv_type'] == 'user_group' and 'user' in data: user = User.by_user_name(data['user']) if user is None: raise ValueError('%s is not a valid user' % data['user']) CSV_GroupUser.from_csv(user, data, log) else: raise ValueError('Invalid csv_type %s or missing required fields' % data['csv_type'])
def create_system(arch=u'i386', type=SystemType.machine, status=None, owner=None, fqdn=None, shared=True, exclude_osmajor=[], exclude_osversion=[], hypervisor=None, kernel_type=None, date_added=None, return_existing=False, private=False, with_power=True, lab_controller=None, **kw): if owner is None: owner = create_user() if fqdn is None: name = get_test_name() fqdn = unique_name(u'system%s.' + name.replace('_', '.')) if status is None: status = SystemStatus.automated if lab_controller is not None else SystemStatus.manual if System.query.filter(System.fqdn == fqdn).count(): if return_existing: system = System.query.filter(System.fqdn == fqdn).first() for property, value in kw.iteritems(): setattr(system, property, value) else: raise ValueError('Attempted to create duplicate system %s' % fqdn) else: system = System(fqdn=fqdn,type=type, owner=owner, status=status, lab_controller=lab_controller, **kw) session.add(system) # Normally the system would be "idle" when first added, and then becomes # "pending" when a user flips it to Automated status. But for simplicity in # the tests, we will just force it back to "idle" here since we know we # just created it. This lets a subsequent call to the scheduler pick it up # immediately, without going through an iteration of # schedule_pending_systems() first. system.scheduler_status = SystemSchedulerStatus.idle if date_added is not None: system.date_added = date_added system.custom_access_policy = SystemAccessPolicy() if not private: system.custom_access_policy.add_rule(SystemPermission.view, everybody=True) if shared: system.custom_access_policy.add_rule( permission=SystemPermission.reserve, everybody=True) if isinstance(arch, list): for a in arch: system.arch.append(Arch.by_name(a)) system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(a), osmajor=osmajor) for osmajor in exclude_osmajor) system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(a), osversion=osversion) for osversion in exclude_osversion) elif arch is not None: system.arch.append(Arch.by_name(arch)) system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(arch), osmajor=osmajor) for osmajor in exclude_osmajor) system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(arch), osversion=osversion) for osversion in exclude_osversion) if with_power: configure_system_power(system) if hypervisor: system.hypervisor = Hypervisor.by_name(hypervisor) if kernel_type: system.kernel_type = KernelType.by_name(kernel_type) system.date_modified = datetime.datetime.utcnow() log.debug('Created system %r', system) return system
def action_import(self, csv_file, *args, **kw): """ TurboGears method to import data from csv """ log = [] try: # ... process CSV file contents here ... missing = object() reader = csv.DictReader(csv_file.file, restkey=missing, restval=missing) for data in reader: if missing in data: log.append('Too many fields on line %s (expecting %s)' % (reader.line_num, len(reader.fieldnames))) continue if any(value is missing for value in data.itervalues()): missing_fields = [field for field, value in data.iteritems() if value is missing] log.append('Missing fields on line %s: %s' % (reader.line_num, ', '.join(missing_fields))) continue if 'csv_type' in data: if data['csv_type'] in system_types and ('fqdn' in data or 'id' in data): if data.get('id', None): try: system = System.query.filter(System.id == data['id']).one() except InvalidRequestError as e: log.append('Error importing system on line %s: Non-existent system id' % reader.line_num) continue else: try: system = System.query.filter(System.fqdn == data['fqdn']).one() except InvalidRequestError: # Create new system with some defaults # Assume the system is broken until proven otherwise. # Also assumes its a machine. we have to pick something try: system = System(fqdn=data['fqdn'], owner=identity.current.user, type=SystemType.machine, status=SystemStatus.broken) session.add(system) except ValueError as e: log.append('Error importing system on line %s: %s' % (reader.line_num, str(e))) continue # new systems are visible to everybody by default system.custom_access_policy = SystemAccessPolicy() system.custom_access_policy.add_rule( SystemPermission.view, everybody=True) if system.can_edit(identity.current.user): # we change the FQDN only when a valid system id is supplied if not data.get('id', None): data.pop('fqdn') try: self.from_csv(system, data, log) except ValueError as e: log.append('Error importing system on line %s: %s' % (reader.line_num, str(e))) if system.id: # System already existed but some or all of the # import data was invalid. session.expire(system) else: # System didn't exist before import but some # or all of the import data was invalid. session.expunge(system) del(system) else: session.add(system) session.flush() else: log.append("You are not the owner of %s" % system.fqdn) elif data['csv_type'] == 'user_group' and \ 'user' in data: user = User.by_user_name(data['user']) if user: CSV_GroupUser.from_csv(user, data, log) else: log.append('%s is not a valid user' % data['user']) else: log.append("Invalid csv_type %s or missing required fields" % data['csv_type']) else: log.append("Missing csv_type from record") except csv.Error, e: session.rollback() log.append('Error parsing CSV file: %s' % e)