Skip to content

Commit

Permalink
add schedule task feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lijie3721 committed Feb 15, 2017
1 parent ccd16ea commit 332dae6
Show file tree
Hide file tree
Showing 54 changed files with 666 additions and 17 deletions.
12 changes: 11 additions & 1 deletion CrazyEye/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
#for celery
from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']


import pymysql
pymysql.install_as_MySQLdb()
pymysql.install_as_MySQLdb()
23 changes: 23 additions & 0 deletions CrazyEye/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#_*_coding:utf-8_*_
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'CrazyEye.settings')

app = Celery('CrazyEye')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
9 changes: 8 additions & 1 deletion CrazyEye/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os,datetime

import logging
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


Expand Down Expand Up @@ -43,6 +43,7 @@
'bernard',
'session_security',
'kingadmin',
'django_celery_beat',


)
Expand Down Expand Up @@ -166,4 +167,10 @@
SSH_CLIENT_PATH = '/usr/local/openssh7/bin/ssh'

SESSION_AUDIT_LOG_DIR = '%s/logs/audit' % BASE_DIR
SCHEDULE_LOG_DIR = '%s/logs/bernard/plan_logs' % BASE_DIR

LOG_LEVEL = logging.DEBUG

#for celery
CELERY_BROKER_URL = 'redis://localhost'
CELERY_RESULT_BACKEND = 'redis://localhost'
1 change: 1 addition & 0 deletions CrazyEye/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

url(r'^login/$',views.login,name='login'),
url(r'^accounts/profile/$',views.personal),
url(r'^task/',include("bernard.urls")),
#url(r'^account/(\d+)/password/change/$',views.password_reset_form, name="password_change_form"),
# url(r'^configure/index/$',views.configure_index,name="table_index"),#显示所有注册的表
# url(r'^configure/(\w+)/$',views.configure_url_dispatch,name="table_list"), #显示每个表的数据
Expand Down
15 changes: 14 additions & 1 deletion bernard/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from django.contrib import admin

from bernard import models
# Register your models here.


class SchedulLogAdmin(admin.ModelAdmin):
list_display = ('plan','status','start_date','end_date')


#admin.site.register(models.Schedule)
admin.site.register(models.Plan)
admin.site.register(models.Stage)
admin.site.register(models.Job)
admin.site.register(models.SCPTask)
admin.site.register(models.SSHTask)
admin.site.register(models.ScheduleLog,SchedulLogAdmin)
7 changes: 4 additions & 3 deletions bernard/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-01-25 09:01
# Generated by Django 1.10.2 on 2017-02-09 08:41
from __future__ import unicode_literals

from django.db import migrations, models
Expand All @@ -11,7 +11,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('web', '0019_auto_20161230_1833'),
('django_celery_beat', '0001_initial'),
('web', '__first__'),
]

operations = [
Expand All @@ -38,8 +39,8 @@ class Migration(migrations.Migration):
name='Schedule',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField()),
('plan', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bernard.Plan')),
('schedule', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_celery_beat.PeriodicTask')),
],
),
migrations.CreateModel(
Expand Down
26 changes: 26 additions & 0 deletions bernard/migrations/0002_auto_20170209_1743.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-02-09 09:43
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('bernard', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='schedule',
name='plan',
),
migrations.RemoveField(
model_name='schedule',
name='schedule',
),
migrations.DeleteModel(
name='Schedule',
),
]
26 changes: 26 additions & 0 deletions bernard/migrations/0003_auto_20170210_1526.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-02-10 07:26
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('bernard', '0002_auto_20170209_1743'),
]

operations = [
migrations.AlterField(
model_name='scptask',
name='job',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='bernard.Job'),
),
migrations.AlterField(
model_name='sshtask',
name='job',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='bernard.Job'),
),
]
25 changes: 25 additions & 0 deletions bernard/migrations/0004_schedulelog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-02-14 10:15
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('bernard', '0003_auto_20170210_1526'),
]

operations = [
migrations.CreateModel(
name='ScheduleLog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status', models.BooleanField(choices=[(0, 'failed'), (1, 'success'), (2, 'error'), (3, 'running')])),
('date', models.DateTimeField(auto_now_add=True)),
('plan', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bernard.Plan')),
],
),
]
20 changes: 20 additions & 0 deletions bernard/migrations/0005_auto_20170215_1016.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-02-15 02:16
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bernard', '0004_schedulelog'),
]

operations = [
migrations.AlterField(
model_name='schedulelog',
name='status',
field=models.SmallIntegerField(choices=[(0, 'failed'), (1, 'success'), (2, 'error'), (3, 'running')]),
),
]
30 changes: 30 additions & 0 deletions bernard/migrations/0006_auto_20170215_1442.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-02-15 06:42
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bernard', '0005_auto_20170215_1016'),
]

operations = [
migrations.RenameField(
model_name='schedulelog',
old_name='date',
new_name='start_date',
),
migrations.AddField(
model_name='schedulelog',
name='end_date',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='schedulelog',
name='errors',
field=models.TextField(blank=True, null=True),
),
]
25 changes: 17 additions & 8 deletions bernard/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from django.db import models
from web.models import BindHosts,HostGroups
from django_celery_beat.models import PeriodicTask
# Create your models here.



class Schedule(models.Model):
"""time sheets"""
plan = models.ForeignKey("Plan")
date = models.DateTimeField()


class Plan(models.Model):
Expand Down Expand Up @@ -44,12 +41,12 @@ class Job(models.Model):
date = models.DateTimeField(auto_now_add=True)

def __str__(self):
return "%s job:%s"%(self.stage, self.name)
return "order:%s, stage:%s, job:%s"%(self.order,self.stage, self.name)


class SSHTask(models.Model):
"""shell script"""
job = models.ForeignKey("Job")
job = models.OneToOneField("Job")
bind_hosts = models.ManyToManyField(BindHosts,blank=True)
host_groups = models.ManyToManyField(HostGroups,blank=True)
commands = models.TextField(verbose_name="ssh commands")
Expand All @@ -60,11 +57,23 @@ def __str__(self):

class SCPTask(models.Model):
"""file transfer by scp command"""
job = models.ForeignKey("Job")
job = models.OneToOneField("Job")
bind_hosts = models.ManyToManyField(BindHosts, blank=True)
host_groups = models.ManyToManyField(HostGroups, blank=True)
local_path = models.CharField(max_length=128)
remote_path = models.CharField(max_length=128)

def __str__(self):
return "%s %s"%(self.local_path,self.remote_path)
return "%s %s"%(self.local_path,self.remote_path)


class ScheduleLog(models.Model):
"""Store Schedule run logs """
plan = models.ForeignKey("Plan")
status_choices = ((0,'failed'),(1,'success'),(2,'error'),(3,'running'))
status = models.SmallIntegerField(choices=status_choices)
errors = models.TextField(blank=True,null=True)
start_date = models.DateTimeField(auto_now_add=True)
end_date = models.DateTimeField(auto_now=True)


Loading

0 comments on commit 332dae6

Please sign in to comment.