def create_job_for_recipes(recipes, owner=None, whiteboard=None, cc=None,product=None, retention_tag=None, group=None, submitter=None, priority=None, **kwargs): if retention_tag is None: retention_tag = RetentionTag.by_tag(u'scratch') # Don't use default, unpredictable else: retention_tag = RetentionTag.by_tag(retention_tag) if owner is None: owner = create_user() if whiteboard is None: whiteboard = unique_name(u'job %s') job = Job(whiteboard=whiteboard, ttasks=sum(r.ttasks for r in recipes), owner=owner, retention_tag=retention_tag, group=group, product=product, submitter=submitter) if cc is not None: job.cc = cc if priority is None: priority = TaskPriority.default_priority() recipe_set = RecipeSet(ttasks=sum(r.ttasks for r in recipes), priority=priority) recipe_set.recipes.extend(recipes) job.recipesets.append(recipe_set) session.add(job) session.flush() log.debug('Created %s', job.t_id) return job
def _process_job_tag_product(self, retention_tag=None, product=None, *args, **kw): """ Process job retention_tag and product """ retention_tag = retention_tag or RetentionTag.get_default().tag try: tag = RetentionTag.by_tag(retention_tag.lower()) except InvalidRequestError: raise BX(_("Invalid retention_tag attribute passed. Needs to be one of %s. You gave: %s" % (','.join([x.tag for x in RetentionTag.get_all()]), retention_tag))) if product is None and tag.requires_product(): raise BX(_("You've selected a tag which needs a product associated with it, \ alternatively you could use one of the following tags %s" % ','.join([x.tag for x in RetentionTag.get_all() if not x.requires_product()]))) elif product is not None and not tag.requires_product(): raise BX(_("Cannot specify a product with tag %s, please use %s as a tag " % (retention_tag,','.join([x.tag for x in RetentionTag.get_all() if x.requires_product()])))) else: pass if tag.requires_product(): try: product = Product.by_name(product) return (tag, product) except ValueError: raise BX(_("You entered an invalid product name: %s" % product)) else: return tag, None
def create_job_for_recipesets(recipesets, owner=None, whiteboard=None, cc=None, product=None, retention_tag=None, group=None, submitter=None, **kwargs): if retention_tag is None: retention_tag = RetentionTag.by_tag( u'scratch') # Don't use default, unpredictable else: retention_tag = RetentionTag.by_tag(retention_tag) if owner is None: owner = create_user() if whiteboard is None: whiteboard = unique_name(u'job %s') job = Job(whiteboard=whiteboard, ttasks=sum(rs.ttasks for rs in recipesets), owner=owner, retention_tag=retention_tag, group=group, product=product, submitter=submitter) if cc is not None: job.cc = cc job.recipesets.extend(recipesets) session.add(job) session.flush() log.debug('Created %s', job.t_id) return job
def set_retention_product(self, job_t_id, retention_tag_name, product_name): """ XML-RPC method to update a job's retention tag, product, or both. There is an important distinction between product_name of None, which means do not change the existing value, vs. empty string, which means clear the existing product. """ job = TaskBase.get_by_t_id(job_t_id) if job.can_change_product(identity.current.user) and \ job.can_change_retention_tag(identity.current.user): if retention_tag_name and product_name: retention_tag = RetentionTag.by_name(retention_tag_name) product = Product.by_name(product_name) result = Utility.update_retention_tag_and_product(job, retention_tag, product) elif retention_tag_name and product_name == '': retention_tag = RetentionTag.by_name(retention_tag_name) result = Utility.update_retention_tag_and_product(job, retention_tag, None) elif retention_tag_name: retention_tag = RetentionTag.by_name(retention_tag_name) result = Utility.update_retention_tag(job, retention_tag) elif product_name: product = Product.by_name(product_name) result = Utility.update_product(job, product) elif product_name == '': result = Utility.update_product(job, None) else: result = {'success': False, 'msg': 'Nothing to do'} if not result['success'] is True: raise BeakerException('Job %s not updated: %s' % (job.id, result.get('msg', 'Unknown reason'))) else: raise BeakerException('No permission to modify %s' % job)
def update(self, id, **kw): # XXX Thus function is awkward and needs to be cleaned up. try: job = Job.by_id(id) except InvalidRequestError: raise cherrypy.HTTPError(status=400, message='Invalid job id %s' % id) if not job.can_change_product(identity.current.user) or not \ job.can_change_retention_tag(identity.current.user): raise cherrypy.HTTPError(status=403, message="You don't have permission to update job id %s" % id) returns = {'success' : True, 'vars':{}} if 'retentiontag' in kw and 'product' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) returns.update(Utility.update_retention_tag_and_product(job, retention_tag, product)) elif 'retentiontag' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) returns.update(Utility.update_retention_tag(job, retention_tag)) elif 'product' in kw: if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) returns.update(Utility.update_product(job, product)) if 'whiteboard' in kw: job.whiteboard = kw['whiteboard'] return returns
def set_retention_product(self, job_t_id, retention_tag_name, product_name): """ XML-RPC method to update a job's retention tag, product, or both. There is an important distinction between product_name of None, which means do not change the existing value, vs. empty string, which means clear the existing product. """ job = TaskBase.get_by_t_id(job_t_id) if job.can_change_product(identity.current.user) and \ job.can_change_retention_tag(identity.current.user): if retention_tag_name and product_name: retention_tag = RetentionTag.by_name(retention_tag_name) product = Product.by_name(product_name) old_tag = job.retention_tag if job.retention_tag else None result = Utility.update_retention_tag_and_product( job, retention_tag, product) job.record_activity(user=identity.current.user, service=u'XMLRPC', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif retention_tag_name and product_name == '': retention_tag = RetentionTag.by_name(retention_tag_name) old_tag = job.retention_tag if job.retention_tag else None result = Utility.update_retention_tag_and_product( job, retention_tag, None) job.record_activity(user=identity.current.user, service=u'XMLRPC', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif retention_tag_name: retention_tag = RetentionTag.by_name(retention_tag_name) old_tag = job.retention_tag if job.retention_tag else None result = Utility.update_retention_tag(job, retention_tag) job.record_activity(user=identity.current.user, service=u'XMLRPC', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif product_name: product = Product.by_name(product_name) result = Utility.update_product(job, product) elif product_name == '': result = Utility.update_product(job, None) else: result = {'success': False, 'msg': 'Nothing to do'} if not result['success'] is True: raise BeakerException( 'Job %s not updated: %s' % (job.id, result.get('msg', 'Unknown reason'))) else: raise BeakerException('No permission to modify %s' % job)
def update(self, id, **kw): # XXX Thus function is awkward and needs to be cleaned up. try: job = Job.by_id(id) except InvalidRequestError: raise cherrypy.HTTPError(status=400, message='Invalid job id %s' % id) if not job.can_change_product(identity.current.user) or not \ job.can_change_retention_tag(identity.current.user): raise cherrypy.HTTPError( status=403, message="You don't have permission to update job id %s" % id) returns = {'success': True, 'vars': {}} if 'retentiontag' in kw and 'product' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) old_tag = job.retention_tag if job.retention_tag else None returns.update( Utility.update_retention_tag_and_product( job, retention_tag, product)) job.record_activity(user=identity.current.user, service=u'WEBUI', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif 'retentiontag' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) old_tag = job.retention_tag if job.retention_tag else None returns.update(Utility.update_retention_tag(job, retention_tag)) job.record_activity(user=identity.current.user, service=u'WEBUI', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif 'product' in kw: if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) returns.update(Utility.update_product(job, product)) if 'whiteboard' in kw: job.whiteboard = kw['whiteboard'] return returns
def edit_default(cls, **kw): is_default = bool(int(kw.get('default'))) id = kw.get('id') needs_product = bool(kw.get('needs_product', None)) tag = RetentionTag.by_id(id) tag.default = is_default tag.needs_product = needs_product
def update_task_product(cls, job, retentiontag_id=None, product_id=None): if product_id is ProductWidget.product_deselected: product = product_id elif product_id is not None: try: product = Product.by_id(product_id) except NoResultFound: raise ValueError('%s is not a valid product' % product_id) else: product=None if retentiontag_id: try: retentiontag = RetentionTag.by_id(retentiontag_id) except NoResultFound: raise ValueError('%s is not a valid retention tag' % retentiontag_id) else: retentiontag = None if retentiontag is None and product is None: return {'success': False} if retentiontag is not None and product is None: #trying to update retentiontag only return cls.check_retentiontag_job(job, retentiontag) elif retentiontag is None and product is not None: #only product return cls.check_product_job(job, product) else: #updating both return cls._update_task_product(job, product, retentiontag)
def tags(self, tags=None, user=None, *args, **kw): if tags is None: tags = Tag.get_all() def show_delete(x): if x.can_delete(): return make_link(url='./delete/%s' % x.id, text='Delete') else: return None def show_tag(x): if x.is_default: #If we are the default, we can't change to not default return x.tag elif user and user.is_admin(): return make_edit_link(x.tag,x.id) else: #no perms to edit return x.tag my_fields = [myPaginateDataGrid.Column(name='tag', title='Tags', getter=lambda x: show_tag(x),options=dict(sortable=True)), myPaginateDataGrid.Column(name='default', title='Default', getter=lambda x: x.default,options=dict(sortable=True)), myPaginateDataGrid.Column(name='delete', title='Delete', getter=lambda x: show_delete(x))] tag_grid = myPaginateDataGrid(fields=my_fields) return_dict = dict(title='Tags', grid = tag_grid, object_count = tags.count(), search_bar = None, search_widget = self.search_widget_form, list = tags) return return_dict
def tags(self, tags=None, user=None, *args, **kw): if tags is None: tags = Tag.get_all() def show_delete(x): if x.can_delete(): return XML('<a class="btn" href="./delete/%s">' '<i class="fa fa-times"/> Delete</a>' % x.id) else: return None def show_tag(x): if x.is_default: #If we are the default, we can't change to not default return x.tag elif user and user.is_admin(): return make_edit_link(x.tag,x.id) else: #no perms to edit return x.tag my_fields = [myPaginateDataGrid.Column(name='tag', title='Tags', getter=lambda x: show_tag(x),options=dict(sortable=True)), myPaginateDataGrid.Column(name='default', title='Default', getter=lambda x: x.default,options=dict(sortable=True)), myPaginateDataGrid.Column(name='delete', title='Delete', getter=lambda x: show_delete(x))] tag_grid = myPaginateDataGrid(fields=my_fields, add_action='./new') return_dict = dict(title='Tags', grid = tag_grid, search_bar = None, search_widget = self.search_widget_form, list = tags) return return_dict
def edit(self, id, **kw): tag = Tag.by_id(id) return dict(form=self.tag_form, title=_(u'Retention tag %s' % tag.tag), action='./save_edit', options={}, value=tag, disabled_fields=['tag'])
def delete(self, id): tag = Tag.by_id(id) if not tag.can_delete(): # Trying to be funny... flash(u'%s is not applicable for deletion' % tag.tag) redirect('/retentiontag/admin') session.delete(tag) flash(u'Successfully deleted %s' % tag.tag) redirect('/retentiontag/admin')
def create_retention_tag(name=None, default=False, needs_product=False): if name is None: name = unique_name(u'tag%s') new_tag = RetentionTag(name, is_default=default, needs_product=needs_product) session.add(new_tag) return new_tag
def save_edit(self, id=None, **kw): retention_tag = Tag.by_id(id) retention_tag.tag = kw['tag'] retention_tag.default = kw['default'] retention_tag.expire_in_days = kw['expire_in_days'] retention_tag.needs_product = kw['needs_product'] flash(_(u"OK")) redirect("./admin")
def edit(self, id, **kw): tag = Tag.by_id(id) return dict( form = self.tag_form, action = './save_edit', options = {}, value = tag, disabled_fields = ['tag'] )
def _process_job_tag_product(self, retention_tag=None, product=None, *args, **kw): """ Process job retention_tag and product """ retention_tag = retention_tag or RetentionTag.get_default().tag try: tag = RetentionTag.by_tag(retention_tag.lower()) except InvalidRequestError: raise BX( _("Invalid retention_tag attribute passed. Needs to be one of %s. You gave: %s" % (','.join([x.tag for x in RetentionTag.get_all() ]), retention_tag))) if product is None and tag.requires_product(): raise BX( _("You've selected a tag which needs a product associated with it, \ alternatively you could use one of the following tags %s" % ','.join([ x.tag for x in RetentionTag.get_all() if not x.requires_product() ]))) elif product is not None and not tag.requires_product(): raise BX( _("Cannot specify a product with tag %s, please use %s as a tag " % (retention_tag, ','.join([ x.tag for x in RetentionTag.get_all() if x.requires_product() ])))) else: pass if tag.requires_product(): try: product = Product.by_name(product) return (tag, product) except ValueError: raise BX(_("You entered an invalid product name: %s" % product)) else: return tag, None
def test_cloning_recipeset_from_job_with_product(self): with session.begin(): job = data_setup.create_job() job.retention_tag = RetentionTag.list_by_requires_product()[0] job.product = Product(u'product_name') b = self.browser login(b) b.get(get_server_base() + 'jobs/clone?job_id=%s' % job.id) cloned_from_job = b.find_element_by_xpath('//textarea[@name="textxml"]').text b.get(get_server_base() + 'jobs/clone?recipeset_id=%s' % job.recipesets[0].id) cloned_from_rs = b.find_element_by_xpath('//textarea[@name="textxml"]').text self.assertEqual(cloned_from_job,cloned_from_rs)
def check_product_job(cls, job, product): """ performs logic needed to determine if changing a retention_tag is valid, returns an error fit for displaying in widget """ retentiontag = job.retention_tag if not retentiontag.requires_product() and \ product != ProductWidget.product_deselected: return{'success': False, 'msg': 'Current retention tag does not support a product', 'vars': {cls._needs_tag: 1, 'VALID_TAGS': [[tag.id,tag.tag] for tag in \ RetentionTag.list_by_requires_product()]}} if retentiontag.requires_product() and \ product == ProductWidget.product_deselected: return{'success': False, 'msg': 'Current retention tag requires a product', 'vars': {cls._needs_tag: 1, 'VALID_TAGS': [[tag.id,tag.tag] for tag in \ RetentionTag.list_by_requires_product(False)]}} return {'success': True}
def update_product(cls, job, product): """ performs logic needed to determine if changing a retention_tag is valid, returns an error fit for displaying in widget """ retentiontag = job.retention_tag if not retentiontag.requires_product() and \ product != None: return{'success': False, 'msg': 'Current retention tag does not support a product', 'vars': {cls._needs_tag: 1, 'VALID_TAGS': [[tag.id,tag.tag] for tag in \ RetentionTag.list_by_requires_product()]}} if retentiontag.requires_product() and \ product == None: return{'success': False, 'msg': 'Current retention tag requires a product', 'vars': {cls._needs_tag: 1, 'VALID_TAGS': [[tag.id,tag.tag] for tag in \ RetentionTag.list_by_requires_product(False)]}} job.product = product return {'success': True}
def test_search_tag(self): with session.begin(): my_job = data_setup.create_job() new_tag = RetentionTag(tag=data_setup.unique_name('mytag%s')) my_job.retention_tag = new_tag b = self.browser # Test with tag. b.get(get_server_base() + 'jobs') b.find_element_by_link_text('Show Search Options').click() b.find_element_by_xpath("//select[@id='jobsearch_0_table'] \ /option[@value='Tag']").click() b.find_element_by_xpath("//select[@id='jobsearch_0_operation'] \ /option[@value='is']").click() b.find_element_by_xpath("//select[@id='jobsearch_0_value']/" "option[normalize-space(text())='%s']" % new_tag.tag).click() b.find_element_by_id('searchform').submit() job_search_result = \ b.find_element_by_xpath('//table[@id="widget"]').text self.assert_('J:%s' % my_job.id in job_search_result)
def populate_db(user_name=None, password=None, user_display_name=None, user_email_address=None): logger.info('Populating tables with pre-defined values if necessary') session.begin() try: admin = Group.by_name(u'admin') except InvalidRequestError: admin = Group(group_name=u'admin', display_name=u'Admin') session.add(admin) try: lab_controller = Group.by_name(u'lab_controller') except InvalidRequestError: lab_controller = Group(group_name=u'lab_controller', display_name=u'Lab Controller') session.add(lab_controller) # Setup User account if user_name: user = User.lazy_create(user_name=user_name.decode('utf8')) if password: user.password = password.decode('utf8') if user_display_name: user.display_name = user_display_name.decode('utf8') if user_email_address: user.email_address = user_email_address.decode('utf8') # Ensure the user is in the 'admin' group as an owner. # Flush for lazy_create. session.flush() user_group_assoc = UserGroup.lazy_create( user_id=user.user_id, group_id=admin.group_id) user_group_assoc.is_owner = True # Create distro_expire perm if not present try: _ = Permission.by_name(u'distro_expire') except NoResultFound: distro_expire_perm = Permission(u'distro_expire') session.add(distro_expire_perm) # Create proxy_auth perm if not present try: _ = Permission.by_name(u'proxy_auth') except NoResultFound: proxy_auth_perm = Permission(u'proxy_auth') session.add(proxy_auth_perm) # Create tag_distro perm if not present try: _ = Permission.by_name(u'tag_distro') except NoResultFound: tag_distro_perm = Permission(u'tag_distro') admin.permissions.append(tag_distro_perm) # Create stop_task perm if not present try: _ = Permission.by_name(u'stop_task') except NoResultFound: stop_task_perm = Permission(u'stop_task') lab_controller.permissions.append(stop_task_perm) admin.permissions.append(stop_task_perm) # Create secret_visible perm if not present try: _ = Permission.by_name(u'secret_visible') except NoResultFound: secret_visible_perm = Permission(u'secret_visible') lab_controller.permissions.append(secret_visible_perm) admin.permissions.append(secret_visible_perm) # Create change_prio perm if not present try: _ = Permission.by_name(u'change_prio') except NoResultFound: change_prio_perm = Permission(u'change_prio') session.add(change_prio_perm) # Setup Hypervisors Table if Hypervisor.query.count() == 0: for h in [u'KVM', u'Xen', u'HyperV', u'VMWare']: session.add(Hypervisor(hypervisor=h)) # Setup kernel_type Table if KernelType.query.count() == 0: for type in [u'default', u'highbank', u'imx', u'omap', u'tegra']: session.add(KernelType(kernel_type=type, uboot=False)) for type in [u'mvebu']: session.add(KernelType(kernel_type=type, uboot=True)) # Setup base Architectures if Arch.query.count() == 0: for arch in [u'i386', u'x86_64', u'ia64', u'ppc', u'ppc64', u'ppc64le', u's390', u's390x', u'armhfp', u'aarch64', u'arm']: session.add(Arch(arch)) # Setup base power types if PowerType.query.count() == 0: for power_type in [u'apc_snmp', u'apc_snmp_then_etherwake', u'bladecenter', u'bladepap', u'drac', u'ether_wake', u'hyper-v', u'ilo', u'integrity', u'ipmilan', u'ipmitool', u'lpar', u'rsa', u'virsh', u'wti']: session.add(PowerType(power_type)) # Setup key types if Key.query.count() == 0: session.add(Key(u'DISKSPACE', True)) session.add(Key(u'COMMENT')) session.add(Key(u'CPUFAMILY', True)) session.add(Key(u'CPUFLAGS')) session.add(Key(u'CPUMODEL')) session.add(Key(u'CPUMODELNUMBER', True)) session.add(Key(u'CPUSPEED', True)) session.add(Key(u'CPUVENDOR')) session.add(Key(u'DISK', True)) session.add(Key(u'FORMFACTOR')) session.add(Key(u'HVM')) session.add(Key(u'MEMORY', True)) session.add(Key(u'MODEL')) session.add(Key(u'MODULE')) session.add(Key(u'NETWORK')) session.add(Key(u'NR_DISKS', True)) session.add(Key(u'NR_ETH', True)) session.add(Key(u'NR_IB', True)) session.add(Key(u'PCIID')) session.add(Key(u'PROCESSORS', True)) session.add(Key(u'RTCERT')) session.add(Key(u'SCRATCH')) session.add(Key(u'STORAGE')) session.add(Key(u'USBID')) session.add(Key(u'VENDOR')) session.add(Key(u'XENCERT')) session.add(Key(u'NETBOOT_METHOD')) if RetentionTag.query.count() == 0: session.add(RetentionTag(tag=u'scratch', is_default=1, expire_in_days=30)) session.add(RetentionTag(tag=u'60days', needs_product=False, expire_in_days=60)) session.add(RetentionTag(tag=u'120days', needs_product=False, expire_in_days=120)) session.add(RetentionTag(tag=u'active', needs_product=True)) session.add(RetentionTag(tag=u'audit', needs_product=True)) config_items = [ # name, description, numeric (u'root_password', u'Plaintext root password for provisioned systems', False), (u'root_password_validity', u"Maximum number of days a user's root password is valid for", True), (u'guest_name_prefix', u'Prefix for names of dynamic guests in OpenStack', False), (u'guest_private_network', u'Network address in CIDR format for private networks' ' of dynamic guests in OpenStack.', False), ] for name, description, numeric in config_items: ConfigItem.lazy_create(name=name, description=description, numeric=numeric) if ConfigItem.by_name(u'root_password').current_value() is None: ConfigItem.by_name(u'root_password').set(u'beaker', user=admin.users[0]) if ConfigItem.by_name(u'guest_private_network').current_value() is None: ConfigItem.by_name(u'guest_private_network').set(u'192.168.10.0/24', user=admin.users[0]) session.commit() session.close() logger.info('Pre-defined values populated')
def save(self, id=None, **kw): retention_tag = Tag(kw['tag'], kw['default'], kw['needs_product']) retention_tag.expire_in_days = kw['expire_in_days'] session.add(retention_tag) flash(_(u"OK")) redirect("./admin")
def by_tag(self, input, *args, **kw): input = input.lower() search = Tag.list_by_tag(input) tags = [match.tag for match in search] return dict(matches=tags)