def check_environment(): auth_url = os.environ.get('OS_AUTH_URL', None) if not auth_url: try: source_env(constants.OPENRC) except IOError as e: if e.errno != errno.EEXIST: raise LOG.debug('OPENRC file not found')
def check_environment(self): try: os.environ['OS_AUTH_URL'] except KeyError: try: source_env(consts.OPENRC) except IOError as e: if e.errno != errno.EEXIST: LOG.error('OPENRC file not found') raise else: LOG.error('OS_AUTH_URL not found')
def update_openrc(self, args): try: openrc_vars = args['openrc'] except KeyError: return result_handler(consts.API_ERROR, 'openrc must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') LOG.info('writing openrc: %s', consts.OPENRC) makedirs(consts.CONF_DIR) lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()] LOG.debug('writing: %s', ''.join(lines)) with open(consts.OPENRC, 'w') as f: f.writelines(lines) LOG.info('writing openrc: Done') LOG.info('source openrc: %s', consts.OPENRC) try: source_env(consts.OPENRC) except Exception: LOG.exception('source openrc failed') return result_handler(consts.API_ERROR, 'source openrc failed') LOG.info('source openrc: Done') openrc_id = str(uuid.uuid4()) self._write_into_database(environment_id, openrc_id, openrc_vars) LOG.info('writing ansible cloud conf') try: self._generate_ansible_conf_file(openrc_vars) except Exception: LOG.exception('write cloud conf failed') return result_handler(consts.API_ERROR, 'genarate ansible conf failed') LOG.info('finish writing ansible cloud conf') return result_handler(consts.API_SUCCESS, { 'openrc': openrc_vars, 'uuid': openrc_id })
def _load_image(self, image_name, image_path): LOG.info('source openrc') source_env(consts.OPENRC) LOG.info('load image') glance_client = get_glance_client() image = glance_client.images.create(name=image_name, visibility='public', disk_format='qcow2', container_format='bare') with open(image_path, 'rb') as f: glance_client.images.upload(image.id, f) LOG.info('Done')
def get(self): try: source_env(consts.OPENRC) except OSError: return result_handler(consts.API_ERROR, 'source openrc error') image_list = openstack_utils.list_images() if image_list is False: return result_handler(consts.API_ERROR, 'get images error') images = {i.name: format_image_info(i) for i in image_list} return result_handler(consts.API_SUCCESS, { 'status': 1, 'images': images })
def upload_openrc(self, args): try: upload_file = args['file'] except KeyError: return result_handler(consts.API_ERROR, 'file must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') LOG.info('writing openrc: %s', consts.OPENRC) makedirs(consts.CONF_DIR) upload_file.save(consts.OPENRC) source_env(consts.OPENRC) LOG.info('parsing openrc') try: openrc_data = self._get_openrc_dict() except Exception: LOG.exception('parse openrc failed') return result_handler(consts.API_ERROR, 'parse openrc failed') openrc_id = str(uuid.uuid4()) self._write_into_database(environment_id, openrc_id, openrc_data) LOG.info('writing ansible cloud conf') try: self._generate_ansible_conf_file(openrc_data) except Exception: LOG.exception('write cloud conf failed') return result_handler(consts.API_ERROR, 'genarate ansible conf failed') LOG.info('finish writing ansible cloud conf') return result_handler(consts.API_SUCCESS, { 'openrc': openrc_data, 'uuid': openrc_id })
def update_openrc(self, args): try: openrc_vars = args['openrc'] except KeyError: return result_handler(consts.API_ERROR, 'openrc must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') LOG.info('writing openrc: %s', consts.OPENRC) makedirs(consts.CONF_DIR) lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()] LOG.debug('writing: %s', ''.join(lines)) with open(consts.OPENRC, 'w') as f: f.writelines(lines) LOG.info('writing openrc: Done') LOG.info('source openrc: %s', consts.OPENRC) try: source_env(consts.OPENRC) except Exception: LOG.exception('source openrc failed') return result_handler(consts.API_ERROR, 'source openrc failed') LOG.info('source openrc: Done') openrc_id = str(uuid.uuid4()) self._write_into_database(environment_id, openrc_id, openrc_vars) LOG.info('writing ansible cloud conf') try: self._generate_ansible_conf_file(openrc_vars) except Exception: LOG.exception('write cloud conf failed') return result_handler(consts.API_ERROR, 'genarate ansible conf failed') LOG.info('finish writing ansible cloud conf') return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars, 'uuid': openrc_id})
def _load_images(self): LOG.info('source openrc') source_env(consts.OPENRC) LOG.info('clean images') cmd = [consts.CLEAN_IMAGES_SCRIPT] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR) _, err = p.communicate() if p.returncode != 0: LOG.error('clean image failed: %s', err) LOG.info('load images') cmd = [consts.LOAD_IMAGES_SCRIPT] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR) _, err = p.communicate() if p.returncode != 0: LOG.error('load image failed: %s', err) LOG.info('Done')
def get(self): try: source_env(consts.OPENRC) except Exception: return result_handler(consts.API_ERROR, 'source openrc error') nova_client = get_nova_client() try: images_list = nova_client.images.list() except Exception: return result_handler(consts.API_ERROR, 'get images error') else: images = { i.name: self.get_info(change_obj_to_dict(i)) for i in images_list } return result_handler(consts.API_SUCCESS, { 'status': 1, 'images': images })
def upload_openrc(self, args): try: upload_file = args['file'] except KeyError: return result_handler(consts.API_ERROR, 'file must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') LOG.info('writing openrc: %s', consts.OPENRC) makedirs(consts.CONF_DIR) upload_file.save(consts.OPENRC) source_env(consts.OPENRC) LOG.info('parsing openrc') try: openrc_data = self._get_openrc_dict() except Exception: LOG.exception('parse openrc failed') return result_handler(consts.API_ERROR, 'parse openrc failed') openrc_id = str(uuid.uuid4()) self._write_into_database(environment_id, openrc_id, openrc_data) LOG.info('writing ansible cloud conf') try: self._generate_ansible_conf_file(openrc_data) except Exception: LOG.exception('write cloud conf failed') return result_handler(consts.API_ERROR, 'genarate ansible conf failed') LOG.info('finish writing ansible cloud conf') return result_handler(consts.API_SUCCESS, {'openrc': openrc_data, 'uuid': openrc_id})
def test_source_env(self, mock_popen): base_env = deepcopy(os.environ) mock_process = mock_popen() output_list = [ 'garbage line before', 'NEW_ENV_VALUE=234', 'garbage line after', ] mock_process.communicate.return_value = os.linesep.join(output_list), '', 0 expected = {'NEW_ENV_VALUE': '234'} result = utils.source_env('my_file') self.assertDictEqual(result, expected) os.environ.clear() os.environ.update(base_env)
def get(self): try: source_env(consts.OPENRC) except: return result_handler(consts.API_ERROR, 'source openrc error') nova_client = get_nova_client() try: images_list = nova_client.images.list() except: return result_handler(consts.API_ERROR, 'get images error') else: images = [ self.get_info(change_obj_to_dict(i)) for i in images_list ] status = 1 if all(i['status'] == 'ACTIVE' for i in images) else 0 if not images: status = 0 return result_handler(consts.API_SUCCESS, { 'status': status, 'images': images })
def _source_file(self, rc_file): utils.source_env(rc_file)
def _source_file(self, rc_file): utils.source_env(rc_file)
def _source_file(rc_file): yardstick_utils.source_env(rc_file)
def _source_file(rc_file): common_utils.source_env(rc_file)