parent
cc21d84be8
commit
07187ad064
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
import os
|
||||
import uuid
|
||||
|
||||
import threading
|
||||
import requests
|
||||
from django.core.files.uploadedfile import InMemoryUploadedFile
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
@ -59,15 +59,12 @@ class HostModelViewSet(GenericViewSet,
|
|||
self.perform_create(create_serializer)
|
||||
instance = create_serializer.instance
|
||||
# 检查输入client部署命令 更新host状态
|
||||
self._client_deploy_cmd_check(instance)
|
||||
|
||||
self.client_deploy_cmd_execute(instance, 'init')
|
||||
host_list_serializer = serializer.HostListSerializer(instance=instance)
|
||||
return success(result=host_list_serializer.data)
|
||||
|
||||
def perform_create(self, ser):
|
||||
client_deploy_cmd = settings.CLIENT_DEPLOY_CMD
|
||||
ser.save(created_by=self.request.user,
|
||||
client_deploy_cmd=client_deploy_cmd)
|
||||
ser.save(created_by=self.request.user)
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
instance = self.check_instance_exist(request, *args, **kwargs)
|
||||
|
@ -86,9 +83,15 @@ class HostModelViewSet(GenericViewSet,
|
|||
instance = self.check_instance_exist(request, *args, **kwargs)
|
||||
if not instance:
|
||||
return not_found()
|
||||
super().destroy(request, *args, **kwargs)
|
||||
self.client_deploy_cmd_execute(instance, 'delete')
|
||||
self.perform_destroy(instance)
|
||||
return success(message="删除成功", code=200, result={})
|
||||
|
||||
def perform_destroy(self, instance: HostModel):
|
||||
instance.deleted_at = human_datetime()
|
||||
instance.deleted_by = self.request.user
|
||||
instance.save()
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
response = super().update(request, *args, **kwargs)
|
||||
return success(result=response.data, message="修改成功")
|
||||
|
@ -97,26 +100,37 @@ class HostModelViewSet(GenericViewSet,
|
|||
instance = self.get_queryset().filter(**kwargs).first()
|
||||
return instance if instance else None
|
||||
|
||||
@staticmethod
|
||||
def _client_deploy_cmd_check(instance: HostModel):
|
||||
task_id = uuid_8()
|
||||
job_instance = JobModel.objects.create(
|
||||
command=instance.client_deploy_cmd,
|
||||
task_id=task_id,
|
||||
created_by=instance.created_by
|
||||
)
|
||||
commands = [
|
||||
{"instance": instance.ip,
|
||||
"cmd": instance.client_deploy_cmd
|
||||
}]
|
||||
job = SshJob(
|
||||
commands,
|
||||
job_instance,
|
||||
update_host_status=True
|
||||
)
|
||||
sche = BackgroundScheduler()
|
||||
sche.add_job(job.run, )
|
||||
sche.start()
|
||||
def client_deploy_cmd_execute(self, instance, exec):
|
||||
if exec == 'init':
|
||||
thread = threading.Thread(target=self.client_deploy_cmd_init, args=(instance,))
|
||||
thread.start()
|
||||
if exec == 'delete':
|
||||
thread = threading.Thread(target=self.client_deploy_cmd_delete, args=(instance,))
|
||||
thread.start()
|
||||
|
||||
def client_deploy_cmd_init(self, instance):
|
||||
url = settings.INIT_SERVER + "api/v1/tasks/"
|
||||
print("url is :%s" % url)
|
||||
data = {"service_name": "node_init",
|
||||
"instance": instance.ip,
|
||||
"update_host_status": True
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': "application/json"
|
||||
}
|
||||
data = json.dumps(data)
|
||||
requests.post(url=url, data=data, headers=headers)
|
||||
|
||||
def client_deploy_cmd_delete(self, instance):
|
||||
url = settings.INIT_SERVER + "api/v1/tasks/"
|
||||
data = {"service_name": "node_delete",
|
||||
"instance": instance.ip
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': "application/json"
|
||||
}
|
||||
data = json.dumps(data)
|
||||
requests.post(url=url, data=data, headers=headers)
|
||||
|
||||
|
||||
class ClusterViewSet(GenericViewSet,
|
||||
|
|
|
@ -54,6 +54,7 @@ class TaskAPIView(GenericViewSet,
|
|||
data = request.data
|
||||
params = data.copy()
|
||||
service_name = data.pop("service_name", None)
|
||||
update_host_status = data.pop("update_host_status", False)
|
||||
task_id = uuid_8()
|
||||
if service_name:
|
||||
SCRIPTS_DIR = settings.SCRIPTS_DIR
|
||||
|
@ -66,7 +67,8 @@ class TaskAPIView(GenericViewSet,
|
|||
resp_scripts = resp.get("commands")
|
||||
username = "admin"
|
||||
user = User.objects.filter(username=username).first()
|
||||
self.ssh_job(resp_scripts, task_id, user, json.dumps(params))
|
||||
self.ssh_job(resp_scripts, task_id, user, json.dumps(params), update_host_status=update_host_status,
|
||||
service_name=service_name)
|
||||
return success(result={"instance_id": task_id})
|
||||
else:
|
||||
return self.default_ssh_job(data, task_id)
|
||||
|
@ -101,14 +103,14 @@ class TaskAPIView(GenericViewSet,
|
|||
logger.error(e)
|
||||
return other_response(message=str(e), code=400, success=False)
|
||||
|
||||
def ssh_job(self, resp_scripts, task_id, user, data=None):
|
||||
def ssh_job(self, resp_scripts, task_id, user, data=None, **kwargs):
|
||||
if not data:
|
||||
job_model = JobModel.objects.create(command=resp_scripts, task_id=task_id,
|
||||
created_by=user)
|
||||
else:
|
||||
job_model = JobModel.objects.create(command=resp_scripts, task_id=task_id,
|
||||
created_by=user, params=data)
|
||||
sch_job = SshJob(resp_scripts, job_model)
|
||||
sch_job = SshJob(resp_scripts, job_model, **kwargs)
|
||||
scheduler.add_job(sch_job.run)
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
|
|
|
@ -37,7 +37,7 @@ MIDDLEWARE = [
|
|||
]
|
||||
|
||||
DEBUG = True
|
||||
|
||||
INIT_SERVER = 'http://127.0.0.1:8001/'
|
||||
|
||||
# Mysql数据库
|
||||
DATABASES = {
|
||||
|
@ -109,7 +109,6 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
|
|||
SCRIPTS_DIR = os.path.join(BASE_DIR, 'service_scripts')
|
||||
|
||||
SERVER_IP = get_ip_address()
|
||||
CLIENT_DEPLOY_CMD = 'ls /root'
|
||||
|
||||
SERVER_LOGS_FILE = os.path.join(BASE_DIR, 'logs', 'sys_om_info.log')
|
||||
ERROR_LOGS_FILE = os.path.join(BASE_DIR, 'logs', 'sys_om_error.log')
|
||||
|
|
|
@ -5,3 +5,4 @@ from .common import *
|
|||
'''
|
||||
|
||||
DEBUG = False
|
||||
INIT_SERVER = 'http://127.0.0.1:7001/'
|
|
@ -31,6 +31,11 @@ class SshJob:
|
|||
ssh_cli = SSH(host.ip, host.port, host.username, host.private_key)
|
||||
with ssh_cli as ssh:
|
||||
status, result = ssh.exec_command(cmd)
|
||||
if self.kwargs.get('update_host_status', None):
|
||||
host.status = status if status == 0 else 1
|
||||
host.save()
|
||||
if self.kwargs.get('service_name', None) == "node_delete":
|
||||
host.delete()
|
||||
if str(status) != '0':
|
||||
update_job(instance=self.job, status="Fail", result=result, host_by=host_ips)
|
||||
break
|
||||
|
@ -48,9 +53,6 @@ class SshJob:
|
|||
output = os.popen(command)
|
||||
result = output.read()
|
||||
update_job(instance=self.job, status="Success", result=result, host_by=host_ips)
|
||||
if self.kwargs.get('update_host_status', None):
|
||||
host.status = status if status == 0 else 1
|
||||
host.save()
|
||||
except socket.timeout:
|
||||
update_job(instance=self.job, status="Fail", result="socket time out")
|
||||
except Exception as e:
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/bash
|
||||
|
||||
host_ip=$1 #本行内容必须存在且不能改动,以便接收参数
|
||||
|
||||
# 以下内容可以自定义
|
||||
arch=$(uname -m)
|
||||
res(){
|
||||
local status=$1
|
||||
local msg=$2
|
||||
[[ $status -eq 0 ]] && echo -e "$host_ip $status delete $msg [OK]" >> result || echo -e "$host_ip $status delete $msg [FAILED]" >> result
|
||||
}
|
||||
|
||||
check_version(){
|
||||
version=`cat /etc/alios-version | grep AliOS_version`
|
||||
echo -e "${version}"
|
||||
}
|
||||
check_version
|
||||
|
||||
rm -f result
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/bash
|
||||
|
||||
host_ip=$1 #本行内容必须存在且不能改动,以便接收参数
|
||||
|
||||
# 以下内容可以自定义
|
||||
arch=$(uname -m)
|
||||
res(){
|
||||
local status=$1
|
||||
local msg=$2
|
||||
[[ ${status} -eq 0 ]] && echo -e "${host_ip} ${msg} [OK]" >> result || echo -e "${host_ip} ${msg} [FAILED]" >> result
|
||||
}
|
||||
|
||||
check_aliosrepo(){
|
||||
let ret=0
|
||||
[[ $(md5sum /etc/yum.repos.d/alios.repo | awk '{ print $1 }') == "8abd867ce49c0f128d457ef741bb73f3" ]] || ((ret++))
|
||||
[[ $(stat -c %a /etc/yum.repos.d/alios.repo) == "644" ]] || ((ret++))
|
||||
res $ret $FUNCNAME
|
||||
}
|
||||
check_aliosrepo
|
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
|
||||
arg = json.loads(sys.argv[1])
|
||||
host_ip = arg.get('instance')
|
||||
path = os.getcwd()
|
||||
path_init = os.path.join(path, "service_scripts", "delete.sh")
|
||||
with open(path_init, 'r') as f:
|
||||
src_command = f.read()
|
||||
command = src_command.replace('$1', host_ip, 1)
|
||||
result = {
|
||||
'commands': [
|
||||
{
|
||||
'instance': host_ip,
|
||||
'cmd': command,
|
||||
}
|
||||
]
|
||||
}
|
||||
print(result)
|
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
|
||||
arg = json.loads(sys.argv[1])
|
||||
host_ip = arg.get('instance')
|
||||
path = os.getcwd()
|
||||
path_init = os.path.join(path, "service_scripts", "init.sh")
|
||||
with open(path_init, 'r') as f:
|
||||
src_command = f.read()
|
||||
command = src_command.replace('$1', host_ip, 1)
|
||||
result = {
|
||||
'commands': [
|
||||
{
|
||||
'instance': host_ip,
|
||||
'cmd': command,
|
||||
}
|
||||
]
|
||||
}
|
||||
print(result)
|
Loading…
Reference in New Issue