def get_tags(): body = request.get_json() if not body: return jsonify({ 'message': 'miss required params', 'code': 104000, }), 400 template = body.get('template') listtags = template.get('listtags') listtasks = template.get('listtasks') if not listtags or not listtasks: return jsonify({ 'message': 'invalid params', 'code': 104001, }), 400 payload = load_ansible_playbook(body) if payload.get('message') is not 'ok': return jsonify(payload), 400 data = payload.get('data') options = data.get('options') wk = Workspace() with build_book_from_db(name=data.get('book_name'), roles=data.get('roles')) as bookspace: if not bookspace: return jsonify({ 'message': 'book not found', 'code': 104000, }), 400 entry = wk.get_book_entry(data.get('book_name'), data.get('entry')) play = PlayBookRunner([data['inventory']], options) play.run(entry) return jsonify({ 'message': 'ok', 'code': 0, 'data': { 'tags': list(play.tags), 'tasks': list(play.tasks), } })
def check_job(_id): record = Job.find_one({'_id': ObjectId(_id)}) if not record: return jsonify({'message': 'job not found', 'code': 104001}), 400 body = {'template': record.get('template'), 'extra': record.get('extra')} payload = load_ansible_playbook(body) if payload.get('message') is not 'ok': return jsonify(payload), 400 data = payload.get('data') options = data.get('options') private_key = data.get('private_key') wk = Workspace() res = wk.load_book_from_db(name=data.get('book_name'), roles=data.get('roles')) if not res: return jsonify({ 'message': 'book not found', 'code': 104000, }), 400 entry = wk.get_book_entry(data.get('book_name'), data.get('entry')) with NamedTemporaryFile('w+t', delete=True) as fd: key_text = get_credential_content_by_id(private_key, 'private_key') if not key_text: return jsonify({ 'message': 'invalid private_key', 'code': 104033, }), 401 fd.write(key_text) fd.seek(0) options['private_key'] = fd.name play = PlayBookRunner(data['inventory'], options) play.run(entry) result = play.get_result() return jsonify({'message': 'ok', 'code': 0, 'data': result})
def add_jobs(): """ add job TODO(sang) :return: json """ body = request.get_json() user = login_user if not body: return jsonify({ 'message': 'miss required params', 'code': 104000, }), 400 job_type = body.get('type') if job_type == 'adhoc': return add_adhoc() current_id = body.get('currentId') record = None if current_id: record = Job.find_by_id(current_id) if not record: return jsonify({'message': 'job not found', 'code': 104044}), 400 payload = load_ansible_playbook(body) if payload.get('message') is not 'ok': return jsonify(payload), 400 data = payload.get('data') options = data.get('options') is_check = body.get('check') private_key = data.get('private_key') wk = Workspace() res = wk.load_book_from_db(name=data.get('book_name'), roles=data.get('roles')) if not res: return jsonify({ 'message': 'book not found', 'code': 104000, }), 400 entry = wk.get_book_entry(data.get('book_name'), data.get('entry')) dry_run = bool(is_check) options['check'] = dry_run if dry_run: with NamedTemporaryFile('w+t', delete=True) as fd: if private_key: key_text = get_credential_content_by_id( private_key, 'private_key') if not key_text: return jsonify({ 'message': 'invalid private_key', 'code': 104033, }), 401 fd.write(key_text) fd.seek(0) options['private_key'] = fd.name play = PlayBookRunner(data['inventory'], options, callback=CallbackModule()) play.run(entry) return jsonify({ 'message': 'ok', 'code': 0, 'data': { 'result': play.get_result(), 'options': options } }) name = data.get('name') existed = Job.find_one({'name': name}) if existed and not current_id: return jsonify({ 'message': 'name existed', 'code': 104001, }), 400 token = str(base64.b64encode(bytes(current_request_id(), 'utf8')), 'utf8') new_record = { 'name': name, 'type': 'playbook', 'token': token, 'description': data.get('description'), 'book_id': data.get('book_id'), 'template': data.get('template'), 'extra': data.get('extra'), 'entry': data['entry'], 'status': data['status'], 'maintainer': [user.get('username')], 'created_at': int(time.time()), 'updated_at': datetime.datetime.now().isoformat(), } if record: Job.update_one({'_id': record['_id']}, update={'$set': new_record}) else: Job.insert_one(new_record) return jsonify({ 'message': 'ok', 'code': 0, })
def run(book_id, run_id, **options): perform = Perform.find_one({'run_id': run_id}) book = Book.find_by_id(book_id) start_at = time.time() state = 'progressing' result = '' old_stdout = sys.stdout old_stderr = sys.stderr sys.stderr = sys.stdout = temp_stdout = Reporter(run_id) try: if book.get('repo') == 'git': vcs = GitDownload(book.get('repo_options')) install_path = vcs.install() # @todo book_name = book.get('name') with build_book_from_db(book_name, build_id=run_id) as dest: if not dest: result = 'install book failed' logger.warning(result) state = 'finish' else: inventory = options['inventory'] if type(inventory == str): inventory = os.path.join(dest, inventory) entry = os.path.join(dest, options['entry'].pop()) if type(options['tags']) == str: options['tags'] = options['tags'].split(',') options['basedir'] = dest print('xxxxxxxxxxxxx????', inventory, options) runner = PlayBookRunner(inventory, options) runner.run([entry]) result = runner.get_result() print('result', result) state = 'finish' except Exception as err: result = str(err) extra = {'run_id': run_id} logger.error('run task with exception: {}'.format(result), extra=extra) state = 'error' raise err finally: content = temp_stdout.getvalue() temp_stdout.close(True) sys.stdout = old_stdout sys.stderr = old_stderr print(content) finish_at = time.time() update = { '$set': { 'start_at': start_at, 'finish_at': finish_at, 'state': state, 'duration': finish_at - start_at, 'result': str(result), 'trace': content, } } Perform.update_one({'_id': perform['_id']}, update=update)
def run_playbook_task(_id, request_id, username, history_id, **kwargs): db = Mongo() record = Job.find_by_id(ObjectId(_id)) task_record = TaskModel.find_one({'request_id': request_id}) if not task_record: return False start_at = time() state = 'progressing' result = '' task_id = task_record.get('_id') job_id = task_record.get('job_id') old_stdout = sys.stdout old_stderr = sys.stderr sys.stderr = sys.stdout = temp_stdout = Reporter(str(task_id)) try: if history_id: history = db.collection('build_history').find_one( {'_id': ObjectId(history_id)}) record = history['job_info'] kwargs = task_record.get('kwargs') template = record.get('template') body = { 'template': record.get('template'), 'extra': record.get('extra') } payload = load_ansible_playbook(body) if payload.get('message') is not 'ok': raise Exception('load ansible options error: ' + payload.get('message')) app_id = template.get('app') if app_id: app_info = Application.find_by_id(ObjectId(app_id)) if not app_info: raise Exception('app not found: {}'.format(app_id)) app_type = app_info.get('type') app_params = app_info.get('params') if kwargs: app_params.update(kwargs) integration = Integration(app_type, app_params) integration.install() data = payload.get('data') options = data.get('options') private_key = data.get('private_key') wk = Workspace() roles = data.get('roles') if history_id: bookspace = wk.build_book(history_id) else: bookname = data.get('book_name') bookspace = wk.load_book_from_db(name=bookname, roles=roles, build_id=task_id) if not bookspace or not os.path.isdir(bookspace): raise Exception('install playbook failed, book name: {}'.format( data.get('book_name'))) entry = os.path.join(bookspace, data.get('entry')) with NamedTemporaryFile('w+t', delete=False) as fd: if private_key: key_text = get_credential_content_by_id( private_key, 'private_key') if not key_text: raise Exception('invalid private_key') fd.write(key_text) fd.seek(0) options['private-key'] = fd.name options['tags'] = ['uptime'] options['verbosity'] = 2 inventory = data.get('inventory') logger.info('ansible-playbook run load inventory: \n{}'.format( yaml.safe_dump(inventory))) play = PlayBookRunner(data.get('inventory'), options, job_id=job_id) play.run(entry) result = play.get_result() builds = db.collection('build_history').count({'job_id': _id}) state = 'finish' # @todo if builds > cache_result_numer: last_one = db.collection('build_history').find_one( {'job_id': _id}, sort=[('_id', 1)]) if last_one: db.fs().delete(last_one.get('file_id')) db.collection('build_history').delete_one( {'_id': last_one['_id']}) with TemporaryDirectory() as dir_name: bookname = data.get('book_name') zip_file = os.path.join(dir_name, bookname) zip_file = make_zip(bookspace, zip_file) with open(zip_file, mode='rb') as stream: filename = bookname + '.zip' file_id = db.save_file(filename=filename, fileobj=stream) store_info = { 'task_id': str(task_id), 'file_id': str(file_id), 'job_id': str(_id), 'job_info': record, 'filename': filename, 'created_at': time(), 'kwargs': kwargs, } db.collection('build_history').insert_one(store_info) shutil.rmtree(bookspace) except Exception as e: result = str(e) extra = {'task_id': task_id} logger.error('run task with exception: {}'.format(str(e)), extra=extra) state = 'error' extra_options = record.get('extra') user = User.find_one({'username': username}) if user: user_id = str(user['_id']) notification = extra_options.get('notification') message = '[error]run job: {}, message: {}'.format( record.get('name'), str(e)) sys.stdout.write(message) if notification and type(notification) == list: Notify().dispatch(user_id=user_id, msg_type='task', content=message, channel=notification) finally: content = temp_stdout.getvalue() temp_stdout.close(True) sys.stdout = old_stdout sys.stderr = old_stderr finish_at = time() update = { '$set': { 'start_at': start_at, 'finish_at': finish_at, 'state': state, 'duration': finish_at - start_at, 'result': result, } } TaskModel.update_one({'_id': task_id}, update=update) trace = { 'task_id': str(task_id), 'request_id': request_id, 'username': username, 'content': str(content), 'created_at': time(), } db.collection('task_logs').insert_one(trace)
def run_task(): payload = request.get_json() if not payload: return jsonify({ 'message': 'invalid params', 'code': 114000, }), 400 run_type = payload.get('type') inventory = payload.get('inventory') private_key = payload.get('private_key') module = payload.get('module') args = payload.get('args') entry = payload.get('entry') become_mehtod = payload.get('become_method') become_user = payload.get('become_user') verbosity = payload.get('verbosity', 0) or 1 extra_options = payload.get('extraOptions') hosts = parse_cmdb_inventory(inventory) if not hosts: return jsonify({ 'message': 'invalid inventory', 'code': 114001, }), 400 options = {} if extra_options: options.update(extra_options) if verbosity: options['verbosity'] = verbosity if become_mehtod: options['become'] = 'yes' options['become_method'] = become_mehtod if become_user: options['become_user'] = become_user if run_type == 'adhoc': if not module: return jsonify({ 'message': 'invalid module', 'code': 114002, }), 400 tasks = [{'action': {'module': module, 'args': args}}] with NamedTemporaryFile('w+t', delete=True) as fd: if private_key: key_text = get_credential_content_by_id( private_key, 'private_key') if not key_text: return jsonify({ 'message': 'invalid private_key', 'code': 104033, }), 401 fd.write(key_text) fd.seek(0) options['private_key'] = fd.name runner = AdHocRunner(hosts, options=options) logger.info('run ansible-adhoc', extra={ 'hosts': hosts, 'options': options }) res = runner.run('all', tasks) result = runner.format_result() return jsonify({'message': 'ok', 'code': 0, 'data': result}) else: if not entry: return jsonify({ 'message': 'invalid entry', 'code': 114004, }), 400 with NamedTemporaryFile('w+t', delete=True) as fd: if private_key: key_text = get_credential_content_by_id( private_key, 'private_key') if not key_text: return jsonify({ 'message': 'invalid private_key', 'code': 104033, }), 401 fd.write(key_text) fd.seek(0) options['private_key'] = fd.name with NamedTemporaryFile('w+t', delete=True) as fh: fh.write(entry) fh.seek(0) runner = PlayBookRunner(hosts, options) logger.info('run ansible-playbook', extra={ 'hosts': hosts, 'options': options }) runner.run(fh.name) result = runner.format_result() return jsonify({'message': 'ok', 'code': 0, 'data': result})
def run(run_id, payload): perform = Perform.find_one({'run_id': run_id}) start_at = time.time() state = 'progressing' result = '' old_stdout = sys.stdout old_stderr = sys.stderr sys.stderr = sys.stdout = temp_stdout = Reporter(run_id) try: run_type = payload.get('run_type') inventory = payload.get('inventory') inventory = parse_cmdb_inventory(inventory) private_key = payload.get('private_key') entry = payload.get('entry') options = payload.get('options') tasks = payload.get('tasks') if run_type == 'adhoc': with NamedTemporaryFile('w+t', delete=True) as fd: if private_key: key_text = get_credential_content_by_id( private_key, 'private_key') if not key_text: raise Exception('invalid private_key') fd.write(key_text) fd.seek(0) options['private_key'] = fd.name runner = AdHocRunner(inventory, options=options) runner.run('all', tasks) result = runner.format_result() state = 'finish' else: with NamedTemporaryFile('w+t', delete=True) as fd: if private_key: key_text = get_credential_content_by_id( private_key, 'private_key') if not key_text: raise Exception('invalid private_key') fd.write(key_text) fd.seek(0) options['private_key'] = fd.name with NamedTemporaryFile('w+t', delete=True) as fh: fh.write(entry) fh.seek(0) runner = PlayBookRunner(inventory, options) runner.run(fh.name) result = runner.format_result() state = 'finish' except Exception as err: result = str(err) extra = {'run_id': run_id} logger.error('run task with exception: {}'.format(result), extra=extra) state = 'error' raise err finally: content = temp_stdout.getvalue() temp_stdout.close(True) sys.stdout = old_stdout sys.stderr = old_stderr finish_at = time.time() update = { '$set': { 'start_at': start_at, 'finish_at': finish_at, 'state': state, 'duration': finish_at - start_at, 'result': str(result), 'trace': content, } } Perform.update_one({'_id': perform['_id']}, update=update)