mirror of https://github.com/openspug/spug
A api update
parent
8213e5d67c
commit
c5440c3687
|
@ -13,8 +13,13 @@ class DeployRequest(models.Model, ModelMixin):
|
|||
('2', '发布中'),
|
||||
('3', '发布成功'),
|
||||
)
|
||||
TYPES = (
|
||||
('1', '正常发布'),
|
||||
('2', '回滚')
|
||||
)
|
||||
app = models.ForeignKey(App, on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=50)
|
||||
type = models.CharField(max_length=2, choices=TYPES, default='1')
|
||||
extra = models.TextField()
|
||||
host_ids = models.TextField()
|
||||
desc = models.CharField(max_length=255, null=True)
|
||||
|
|
|
@ -24,7 +24,8 @@ def deploy_dispatch(request, req, token):
|
|||
TASK_ID=str(req.id),
|
||||
ENV_ID=str(req.app.env_id),
|
||||
ENV_KEY=req.app.env.key,
|
||||
VERSION=req.version
|
||||
VERSION=req.version,
|
||||
DEPLOY_TYPE=req.type
|
||||
)
|
||||
if req.app.extend == '1':
|
||||
env.update(json.loads(req.app.extend_obj.custom_envs))
|
||||
|
@ -50,32 +51,33 @@ def _ext1_deploy(request, req, helper, env):
|
|||
else:
|
||||
tree_ish = extras[1]
|
||||
env.update(TAG=extras[1])
|
||||
helper.local(f'cd {REPOS_DIR} && rm -rf {req.app_id}_*')
|
||||
helper.send_step('local', 1, '完成\r\n')
|
||||
if req.type == '1':
|
||||
helper.local(f'cd {REPOS_DIR} && rm -rf {req.app_id}_*')
|
||||
helper.send_step('local', 1, '完成\r\n')
|
||||
|
||||
if extend.hook_pre_server:
|
||||
helper.send_step('local', 2, f'{human_time()} 检出前任务...\r\n')
|
||||
helper.local(f'cd /tmp && {extend.hook_pre_server}', env)
|
||||
if extend.hook_pre_server:
|
||||
helper.send_step('local', 2, f'{human_time()} 检出前任务...\r\n')
|
||||
helper.local(f'cd /tmp && {extend.hook_pre_server}', env)
|
||||
|
||||
helper.send_step('local', 3, f'{human_time()} 执行检出... ')
|
||||
git_dir = os.path.join(REPOS_DIR, str(app.id))
|
||||
command = f'cd {git_dir} && git archive --prefix={env.VERSION}/ {tree_ish} | (cd .. && tar xf -)'
|
||||
helper.local(command)
|
||||
helper.send_step('local', 3, '完成\r\n')
|
||||
helper.send_step('local', 3, f'{human_time()} 执行检出... ')
|
||||
git_dir = os.path.join(REPOS_DIR, str(app.id))
|
||||
command = f'cd {git_dir} && git archive --prefix={env.VERSION}/ {tree_ish} | (cd .. && tar xf -)'
|
||||
helper.local(command)
|
||||
helper.send_step('local', 3, '完成\r\n')
|
||||
|
||||
if extend.hook_post_server:
|
||||
helper.send_step('local', 4, f'{human_time()} 检出后任务...\r\n')
|
||||
helper.local(f'cd {os.path.join(REPOS_DIR, env.VERSION)} && {extend.hook_post_server}', env)
|
||||
if extend.hook_post_server:
|
||||
helper.send_step('local', 4, f'{human_time()} 检出后任务...\r\n')
|
||||
helper.local(f'cd {os.path.join(REPOS_DIR, env.VERSION)} && {extend.hook_post_server}', env)
|
||||
|
||||
helper.send_step('local', 5, f'\r\n{human_time()} 执行打包... ')
|
||||
filter_rule, exclude, contain = json.loads(extend.filter_rule), '', env.VERSION
|
||||
files = helper.parse_filter_rule(filter_rule['data'])
|
||||
if files:
|
||||
if filter_rule['type'] == 'exclude':
|
||||
exclude = ' '.join(f'--exclude={x}' for x in files)
|
||||
else:
|
||||
contain = ' '.join(f'{env.VERSION}/{x}' for x in files)
|
||||
helper.local(f'cd {REPOS_DIR} && tar zcf {env.VERSION}.tar.gz {exclude} {contain}')
|
||||
helper.send_step('local', 5, f'\r\n{human_time()} 执行打包... ')
|
||||
filter_rule, exclude, contain = json.loads(extend.filter_rule), '', env.VERSION
|
||||
files = helper.parse_filter_rule(filter_rule['data'])
|
||||
if files:
|
||||
if filter_rule['type'] == 'exclude':
|
||||
exclude = ' '.join(f'--exclude={x}' for x in files)
|
||||
else:
|
||||
contain = ' '.join(f'{env.VERSION}/{x}' for x in files)
|
||||
helper.local(f'cd {REPOS_DIR} && tar zcf {env.VERSION}.tar.gz {exclude} {contain}')
|
||||
helper.send_step('local', 6, f'完成')
|
||||
with futures.ThreadPoolExecutor(max_workers=min(16, os.cpu_count() + 4)) as executor:
|
||||
threads = []
|
||||
|
@ -99,18 +101,19 @@ 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} ]')
|
||||
if code == 0:
|
||||
helper.send_error(host.id, f'please make sure the {extend.dst_dir!r} is not exists.')
|
||||
# clean
|
||||
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}')
|
||||
# transfer files
|
||||
tar_gz_file = f'{env.VERSION}.tar.gz'
|
||||
try:
|
||||
ssh.put_file(os.path.join(REPOS_DIR, tar_gz_file), os.path.join(extend.dst_repo, tar_gz_file))
|
||||
except Exception as e:
|
||||
helper.send_error(host.id, f'exception: {e}')
|
||||
if env.DEPLOY_TYPE == '1':
|
||||
# clean
|
||||
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}')
|
||||
# transfer files
|
||||
tar_gz_file = f'{env.VERSION}.tar.gz'
|
||||
try:
|
||||
ssh.put_file(os.path.join(REPOS_DIR, tar_gz_file), os.path.join(extend.dst_repo, tar_gz_file))
|
||||
except Exception as e:
|
||||
helper.send_error(host.id, f'exception: {e}')
|
||||
|
||||
command = f'cd {extend.dst_repo} && tar xf {tar_gz_file} && rm -f {env.APP_ID}_*.tar.gz'
|
||||
helper.remote(host.id, ssh, command)
|
||||
command = f'cd {extend.dst_repo} && tar xf {tar_gz_file} && rm -f {env.APP_ID}_*.tar.gz'
|
||||
helper.remote(host.id, ssh, command)
|
||||
helper.send_step(h_id, 1, '完成\r\n')
|
||||
|
||||
# pre host
|
||||
|
|
|
@ -58,17 +58,56 @@ class RequestView(View):
|
|||
DeployRequest.objects.create(created_by=request.user, **form)
|
||||
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):
|
||||
def get(self, request, r_id):
|
||||
req = DeployRequest.objects.filter(pk=r_id).first()
|
||||
if not req:
|
||||
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({
|
||||
'app_name': req.app.name,
|
||||
'env_name': req.app.env.name,
|
||||
'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):
|
||||
|
@ -81,13 +120,12 @@ class RequestDetailView(View):
|
|||
token = uuid.uuid4().hex
|
||||
outputs = {str(x.id): {'data': ''} for x in hosts}
|
||||
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'
|
||||
if not req.version:
|
||||
req.version = f'{req.app_id}_{req.id}_{datetime.now().strftime("%Y%m%d%H%M%S")}'
|
||||
req.save()
|
||||
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):
|
||||
form, error = JsonParser(
|
||||
|
|
|
@ -58,7 +58,7 @@ class SSH:
|
|||
chan.settimeout(timeout)
|
||||
chan.set_combine_stderr(True)
|
||||
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}'
|
||||
chan.exec_command(command)
|
||||
out = chan.makefile("r", -1)
|
||||
|
@ -70,7 +70,7 @@ class SSH:
|
|||
chan.settimeout(timeout)
|
||||
chan.set_combine_stderr(True)
|
||||
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}'
|
||||
chan.exec_command(command)
|
||||
stdout = chan.makefile("r", -1)
|
||||
|
|
Loading…
Reference in New Issue