2018-12-10 02:11:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import uuid
|
|
|
|
import json
|
|
|
|
|
2019-10-24 02:23:31 +00:00
|
|
|
from celery.exceptions import SoftTimeLimitExceeded
|
2018-12-10 02:11:54 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.utils.translation import ugettext
|
|
|
|
from django.db import models
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
from terminal.notifications import CommandExecutionAlert
|
2020-06-04 12:00:39 +00:00
|
|
|
from common.utils import lazyproperty
|
2019-01-15 02:23:30 +00:00
|
|
|
from orgs.models import Organization
|
2020-03-12 08:24:38 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2021-03-12 11:05:59 +00:00
|
|
|
from orgs.utils import current_org, tmp_to_org
|
2018-12-10 02:11:54 +00:00
|
|
|
from ..ansible.runner import CommandRunner
|
|
|
|
from ..inventory import JMSInventory
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class CommandExecution(OrgModelMixin):
|
2018-12-10 02:11:54 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2020-05-09 06:50:22 +00:00
|
|
|
hosts = models.ManyToManyField('assets.Asset')
|
|
|
|
run_as = models.ForeignKey('assets.SystemUser', on_delete=models.CASCADE)
|
2018-12-10 02:11:54 +00:00
|
|
|
command = models.TextField(verbose_name=_("Command"))
|
|
|
|
_result = models.TextField(blank=True, null=True, verbose_name=_('Result'))
|
2020-05-09 06:50:22 +00:00
|
|
|
user = models.ForeignKey('users.User', on_delete=models.CASCADE, null=True)
|
2020-05-09 03:28:39 +00:00
|
|
|
is_finished = models.BooleanField(default=False, verbose_name=_('Is finished'))
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
|
|
|
date_start = models.DateTimeField(null=True, verbose_name=_('Date start'))
|
|
|
|
date_finished = models.DateTimeField(null=True, verbose_name=_('Date finished'))
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.command[:10]
|
|
|
|
|
2021-03-12 11:05:59 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
with tmp_to_org(self.run_as.org_id):
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
@property
|
|
|
|
def inventory(self):
|
2020-03-16 07:23:15 +00:00
|
|
|
if self.run_as.username_same_with_user:
|
|
|
|
username = self.user.username
|
|
|
|
else:
|
|
|
|
username = self.run_as.username
|
2020-11-17 03:36:35 +00:00
|
|
|
inv = JMSInventory(self.hosts.all(), run_as=username, system_user=self.run_as)
|
2020-03-16 07:23:15 +00:00
|
|
|
return inv
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2020-06-04 12:00:39 +00:00
|
|
|
@lazyproperty
|
|
|
|
def run_as_display(self):
|
|
|
|
return str(self.run_as)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user_display(self):
|
|
|
|
return str(self.user)
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
@property
|
|
|
|
def result(self):
|
|
|
|
if self._result:
|
|
|
|
return json.loads(self._result)
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@result.setter
|
|
|
|
def result(self, item):
|
|
|
|
self._result = json.dumps(item)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_success(self):
|
|
|
|
if 'error' in self.result:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-12-11 03:09:46 +00:00
|
|
|
def get_hosts_names(self):
|
|
|
|
return ','.join(self.hosts.all().values_list('hostname', flat=True))
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
def run(self):
|
|
|
|
print('-'*10 + ' ' + ugettext('Task start') + ' ' + '-'*10)
|
2019-01-15 02:23:30 +00:00
|
|
|
org = Organization.get_instance(self.run_as.org_id)
|
|
|
|
org.change_to()
|
2018-12-10 02:11:54 +00:00
|
|
|
self.date_start = timezone.now()
|
|
|
|
ok, msg = self.run_as.is_command_can_run(self.command)
|
|
|
|
if ok:
|
|
|
|
runner = CommandRunner(self.inventory)
|
|
|
|
try:
|
2019-12-23 03:22:46 +00:00
|
|
|
host = self.hosts.first()
|
2020-11-16 10:22:07 +00:00
|
|
|
if host and host.is_windows():
|
2019-12-21 11:44:25 +00:00
|
|
|
shell = 'win_shell'
|
2021-07-08 06:36:18 +00:00
|
|
|
elif host and host.is_unixlike():
|
|
|
|
shell = 'shell'
|
2019-12-21 11:44:25 +00:00
|
|
|
else:
|
2021-07-08 06:32:03 +00:00
|
|
|
shell = 'raw'
|
2019-12-21 11:44:25 +00:00
|
|
|
result = runner.execute(self.command, 'all', module=shell)
|
2018-12-10 02:11:54 +00:00
|
|
|
self.result = result.results_command
|
2019-10-24 02:23:31 +00:00
|
|
|
except SoftTimeLimitExceeded as e:
|
|
|
|
print("Run timeout than 60s")
|
|
|
|
self.result = {"error": str(e)}
|
2018-12-10 02:11:54 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Error occur: {}".format(e))
|
|
|
|
self.result = {"error": str(e)}
|
|
|
|
else:
|
|
|
|
msg = _("Command `{}` is forbidden ........").format(self.command)
|
|
|
|
print('\033[31m' + msg + '\033[0m')
|
2021-05-31 09:20:38 +00:00
|
|
|
CommandExecutionAlert({
|
2021-01-21 07:18:54 +00:00
|
|
|
'input': self.command,
|
|
|
|
'assets': self.hosts.all(),
|
|
|
|
'user': str(self.user),
|
|
|
|
'risk_level': 5,
|
2021-05-31 09:20:38 +00:00
|
|
|
}).publish_async()
|
2018-12-10 02:11:54 +00:00
|
|
|
self.result = {"error": msg}
|
2020-03-12 08:24:38 +00:00
|
|
|
self.org_id = self.run_as.org_id
|
2018-12-10 02:11:54 +00:00
|
|
|
self.is_finished = True
|
|
|
|
self.date_finished = timezone.now()
|
|
|
|
self.save()
|
|
|
|
print('-'*10 + ' ' + ugettext('Task end') + ' ' + '-'*10)
|
|
|
|
return self.result
|