def push_system_user_period(): from ops.utils import update_or_create_ansible_task clusters = Cluster.objects.all() for cluster in clusters: tasks = [] system_users = [ system_user for system_user in cluster.systemuser_set.all() if system_user.auto_push ] if not system_users: return for system_user in system_users: tasks.extend(get_push_system_user_tasks(system_user)) task_name = _("Push cluster system users to assets period: {}").format( cluster.name) hosts = [asset.hostname for asset in cluster.assets.all()] update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=60 * 60 * 24, is_periodic=True, )
def update_assets_hardware_info_period(): """ Update asset hardware period task :return: """ from ops.utils import update_or_create_ansible_task task_name = _("Update assets hardware info period") hostname_list = [ asset.hostname for asset in Asset.objects.all() if asset.is_active and asset.is_unixlike() ] tasks = const.UPDATE_ASSETS_HARDWARE_TASKS # Only create, schedule by celery beat update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=60 * 60 * 24, is_periodic=True, callback=set_assets_hardware_info.name, )
def push_system_user_util(system_user, assets, task_name): from ops.utils import update_or_create_ansible_task if not system_user.is_need_push(): msg = _("Push system user task skip, auto push not enable or " "protocol is not ssh or rdp: {}").format(system_user.name) logger.info(msg) return {} # Set root as system user is dangerous if system_user.username.lower() in ["root", "administrator"]: msg = _("For security, do not push user {}".format(system_user.username)) logger.info(msg) return {} hosts = clean_hosts(assets) if not hosts: return {} hosts = clean_hosts_by_protocol(system_user, hosts) if not hosts: return {} for host in hosts: system_user.load_specific_asset_auth(host) tasks = get_push_system_user_tasks(host, system_user) if not tasks: continue task, created = update_or_create_ansible_task( task_name=task_name, hosts=[host], tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=system_user.org_id, ) task.run()
def test_asset_connectivity_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Test assets connectivity") hosts = clean_hosts(assets) if not hosts: return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS created_by = assets[0].org_id task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=created_by, ) result = task.run() summary = result[1] for asset in assets: if asset.hostname in summary.get('dark', {}): asset.connectivity = asset.UNREACHABLE elif asset.hostname in summary.get('contacted', []): asset.connectivity = asset.REACHABLE else: asset.connectivity = asset.UNKNOWN return summary
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Update some assets hardware info") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hostname_list = [asset.hostname for asset in assets] task, created = update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() # Todo: may be somewhere using # Manual run callback function assets_updated = set_assets_hardware_info(result) return result
def push_file_util(asset, dest_path, task_name, file_path): from ops.utils import update_or_create_ansible_task logger.info("start push {} to {}:{}".format(file_path, asset.ip, dest_path)) hosts = [asset.fullname] tasks = const.PUSH_FILE_TASK tasks[0]['action']['args'] = "src={0} dest={1}".format( file_path, dest_path) task, create = update_or_create_ansible_task(task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System') result = task.run() if result[0]['ok']: logger.info("push {} to {}:{} successful".format( file_path, asset.ip, dest_path)) else: print(asset.ip + 'failed') logger.error("push {} to {}:{} failed".format(file_path, asset.ip, dest_path)) return True
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Update some assets hardware info") # task_name = _("更新资产硬件信息") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hostname_list = [asset.fullname for asset in assets if asset.is_active and asset.is_unixlike()] if not hostname_list: logger.info("Not hosts get, may be asset is not active or not unixlike platform") return {} task, created = update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() # Todo: may be somewhere using # Manual run callback function set_assets_hardware_info(result) return result
def test_system_user_connectability_util(system_user, task_name): """ Test system cant connect his assets or not. :param system_user: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task assets = system_user.get_clusters_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_SYSTEM_USER_CONN_TASKS if not hosts: logger.info("No hosts, passed") return {} task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as=system_user.name, created_by="System", ) result = task.run() set_system_user_connectablity_info(result, system_user=system_user.name) return result
def update_assets_hardware_info_period(): """ Update asset hardware period task :return: """ from ops.utils import update_or_create_ansible_task task_name = _("Update assets hardware info period") hostname_list = [asset.hostname for asset in Asset.objects.all()] tasks = const.UPDATE_ASSETS_HARDWARE_TASKS # Only create, schedule by celery beat update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=60*60*24, is_periodic=True, callback=set_assets_hardware_info.name, )
def backup_asset_app_file_util(asset, task_name, app_name): from ops.utils import update_or_create_ansible_task version = get_last_version(app_name) if not version: logger.info("no version history found {0}".format(app_name)) return False hosts = [asset.fullname] tasks = const.BACKUP_FILE tasks[0]['action']['args'] = "{0} {1} {2}".format(BACKUP_SCRIPT_DIR, app_name, version) task, create = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System' ) result = task.run() if result[1]['dark']: logger.info('{0} Backup Failed! {1}'.format(app_name, result[1]['dark'])) return result[1]['dark'] if not save_backup_path(app_name, version): return False logger.info('{0} backup complete.'.format(version)) return result
def test_asset_connectability_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Test assets connectability") # task_name = _("测试资产可连接性") hosts = [] for asset in assets: if not asset.is_active: msg = _("Asset has been disabled, skip: {}").format(asset) logger.info(msg) continue if not asset.support_ansible(): msg = _("Asset may not be support ansible, skip: {}").format(asset) logger.info(msg) continue hosts.append(asset) if not hosts: logger.info(_("No assets, task stop")) return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS created_by = assets[0].org_id task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=created_by, ) result = task.run() summary = result[1] for k in summary.get('dark'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 0, CACHE_MAX_TIME) for k in summary.get('contacted'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 1, CACHE_MAX_TIME) return summary
def modify_asset_root_password_util(asset, password, task_name): from ops.utils import update_or_create_ansible_task hosts = [asset.fullname] tasks = [{ "name": "Reset asset deploy password", "action": { "module": "shell", "args": "sudo echo deploy:{} | sudo chpasswd".format(password), } }] TASK_OPTIONS = { 'timeout': 10, 'forks': 10, } task, create = update_or_create_ansible_task(task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=TASK_OPTIONS, run_as_admin=True, created_by='System') print(tasks) return task.run()
def gather_asset_users(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Gather assets users") assets = clean_hosts(assets) if not assets: return hosts_category = { 'linux': { 'hosts': [], 'tasks': const.GATHER_ASSET_USERS_TASKS }, 'windows': { 'hosts': [], 'tasks': const.GATHER_ASSET_USERS_TASKS_WINDOWS } } for asset in assets: hosts_list = hosts_category['windows']['hosts'] if asset.is_windows() \ else hosts_category['linux']['hosts'] hosts_list.append(asset) results = {'linux': defaultdict(dict), 'windows': defaultdict(dict)} for k, value in hosts_category.items(): if not value['hosts']: continue _task_name = '{}: {}'.format(task_name, k) task, created = update_or_create_ansible_task( task_name=_task_name, hosts=value['hosts'], tasks=value['tasks'], pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=value['hosts'][0].org_id, ) raw, summary = task.run() results[k].update(raw['ok']) add_asset_users(assets, results)
def test_system_user_connectivity_util(system_user, assets, task_name): """ Test system cant connect his assets or not. :param system_user: :param assets: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task tasks = const.TEST_SYSTEM_USER_CONN_TASKS hosts = clean_hosts(assets) if not hosts: return {} task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as=system_user, created_by=system_user.org_id, ) result = task.run() set_system_user_connectivity_info(system_user, result) return result
def test_asset_connectability_util(asset, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Test asset connectability") hosts = [asset.hostname] if not hosts: logger.info("No hosts, passed") return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() summary = result[1] if summary.get('dark'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 0, CACHE_MAX_TIME) else: cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 1, CACHE_MAX_TIME) return summary
def push_build_file_to_asset_util(asset, task_name, app_name): from ops.utils import update_or_create_ansible_task hosts = [asset.fullname] tasks = const.COPY_FILE_TO_TASK tasks[0]['action']['args'] = "creates=/data/{0} {1} {2}".format(app_name, CREATE_PROJECT_SCRIPT_DIR, app_name) tasks[1]['action']['args'] = "src={0} dest={1}".format( get_deploy_file_path(app_name), get_deploy_file_path(app_name) ) tasks[2]['action']['args'] = "path={0} state=absent".format(get_remote_data_path(app_name)) tasks[3]['action']['args'] = "{0} {1} {2}".format(COMPRESS_SCRIPT_DIR, app_name, get_version(app_name)) tasks[4]['action']['args'] = "src={0} state=link path={1}".format( get_deploy_jar_path(app_name), get_remote_data_path(app_name) ) tasks[5]['action']['args'] = "{0} {1}".format(CHOWN_SCRIPT_DIR, app_name) tasks[6]['action']['args'] = "/etc/init.d/APP {0} {1}".format('stop', app_name) tasks[7]['action']['args'] = "/etc/init.d/APP {0} {1}".format('start', app_name) task, create = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System' ) result = task.run() return result
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Update some assets hardware info") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hosts = [] for asset in assets: if not asset.is_active: msg = _("Asset has been disabled, skipped: {}").format(asset) logger.info(msg) continue if not asset.support_ansible(): msg = _("Asset may not be support ansible, skipped: {}").format(asset) logger.info(msg) continue hosts.append(asset) if not hosts: logger.info(_("No assets matched, stop task")) return {} created_by = str(assets[0].org_id) task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, created_by=created_by, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, ) result = task.run() # Todo: may be somewhere using # Manual run callback function set_assets_hardware_info(assets, result) return result
def test_system_user_connectability_util(system_user, assets, task_name): """ Test system cant connect his assets or not. :param system_user: :param assets: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task hosts = [] tasks = const.TEST_SYSTEM_USER_CONN_TASKS for asset in assets: if not asset.is_active: msg = _("Asset has been disabled, skip: {}").format(asset) logger.info(msg) continue if not asset.support_ansible(): msg = _("Asset may not be support ansible, skip: {}").format(asset) logger.info(msg) continue hosts.append(asset) if not hosts: logger.info(_("No assets matched, stop task")) return {} task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as=system_user, created_by=system_user.org_id, ) result = task.run() set_system_user_connectablity_info(result, system_user=system_user) return result
def test_asset_connectivity_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Test assets connectivity") hosts = clean_hosts(assets) if not hosts: return {} hosts_category = { 'linux': { 'hosts': [], 'tasks': const.TEST_ADMIN_USER_CONN_TASKS }, 'windows': { 'hosts': [], 'tasks': const.TEST_WINDOWS_ADMIN_USER_CONN_TASKS } } for host in hosts: hosts_list = hosts_category['windows']['hosts'] if host.is_windows() \ else hosts_category['linux']['hosts'] hosts_list.append(host) results_summary = dict(contacted=defaultdict(dict), dark=defaultdict(dict), success=True) created_by = assets[0].org_id for k, value in hosts_category.items(): if not value['hosts']: continue task, created = update_or_create_ansible_task( task_name=task_name, hosts=value['hosts'], tasks=value['tasks'], pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=created_by, ) result = task.run() summary = result[1] success = summary.get('success', False) contacted = summary.get('contacted', {}) dark = summary.get('dark', {}) results_summary['success'] &= success results_summary['contacted'].update(contacted) results_summary['dark'].update(dark) for asset in assets: if asset.hostname in results_summary.get('dark', {}): asset.connectivity = asset.UNREACHABLE elif asset.hostname in results_summary.get('contacted', []): asset.connectivity = asset.REACHABLE else: asset.connectivity = asset.UNKNOWN return results_summary
def test_system_user_connectivity_util(system_user, assets, task_name): """ Test system cant connect his assets or not. :param system_user: :param assets: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task hosts = clean_hosts(assets) if not hosts: return {} hosts = clean_hosts_by_protocol(system_user, hosts) if not hosts: return {} hosts_category = { 'linux': { 'hosts': [], 'tasks': const.TEST_SYSTEM_USER_CONN_TASKS }, 'windows': { 'hosts': [], 'tasks': const.TEST_WINDOWS_SYSTEM_USER_CONN_TASKS } } for host in hosts: hosts_list = hosts_category['windows']['hosts'] if host.is_windows() \ else hosts_category['linux']['hosts'] hosts_list.append(host) results_summary = dict(contacted=defaultdict(dict), dark=defaultdict(dict), success=True) for k, value in hosts_category.items(): if not value['hosts']: continue task, created = update_or_create_ansible_task( task_name=task_name, hosts=value['hosts'], tasks=value['tasks'], pattern='all', options=const.TASK_OPTIONS, run_as=system_user.username, created_by=system_user.org_id, ) result = task.run() summary = result[1] success = summary.get('success', False) contacted = summary.get('contacted', {}) dark = summary.get('dark', {}) results_summary['success'] &= success results_summary['contacted'].update(contacted) results_summary['dark'].update(dark) set_system_user_connectivity_info(system_user, results_summary) return results_summary
def test_asset_user_connectivity_util(asset_user, task_name, run_as_admin=False): """ :param asset_user: <AuthBook>对象 :param task_name: :param run_as_admin: :return: """ from ops.utils import update_or_create_ansible_task if not check_asset_can_run_ansible(asset_user.asset): return tasks = get_test_asset_user_connectivity_tasks(asset_user.asset) if not tasks: logger.debug("No tasks ") return args = (task_name, ) kwargs = { 'hosts': [asset_user.asset], 'tasks': tasks, 'pattern': 'all', 'options': const.TASK_OPTIONS, 'created_by': asset_user.org_id, } if run_as_admin: kwargs["run_as_admin"] = True else: kwargs["run_as"] = asset_user.username task, created = update_or_create_ansible_task(*args, **kwargs) raw, summary = task.run() asset_user.set_connectivity(summary)
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Update some assets hardware info") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hosts = clean_hosts(assets) if not hosts: return {} created_by = str(assets[0].org_id) task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, created_by=created_by, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, ) result = task.run() set_assets_hardware_info(assets, result) return result
def test_admin_user_connectability_util(admin_user, task_name): """ Test asset admin user can connect or not. Using ansible api do that :param admin_user: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task assets = admin_user.get_related_assets() hosts = [ asset.hostname for asset in assets if asset.is_active and asset.is_unixlike() ] if not hosts: return tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() set_admin_user_connectability_info(result, admin_user=admin_user.name) return result
def test_system_user_connectability_period(): from ops.utils import update_or_create_ansible_task system_users = SystemUser.objects.all() for system_user in system_users: task_name = _("Test system user connectability period: {}").format( system_user.name ) assets = system_user.get_clusters_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_SYSTEM_USER_CONN_TASKS update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=False, run_as=system_user.name, created_by='System', interval=3600, is_periodic=True, callback=set_admin_user_connectability_info.name, )
def push_system_user_util(system_users, assets, task_name): from ops.utils import update_or_create_ansible_task tasks = [] for system_user in system_users: if not system_user.is_need_push(): msg = "push system user `{}` passed, may be not auto push or ssh " \ "protocol is not ssh".format(system_user.name) logger.info(msg) continue tasks.extend(get_push_system_user_tasks(system_user)) if not tasks: logger.info("Not tasks, passed") return {} hosts = [ asset.hostname for asset in assets if asset.is_active and asset.is_unixlike() ] if not hosts: logger.info("Not hosts, passed") return {} task, created = update_or_create_ansible_task(task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System') return task.run()
def test_system_user_connectability_period(): from ops.utils import update_or_create_ansible_task system_users = SystemUser.objects.all() for system_user in system_users: task_name = _("Test system user connectability period: {}").format( system_user.name ) assets = system_user.get_clusters_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_SYSTEM_USER_CONN_TASKS update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=False, run_as=system_user.name, created_by='System', interval=3600, is_periodic=True, callback=set_admin_user_connectability_info.name, )
def test_admin_user_connectability_util(admin_user, task_name): """ Test asset admin user can connect or not. Using ansible api do that :param admin_user: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task assets = admin_user.get_related_assets() hosts = [] for asset in assets: if not asset.is_active: msg = _("Asset has been disabled, skipped: {}").format(asset) logger.info(msg) continue if not asset.support_ansible(): msg = _("Asset may not be support ansible, skipped: {}").format(asset) logger.info(msg) continue hosts.append(asset) if not hosts: logger.info(_("No assets matched, stop task")) return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=admin_user.org_id, ) result = task.run() set_admin_user_connectability_info(result, admin_user=admin_user.name) return result
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: # task_name = _("Update some assets hardware info") task_name = _("更新资产硬件信息") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hostname_list = [ asset.fullname for asset in assets if asset.is_active and asset.is_unixlike() ] if not hostname_list: logger.info( "Not hosts get, may be asset is not active or not unixlike platform" ) return {} task, created = update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() # Todo: may be somewhere using # Manual run callback function set_assets_hardware_info(result) return result
def rollback_asset_app_version_util(asset, task_name, app_name, version): from ops.utils import update_or_create_ansible_task hosts = [asset.fullname] tasks = const.ROLLBACK_TASK # unpack tasks[0]['action']['args'] = "{0} {1} {2} {3}".format( UNPACK_SCRIPT_DIR, get_backup_path(app_name, version), get_backup_directory(app_name, version), app_name ) # remove link tasks[1]['action']['args'] = "path={0} state=absent".format(get_remote_data_path(app_name)) # create new link tasks[2]['action']['args'] = "src={0} state=link path={1}".format( os.path.join(get_version_path(app_name, version), app_name+'.jar'), get_remote_data_path(app_name) ) task, create = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System' ) result = task.run() deploy_file_path = get_version_path(app_name, version) if result[0]['ok']: update_deploy_info(app_name, deploy_file_path) # logger.info(result[0]['ok']) return result
def push_system_user_util(system_user, assets, task_name): from ops.utils import update_or_create_ansible_task if not system_user.is_need_push(): msg = _("Push system user task skip, auto push not enable or " "protocol is not ssh: {}").format(system_user.name) logger.info(msg) return tasks = get_push_system_user_tasks(system_user) hosts = [] for asset in assets: if not asset.is_active: msg = _("Asset has been disabled, skip: {}").format(asset) logger.info(msg) continue if not asset.support_ansible(): msg = _("Asset may not be support ansible, skip: {}").format(asset) logger.info(msg) continue hosts.append(asset) if not hosts: logger.info(_("No assets matched, stop task")) return {} task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=system_user.org_id, ) return task.run()
def rollback_check_backup_file_exist_util(asset, task_name, app_name, version): from ops.utils import update_or_create_ansible_task simple_result = None backup_path = get_backup_path(app_name, version) if not backup_path: return False tasks = const.CHECK_FILE_TASK hosts = [asset.fullname] tasks[0]['action']['args'] = "if [ -f '{0}' ]; then echo 'exist'; else echo 'not'; fi".format(backup_path) print(tasks) task, create = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System' ) result = task.run() if result[1]['dark']: logger.error(result[1]['dark']) return False if result[0]['ok']: simple_result = result[0]['ok'][asset.fullname]['CHECK_FILE_EXIST']['stdout'] logger.info(simple_result) return simple_result
def test_asset_connectability_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: # task_name = _("Test assets connectability") task_name = _("测试资产可连接性") hosts = [ asset.fullname for asset in assets if asset.is_active and asset.is_unixlike() ] if not hosts: logger.info("No hosts, passed") return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() summary = result[1] for k in summary.get('dark'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 0, CACHE_MAX_TIME) for k in summary.get('contacted'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 1, CACHE_MAX_TIME) return summary
def test_admin_user_connectability_period(): """ A period task that update the ansible task period """ from ops.utils import update_or_create_ansible_task admin_users = AdminUser.objects.all() for admin_user in admin_users: task_name = _("Test admin user connectability period: {}").format(admin_user) assets = admin_user.get_related_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_ADMIN_USER_CONN_TASKS update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=3600, is_periodic=True, callback=set_admin_user_connectability_info.name, )
def test_admin_user_connectability_period(): """ A period task that update the ansible task period """ from ops.utils import update_or_create_ansible_task admin_users = AdminUser.objects.all() for admin_user in admin_users: task_name = _("Test admin user connectability period: {}").format(admin_user) assets = admin_user.get_related_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_ADMIN_USER_CONN_TASKS update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=3600, is_periodic=True, callback=set_admin_user_connectability_info.name, )
def push_system_user_period(): from ops.utils import update_or_create_ansible_task clusters = Cluster.objects.all() for cluster in clusters: tasks = [] system_users = [system_user for system_user in cluster.systemuser_set.all() if system_user.auto_push] if not system_users: return for system_user in system_users: tasks.extend(get_push_system_user_tasks(system_user)) task_name = _("Push cluster system users to assets period: {}").format( cluster.name ) hosts = [asset.hostname for asset in cluster.assets.all()] update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=60*60*24, is_periodic=True, )
def update_assets_hardware_info_period(): """ Update asset hardware period task :return: """ if PERIOD_TASK != "on": logger.debug("Period task disabled, update assets hardware info pass") return from ops.utils import update_or_create_ansible_task # task_name = _("Update assets hardware info period") task_name = _("定期更新资产硬件信息") hostname_list = [ asset.hostname for asset in Asset.objects.all() if asset.is_active and asset.is_unixlike() ] tasks = const.UPDATE_ASSETS_HARDWARE_TASKS # Only create, schedule by celery beat update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', interval=60*60*24, is_periodic=True, callback=set_assets_hardware_info.name, )
def test_asset_user_connectivity_util(asset_user, task_name): """ :param asset_user: <AuthBook>对象 :param task_name: :return: """ from ops.utils import update_or_create_ansible_task tasks = const.TEST_ASSET_USER_CONN_TASKS if not check_asset_can_run_ansible(asset_user.asset): return task, created = update_or_create_ansible_task( task_name, hosts=[asset_user.asset], tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as=asset_user.username, created_by=asset_user.org_id ) result = task.run() set_asset_user_connectivity_info(asset_user, result)
def test_admin_user_connectability_util(admin_user, task_name): """ Test asset admin user can connect or not. Using ansible api do that :param admin_user: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task assets = admin_user.get_related_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() set_admin_user_connectability_info(result, admin_user=admin_user.name) return result
def push_system_user_util(system_users, assets, task_name): from ops.utils import update_or_create_ansible_task tasks = [] for system_user in system_users: tasks.extend(get_push_system_user_tasks(system_user)) if not tasks: logger.info("Not tasks, passed") return {} hosts = [asset.hostname for asset in assets] if not hosts: logger.info("Not hosts, passed") return {} task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System' ) return task.run()
def push_system_user_util(system_user, assets, task_name): from ops.utils import update_or_create_ansible_task if not system_user.is_need_push(): msg = _("Push system user task skip, auto push not enable or " "protocol is not ssh: {}").format(system_user.name) logger.info(msg) return hosts = clean_hosts(assets) if not hosts: return {} for host in hosts: system_user.load_specific_asset_auth(host) tasks = get_push_system_user_tasks(system_user) task, created = update_or_create_ansible_task( task_name=task_name, hosts=[host], tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=system_user.org_id, ) task.run()
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Update some assets hardware info") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hostname_list = [asset.hostname for asset in assets] task, created = update_or_create_ansible_task( task_name, hosts=hostname_list, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() # Todo: may be somewhere using # Manual run callback function assets_updated = set_assets_hardware_info(result) return result
def test_system_user_connectivity_util(system_user, assets, task_name): """ Test system cant connect his assets or not. :param system_user: :param assets: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task tasks = const.TEST_SYSTEM_USER_CONN_TASKS hosts = clean_hosts(assets) if not hosts: return {} task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as=system_user.username, created_by=system_user.org_id, ) result = task.run() set_system_user_connectivity_info(system_user, result) return result
def test_system_user_connectability_util(system_user, task_name): """ Test system cant connect his assets or not. :param system_user: :param task_name: :return: """ from ops.utils import update_or_create_ansible_task assets = system_user.get_clusters_assets() hosts = [asset.hostname for asset in assets] tasks = const.TEST_SYSTEM_USER_CONN_TASKS if not hosts: logger.info("No hosts, passed") return {} task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as=system_user.name, created_by="System", ) result = task.run() set_system_user_connectablity_info(result, system_user=system_user.name) return result
def update_assets_hardware_info_util(assets, task_name=None): """ Using ansible api to update asset hardware info :param assets: asset seq :param task_name: task_name running :return: result summary ['contacted': {}, 'dark': {}] """ from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Update some assets hardware info") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hosts = clean_hosts(assets) if not hosts: return {} created_by = str(assets[0].org_id) task, created = update_or_create_ansible_task( task_name, hosts=hosts, tasks=tasks, created_by=created_by, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, ) result = task.run() set_assets_hardware_info(assets, result) return result
def test_asset_connectability_util(asset, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Test asset connectability") hosts = [asset.hostname] if not hosts: logger.info("No hosts, passed") return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() summary = result[1] if summary.get('dark'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 0, CACHE_MAX_TIME) else: cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 1, CACHE_MAX_TIME) return summary
def test_asset_connectability_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: # task_name = _("Test assets connectability") task_name = _("测试资产可连接性") hosts = [asset.hostname for asset in assets if asset.is_active and asset.is_unixlike()] if not hosts: logger.info("No hosts, passed") return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System', ) result = task.run() summary = result[1] for k in summary.get('dark'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 0, CACHE_MAX_TIME) for k in summary.get('contacted'): cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 1, CACHE_MAX_TIME) return summary
def push_system_user_util(system_users, assets, task_name): from ops.utils import update_or_create_ansible_task tasks = [] for system_user in system_users: if not system_user.is_need_push(): msg = "push system user `{}` passed, may be not auto push or ssh " \ "protocol is not ssh".format(system_user.name) logger.info(msg) continue tasks.extend(get_push_system_user_tasks(system_user)) if not tasks: logger.info("Not tasks, passed") return {} hosts = [asset.hostname for asset in assets if asset.is_active and asset.is_unixlike()] if not hosts: logger.info("Not hosts, passed") return {} task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by='System' ) return task.run()
def test_asset_connectivity_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: task_name = _("Test assets connectivity") hosts = clean_hosts(assets) if not hosts: return {} tasks = const.TEST_ADMIN_USER_CONN_TASKS created_by = assets[0].org_id task, created = update_or_create_ansible_task( task_name=task_name, hosts=hosts, tasks=tasks, pattern='all', options=const.TASK_OPTIONS, run_as_admin=True, created_by=created_by, ) result = task.run() summary = result[1] for asset in assets: if asset.hostname in summary.get('dark', {}): asset.connectivity = asset.UNREACHABLE elif asset.hostname in summary.get('contacted', []): asset.connectivity = asset.REACHABLE else: asset.connectivity = asset.UNKNOWN return summary