mirror of https://github.com/openspug/spug
A api update
parent
8213e5d67c
commit
c5440c3687
|
@ -13,8 +13,13 @@ class DeployRequest(models.Model, ModelMixin):
|
||||||
('2', '发布中'),
|
('2', '发布中'),
|
||||||
('3', '发布成功'),
|
('3', '发布成功'),
|
||||||
)
|
)
|
||||||
|
TYPES = (
|
||||||
|
('1', '正常发布'),
|
||||||
|
('2', '回滚')
|
||||||
|
)
|
||||||
app = models.ForeignKey(App, on_delete=models.CASCADE)
|
app = models.ForeignKey(App, on_delete=models.CASCADE)
|
||||||
name = models.CharField(max_length=50)
|
name = models.CharField(max_length=50)
|
||||||
|
type = models.CharField(max_length=2, choices=TYPES, default='1')
|
||||||
extra = models.TextField()
|
extra = models.TextField()
|
||||||
host_ids = models.TextField()
|
host_ids = models.TextField()
|
||||||
desc = models.CharField(max_length=255, null=True)
|
desc = models.CharField(max_length=255, null=True)
|
||||||
|
|
|
@ -24,7 +24,8 @@ def deploy_dispatch(request, req, token):
|
||||||
TASK_ID=str(req.id),
|
TASK_ID=str(req.id),
|
||||||
ENV_ID=str(req.app.env_id),
|
ENV_ID=str(req.app.env_id),
|
||||||
ENV_KEY=req.app.env.key,
|
ENV_KEY=req.app.env.key,
|
||||||
VERSION=req.version
|
VERSION=req.version,
|
||||||
|
DEPLOY_TYPE=req.type
|
||||||
)
|
)
|
||||||
if req.app.extend == '1':
|
if req.app.extend == '1':
|
||||||
env.update(json.loads(req.app.extend_obj.custom_envs))
|
env.update(json.loads(req.app.extend_obj.custom_envs))
|
||||||
|
@ -50,6 +51,7 @@ def _ext1_deploy(request, req, helper, env):
|
||||||
else:
|
else:
|
||||||
tree_ish = extras[1]
|
tree_ish = extras[1]
|
||||||
env.update(TAG=extras[1])
|
env.update(TAG=extras[1])
|
||||||
|
if req.type == '1':
|
||||||
helper.local(f'cd {REPOS_DIR} && rm -rf {req.app_id}_*')
|
helper.local(f'cd {REPOS_DIR} && rm -rf {req.app_id}_*')
|
||||||
helper.send_step('local', 1, '完成\r\n')
|
helper.send_step('local', 1, '完成\r\n')
|
||||||
|
|
||||||
|
@ -99,6 +101,7 @@ def _deploy_host(helper, h_id, extend, env):
|
||||||
code, _ = ssh.exec_command(f'mkdir -p {extend.dst_repo} && [ -e {extend.dst_dir} ] && [ ! -L {extend.dst_dir} ]')
|
code, _ = ssh.exec_command(f'mkdir -p {extend.dst_repo} && [ -e {extend.dst_dir} ] && [ ! -L {extend.dst_dir} ]')
|
||||||
if code == 0:
|
if code == 0:
|
||||||
helper.send_error(host.id, f'please make sure the {extend.dst_dir!r} is not exists.')
|
helper.send_error(host.id, f'please make sure the {extend.dst_dir!r} is not exists.')
|
||||||
|
if env.DEPLOY_TYPE == '1':
|
||||||
# clean
|
# clean
|
||||||
clean_command = f'ls -rd {env.APP_ID}_* | tail -n +{extend.versions + 1} | xargs rm -rf'
|
clean_command = f'ls -rd {env.APP_ID}_* | tail -n +{extend.versions + 1} | xargs rm -rf'
|
||||||
helper.remote(host.id, ssh, f'cd {extend.dst_repo} && rm -rf {env.VERSION} && {clean_command}')
|
helper.remote(host.id, ssh, f'cd {extend.dst_repo} && rm -rf {env.VERSION} && {clean_command}')
|
||||||
|
|
|
@ -58,17 +58,56 @@ class RequestView(View):
|
||||||
DeployRequest.objects.create(created_by=request.user, **form)
|
DeployRequest.objects.create(created_by=request.user, **form)
|
||||||
return json_response(error=error)
|
return json_response(error=error)
|
||||||
|
|
||||||
|
def put(self, request):
|
||||||
|
form, error = JsonParser(
|
||||||
|
Argument('id', type=int, help='缺少必要参数'),
|
||||||
|
Argument('action', filter=lambda x: x in ('check', 'do'), help='参数错误')
|
||||||
|
).parse(request.body)
|
||||||
|
if error is None:
|
||||||
|
req = DeployRequest.objects.filter(pk=form.id).first()
|
||||||
|
if not req:
|
||||||
|
return json_response(error='未找到指定发布申请')
|
||||||
|
pre_req = DeployRequest.objects.filter(app_id=req.app_id, id__lt=req.id, status='3').first()
|
||||||
|
if not pre_req:
|
||||||
|
return json_response(error='未找到该应用可以用于回滚的版本')
|
||||||
|
if form.action == 'check':
|
||||||
|
return json_response({'date': pre_req.created_at, 'name': pre_req.name})
|
||||||
|
DeployRequest.objects.create(
|
||||||
|
app_id=req.app_id,
|
||||||
|
name=f'{req.name} - 回滚',
|
||||||
|
type='2',
|
||||||
|
extra=pre_req.extra,
|
||||||
|
host_ids=req.host_ids,
|
||||||
|
status='0',
|
||||||
|
desc='自动回滚至该应用的上个版本',
|
||||||
|
version=pre_req.version,
|
||||||
|
created_by=request.user
|
||||||
|
)
|
||||||
|
return json_response(error=error)
|
||||||
|
|
||||||
|
def delete(self, request):
|
||||||
|
form, error = JsonParser(
|
||||||
|
Argument('id', type=int, help='缺少必要参数')
|
||||||
|
).parse(request.body)
|
||||||
|
if error is None:
|
||||||
|
DeployRequest.objects.filter(pk=form.id, status__in=('0', '1', '-1')).delete()
|
||||||
|
return json_response(error=error)
|
||||||
|
|
||||||
|
|
||||||
class RequestDetailView(View):
|
class RequestDetailView(View):
|
||||||
def get(self, request, r_id):
|
def get(self, request, r_id):
|
||||||
req = DeployRequest.objects.filter(pk=r_id).first()
|
req = DeployRequest.objects.filter(pk=r_id).first()
|
||||||
if not req:
|
if not req:
|
||||||
return json_response(error='为找到指定发布申请')
|
return json_response(error='为找到指定发布申请')
|
||||||
|
hosts = Host.objects.filter(id__in=json.loads(req.host_ids))
|
||||||
|
targets = [{'id': x.id, 'title': f'{x.name}({x.hostname}:{x.port})'} for x in hosts]
|
||||||
return json_response({
|
return json_response({
|
||||||
'app_name': req.app.name,
|
'app_name': req.app.name,
|
||||||
'env_name': req.app.env.name,
|
'env_name': req.app.env.name,
|
||||||
'status': req.status,
|
'status': req.status,
|
||||||
'status_alias': req.get_status_display()
|
'type': req.type,
|
||||||
|
'status_alias': req.get_status_display(),
|
||||||
|
'targets': targets
|
||||||
})
|
})
|
||||||
|
|
||||||
def post(self, request, r_id):
|
def post(self, request, r_id):
|
||||||
|
@ -81,13 +120,12 @@ class RequestDetailView(View):
|
||||||
token = uuid.uuid4().hex
|
token = uuid.uuid4().hex
|
||||||
outputs = {str(x.id): {'data': ''} for x in hosts}
|
outputs = {str(x.id): {'data': ''} for x in hosts}
|
||||||
outputs.update(local={'data': f'{human_time()} 建立接连... '})
|
outputs.update(local={'data': f'{human_time()} 建立接连... '})
|
||||||
targets = [{'id': x.id, 'title': f'{x.name}({x.hostname}:{x.port})'} for x in hosts]
|
|
||||||
req.status = '2'
|
req.status = '2'
|
||||||
if not req.version:
|
if not req.version:
|
||||||
req.version = f'{req.app_id}_{req.id}_{datetime.now().strftime("%Y%m%d%H%M%S")}'
|
req.version = f'{req.app_id}_{req.id}_{datetime.now().strftime("%Y%m%d%H%M%S")}'
|
||||||
req.save()
|
req.save()
|
||||||
Thread(target=deploy_dispatch, args=(request, req, token)).start()
|
Thread(target=deploy_dispatch, args=(request, req, token)).start()
|
||||||
return json_response({'token': token, 'outputs': outputs, 'targets': targets})
|
return json_response({'token': token, 'type': req.type, 'outputs': outputs})
|
||||||
|
|
||||||
def patch(self, request, r_id):
|
def patch(self, request, r_id):
|
||||||
form, error = JsonParser(
|
form, error = JsonParser(
|
||||||
|
|
|
@ -58,7 +58,7 @@ class SSH:
|
||||||
chan.settimeout(timeout)
|
chan.settimeout(timeout)
|
||||||
chan.set_combine_stderr(True)
|
chan.set_combine_stderr(True)
|
||||||
if environment:
|
if environment:
|
||||||
str_env = ' '.join(f'{k}={v}' for k, v in environment.items())
|
str_env = ' '.join(f"{k}='{v}'" for k, v in environment.items())
|
||||||
command = f'export {str_env} && {command}'
|
command = f'export {str_env} && {command}'
|
||||||
chan.exec_command(command)
|
chan.exec_command(command)
|
||||||
out = chan.makefile("r", -1)
|
out = chan.makefile("r", -1)
|
||||||
|
@ -70,7 +70,7 @@ class SSH:
|
||||||
chan.settimeout(timeout)
|
chan.settimeout(timeout)
|
||||||
chan.set_combine_stderr(True)
|
chan.set_combine_stderr(True)
|
||||||
if environment:
|
if environment:
|
||||||
str_env = ' '.join(f'{k}={v}' for k, v in environment.items())
|
str_env = ' '.join(f"{k}='{v}'" for k, v in environment.items())
|
||||||
command = f'export {str_env} && {command}'
|
command = f'export {str_env} && {command}'
|
||||||
chan.exec_command(command)
|
chan.exec_command(command)
|
||||||
stdout = chan.makefile("r", -1)
|
stdout = chan.makefile("r", -1)
|
||||||
|
|
Loading…
Reference in New Issue