Skip to content

Commit

Permalink
add deploy by tag
Browse files Browse the repository at this point in the history
  • Loading branch information
guohongze committed Feb 13, 2018
1 parent 0f9720c commit 282a78d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 49 deletions.
10 changes: 10 additions & 0 deletions delivery/delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ def log2(request, project_id):
except IOError:
ret = "Program is Deploying Please waiting<br>"
return HttpResponse(ret)


@login_required()
@permission_verify()
def task_stop(request, project_id):
project = Delivery.objects.get(job_name_id=project_id)
project.bar_data = 0
project.status = False
project.save()
return HttpResponse("task stop ok")
1 change: 1 addition & 0 deletions delivery/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Meta:
widgets = {
'job_name': widgets.Select(attrs={'class': 'form-control','style': 'width:450px;'}),
'deploy_num': widgets.TextInput(attrs={'class': 'form-control', 'style': 'width:450px;', "readonly": "readonly"}),
'version': widgets.TextInput(attrs={'class': 'form-control', 'style': 'width:450px;'}),
'description': widgets.Textarea(attrs={'class': 'form-control', 'style': 'width:450px; height:100px'}),
'deploy_policy': widgets.Select(attrs={'class': 'form-control', 'style': 'width:450px;'}),
'shell': widgets.Textarea(attrs={'class': 'form-control', 'style': 'width:450px; height:100px'}),
Expand Down
5 changes: 3 additions & 2 deletions delivery/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

class Delivery(models.Model):
job_name = models.OneToOneField(Project, verbose_name=u"项目名")
deploy_num = models.IntegerField(verbose_name=u"当前部署次数", default=0)
description = models.CharField(max_length=255, verbose_name=u"描述", null=True, blank=True)
description = models.CharField(max_length=255, verbose_name=u"项目描述", null=True, blank=True)
deploy_policy = models.CharField(max_length=255, choices=DEPLOY_POLICY, verbose_name=u"部署策略")
version = models.CharField(max_length=255, verbose_name=u"版本信息", blank=True)
build_clean = models.BooleanField(verbose_name=u"清理构建", default=False)
shell = models.CharField(max_length=255, verbose_name=u"shell", blank=True)
shell_position = models.BooleanField(verbose_name=u"本地执行", default=False)
status = models.BooleanField(verbose_name=u"部署状态", default=False)
deploy_num = models.IntegerField(verbose_name=u"部署次数", default=0)
bar_data = models.IntegerField(default=0)
auth = models.ForeignKey(
AuthInfo, verbose_name=u"认证信息",
Expand Down
79 changes: 34 additions & 45 deletions delivery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@shared_task
def deploy(job_name, server_list, app_path, source_address, project_id, auth_info):
ret = []
cmd = ""
p1 = Delivery.objects.get(job_name_id=project_id)
job_workspace = "/var/opt/adminset/workspace/{0}/".format(job_name)
log_file = job_workspace + 'logs/deploy-' + str(p1.deploy_num) + ".log"
Expand All @@ -23,13 +24,15 @@ def deploy(job_name, server_list, app_path, source_address, project_id, auth_inf
p1.bar_data = 20
p1.save()
sleep(2)
if p1.build_clean:
if p1.build_clean or p1.version:
try:
shutil.rmtree("{0}code/".format(job_workspace))
except:
print "code dir is not exists, build clean over"

cmd = code_clone(job_workspace, auth_info, source_address, p1)
if p1.job_name.source_type == "git":
cmd = git_clone(job_workspace, auth_info, source_address, p1)
if p1.job_name.source_type == "svn":
cmd = svn_clone(job_workspace, auth_info, source_address, p1)
with open(log_file, 'w+') as f:
f.writelines(cmd)
data = cmd_exec(cmd)
Expand Down Expand Up @@ -75,51 +78,37 @@ def parser_url(source_address, url_len, user_len, auth_info, url_type=None):
return final_add


def code_clone(job_workspace, auth_info, source_address, p1):
def git_clone(job_workspace, auth_info, source_address, p1):
if os.path.exists("{0}code/.git".format(job_workspace)):
cmd = "cd {0}code/ && git pull".format(job_workspace)
return cmd
if os.path.exists("{0}code/.svn".format(job_workspace)):
cmd = "cd {0}code/ && svn update".format(job_workspace)
return cmd
if p1.job_name.source_type == "git" and not p1.job_name.source_address.startswith("http"):
if auth_info and p1.job_name.source_address.startswith("http"):
url_type = re.search(r'(@)', source_address)
if url_type:
user_len = len(auth_info["username"])
if source_address.startswith("https://"):
url_len = 8
else:
url_len = 7
source_address = parser_url(source_address, url_len, user_len, auth_info, url_type)
else:
if source_address.startswith("https://"):
url_len = 8
else:
url_len = 7
source_address = parser_url(source_address, url_len, auth_info, url_type)
if p1.version:
cmd = "git clone -b {2} {0} {1}code/".format(source_address, job_workspace, p1.version)
else:
cmd = "git clone {0} {1}code/".format(source_address, job_workspace)
return cmd
return cmd

if auth_info:
if p1.job_name.source_type == "svn":
cmd = "svn --non-interactive --trust-server-cert --username {2} --password {3} checkout {0} {1}code/".format(
source_address, job_workspace, auth_info["username"], auth_info["password"])
return cmd
if p1.job_name.source_type == "git" and p1.job_name.source_address.startswith("http"):
url_type = re.search(r'(@)', source_address)
if url_type:
user_len = len(auth_info["username"])
if source_address.startswith("https://"):
url_len = 8
source_address = parser_url(source_address, url_len, user_len, auth_info, url_type)
else:
url_len = 7
source_address = parser_url(source_address, url_len, user_len, auth_info, url_type)
else:
if source_address.startswith("https://"):
url_len = 8
source_address = parser_url(source_address, url_len, auth_info, url_type)
else:
source_address = source_address[:7] + auth_info["username"] + ":" + auth_info["password"] \
+ "@" + source_address[7:]
cmd = "git clone {0} {1}code/".format(source_address, job_workspace)
return cmd

# else:
# if p1.job_name.source_type == "git":
# if os.path.exists("{0}code/.git".format(job_workspace)):
# cmd = "cd {0}code/ && git pull".format(job_workspace)
# else:
# cmd = "git clone {0} {1}code/".format(source_address, job_workspace)
# if p1.job_name.source_type == "svn":
# if os.path.exists("{0}code/.svn".format(job_workspace)):
# cmd = "cd {0}code/ && svn update".format(job_workspace)
# else:
# cmd = "svn checkout {0} {1}code/".format(
# source_address, job_workspace)
def svn_clone(job_workspace, auth_info, source_address, p1):
if os.path.exists("{0}code/.svn".format(job_workspace)):
cmd = "svn --non-interactive --trust-server-cert --username {2} --password {3} update {0} {1}code/".format(
source_address, job_workspace, auth_info["username"], auth_info["password"])
else:
cmd = "svn --non-interactive --trust-server-cert --username {2} --password {3} checkout {0} {1}code/".format(
source_address, job_workspace, auth_info["username"], auth_info["password"])
return cmd
1 change: 1 addition & 0 deletions delivery/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
url(r'^log/(?P<project_id>\d+)/$', delivery.log, name='delivery_log'),
url(r'^log2/(?P<project_id>\d+)/$', delivery.log2, name='delivery_log2'),
url(r'^deploy/(?P<project_id>\d+)/$', delivery.delivery_deploy, name='delivery_deploy'),
url(r'^taskstop/(?P<project_id>\d+)/$', delivery.task_stop, name='delivery_taskstop'),
url(r'^delete/$', delivery.delivery_del, name='delivery_del'),
]
2 changes: 1 addition & 1 deletion templates/delivery/delivery_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</div>
{% endfor %}
<p></p>
<input type="submit" class="btn btn-primary" style="width: 60pt" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" class="btn btn-primary" style="width: 60pt" value="保存">&nbsp;&nbsp;&nbsp;&nbsp;
<a href="{% url 'delivery_list' %}"><li style="width: 60pt" class="btn btn-primary" value="">返回</li></a><br>
</form>
</div>
Expand Down
9 changes: 9 additions & 0 deletions templates/delivery/delivery_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<a href="#"><li onclick="return deploy(this.value);" class="btn btn-sm btn-default" value="{{ project.job_name_id }}">部署</li></a>
<a href="#"><li style="list-style-type:none" class="btn btn-sm btn-default" onclick="return tanchu(this.value)" value="{{ project.job_name_id }}">日志</li></a>
<a href="{% url "delivery_edit" project.id %}"><span class="btn btn-sm btn-default">编辑</span></a>
<a href="#"><li onclick="return taskstop(this.value);" class="btn btn-sm btn-default" value="{{ project.job_name_id }}">终止</li></a>
<a href="{% url "delivery_del"%}?project_id={{ project.id }}" onclick="return confirm('确认删除项目吗?')"><span class="btn btn-sm btn-danger ">删除</span></a>
</td>
</tr>
Expand Down Expand Up @@ -152,6 +153,14 @@
});
}
}
function taskstop(args) {
if (confirm("确定终止部署任务?")) {
var pb = document.getElementById("progress_bar_"+args);
pb.style.display = "none";
$.get("/delivery/taskstop/"+args+"/",function(data,status){
});
}
}
</script>
<script>
var v1 = window.setInterval("status()",2000);
Expand Down
2 changes: 1 addition & 1 deletion templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<div class="pull-right hidden-xs">
</div>
<!-- Default to the left bar -->
<a href="http://adminset.cn" target="view_window"></a> AdminSet v0.7.5
<a href="http://adminset.cn" target="view_window"></a> AdminSet v0.7.6
</footer>

0 comments on commit 282a78d

Please sign in to comment.