def load_inventory(content, group='all'): # content = yaml.safe_load(content) # if not content: # raise yaml.YAMLError('parse inventory error') content = parser_inventory(content, True) inventory = dict() if group == 'all': inventory['all'] = dict() ungrouped = dict() for key, value in content.items(): if not value.get('hosts'): continue ungrouped.update(value.get('hosts')) inventory['all']['hosts'] = ungrouped return json.dumps(inventory) if not content.get('group'): raise ValueError('inventory group: ' + group + ' not found') inventory[group] = content.get('group') return json.dumps(inventory)
def parse_file_inventory(inventory_params): data = {} if type(inventory_params) != list: inventory_params = [inventory_params] for inventory_str in inventory_params: inventory_list = inventory_str.split('@') if len(inventory_list) is not 3: return False mark_name, _id, group = inventory_list record = Playbook.find_one({'_id': ObjectId(_id)}) if not record: return False node = parser_inventory(record['content'], True) # @todo make same as cmdb # record['content'] = json.dumps(record['content']) # record['limit'] = group data.update(node) return data
def test(): # jks.save_artifacts(config.workspace['tmp'], 'upward', '18') filename = config.workspace.get('tmp') + '/hosts' result = parser_inventory(filename) return jsonify({'message': 'ok', 'code': 0, 'data': result})
def explore(): payload = request.get_json() form = request.form payload = payload or form if not payload: return jsonify({ 'message': 'empty request payload', 'code': 144000, }), 400 explore_type = payload.get('type') credential = payload.get('credential') # if not credential: # return jsonify({ # 'message': 'param credential required', # 'code': 144000, # }), 400 private_key = '' if credential: credential = Credential.find_by_id(credential) if not credential or not credential.get('status'): return jsonify({ 'message': 'invalid credential', 'code': 144040, }), 404 vault = Vault({'vault_pass': config.vault.get('secret')}) body = credential['body'] private_key = vault.decrypt_string(body[credential['type']]) maintainer = payload.get('maintainer') if not maintainer or type(maintainer) != list: maintainer = [login_user.get('username')] else: maintainer.extend(login_user.get('username')) if explore_type == 'manual': region = payload.get('region') group = payload.get('group') ssh_host = payload.get('ssh_host') ssh_user = payload.get('ssh_user') ssh_port = payload.get('ssh_port') or 22 if not ssh_host or not ssh_user or not ssh_port: return jsonify({ 'message': 'illegal ssh connection params', 'code': 144001, }), 400 region_record = Region.find_one({'_id': ObjectId(region)}) if not region_record: return jsonify({ 'message': 'invalid region', 'code': 144002, }), 400 group_record = Group.find_by_ids(group) if not group_record: return jsonify({ 'message': 'invalid group', 'code': 144003, }), 400 options = { 'remote_user': ssh_user, 'verbosity': 3, } hosts = ssh_host + ':' + str(ssh_port) + ',' runner = setup(hosts, options, private_key) result = runner.get_result() data = process_ansible_setup(result) records = [] if not data: return jsonify({ 'code': 144009, 'message': 'fetch target host failed', 'data': result }), 406 for item in data: item['ansible_ssh_host'] = ssh_host item['ansible_ssh_user'] = ssh_user item['ansible_ssh_port'] = ssh_port item['maintainer'] = maintainer item['group'] = list(map(lambda i: str(i['_id']), group_record)) item['add_by'] = login_user.get('username') item['state'] = 'active' where = {'ansible_ssh_host': item['ansible_ssh_host']} update = {'$set': item} result = db.collection('machines').update_one(where, update, upsert=True) extra = item.copy() extra['_id'] = result.upserted_id logger.info('add machines hostname: '.format({item['hostname']}), extra={'record': data}) records.append(item) # def func(item): # item['ansible_ssh_host'] = ssh_host # item['ansible_ssh_user'] = ssh_user # item['ansible_ssh_port'] = ssh_port # item['maintainer'] = maintainer # item['group'] = list(map(lambda i: str(i['_id']), group_record)) # item['add_by'] = login_user.get('username') # item['state'] = 'active' # # return item # # data = list(map(func, data)) # db.collection('machines').insert_many(data) return jsonify({ 'message': 'ok', 'code': 0, 'data': records, 'records': data }) files = request.files if not files: return jsonify({ 'message': 'illegal param', 'code': 104000, }), 400 file = files.get('inventory') if not files: return jsonify({ 'message': 'files required', 'code': 104001, }), 400 sources = file.read().decode('utf-8') # inventory is not only yaml with NamedTemporaryFile('w+t', delete=True) as fd: fd.write(sources) fd.seek(0) options = { 'verbosity': 0, } runner = setup(fd.name, options, private_key) result = runner.get_result() data = process_ansible_setup(result) if not data: return jsonify({ 'code': 144009, 'message': 'fetch target host failed', 'data': result }), 400 manager = runner.inventory hosts = parser_inventory(manager) records = [] default_region = db.collection('regions').find_one({'name': 'default'}) if not default_region: result = db.collection('regions').insert_one({ 'name': 'default', 'add_by': None, 'description': 'auto generate', 'created_at': time.time(), }) region_id = str(result.inserted_id) else: region_id = str(default_region.get('_id')) for node in data: hostname = node.get('ansible_hostname') for group, host in hosts.items(): where = {'name': group} # insert_data = {'$set': insert_data} existed = db.collection('groups').find_one(where) if not existed: insert_data = { 'name': group, 'region': region_id, 'description': 'auto generator', 'state': 'active', 'add_by': login_user.get('username'), 'maintainer': None, 'created_at': int(time.time()) } insert_result = db.collection('groups').insert_one( insert_data) group = insert_result.inserted_id else: group = existed['_id'] if host.get('name') == hostname: vars = host.get('vars') node['ansible_ssh_host'] = vars.get('ansible_ssh_host') node['ansible_ssh_user'] = vars.get( 'ansible_ssh_user', 'root') node['ansible_ssh_port'] = vars.get( 'ansible_ssh_port', '22') node['group'] = [group] node['add_by'] = login_user.get('username') node['state'] = 'active' break records.append(node) for record in records: where = {'ansible_ssh_host': record['ansible_ssh_host']} update = {'$set': record} result = db.collection('machines').update_one(where, update, upsert=True) extra = record.copy() extra['_id'] = result.upserted_id logger.info('add machines hostname: '.format({record['hostname']}), extra={'record': data}) return jsonify({ 'message': 'ok', 'code': 0, 'data': data, 'records': records, })