Commit e0a3fafb authored by ibuler's avatar ibuler

[Bugfix] 详见trello

parent 3804ab53
# ~*~ coding: utf-8 ~*~
import json
import re
from celery import shared_task
from django.core.cache import cache
......@@ -18,6 +19,7 @@ FORKS = 10
TIMEOUT = 60
logger = get_logger(__file__)
CACHE_MAX_TIME = 60*60*60
disk_pattern = re.compile(r'^hd|sd')
@shared_task
......@@ -57,7 +59,7 @@ def set_assets_hardware_info(result, **kwargs):
___memory = '%s %s' % capacity_convert('{} MB'.format(info['ansible_memtotal_mb']))
disk_info = {}
for dev, dev_info in info['ansible_devices'].items():
if dev_info['removable'] == '0':
if disk_pattern.match(dev) and dev_info['removable'] == '0':
disk_info[dev] = dev_info['size']
___disk_total = '%s %s' % sum_capacity(disk_info.values())
___disk_info = json.dumps(disk_info)
......
......@@ -16,6 +16,7 @@ logger = get_logger(__file__)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')
from django.conf import settings
from django.core.cache import cache
app = Celery('jumpserver')
......@@ -176,6 +177,9 @@ def after_app_shutdown_clean(func):
@worker_ready.connect
def on_app_ready(sender=None, headers=None, body=None, **kwargs):
if cache.get("CELERY_APP_READY", 0) == 1:
return
cache.set("CELERY_APP_READY", 1, 10)
logger.debug("App ready signal recv")
logger.debug("Start need start task: [{}]".format(
", ".join(__AFTER_APP_READY_RUN_TASKS))
......@@ -186,6 +190,9 @@ def on_app_ready(sender=None, headers=None, body=None, **kwargs):
@worker_shutdown.connect
def after_app_shutdown(sender=None, headers=None, body=None, **kwargs):
if cache.get("CELERY_APP_SHUTDOWN", 0) == 1:
return
cache.set("CELERY_APP_SHUTDOWN", 1, 10)
from django_celery_beat.models import PeriodicTask
logger.debug("App shutdown signal recv")
logger.debug("Clean need cleaned period tasks: [{}]".format(
......
......@@ -6,9 +6,17 @@ from django.utils.translation import ugettext_lazy as _
# from .hands import User, UserGroup, Asset, AssetGroup, SystemUser
from .models import AssetPermission
from users.models import User
class AssetPermissionForm(forms.ModelForm):
users = forms.ModelMultipleChoiceField(
queryset=User.objects.exclude(role=User.ROLE_APP),
widget=forms.SelectMultiple(
attrs={'class': 'select2', 'data-placeholder': _('Select users')},
)
)
class Meta:
model = AssetPermission
fields = [
......@@ -16,9 +24,6 @@ class AssetPermissionForm(forms.ModelForm):
'system_users', 'is_active', 'date_expired', 'comment',
]
widgets = {
'users': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select users')}),
'user_groups': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select user groups')}),
......
......@@ -3,15 +3,15 @@
<li class="nav-header">
<div class="dropdown profile-element">
<span>
<img alt="image" class="img-circle" width="48" height="48" src="{{ user.avatar_url }}"/>
<img alt="image" class="img-circle" width="48" height="48" src="{{ request.user.avatar_url }}"/>
</span>
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear">
<span class="block m-t-xs">
<strong class="font-bold"> {{ user.name }}<span style="color: #8095a8"></span></strong>
<strong class="font-bold"> {{ request.user.name }}<span style="color: #8095a8"></span></strong>
</span>
<span class="text-muted text-xs block">
{{ user.get_role_display | default:_('User') }}<b class="caret"></b>
{{ request.user.get_role_display | default:_('User') }}<b class="caret"></b>
</span>
</span>
</a>
......
......@@ -7,6 +7,5 @@ class TerminalConfig(AppConfig):
name = 'terminal'
def ready(self):
from .signals import on_app_ready
on_app_ready.send(self.__class__)
super().ready()
from . import signals_handler
return super().ready()
\ No newline at end of file
......@@ -7,9 +7,9 @@ from django.utils.translation import ugettext_lazy as _
class AbstractSessionCommand(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
user = models.CharField(max_length=64, verbose_name=_("User"))
asset = models.CharField(max_length=128, verbose_name=_("Asset"))
system_user = models.CharField(max_length=64, verbose_name=_("System user"))
user = models.CharField(max_length=64, db_index=True, verbose_name=_("User"))
asset = models.CharField(max_length=128, db_index=True, verbose_name=_("Asset"))
system_user = models.CharField(max_length=64, db_index=True, verbose_name=_("System user"))
input = models.CharField(max_length=128, db_index=True, verbose_name=_("Input"))
output = models.CharField(max_length=1024, blank=True, verbose_name=_("Output"))
session = models.CharField(max_length=36, db_index=True, verbose_name=_("Session"))
......
......@@ -3,47 +3,35 @@
import threading
import time
from celery import shared_task
from django.core.cache import cache
from django.dispatch import Signal, receiver
from django.db.utils import ProgrammingError, OperationalError
from common.utils import get_logger
from common.celery import after_app_ready_start, register_as_period_task, \
after_app_shutdown_clean
from .const import ASSETS_CACHE_KEY, USERS_CACHE_KEY, SYSTEM_USER_CACHE_KEY
RUNNING = False
logger = get_logger(__file__)
on_app_ready = Signal()
@receiver(on_app_ready)
def on_app_ready_set_cache(sender, **kwargs):
@shared_task
@register_as_period_task(interval=3600)
@after_app_ready_start
@after_app_shutdown_clean
def set_session_info_cache():
logger.debug("")
from .utils import get_session_asset_list, get_session_user_list, \
get_session_system_user_list
global RUNNING
def set_cache():
while True:
try:
assets = get_session_asset_list()
users = get_session_user_list()
system_users = get_session_system_user_list()
cache.set(ASSETS_CACHE_KEY, assets)
cache.set(USERS_CACHE_KEY, users)
cache.set(SYSTEM_USER_CACHE_KEY, system_users)
except (ProgrammingError, OperationalError):
pass
finally:
time.sleep(10)
if RUNNING:
return
threads = []
thread = threading.Thread(target=set_cache)
threads.append(thread)
try:
assets = get_session_asset_list()
users = get_session_user_list()
system_users = get_session_system_user_list()
logger.debug("Receive app ready signal: set cache task start")
for t in threads:
t.daemon = True
t.start()
RUNNING = True
cache.set(ASSETS_CACHE_KEY, assets)
cache.set(USERS_CACHE_KEY, users)
cache.set(SYSTEM_USER_CACHE_KEY, system_users)
except (ProgrammingError, OperationalError):
pass
......@@ -28,15 +28,15 @@
</div>
</div>
<div class="input-group">
<select class="select2 form-control" name="username">
<select class="select2 form-control" name="user">
<option value="">{% trans 'User' %}</option>
{% for u in user_list %}
<option value="{{ u }}" {% if u == username %} selected {% endif %}>{{ u }}</option>
<option value="{{ u }}" {% if u == user %} selected {% endif %}>{{ u }}</option>
{% endfor %}
</select>
</div>
<div class="input-group">
<select class="select2 form-control" name="ip">
<select class="select2 form-control" name="asset">
<option value="">{% trans 'Asset' %}</option>
{% for a in asset_list %}
<option value="{{ a }}" {% if a == asset %} selected {% endif %}>{{ a }}</option>
......@@ -68,12 +68,12 @@
<thead>
<tr>
<th data-toggle="true">ID</th>
<th>Command</th>
<th>Username</th>
<th>IP</th>
<th>System user</th>
<th>Session</th>
<th>Datetime</th>
<th>{% trans 'Command' %}</th>
<th>{% trans 'User' %}</th>
<th>{% trans 'Asset' %}</th>
<th>{% trans 'System user'%}</th>
<th>{% trans 'Session' %}</th>
<th>{% trans 'Datetime' %}</th>
<th data-hide="all"></th>
</tr>
</thead>
......
......@@ -31,7 +31,7 @@
<select class="select2 form-control" name="user">
<option value="">{% trans 'User' %}</option>
{% for u in user_list %}
<option value="{{ u }}" {% if u == username %} selected {% endif %}>{{ u }}</option>
<option value="{{ u }}" {% if u == user %} selected {% endif %}>{{ u }}</option>
{% endfor %}
</select>
</div>
......
......@@ -25,6 +25,10 @@ class CommandListView(DatetimeSearchMixin, ListView):
date_format = '%m/%d/%Y'
def get_queryset(self):
self.command = self.request.GET.get('command', '')
self.user = self.request.GET.get("user", '')
self.asset = self.request.GET.get('asset', '')
self.system_user = self.request.GET.get('system_user', '')
filter_kwargs = dict()
filter_kwargs['date_from'] = self.date_from
filter_kwargs['date_to'] = self.date_to
......@@ -36,21 +40,20 @@ class CommandListView(DatetimeSearchMixin, ListView):
filter_kwargs['system_user'] = self.system_user
if self.command:
filter_kwargs['input'] = self.command
if filter_kwargs:
self.queryset = command_store.filter(**filter_kwargs)
return self.queryset
queryset = command_store.filter(**filter_kwargs)
return queryset
def get_context_data(self, **kwargs):
context = {
'app': _('Terminal'),
'action': _('Command list'),
'user_list': utils.get_user_list_from_cache(),
'asset_list': utils.get_asset_list_from_cache(),
'system_user_list': utils.get_system_user_list_from_cache(),
'user_list': utils.get_session_user_list(),
'asset_list': utils.get_session_asset_list(),
'system_user_list': utils.get_session_system_user_list(),
'command': self.command,
'date_from': self.date_from,
'date_to': self.date_to,
'username': self.user,
'user': self.user,
'asset': self.asset,
'system_user': self.system_user,
}
......
......@@ -59,7 +59,7 @@ class SessionListView(AdminUserRequiredMixin, DatetimeSearchMixin, ListView):
'system_user_list': utils.get_system_user_list_from_cache(),
'date_from': self.date_from,
'date_to': self.date_to,
'username': self.user,
'user': self.user,
'asset': self.asset,
'system_user': self.system_user,
}
......
......@@ -22,10 +22,14 @@ signer = get_signer()
class User(AbstractUser):
ROLE_ADMIN = 'Admin'
ROLE_USER = 'User'
ROLE_APP = 'App'
ROLE_CHOICES = (
('Admin', 'Administrator'),
('User', 'User'),
('App', 'Application')
(ROLE_ADMIN, 'Administrator'),
(ROLE_USER, 'User'),
(ROLE_APP, 'Application')
)
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
username = models.CharField(max_length=128, unique=True, verbose_name=_('Username'))
......
......@@ -220,7 +220,7 @@ class LoginLogListView(DatetimeSearchMixin, ListView):
date_format = '%m/%d/%Y'
def get_queryset(self):
self.username = self.request.GET.get('username', '')
self.username = self.request.GET.get('user', '')
self.keyword = self.request.GET.get("keyword", '')
queryset = super().get_queryset()
......@@ -228,7 +228,7 @@ class LoginLogListView(DatetimeSearchMixin, ListView):
datetime__gt=self.date_from, datetime__lt=self.date_to
)
if self.username:
queryset = self.queryset.filter(username=self.username)
queryset = queryset.filter(username=self.username)
if self.keyword:
queryset = self.queryset.filter(
Q(ip__contains=self.keyword) |
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment