def _maintain(project, node, node_label): """Helper function to execute maintenance tasks. Powers off the node, checks for the existence of maintenance pool config options, and posts to the maintenance URL if they exist.""" logger = logging.getLogger(__name__) if (cfg.has_option('maintenance', 'maintenance_project') and cfg.has_option('maintenance', 'url')): maintenance_proj = get_or_404( model.Project, cfg.get('maintenance', 'maintenance_project')) if (project == maintenance_proj): # Already in maintenance pool return elif (cfg.has_option('maintenance', 'maintenance_project')): raise errors.NotFoundError("Maintenance URL not in hil.cfg.") elif (cfg.has_option('maintenance', 'url')): raise errors.NotFoundError("Maintenance project not in hil.cfg.") else: return if (cfg.has_option('maintenance', 'shutdown')): node.obm.power_off() maintenance_proj.nodes.append(node) url = cfg.get('maintenance', 'url') payload = json.dumps({'node': node_label}) try: response = requests.post(url, headers={'Content-Type': 'application/json'}, data=payload) except requests.ConnectionError: logger.warn('POST to maintenance service' ' failed: connection failed') if (not 200 <= response < 300): logger.warn('POST to maintenance service' ' failed with response: %s', response.text)
def headnode_create(headnode, project, base_img): """Create headnode. If a headnode with the same name already exists, a DuplicateError will be raised. If the project does not exist, a NotFoundError will be raised. If the base image does not exist (is not specified in hil.cfg) a BadArgumentError will be raised. """ valid_imgs = cfg.get('headnode', 'base_imgs') valid_imgs = [img.strip() for img in valid_imgs.split(',')] if base_img not in valid_imgs: raise errors.BadArgumentError('Provided image is not a valid image.') absent_or_conflict(model.Headnode, headnode) project = get_or_404(model.Project, project) get_auth_backend().require_project_access(project) headnode = model.Headnode(project, headnode, base_img) db.session.add(headnode) db.session.commit()
def headnode_create(headnode, project, base_img): """Create headnode. If a headnode with the same name already exists, a DuplicateError will be raised. If the project does not exist, a NotFoundError will be raised. If the base image does not exist (is not specified in hil.cfg) a BadArgumentError will be raised. """ valid_imgs = cfg.get('headnode', 'base_imgs') valid_imgs = [img.strip() for img in valid_imgs.split(',')] if base_img not in valid_imgs: raise BadArgumentError('Provided image is not a valid image.') _assert_absent(model.Headnode, headnode) project = _must_find(model.Project, project) get_auth_backend().require_project_access(project) headnode = model.Headnode(project, headnode, base_img) db.session.add(headnode) db.session.commit()
def _on_virt_uri(args_list): """Make an argument list to libvirt tools use right URI. This will work for virt-clone and virsh, at least. It gets the appropriate endpoint URI from the config file. """ libvirt_endpoint = cfg.get('headnode', 'libvirt_endpoint') return [args_list[0], '--connect', libvirt_endpoint] + args_list[1:]
def configure(): """Configure HIL, and test which db we're using. Skips the test unless we're using postgres. """ config_testsuite() if not cfg.get('database', 'uri').startswith('postgresql:'): pytest.skip('Database migrations are only supported for postgresql.') init_db()
def object_url(*args): # Prefer an environmental variable for getting the endpoint if available. url = os.environ.get('HIL_ENDPOINT') if url is None: url = cfg.get('client', 'endpoint') for arg in args: url += '/' + urllib.quote(arg, '') return url
def init_db(uri=None): """Start up the DB connection. `uri` is the uri to use for the database. If it is None, the uri from the config file will be used. """ if uri is None: uri = cfg.get('database', 'uri') app.config.update(SQLALCHEMY_DATABASE_URI=uri)
def list_headnode_images(): """Show headnode images listed in config file. Returns a JSON array of strings representing a list of headnode images. Example: '["headnode1.img", "headnode2.img", "headnode3.img"]' """ valid_imgs = cfg.get('headnode', 'base_imgs') valid_imgs = sorted([img.strip() for img in valid_imgs.split(',')]) return json.dumps(valid_imgs)
def get_vlan_list(): """Return a list of vlans in the module's config section. This is for use by the ``create_bridges`` script. """ vlan_str = cfg.get(__name__, 'vlans') returnee = [] for r in vlan_str.split(","): r = r.strip().split("-") if len(r) == 1: returnee.append(int(r[0])) else: returnee += range(int(r[0]), int(r[1])+1) return returnee
def _maintain(project, node, node_label): """Helper function to execute maintenance tasks. Powers off the node, checks for the existence of maintenance pool config options, and posts to the maintenance URL if they exist.""" logger = logging.getLogger(__name__) if (cfg.has_option('maintenance', 'maintenance_project') and cfg.has_option('maintenance', 'url')): maintenance_proj = get_or_404( model.Project, cfg.get('maintenance', 'maintenance_project') ) if (project == maintenance_proj): # Already in maintenance pool return elif (cfg.has_option('maintenance', 'maintenance_project')): raise errors.NotFoundError("Maintenance URL not in hil.cfg.") elif (cfg.has_option('maintenance', 'url')): raise errors.NotFoundError("Maintenance project not in hil.cfg.") else: return if (cfg.has_option('maintenance', 'shutdown')): node.obm.power_off() maintenance_proj.nodes.append(node) url = cfg.get('maintenance', 'url') payload = json.dumps({'node': node_label}) try: response = requests.post(url, headers={'Content-Type': 'application/json'}, data=payload) except requests.ConnectionError: logger.warn('POST to maintenance service' ' failed: connection failed') if (not 200 <= response < 300): logger.warn('POST to maintenance service' ' failed with response: %s', response.text)
def setup(*args, **kwargs): if not cfg.has_section(__name__): logger.error('No section for [%s] in hil.cfg; authentication will ' 'not work without this. Please add this section and try ' 'again.', __name__) sys.exit(1) keystone_cfg = {} for key in cfg.options(__name__): keystone_cfg[key] = cfg.get(__name__, key) # Great job with the API design Openstack! </sarcasm> factory = filter_factory(keystone_cfg) app.wsgi_app = factory(app.wsgi_app) auth.set_auth_backend(KeystoneAuthBackend())
def object_url(*args): """Return a url with a prefix of the HIL endpoint, and args as the (remaining) segments of the path. TODO: This function's name is no longer very accurate. As soon as it is safe, we should change it to something more generic. """ # Prefer an environmental variable for getting the endpoint if available. url = os.environ.get('HIL_ENDPOINT') if url is None: config.setup() url = cfg.get('client', 'endpoint') for arg in args: url += '/' + urllib.quote(arg, '') return url
def setup(*args, **kwargs): if not cfg.has_section(__name__): logger.error( 'No section for [%s] in hil.cfg; authentication will ' 'not work without this. Please add this section and try ' 'again.', __name__) sys.exit(1) keystone_cfg = {} for key in cfg.options(__name__): keystone_cfg[key] = cfg.get(__name__, key) # Great job with the API design Openstack! </sarcasm> factory = filter_factory(keystone_cfg) app.wsgi_app = factory(app.wsgi_app) auth.set_auth_backend(KeystoneAuthBackend())
def configure(): config_testsuite() if not cfg.get('database', 'uri').startswith('postgresql:'): pytest.skip('Database migrations are only supported for postgresql.') init_db()