Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
419876b5
Commit
419876b5
authored
Nov 13, 2016
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update heatbeat
parent
26352174
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
77 additions
and
82 deletions
+77
-82
celerybeat-schedule.db
apps/celerybeat-schedule.db
+0
-0
celerybeat.pid
apps/celerybeat.pid
+0
-1
__init__.py
apps/common/__init__.py
+1
-2
celery.py
apps/common/celery.py
+20
-20
tasks.py
apps/common/tasks.py
+2
-33
settings.py
apps/jumpserver/settings.py
+14
-0
api.py
apps/terminal/api.py
+15
-14
models.py
apps/terminal/models.py
+2
-3
serializers.py
apps/terminal/serializers.py
+13
-4
tasks.py
apps/terminal/tasks.py
+5
-0
urls.py
apps/terminal/urls.py
+4
-4
run_server.py
run_server.py
+1
-1
No files found.
apps/celerybeat-schedule.db
deleted
100644 → 0
View file @
26352174
apps/celerybeat.pid
deleted
100644 → 0
View file @
26352174
64256
apps/common/__init__.py
View file @
419876b5
...
@@ -2,5 +2,4 @@ from __future__ import absolute_import
...
@@ -2,5 +2,4 @@ from __future__ import absolute_import
# This will make sure the app is always imported when
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
# Django starts so that shared_task will use this app.
# from .celery import app as celery_app
from
.celery
import
app
as
celery_app
apps/common/celery.py
View file @
419876b5
#
#
~*~ coding: utf-8 ~*~
# ~*~ coding: utf-8 ~*~
#
#
from __future__ import absolute_import, unicode_literals
from
__future__
import
absolute_import
,
unicode_literals
#
import os
import
os
#
from
datetime
import
timedelta
# from celery import Celery
#
from
celery
import
Celery
# # set the default Django settings module for the 'celery' program.
#
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')
#
set the default Django settings module for the 'celery' program.
#
os
.
environ
.
setdefault
(
'DJANGO_SETTINGS_MODULE'
,
'jumpserver.settings'
)
# from django.conf import settings
#
from
django.conf
import
settings
# app = Celery('jumpserver')
#
app
=
Celery
(
'jumpserver'
)
# # Using a string here means the worker will not have to
#
# pickle the object when using Windows.
#
Using a string here means the worker will not have to
#
app.config_from_object('django.conf:settings')
#
pickle the object when using Windows.
#
app
.
config_from_object
(
'django.conf:settings'
)
#
app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS])
app
.
autodiscover_tasks
(
lambda
:
[
app_config
.
split
(
'.'
)[
0
]
for
app_config
in
settings
.
INSTALLED_APPS
])
#
apps/common/tasks.py
View file @
419876b5
from
__future__
import
absolute_import
from
__future__
import
absolute_import
import
os
# from celery import shared_task
from
celery
import
shared_task
from
celery.schedules
import
crontab
from
django.core.mail
import
send_mail
from
django.core.mail
import
send_mail
# from django.conf import settings
# from common import celery_app
from
celery
import
Celery
# set the default Django settings module for the 'celery' program.
os
.
environ
.
setdefault
(
'DJANGO_SETTINGS_MODULE'
,
'jumpserver.settings'
)
from
django.conf
import
settings
from
django.conf
import
settings
from
common
import
celery_app
as
app
app
=
Celery
(
'jumpserver'
)
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app
.
config_from_object
(
'django.conf:settings'
)
app
.
autodiscover_tasks
(
lambda
:
[
app_config
.
split
(
'.'
)[
0
]
for
app_config
in
settings
.
INSTALLED_APPS
])
@app.task
@app.task
...
@@ -44,17 +27,3 @@ def send_mail_async(*args, **kwargs):
...
@@ -44,17 +27,3 @@ def send_mail_async(*args, **kwargs):
args
=
tuple
(
args
)
args
=
tuple
(
args
)
send_mail
(
*
args
,
**
kwargs
)
send_mail
(
*
args
,
**
kwargs
)
# @celery_app.task
# def test(arg):
# print(arg)
# celery_app.conf.beat_schedule = {
# 'add-every-30-seconds': {
# 'task': 'common.test',
# 'schedule': crontab(minute='*/1'),
# 'args': ('nihao',)
# }
# }
apps/jumpserver/settings.py
View file @
419876b5
...
@@ -14,6 +14,7 @@ import os
...
@@ -14,6 +14,7 @@ import os
import
sys
import
sys
from
django.urls
import
reverse_lazy
from
django.urls
import
reverse_lazy
from
datetime
import
timedelta
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
...
@@ -284,6 +285,19 @@ BROKER_URL = 'redis://%(password)s%(host)s:%(port)s/3' % {
...
@@ -284,6 +285,19 @@ BROKER_URL = 'redis://%(password)s%(host)s:%(port)s/3' % {
}
}
CELERY_RESULT_BACKEND
=
BROKER_URL
CELERY_RESULT_BACKEND
=
BROKER_URL
# TERMINAL_HEATBEAT_INTERVAL = CONFIG.TERMINAL_HEATBEAT_INTERVAL or 30
# crontab job
# CELERYBEAT_SCHEDULE = {
# Check terminal is alive every 10m
# 'check_terminal_alive': {
# 'task': 'terminal.tasks.check_terminal_alive',
# 'schedule': timedelta(seconds=TERMINAL_HEATBEAT_INTERVAL),
# 'args': (),
# },
# }
# Cache use redis
# Cache use redis
CACHES
=
{
CACHES
=
{
'default'
:
{
'default'
:
{
...
...
apps/terminal/api.py
View file @
419876b5
...
@@ -4,17 +4,17 @@
...
@@ -4,17 +4,17 @@
from
django.core.cache
import
cache
from
django.core.cache
import
cache
from
django.conf
import
settings
from
django.conf
import
settings
from
rest_framework.generics
import
ListCreateAPIView
,
RetrieveUpdateDestroyAPIView
from
rest_framework.generics
import
ListCreateAPIView
,
RetrieveUpdateDestroyAPIView
from
rest_framework
import
viewsets
from
rest_framework.views
import
APIView
,
Response
from
rest_framework.views
import
APIView
,
Response
from
rest_framework.viewsets
import
ModelViewSet
from
rest_framework.permissions
import
AllowAny
from
rest_framework.permissions
import
AllowAny
from
common.utils
import
signer
,
get_object_or_none
from
common.utils
import
signer
,
get_object_or_none
from
.models
import
Terminal
,
HeatbeatFailedLog
from
.models
import
Terminal
,
TerminalHeatbeat
from
.serializers
import
TerminalSerializer
,
TerminalHeatbeatSerializer
from
.serializers
import
TerminalSerializer
,
TerminalHeatbeatSerializer
from
.hands
import
IsSuperUserOrTerminalUser
from
.hands
import
IsSuperUserOrTerminalUser
class
TerminalViewSet
(
ModelViewSet
):
class
TerminalViewSet
(
viewsets
.
ModelViewSet
):
queryset
=
Terminal
.
objects
.
all
()
queryset
=
Terminal
.
objects
.
all
()
serializer_class
=
TerminalSerializer
serializer_class
=
TerminalSerializer
permission_classes
=
(
AllowAny
,)
permission_classes
=
(
AllowAny
,)
...
@@ -46,18 +46,19 @@ class TerminalViewSet(ModelViewSet):
...
@@ -46,18 +46,19 @@ class TerminalViewSet(ModelViewSet):
return
Response
(
data
=
{
'msg'
:
'Secrete key invalid'
},
status
=
401
)
return
Response
(
data
=
{
'msg'
:
'Secrete key invalid'
},
status
=
401
)
class
TerminalHeatbeatApi
(
APIView
):
class
TerminalHeatbeatApi
(
ListCreate
APIView
):
# model = HeatbeatFailedLog
queryset
=
TerminalHeatbeat
.
objects
.
all
()
#
serializer_class = TerminalHeatbeatSerializer
serializer_class
=
TerminalHeatbeatSerializer
permission_classes
=
(
IsSuperUserOrTerminalUser
,)
permission_classes
=
(
IsSuperUserOrTerminalUser
,)
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
terminal_id
=
request
.
user
.
id
cache
.
set
(
'terminal_heatbeat_
%
s'
%
terminal_id
,
settings
.
CONFIG
.
TERMINAL_HEATBEAT_INTERVAL
*
3
)
return
Response
({
'msg'
:
'Success'
})
class
TerminalHeatbeatViewSet
(
viewsets
.
ModelViewSet
):
queryset
=
TerminalHeatbeat
.
objects
.
all
()
serializer_class
=
TerminalHeatbeatSerializer
permission_classes
=
(
IsSuperUserOrTerminalUser
,)
def
create
(
self
,
request
,
*
args
,
**
kwargs
):
terminal
=
request
.
user
TerminalHeatbeat
.
objects
.
create
(
terminal
=
terminal
)
return
Response
({
'msg'
:
'Success'
})
# class TerminalApiDetailUpdateDetailApi(RetrieveUpdateDestroyAPIView):
# queryset = Terminal.objects.all()
# serializer_class = TerminalSerializer
# permission_classes = (IsSuperUserOrTerminalUser,)
apps/terminal/models.py
View file @
419876b5
...
@@ -36,10 +36,9 @@ class Terminal(models.Model):
...
@@ -36,10 +36,9 @@ class Terminal(models.Model):
ordering
=
[
'is_active'
]
ordering
=
[
'is_active'
]
class
HeatbeatFailedLog
(
models
.
Model
):
class
TerminalHeatbeat
(
models
.
Model
):
"""Terminal heatbeat failed log"""
terminal
=
models
.
ForeignKey
(
Terminal
,
on_delete
=
models
.
CASCADE
)
terminal
=
models
.
ForeignKey
(
Terminal
,
on_delete
=
models
.
CASCADE
)
date_created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
date_created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
class
Meta
:
class
Meta
:
db_table
=
'
heatbeat_failed_log
'
db_table
=
'
terminal_heatbeat
'
apps/terminal/serializers.py
View file @
419876b5
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#
#
from
django.utils
import
timezone
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
.models
import
Terminal
,
HeatbeatFailedLog
from
.models
import
Terminal
,
TerminalHeatbeat
from
.hands
import
ProxyLog
from
.hands
import
ProxyLog
class
TerminalSerializer
(
serializers
.
ModelSerializer
):
class
TerminalSerializer
(
serializers
.
ModelSerializer
):
proxy_online
=
serializers
.
SerializerMethodField
()
proxy_online
=
serializers
.
SerializerMethodField
()
is_alive
=
serializers
.
SerializerMethodField
()
class
Meta
:
class
Meta
:
model
=
Terminal
model
=
Terminal
fields
=
[
'id'
,
'name'
,
'ip'
,
'type'
,
'url'
,
'comment'
,
fields
=
[
'id'
,
'name'
,
'ip'
,
'type'
,
'url'
,
'comment'
,
'is_active'
,
'get_type_display'
,
'proxy_online'
]
'is_active'
,
'get_type_display'
,
'proxy_online'
,
'is_alive'
]
@staticmethod
@staticmethod
def
get_proxy_online
(
obj
):
def
get_proxy_online
(
obj
):
return
ProxyLog
.
objects
.
filter
(
terminal
=
obj
.
name
,
is_finished
=
False
)
.
count
()
return
ProxyLog
.
objects
.
filter
(
terminal
=
obj
.
name
,
is_finished
=
False
)
.
count
()
@staticmethod
def
get_is_alive
(
obj
):
log
=
obj
.
terminalheatbeat_set
.
last
()
if
timezone
.
now
()
-
log
.
date_created
>
timezone
.
timedelta
(
seconds
=
600
):
return
False
else
:
return
True
class
TerminalHeatbeatSerializer
(
serializers
.
ModelSerializer
):
class
TerminalHeatbeatSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
class
Meta
:
model
=
HeatbeatFailedLog
model
=
TerminalHeatbeat
fields
=
[
'terminal'
]
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
apps/terminal/tasks.py
0 → 100644
View file @
419876b5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
apps/terminal/urls.py
View file @
419876b5
...
@@ -16,10 +16,10 @@ urlpatterns = [
...
@@ -16,10 +16,10 @@ urlpatterns = [
]
]
router
=
routers
.
DefaultRouter
()
router
=
routers
.
DefaultRouter
()
router
.
register
(
r'v1/terminal/heatbeat'
,
api
.
TerminalHeatbeatViewSet
,
'api-terminal-heatbeat'
)
router
.
register
(
r'v1/terminal'
,
api
.
TerminalViewSet
,
'api-terminal'
)
router
.
register
(
r'v1/terminal'
,
api
.
TerminalViewSet
,
'api-terminal'
)
# urlpatterns += [
urlpatterns
+=
[
# url(r'v1/terminal/heatbeat/', api.TerminalHeatbeatApi.as_view(), name='api-terminal-heatbeat')
url
(
r'^v1/terminal/heatbeat/$'
,
api
.
TerminalHeatbeatApi
.
as_view
(),
name
=
'terminal-heatbeat-api'
),
# ]
]
urlpatterns
+=
router
.
urls
urlpatterns
+=
router
.
urls
run_server.py
View file @
419876b5
...
@@ -30,7 +30,7 @@ def start_celery():
...
@@ -30,7 +30,7 @@ def start_celery():
os
.
chdir
(
apps_dir
)
os
.
chdir
(
apps_dir
)
os
.
environ
.
setdefault
(
'C_FORCE_ROOT'
,
'1'
)
os
.
environ
.
setdefault
(
'C_FORCE_ROOT'
,
'1'
)
print
(
'start celery'
)
print
(
'start celery'
)
subprocess
.
call
(
'celery -A common worker -
l info
'
,
shell
=
True
)
subprocess
.
call
(
'celery -A common worker -
B -s /tmp/celerybeat-schedule -l info
'
,
shell
=
True
)
def
main
():
def
main
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment