-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobs.py
264 lines (222 loc) · 7.56 KB
/
jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from django.shortcuts import render, redirect
from django_celery_beat.models import PeriodicTask, IntervalSchedule, CrontabSchedule
from django_celery_results.models import TaskResult
from django.contrib.auth.decorators import login_required
from accounts.permission import permission_verify
from cmdb.api import get_object
from forms import PeriodicTaskForm, IntervalForm, CrontabForm, TaskResultForm
from subprocess import Popen, PIPE
import os, time
@login_required
@permission_verify()
def index(request):
temp_name = "setup/setup-header.html"
jobs_info = PeriodicTask.objects.all()
return render(request, 'setup/job_list.html', locals())
@login_required
@permission_verify()
def job_edit(request, ids):
status = 0
obj = get_object(PeriodicTask, id=ids)
if request.method == 'POST':
form = PeriodicTaskForm(request.POST, instance=obj)
if form.is_valid():
form.save()
status = 1
else:
status = 2
else:
form = PeriodicTaskForm(instance=obj)
return render(request, 'setup/job_edit.html', locals())
@login_required()
@permission_verify()
def job_add(request):
temp_name = "setup/setup-header.html"
if request.method == "POST":
a_form = PeriodicTaskForm(request.POST)
if a_form.is_valid():
a_form.save()
tips = u"增加成功!"
display_control = ""
return redirect("/setup/job/list/")
else:
tips = u"增加失败!"
display_control = ""
return render(request, "setup/job_add.html", locals())
else:
display_control = "none"
a_form = PeriodicTaskForm()
return render(request, "setup/job_add.html", locals())
@login_required()
@permission_verify()
def job_del(request):
temp_name = "setup/setup-header.html"
if request.method == 'POST':
jobs = request.POST.getlist('idc_check', [])
if jobs:
for n in jobs:
PeriodicTask.objects.filter(id=n).delete()
jobs_info = PeriodicTask.objects.all()
return render(request, "setup/job_list.html", locals())
@login_required()
@permission_verify()
def job_interval_list(request):
temp_name = "setup/setup-header.html"
interval_info = IntervalSchedule.objects.all()
return render(request, 'setup/interval_list.html', locals())
@login_required
@permission_verify()
def job_interval_edit(request, ids):
status = 0
obj = get_object(IntervalSchedule, id=ids)
if request.method == 'POST':
form = IntervalForm(request.POST, instance=obj)
if form.is_valid():
form.save()
status = 1
else:
status = 2
else:
form = IntervalForm(instance=obj)
return render(request, 'setup/interval_edit.html', locals())
@login_required
@permission_verify()
def job_interval_del(request):
temp_name = "setup/setup-header.html"
if request.method == 'POST':
intervals = request.POST.getlist('idc_check', [])
if intervals:
for n in intervals:
IntervalSchedule.objects.filter(id=n).delete()
interval_info = IntervalSchedule.objects.all()
return render(request, "setup/interval_list.html", locals())
@login_required()
@permission_verify()
def job_interval_add(request):
temp_name = "setup/setup-header.html"
if request.method == "POST":
a_form = IntervalForm(request.POST)
if a_form.is_valid():
a_form.save()
tips = u"增加成功!"
display_control = ""
return redirect("/setup/job/interval/list/")
else:
tips = u"增加失败!"
display_control = ""
return render(request, "setup/interval_add.html", locals())
else:
display_control = "none"
a_form = IntervalForm()
return render(request, "setup/interval_add.html", locals())
@login_required()
@permission_verify()
def job_crontab_list(request):
temp_name = "setup/setup-header.html"
crontab_info = CrontabSchedule.objects.all()
return render(request, 'setup/crontab_list.html', locals())
@login_required
@permission_verify()
def job_crontab_edit(request, ids):
status = 0
obj = get_object(CrontabSchedule, id=ids)
if request.method == 'POST':
form = CrontabForm(request.POST, instance=obj)
if form.is_valid():
form.save()
status = 1
else:
status = 2
else:
form = CrontabForm(instance=obj)
return render(request, 'setup/crontab_edit.html', locals())
@login_required
@permission_verify()
def job_crontab_del(request):
temp_name = "setup/setup-header.html"
if request.method == 'POST':
crontabs = request.POST.getlist('idc_check', [])
if crontabs:
for n in crontabs:
CrontabSchedule.objects.filter(id=n).delete()
crontab_info = CrontabSchedule.objects.all()
return render(request, "setup/crontab_list.html", locals())
@login_required()
@permission_verify()
def job_crontab_add(request):
temp_name = "setup/setup-header.html"
if request.method == "POST":
a_form = CrontabForm(request.POST)
if a_form.is_valid():
a_form.save()
tips = u"增加成功!"
display_control = ""
return redirect("/setup/job/crontab/list/")
else:
tips = u"增加失败!"
display_control = ""
return render(request, "setup/crontab_add.html", locals())
else:
display_control = "none"
a_form = CrontabForm()
return render(request, "setup/crontab_add.html", locals())
@login_required()
@permission_verify()
def job_result_list(request):
temp_name = "setup/setup-header.html"
result_info = TaskResult.objects.all().order_by("-id")
return render(request, 'setup/result_list.html', locals())
@login_required
@permission_verify()
def job_result_edit(request, ids):
status = 0
obj = get_object(TaskResult, id=ids)
if request.method == 'POST':
form = TaskResultForm(request.POST, instance=obj)
if form.is_valid():
form.save()
status = 1
else:
status = 2
else:
form = TaskResultForm(instance=obj)
return render(request, 'setup/result_edit.html', locals())
@login_required
@permission_verify()
def job_result_del(request):
temp_name = "setup/setup-header.html"
if request.method == 'POST':
results = request.POST.getlist('idc_check', [])
if results:
for n in results:
TaskResult.objects.filter(id=n).delete()
result_info = TaskResult.objects.all()
return render(request, "setup/result_list.html", locals())
@login_required
@permission_verify()
def job_backend(request):
temp_name = "setup/setup-header.html"
celery_file = os.path.exists('/var/opt/adminset/pid/w1.pid')
beat_file = os.path.exists('/var/opt/adminset/pid/beat.pid')
if celery_file:
celery_disable = "disabled"
celery_stop_disable = ""
else:
celery_disable = ""
celery_stop_disable = "disabled"
if beat_file:
beat_disable = "disabled"
beat_stop_disable = ""
else:
beat_disable = ""
beat_stop_disable = "disabled"
return render(request, "setup/job_backend.html", locals())
@login_required
@permission_verify()
def job_backend_task(request, name, action):
cmd = "service "+name+" "+action
Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
time.sleep(3)
return redirect("/setup/job/backend/")