2017-03-15 16:19:47 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
import json
|
2017-12-29 18:29:29 +00:00
|
|
|
import re
|
2017-03-15 16:19:47 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
from celery import shared_task
|
2017-04-05 11:09:51 +00:00
|
|
|
from django.core.cache import cache
|
2017-12-29 15:53:45 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-04-05 11:09:51 +00:00
|
|
|
|
2017-12-13 09:21:08 +00:00
|
|
|
from common.utils import get_object_or_none, capacity_convert, \
|
|
|
|
sum_capacity, encrypt_password, get_logger
|
2017-12-24 10:53:07 +00:00
|
|
|
from common.celery import register_as_period_task, after_app_shutdown_clean, \
|
|
|
|
after_app_ready_start, app as celery_app
|
|
|
|
|
2017-12-29 15:53:45 +00:00
|
|
|
from .models import SystemUser, AdminUser, Asset, Cluster
|
2017-12-15 07:50:15 +00:00
|
|
|
from . import const
|
2017-03-15 16:19:47 +00:00
|
|
|
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
FORKS = 10
|
|
|
|
TIMEOUT = 60
|
|
|
|
logger = get_logger(__file__)
|
2017-12-13 09:21:08 +00:00
|
|
|
CACHE_MAX_TIME = 60*60*60
|
2018-01-26 08:06:23 +00:00
|
|
|
disk_pattern = re.compile(r'^hd|sd|xvd|vd')
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def set_assets_hardware_info(result, **kwargs):
|
2017-12-24 10:53:07 +00:00
|
|
|
"""
|
2018-01-08 11:16:28 +00:00
|
|
|
Using ops task run result, to update asset info
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
@shared_task must be exit, because we using it as a task callback, is must
|
|
|
|
be a celery task also
|
|
|
|
:param result:
|
|
|
|
:param kwargs: {task_name: ""}
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
result_raw = result[0]
|
2017-12-15 07:50:15 +00:00
|
|
|
assets_updated = []
|
2017-12-28 08:06:07 +00:00
|
|
|
for hostname, info in result_raw.get('ok', {}).items():
|
2018-01-30 02:38:40 +00:00
|
|
|
info = info.get('setup', {}).get('ansible_facts', {})
|
|
|
|
if not info:
|
2018-01-26 08:21:14 +00:00
|
|
|
logger.error("Get asset info failed: {}".format(hostname))
|
2017-03-15 16:19:47 +00:00
|
|
|
continue
|
2017-12-15 07:50:15 +00:00
|
|
|
|
2017-03-15 16:19:47 +00:00
|
|
|
asset = get_object_or_none(Asset, hostname=hostname)
|
|
|
|
if not asset:
|
|
|
|
continue
|
|
|
|
|
2018-01-30 02:38:40 +00:00
|
|
|
___vendor = info.get('ansible_system_vendor', 'Unknown')
|
2018-02-09 07:24:44 +00:00
|
|
|
___model = info.get('ansible_product_name', 'Unknown')
|
2018-01-30 02:38:40 +00:00
|
|
|
___sn = info.get('ansible_product_serial', 'Unknown')
|
2017-03-15 16:19:47 +00:00
|
|
|
|
2018-01-30 02:38:40 +00:00
|
|
|
for ___cpu_model in info.get('ansible_processor', []):
|
2018-01-26 08:21:14 +00:00
|
|
|
if ___cpu_model.endswith('GHz') or ___cpu_model.startswith("Intel"):
|
2017-03-15 16:19:47 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
___cpu_model = 'Unknown'
|
2018-01-26 09:17:37 +00:00
|
|
|
___cpu_model = ___cpu_model[:64]
|
2018-01-30 04:09:33 +00:00
|
|
|
___cpu_count = info.get('ansible_processor_count', 0)
|
2018-01-26 08:28:40 +00:00
|
|
|
___cpu_cores = info.get('ansible_processor_cores', None) or len(info.get('ansible_processor', []))
|
2018-01-30 02:38:40 +00:00
|
|
|
___memory = '%s %s' % capacity_convert('{} MB'.format(info.get('ansible_memtotal_mb')))
|
2017-03-15 16:19:47 +00:00
|
|
|
disk_info = {}
|
2018-01-30 02:38:40 +00:00
|
|
|
for dev, dev_info in info.get('ansible_devices', {}).items():
|
2017-12-29 18:29:29 +00:00
|
|
|
if disk_pattern.match(dev) and dev_info['removable'] == '0':
|
2017-03-15 16:19:47 +00:00
|
|
|
disk_info[dev] = dev_info['size']
|
|
|
|
___disk_total = '%s %s' % sum_capacity(disk_info.values())
|
|
|
|
___disk_info = json.dumps(disk_info)
|
|
|
|
|
2018-01-30 02:38:40 +00:00
|
|
|
___platform = info.get('ansible_system', 'Unknown')
|
|
|
|
___os = info.get('ansible_distribution', 'Unknown')
|
|
|
|
___os_version = info.get('ansible_distribution_version', 'Unknown')
|
|
|
|
___os_arch = info.get('ansible_architecture', 'Unknown')
|
|
|
|
___hostname_raw = info.get('ansible_hostname', 'Unknown')
|
2017-03-15 16:19:47 +00:00
|
|
|
|
|
|
|
for k, v in locals().items():
|
|
|
|
if k.startswith('___'):
|
|
|
|
setattr(asset, k.strip('_'), v)
|
|
|
|
asset.save()
|
2017-12-15 07:50:15 +00:00
|
|
|
assets_updated.append(asset)
|
|
|
|
return assets_updated
|
2017-12-12 04:19:45 +00:00
|
|
|
|
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def update_assets_hardware_info_util(assets, task_name=None):
|
2017-12-15 07:50:15 +00:00
|
|
|
"""
|
|
|
|
Using ansible api to update asset hardware info
|
|
|
|
:param assets: asset seq
|
|
|
|
:param task_name: task_name running
|
|
|
|
:return: result summary ['contacted': {}, 'dark': {}]
|
|
|
|
"""
|
2017-12-24 10:53:07 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
2017-12-29 15:53:45 +00:00
|
|
|
if task_name is None:
|
|
|
|
task_name = _("Update some assets hardware info")
|
2017-12-15 07:50:15 +00:00
|
|
|
tasks = const.UPDATE_ASSETS_HARDWARE_TASKS
|
2018-03-07 04:13:03 +00:00
|
|
|
hostname_list = [asset.hostname for asset in assets if asset.is_active and asset.is_unixlike()]
|
2017-12-29 15:53:45 +00:00
|
|
|
task, created = update_or_create_ansible_task(
|
2017-12-15 07:50:15 +00:00
|
|
|
task_name, hosts=hostname_list, tasks=tasks, pattern='all',
|
|
|
|
options=const.TASK_OPTIONS, run_as_admin=True, created_by='System',
|
|
|
|
)
|
|
|
|
result = task.run()
|
2017-12-24 10:53:07 +00:00
|
|
|
# Todo: may be somewhere using
|
|
|
|
# Manual run callback function
|
2018-02-09 03:12:40 +00:00
|
|
|
set_assets_hardware_info(result)
|
2017-12-24 10:53:07 +00:00
|
|
|
return result
|
2017-03-15 16:19:47 +00:00
|
|
|
|
|
|
|
|
2017-04-04 13:47:58 +00:00
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def update_asset_hardware_info_manual(asset):
|
|
|
|
task_name = _("Update asset hardware info")
|
|
|
|
return update_assets_hardware_info_util([asset], task_name=task_name)
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task
|
|
|
|
@register_as_period_task(interval=3600)
|
|
|
|
@after_app_ready_start
|
|
|
|
@after_app_shutdown_clean
|
|
|
|
def update_assets_hardware_info_period():
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
Update asset hardware period task
|
|
|
|
:return:
|
|
|
|
"""
|
2017-12-24 10:53:07 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
2017-12-29 15:53:45 +00:00
|
|
|
task_name = _("Update assets hardware info period")
|
2018-03-07 04:13:03 +00:00
|
|
|
hostname_list = [
|
|
|
|
asset.hostname for asset in Asset.objects.all()
|
|
|
|
if asset.is_active and asset.is_unixlike()
|
|
|
|
]
|
2017-12-24 10:53:07 +00:00
|
|
|
tasks = const.UPDATE_ASSETS_HARDWARE_TASKS
|
|
|
|
|
|
|
|
# Only create, schedule by celery beat
|
2017-12-29 15:53:45 +00:00
|
|
|
update_or_create_ansible_task(
|
2017-12-24 10:53:07 +00:00
|
|
|
task_name, hosts=hostname_list, tasks=tasks, pattern='all',
|
|
|
|
options=const.TASK_OPTIONS, run_as_admin=True, created_by='System',
|
2017-12-29 15:53:45 +00:00
|
|
|
interval=60*60*24, is_periodic=True, callback=set_assets_hardware_info.name,
|
2017-12-24 10:53:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
## ADMIN USER CONNECTIVE ##
|
|
|
|
|
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def set_admin_user_connectability_info(result, **kwargs):
|
2017-12-24 10:53:07 +00:00
|
|
|
admin_user = kwargs.get("admin_user")
|
|
|
|
task_name = kwargs.get("task_name")
|
|
|
|
if admin_user is None and task_name is not None:
|
|
|
|
admin_user = task_name.split(":")[-1]
|
|
|
|
|
2017-12-29 15:53:45 +00:00
|
|
|
raw, summary = result
|
2017-12-24 10:53:07 +00:00
|
|
|
cache_key = const.ADMIN_USER_CONN_CACHE_KEY.format(admin_user)
|
|
|
|
cache.set(cache_key, summary, CACHE_MAX_TIME)
|
|
|
|
|
|
|
|
for i in summary.get('contacted', []):
|
|
|
|
asset_conn_cache_key = const.ASSET_ADMIN_CONN_CACHE_KEY.format(i)
|
|
|
|
cache.set(asset_conn_cache_key, 1, CACHE_MAX_TIME)
|
|
|
|
|
|
|
|
for i, msg in summary.get('dark', {}).items():
|
|
|
|
asset_conn_cache_key = const.ASSET_ADMIN_CONN_CACHE_KEY.format(i)
|
|
|
|
cache.set(asset_conn_cache_key, 0, CACHE_MAX_TIME)
|
|
|
|
logger.error(msg)
|
2017-04-05 11:09:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
2017-12-24 10:53:07 +00:00
|
|
|
def test_admin_user_connectability_util(admin_user, task_name):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
Test asset admin user can connect or not. Using ansible api do that
|
|
|
|
:param admin_user:
|
2017-12-24 10:53:07 +00:00
|
|
|
:param task_name:
|
2017-12-10 16:29:25 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2017-12-24 10:53:07 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
2017-12-13 09:21:08 +00:00
|
|
|
|
2017-12-12 04:19:45 +00:00
|
|
|
assets = admin_user.get_related_assets()
|
2018-03-07 04:13:03 +00:00
|
|
|
hosts = [asset.hostname for asset in assets
|
|
|
|
if asset.is_active and asset.is_unixlike()]
|
2018-02-25 10:08:00 +00:00
|
|
|
if not hosts:
|
|
|
|
return
|
2017-12-15 07:50:15 +00:00
|
|
|
tasks = const.TEST_ADMIN_USER_CONN_TASKS
|
2017-12-24 10:53:07 +00:00
|
|
|
task, created = update_or_create_ansible_task(
|
2017-12-15 07:50:15 +00:00
|
|
|
task_name=task_name, hosts=hosts, tasks=tasks, pattern='all',
|
|
|
|
options=const.TASK_OPTIONS, run_as_admin=True, created_by='System',
|
|
|
|
)
|
|
|
|
result = task.run()
|
2017-12-29 15:53:45 +00:00
|
|
|
set_admin_user_connectability_info(result, admin_user=admin_user.name)
|
2017-12-24 10:53:07 +00:00
|
|
|
return result
|
2017-12-15 07:50:15 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
@celery_app.task
|
|
|
|
@register_as_period_task(interval=3600)
|
|
|
|
@after_app_ready_start
|
|
|
|
@after_app_shutdown_clean
|
2017-12-10 16:29:25 +00:00
|
|
|
def test_admin_user_connectability_period():
|
2017-12-24 10:53:07 +00:00
|
|
|
"""
|
|
|
|
A period task that update the ansible task period
|
|
|
|
"""
|
2017-12-15 07:50:15 +00:00
|
|
|
admin_users = AdminUser.objects.all()
|
|
|
|
for admin_user in admin_users:
|
2018-02-25 10:08:00 +00:00
|
|
|
task_name = _("Test admin user connectability period: {}".format(admin_user.name))
|
|
|
|
test_admin_user_connectability_util(admin_user, task_name)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2017-12-12 04:19:45 +00:00
|
|
|
@shared_task
|
2017-12-24 10:53:07 +00:00
|
|
|
def test_admin_user_connectability_manual(admin_user):
|
2017-12-29 15:53:45 +00:00
|
|
|
task_name = _("Test admin user connectability: {}").format(admin_user.name)
|
2017-12-24 10:53:07 +00:00
|
|
|
return test_admin_user_connectability_util.delay(admin_user, task_name)
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def test_asset_connectability_util(asset, task_name=None):
|
2017-12-24 10:53:07 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
|
|
|
|
2017-12-29 15:53:45 +00:00
|
|
|
if task_name is None:
|
2018-01-04 01:29:54 +00:00
|
|
|
task_name = _("Test asset connectability")
|
2017-12-29 15:53:45 +00:00
|
|
|
hosts = [asset.hostname]
|
2018-01-05 09:57:02 +00:00
|
|
|
if not hosts:
|
|
|
|
logger.info("No hosts, passed")
|
|
|
|
return {}
|
2017-12-15 07:50:15 +00:00
|
|
|
tasks = const.TEST_ADMIN_USER_CONN_TASKS
|
2017-12-24 10:53:07 +00:00
|
|
|
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',
|
2017-12-15 09:07:52 +00:00
|
|
|
)
|
2017-12-15 07:50:15 +00:00
|
|
|
result = task.run()
|
2017-12-24 10:53:07 +00:00
|
|
|
summary = result[1]
|
|
|
|
if summary.get('dark'):
|
2017-12-29 15:53:45 +00:00
|
|
|
cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 0,
|
|
|
|
CACHE_MAX_TIME)
|
2017-12-10 16:29:25 +00:00
|
|
|
else:
|
2017-12-29 15:53:45 +00:00
|
|
|
cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 1,
|
|
|
|
CACHE_MAX_TIME)
|
|
|
|
return summary
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2017-12-29 15:53:45 +00:00
|
|
|
@shared_task
|
|
|
|
def test_asset_connectability_manual(asset):
|
|
|
|
summary = test_asset_connectability_util(asset)
|
|
|
|
|
|
|
|
if summary.get('dark'):
|
|
|
|
return False, summary['dark']
|
|
|
|
else:
|
|
|
|
return True, ""
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
## System user connective ##
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def set_system_user_connectablity_info(result, **kwargs):
|
2017-12-24 10:53:07 +00:00
|
|
|
summary = result[1]
|
|
|
|
task_name = kwargs.get("task_name")
|
|
|
|
system_user = kwargs.get("system_user")
|
|
|
|
if system_user is None:
|
|
|
|
system_user = task_name.split(":")[-1]
|
|
|
|
cache_key = const.SYSTEM_USER_CONN_CACHE_KEY.format(system_user)
|
|
|
|
cache.set(cache_key, summary, CACHE_MAX_TIME)
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def test_system_user_connectability_util(system_user, task_name):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
Test system cant connect his assets or not.
|
|
|
|
:param system_user:
|
2017-12-24 10:53:07 +00:00
|
|
|
:param task_name:
|
2017-12-10 16:29:25 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2018-02-25 10:08:00 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
|
|
|
assets = system_user.assets
|
2018-03-07 04:13:03 +00:00
|
|
|
hosts = [asset.hostname for asset in assets if asset.is_active and asset.is_unixlike()]
|
2018-02-25 10:08:00 +00:00
|
|
|
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
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def test_system_user_connectability_manual(system_user):
|
2018-01-05 09:57:02 +00:00
|
|
|
task_name = _("Test system user connectability: {}").format(system_user)
|
2017-12-24 10:53:07 +00:00
|
|
|
return test_system_user_connectability_util(system_user, task_name)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
2017-12-24 10:53:07 +00:00
|
|
|
@register_as_period_task(interval=3600)
|
|
|
|
@after_app_ready_start
|
|
|
|
@after_app_shutdown_clean
|
2017-12-10 16:29:25 +00:00
|
|
|
def test_system_user_connectability_period():
|
2018-02-25 10:08:00 +00:00
|
|
|
system_users = SystemUser.objects.all()
|
|
|
|
for system_user in system_users:
|
|
|
|
task_name = _("test system user connectability period: {}".format(system_user))
|
|
|
|
test_system_user_connectability_util(system_user, task_name)
|
2017-12-13 09:21:08 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
#### Push system user tasks ####
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
def get_push_system_user_tasks(system_user):
|
2018-01-08 11:16:28 +00:00
|
|
|
# Set root as system user is dangerous
|
|
|
|
if system_user.username == "root":
|
|
|
|
return []
|
|
|
|
|
2018-02-02 11:04:09 +00:00
|
|
|
tasks = []
|
|
|
|
if system_user.password:
|
|
|
|
tasks.append({
|
2017-12-15 07:50:15 +00:00
|
|
|
'name': 'Add user {}'.format(system_user.username),
|
2017-12-10 16:29:25 +00:00
|
|
|
'action': {
|
|
|
|
'module': 'user',
|
|
|
|
'args': 'name={} shell={} state=present password={}'.format(
|
|
|
|
system_user.username, system_user.shell,
|
2018-01-08 11:16:28 +00:00
|
|
|
encrypt_password(system_user.password, salt="K3mIlKK"),
|
2017-12-10 16:29:25 +00:00
|
|
|
),
|
|
|
|
}
|
2018-02-02 11:04:09 +00:00
|
|
|
})
|
|
|
|
if system_user.public_key:
|
|
|
|
tasks.append({
|
2017-12-15 07:50:15 +00:00
|
|
|
'name': 'Set {} authorized key'.format(system_user.username),
|
2017-12-10 16:29:25 +00:00
|
|
|
'action': {
|
|
|
|
'module': 'authorized_key',
|
|
|
|
'args': "user={} state=present key='{}'".format(
|
|
|
|
system_user.username, system_user.public_key
|
|
|
|
)
|
|
|
|
}
|
2018-02-02 11:04:09 +00:00
|
|
|
})
|
|
|
|
if system_user.sudo:
|
|
|
|
tasks.append({
|
2017-12-15 07:50:15 +00:00
|
|
|
'name': 'Set {} sudo setting'.format(system_user.username),
|
2017-12-10 16:29:25 +00:00
|
|
|
'action': {
|
|
|
|
'module': 'lineinfile',
|
|
|
|
'args': "dest=/etc/sudoers state=present regexp='^{0} ALL=' "
|
|
|
|
"line='{0} ALL=(ALL) NOPASSWD: {1}' "
|
|
|
|
"validate='visudo -cf %s'".format(
|
|
|
|
system_user.username,
|
|
|
|
system_user.sudo,
|
|
|
|
)
|
|
|
|
}
|
2018-02-02 11:04:09 +00:00
|
|
|
})
|
2017-12-10 16:29:25 +00:00
|
|
|
return tasks
|
|
|
|
|
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
@shared_task
|
2017-12-29 15:53:45 +00:00
|
|
|
def push_system_user_util(system_users, assets, task_name):
|
2017-12-24 10:53:07 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
2017-12-29 15:53:45 +00:00
|
|
|
tasks = []
|
|
|
|
for system_user in system_users:
|
2018-02-09 03:12:40 +00:00
|
|
|
if system_user.is_need_push():
|
|
|
|
tasks.extend(get_push_system_user_tasks(system_user))
|
2017-12-29 15:53:45 +00:00
|
|
|
|
|
|
|
if not tasks:
|
2018-01-05 09:57:02 +00:00
|
|
|
logger.info("Not tasks, passed")
|
|
|
|
return {}
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2018-03-07 04:13:03 +00:00
|
|
|
hosts = [asset.hostname for asset in assets if asset.is_active and asset.is_unixlike()]
|
2018-01-05 09:57:02 +00:00
|
|
|
if not hosts:
|
|
|
|
logger.info("Not hosts, passed")
|
|
|
|
return {}
|
2017-12-29 15:53:45 +00:00
|
|
|
task, created = update_or_create_ansible_task(
|
2017-12-24 10:53:07 +00:00
|
|
|
task_name=task_name, hosts=hosts, tasks=tasks, pattern='all',
|
|
|
|
options=const.TASK_OPTIONS, run_as_admin=True, created_by='System'
|
|
|
|
)
|
|
|
|
return task.run()
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2018-02-09 03:12:40 +00:00
|
|
|
def get_node_push_system_user_task_name(system_user, node):
|
|
|
|
return _("Push system user to node: {} => {}").format(
|
|
|
|
system_user.name,
|
|
|
|
node.value
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def push_system_user_to_node(system_user, node):
|
|
|
|
assets = node.get_all_assets()
|
|
|
|
task_name = get_node_push_system_user_task_name(system_user, node)
|
|
|
|
push_system_user_util.delay([system_user], assets, task_name)
|
|
|
|
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
@shared_task
|
2018-02-09 03:12:40 +00:00
|
|
|
def push_system_user_related_nodes(system_user):
|
|
|
|
nodes = system_user.nodes.all()
|
|
|
|
for node in nodes:
|
|
|
|
push_system_user_to_node(system_user, node)
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def push_system_user_to_assets_manual(system_user):
|
|
|
|
push_system_user_related_nodes(system_user)
|
|
|
|
|
|
|
|
|
|
|
|
def push_node_system_users_to_asset(node, assets):
|
|
|
|
system_users = []
|
|
|
|
nodes = node.ancestor_with_node
|
|
|
|
# 获取该节点所有父节点有的系统用户, 然后推送
|
|
|
|
for n in nodes:
|
|
|
|
system_users.extend(list(n.systemuser_set.all()))
|
|
|
|
|
|
|
|
if system_users:
|
|
|
|
task_name = _("Push system users to node: {}").format(node.value)
|
|
|
|
push_system_user_util.delay(system_users, assets, task_name)
|
2017-03-15 16:19:47 +00:00
|
|
|
|
|
|
|
|
2018-02-25 10:08:00 +00:00
|
|
|
# @shared_task
|
|
|
|
# @register_as_period_task(interval=3600)
|
|
|
|
# @after_app_ready_start
|
|
|
|
# # @after_app_shutdown_clean
|
|
|
|
# def push_system_user_period():
|
|
|
|
# for system_user in SystemUser.objects.all():
|
|
|
|
# push_system_user_related_nodes(system_user)
|