def _try_autocreate(user_name): """ If the necessary WSGI environment variables are populated, automatically creates a new Beaker user account based on their values and returns it. Otherwise returns None. """ from bkr.server.model import session, User if not flask.request.environ.get('REMOTE_USER_FULLNAME'): log.debug('User autocreation attempted for %r but ' 'REMOTE_USER_FULLNAME env var was not populated', user_name) return if not flask.request.environ.get('REMOTE_USER_EMAIL'): log.debug('User autocreation attempted for %r but ' 'REMOTE_USER_EMAIL env var was not populated', user_name) return user = User() user.user_name = user_name.decode('utf8') user.display_name = flask.request.environ['REMOTE_USER_FULLNAME'].decode('utf8') user.email_address = flask.request.environ['REMOTE_USER_EMAIL'].decode('utf8') session.add(user) session.flush() log.debug('Autocreated user %s', user) return user
def _try_autocreate(user_name): """ If the necessary WSGI environment variables are populated, automatically creates a new Beaker user account based on their values and returns it. Otherwise returns None. """ from bkr.server.model import session, User if not flask.request.environ.get('REMOTE_USER_FULLNAME'): log.debug( 'User autocreation attempted for %r but ' 'REMOTE_USER_FULLNAME env var was not populated', user_name) return if not flask.request.environ.get('REMOTE_USER_EMAIL'): log.debug( 'User autocreation attempted for %r but ' 'REMOTE_USER_EMAIL env var was not populated', user_name) return user = User() user.user_name = user_name.decode('utf8') user.display_name = flask.request.environ['REMOTE_USER_FULLNAME'].decode( 'utf8') user.email_address = flask.request.environ['REMOTE_USER_EMAIL'].decode( 'utf8') session.add(user) session.flush() log.debug('Autocreated user %s', user) return user
def delete_powertype(id): """ Deletes a power type by the given id. :param id: The id of the power type to be deleted. :status 204: Power type successfully deleted. :status 400: Power type is referenced by systems. :status 404: Power type can not be found. """ try: powertype = PowerType.by_id(id) except NoResultFound: raise NotFound404('Power type: %s does not exist' % id) systems_referenced = System.query.join(System.power).filter( Power.power_type == powertype).count() if systems_referenced: raise BadRequest400('Power type %s still referenced by %i systems' % ( powertype.name, systems_referenced)) session.delete(powertype) activity = Activity(identity.current.user, u'HTTP', u'Deleted', u'PowerType', powertype.name) session.add(activity) return '', 204
def delete_powertype(id): """ Deletes a power type by the given id. :param id: The id of the power type to be deleted. :status 204: Power type successfully deleted. :status 400: Power type is referenced by systems. :status 404: Power type can not be found. """ try: powertype = PowerType.by_id(id) except NoResultFound: raise NotFound404('Power type: %s does not exist' % id) systems_referenced = System.query.join( System.power).filter(Power.power_type == powertype).count() if systems_referenced: raise BadRequest400('Power type %s still referenced by %i systems' % (powertype.name, systems_referenced)) session.delete(powertype) activity = Activity(identity.current.user, u'HTTP', u'Deleted', u'PowerType', powertype.name) session.add(activity) return '', 204
def setUp(self): self.powertype_name = unique_name('beerpowered%s') with session.begin(): self.powertype = PowerType(name=self.powertype_name) session.add(self.powertype) self.s = requests.Session() login(self.s)
def test_deletes_power_type_successfully(self): powertype_name = unique_name('beerpowered%s') with session.begin(): pt = PowerType(name=powertype_name) session.add(pt) activity_count = Activity.query.count() b = self.browser b.get(get_server_base() + 'powertypes/') b.find_element_by_xpath('//li[contains(., "%s")]/button' % pt.name).click() b.find_element_by_xpath('//ul[contains(@class, "power-types-list") and ' 'not(./li[contains(., "%s")])]' % pt.name) with session.begin(): session.expire_all() self.assertEqual(0, session.query(PowerType).filter_by(name=powertype_name).count()) self.assertEqual(activity_count + 1, Activity.query.count())
def test_deletes_power_type_successfully(self): powertype_name = unique_name('beerpowered%s') with session.begin(): pt = PowerType(name=powertype_name) session.add(pt) activity_count = Activity.query.count() b = self.browser b.get(get_server_base() + 'powertypes/') b.find_element_by_xpath('//li[contains(., "%s")]/button' % pt.name).click() b.find_element_by_xpath( '//ul[contains(@class, "power-types-list") and ' 'not(./li[contains(., "%s")])]' % pt.name) with session.begin(): session.expire_all() self.assertEqual( 0, session.query(PowerType).filter_by( name=powertype_name).count()) self.assertEqual(activity_count + 1, Activity.query.count())
def generate_kickstart(install_options, distro_tree, system, user, recipe=None, ks_appends=None, kickstart=None, installation=None): if recipe: lab_controller = recipe.recipeset.lab_controller elif system: lab_controller = system.lab_controller else: raise ValueError("Must specify either a system or a recipe") recipe_whiteboard = job_whiteboard = '' if recipe: if recipe.whiteboard: recipe_whiteboard = recipe.whiteboard if recipe.recipeset.job.whiteboard: job_whiteboard = recipe.recipeset.job.whiteboard installation = installation if installation is not None else recipe.installation # User-supplied templates don't get access to our model objects, in case # they do something foolish/naughty. restricted_context = { 'kernel_options_post': install_options.kernel_options_post_str, 'recipe_whiteboard': recipe_whiteboard, 'job_whiteboard': job_whiteboard, 'distro_tree': RestrictedDistroTree(installation.osmajor, installation.osminor, installation.distro_name, installation.variant, installation.arch.arch, installation.tree_url, distro_tree), 'distro': RestrictedDistro(installation.osmajor, installation.osminor, installation.distro_name), 'lab_controller': RestrictedLabController(lab_controller), 'recipe': RestrictedRecipe(recipe) if recipe else None, 'ks_appends': ks_appends or [], } restricted_context.update(install_options.ks_meta) # System templates and snippets have access to more useful stuff. context = dict(restricted_context) context.update({ 'system': system, 'lab_controller': lab_controller, 'user': user, 'recipe': recipe, 'config': config, }) if distro_tree: context.update({ 'distro_tree': distro_tree, 'distro': distro_tree.distro, }) else: # But user-defined distros only get access to our "Restricted" model objects context.update({ 'distro_tree': RestrictedDistroTree(installation.osmajor, installation.osminor, installation.distro_name, installation.variant, installation.arch.arch, installation.tree_url), 'distro': RestrictedDistro(installation.osmajor, installation.osminor, installation.distro_name), }) snippet_locations = [] if system: snippet_locations.append( 'snippets/per_system/%%s/%s' % system.fqdn) snippet_locations.extend([ 'snippets/per_lab/%%s/%s' % lab_controller.fqdn, 'snippets/per_osversion/%%s/%s.%s' % (installation.osmajor, installation.osminor), 'snippets/per_osmajor/%%s/%s' % installation.osmajor, 'snippets/%s', ]) def snippet(name): template = None candidates = [location % name for location in snippet_locations] for candidate in candidates: try: template = template_env.get_template(candidate) break except jinja2.TemplateNotFound: continue if template: retval = template.render(context) if retval and not retval.endswith('\n'): retval += '\n' return retval else: return u'# no snippet data for %s\n' % name restricted_context['snippet'] = snippet context['snippet'] = snippet with TemplateRenderingEnvironment(): if kickstart: template = template_env.from_string( "{% snippet 'install_method' %}\n" + kickstart) result = template.render(restricted_context) else: template = kickstart_template(installation.osmajor) result = template.render(context) rendered_kickstart = RenderedKickstart(kickstart=result) session.add(rendered_kickstart) session.flush() # so that it has an id return rendered_kickstart
def generate_kickstart(install_options, distro_tree, system, user, recipe=None, ks_appends=None, kickstart=None): if recipe: lab_controller = recipe.recipeset.lab_controller elif system: lab_controller = system.lab_controller else: raise ValueError("Must specify either a system or a recipe") # User-supplied templates don't get access to our model objects, in case # they do something foolish/naughty. recipe_whiteboard = job_whiteboard = '' if recipe: if recipe.whiteboard: recipe_whiteboard = recipe.whiteboard if recipe.recipeset.job.whiteboard: job_whiteboard = recipe.recipeset.job.whiteboard restricted_context = { 'kernel_options_post': install_options.kernel_options_post_str, 'recipe_whiteboard': recipe_whiteboard, 'job_whiteboard': job_whiteboard, } restricted_context.update(install_options.ks_meta) # XXX find a better place to set this, perhaps from the kickstart templates rhel_osmajor = ['RedHatEnterpriseLinux6', 'RedHatEnterpriseLinux7', 'RedHatServerforARMDevelopmentPreview2'] if distro_tree.distro.osversion.osmajor.osmajor in rhel_osmajor \ or distro_tree.distro.osversion.osmajor.osmajor.startswith('Fedora'): restricted_context['end'] = '%end' # System templates and snippets have access to more useful stuff. context = dict(restricted_context) context.update({ 'distro_tree': distro_tree, 'distro': distro_tree.distro, 'system': system, 'lab_controller': lab_controller, 'user': user, 'recipe': recipe, 'config': config, 'ks_appends': ks_appends or [], }) snippet_locations = [] if system: snippet_locations.append( 'snippets/per_system/%%s/%s' % system.fqdn) snippet_locations.extend([ 'snippets/per_lab/%%s/%s' % lab_controller.fqdn, 'snippets/per_osversion/%%s/%s' % distro_tree.distro.osversion, 'snippets/per_osmajor/%%s/%s' % distro_tree.distro.osversion.osmajor, 'snippets/%s', ]) def snippet(name): template = None candidates = [location % name for location in snippet_locations] for candidate in candidates: try: template = template_env.get_template(candidate) break except jinja2.TemplateNotFound: continue if template: retval = template.render(context) if retval and not retval.endswith('\n'): retval += '\n' return retval else: return u'# no snippet data for %s\n' % name restricted_context['snippet'] = snippet context['snippet'] = snippet with TemplateRenderingEnvironment(): if kickstart: template = template_env.from_string( "{% snippet 'install_method' %}\n" + kickstart) result = template.render(restricted_context) else: template = kickstart_template(distro_tree) result = template.render(context) rendered_kickstart = RenderedKickstart(kickstart=result) session.add(rendered_kickstart) session.flush() # so that it has an id return rendered_kickstart