7 from oslo_serialization import jsonutils
9 from api import ApiResource
10 from api.database.v2.handlers import V2OpenrcHandler
11 from api.database.v2.handlers import V2EnvironmentHandler
12 from yardstick.common import constants as consts
13 from yardstick.common.utils import result_handler
14 from yardstick.common.utils import makedirs
15 from yardstick.common.utils import source_env
17 LOG = logging.getLogger(__name__)
18 LOG.setLevel(logging.DEBUG)
21 class V2Openrc(ApiResource):
24 return self._dispatch_post()
26 def upload_openrc(self, args):
28 upload_file = args['file']
30 return result_handler(consts.API_ERROR, 'file must be provided')
33 environment_id = args['environment_id']
35 return result_handler(consts.API_ERROR, 'environment_id must be provided')
37 LOG.info('writing openrc: %s', consts.OPENRC)
38 makedirs(consts.CONF_DIR)
39 upload_file.save(consts.OPENRC)
40 source_env(consts.OPENRC)
42 LOG.info('parsing openrc')
44 openrc_data = self._get_openrc_dict()
46 LOG.exception('parse openrc failed')
47 return result_handler(consts.API_ERROR, 'parse openrc failed')
49 openrc_id = str(uuid.uuid4())
50 self._write_into_database(environment_id, openrc_id, openrc_data)
52 LOG.info('writing ansible cloud conf')
54 self._generate_ansible_conf_file(openrc_data)
56 LOG.exception('write cloud conf failed')
57 return result_handler(consts.API_ERROR, 'genarate ansible conf failed')
58 LOG.info('finish writing ansible cloud conf')
60 return result_handler(consts.API_SUCCESS, {'openrc': openrc_data, 'uuid': openrc_id})
62 def update_openrc(self, args):
64 openrc_vars = args['openrc']
66 return result_handler(consts.API_ERROR, 'openrc must be provided')
69 environment_id = args['environment_id']
71 return result_handler(consts.API_ERROR, 'environment_id must be provided')
73 LOG.info('writing openrc: %s', consts.OPENRC)
74 makedirs(consts.CONF_DIR)
76 lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
77 LOG.debug('writing: %s', ''.join(lines))
78 with open(consts.OPENRC, 'w') as f:
80 LOG.info('writing openrc: Done')
82 LOG.info('source openrc: %s', consts.OPENRC)
84 source_env(consts.OPENRC)
86 LOG.exception('source openrc failed')
87 return result_handler(consts.API_ERROR, 'source openrc failed')
88 LOG.info('source openrc: Done')
90 openrc_id = str(uuid.uuid4())
91 self._write_into_database(environment_id, openrc_id, openrc_vars)
93 LOG.info('writing ansible cloud conf')
95 self._generate_ansible_conf_file(openrc_vars)
97 LOG.exception('write cloud conf failed')
98 return result_handler(consts.API_ERROR, 'genarate ansible conf failed')
99 LOG.info('finish writing ansible cloud conf')
101 return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars, 'uuid': openrc_id})
103 def _write_into_database(self, environment_id, openrc_id, openrc_data):
104 LOG.info('writing openrc to database')
105 openrc_handler = V2OpenrcHandler()
108 'environment_id': environment_id,
109 'content': jsonutils.dumps(openrc_data)
111 openrc_handler.insert(openrc_init_data)
113 LOG.info('binding openrc to environment: %s', environment_id)
114 environment_handler = V2EnvironmentHandler()
115 environment_handler.update_attr(environment_id, {'openrc_id': openrc_id})
117 def _get_openrc_dict(self):
118 with open(consts.OPENRC) as f:
119 content = f.readlines()
123 m = re.search(r'(\ .*)=(.*)', line)
126 value = os.environ[m.group(1).strip()]
130 result.update({m.group(1).strip(): value})
134 def _generate_ansible_conf_file(self, openrc_data):
143 black_list = ['OS_IDENTITY_API_VERSION', 'OS_IMAGE_API_VERSION']
145 for k, v in openrc_data.items():
146 if k.startswith('OS') and k not in black_list:
148 ansible_conf['clouds']['opnfv']['auth'][key] = v
151 value = openrc_data['OS_IDENTITY_API_VERSION']
155 ansible_conf['clouds']['opnfv']['identity_api_version'] = value
157 makedirs(consts.OPENSTACK_CONF_DIR)
158 with open(consts.CLOUDS_CONF, 'w') as f:
159 yaml.dump(ansible_conf, f, default_flow_style=False)