实验室上机CODE自动化

背景

官方仅提供code脚本,需要ssh至集群登陆节点,执行该code脚本,来生成上机code,对于实验室人员操作,不是很方便!

upload successful

优化

upload successful
upload successful

CODE
from django.shortcuts import render, HttpResponse
from datetime import datetime
from django.views.generic.base import View
from .models import Company, Level, DataPath, DistributePath, Owner, Group, Device, ProjectInfo
from .forms import RegisterForm
from utils.GenerateNumber import gene_random_number
import json
import time
from utils.ansible_api_adhoc import ANSRunner
from utils.email_send import SendMail
# Create your views here.


class LabIndexView(View):
def get(self, request):
current_year = datetime.now().year
all_companys = Company.objects.exclude(name='root')
all_levels = Level.objects.all()
all_data_paths = DataPath.objects.all()
all_distribute_paths = DistributePath.objects.all()
all_owners = Owner.objects.all()
all_groups = Group.objects.all()
all_devices = Device.objects.all()
return render(request, "lab-index.html", {
'current_year': current_year,
'all_companys': all_companys,
'all_levels': all_levels,
'all_data_paths': all_data_paths,
'all_distribute_paths': all_distribute_paths,
'all_owners': all_owners,
'all_groups': all_groups,
'all_devices': all_devices
})

def post(self, request):
current_year = datetime.now().year
register_form = RegisterForm(request.POST)
if register_form.is_valid():
if request.META.has_key('HTTP_X_FORWARDED_FOR'):
ip = request.META['HTTP_X_FORWARDED_FOR']
else:
ip = request.META['REMOTE_ADDR']
company = request.POST.get('company', '')
level = request.POST.get('level', '')
data_path = request.POST.get('data_path', '')
distribute_path = request.POST.get('distribute_path', '')
owner = request.POST.get('owner', '')
group = request.POST.get('group', '')
device = request.POST.get('device', '')
code = gene_random_number()

info_join = []
for i in code, company, level, distribute_path, owner, group, device, data_path:
info_join.append(i)
info_join = ','.join(info_join)
# /YZGROUP/read/read.txt
job = ANSRunner()
job.run_model(host_list='20.0.0.10', module_name='shell',
module_args='echo {0} >> /YZGROUP/read/.{1}.sortlog'.format(info_join, code))
res = job.get_model_result()

if res['success']:
data = {
'company': company,
'level': level,
'data_path': data_path,
'distribute_path': distribute_path,
'owner': owner,
'group': group,
'device': device,
'code': code,
'email': ['sen.guo@genewiz.com.cn','fang.xiao@genewiz.com.cn','ruikai.jia@genewiz.com.cn','jianhu.yong@genewiz.com.cn'],
#'email': ['zongliang.guo@geneseeq.com', '453576956@qq.com'],
'ip': ip,
'current_year': current_year
}
if SendMail(data, send_type='lab_code'):
ProjectInfo.objects.create(
number=code,
company=Company.objects.get(name=company),
level=Level.objects.get(name=level),
distribute_path=DistributePath.objects.get(path=distribute_path),
owner=Owner.objects.get(name=owner),
group=Group.objects.get(name=group),
device=Device.objects.get(type=device),
data_path=DataPath.objects.get(path=data_path)
)
return render(request, 'lab-success.html', {
'status': 'success',
'company': company,
'level': level,
'data_path': data_path,
'distribute_path': distribute_path,
'owner': owner,
'group': group,
'device': device,
'code': code,
'ip': ip,
'time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
'current_year': current_year
})
else:
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
return HttpResponse(json.dumps(register_form.errors, indent=4, ensure_ascii=False), content_type='application/json')
0%