Skip to content

Commit

Permalink
add delivery user and port
Browse files Browse the repository at this point in the history
  • Loading branch information
guohongze committed Nov 21, 2018
1 parent 8ecaaa5 commit 1f103ab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions appconf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AuthInfo(models.Model):
dis_name = models.CharField(u"认证标识", max_length=50, unique=True, blank=False)
username = models.CharField(u"用户名", max_length=50, blank=True)
password = models.CharField(u"密码", max_length=50, blank=True)
deploy_port = models.IntegerField(u"端口", default=22)
private_key = models.CharField(u"密钥", max_length=100, blank=True)
memo = models.TextField(u"备注信息", max_length=200, blank=True)

Expand Down
8 changes: 6 additions & 2 deletions delivery/deli.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ def delivery_deploy(request, project_id):
job_name = project.job_name.name
source_address = project.job_name.source_address
app_path = project.job_name.appPath
source_auth = project.source_auth
if project.auth:
auth_info = {"username": project.auth.username, "password": project.auth.password}
auth_info = {"username": project.auth.username,
"password": project.auth.password,
"deploy_port": project.auth.deploy_port,
}
else:
auth_info = None
project.status = True
Expand All @@ -113,7 +117,7 @@ def delivery_deploy(request, project_id):
server_list.append(server_ip)
project.bar_data = 15
rsync_status = project.rsync_delete
deploy.delay(job_name, server_list, app_path, source_address, project_id, auth_info, rsync_status)
deploy.delay(job_name, server_list, app_path, source_address, project_id, auth_info, rsync_status, source_auth)
return HttpResponse("ok")


Expand Down
1 change: 1 addition & 0 deletions delivery/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Delivery(models.Model):
status = models.BooleanField(verbose_name=u"部署状态", default=False)
deploy_num = models.IntegerField(verbose_name=u"部署次数", default=0)
bar_data = models.IntegerField(default=0)
source_auth = models.BooleanField(verbose_name=u"源码认证", default=False)
auth = models.ForeignKey(
AuthInfo, verbose_name=u"认证信息",
null=True, blank=True,
Expand Down
36 changes: 25 additions & 11 deletions delivery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@


@shared_task
def deploy(job_name, server_list, app_path, source_address, project_id, auth_info, rsync_status):
def deploy(job_name, server_list, app_path, source_address, project_id, auth_info, rsync_status, source_auth):
cmd = ""
try:
username = auth_info["username"]
deploy_port = auth_info["deploy_port"]
except:
username = "root"
deploy_port = 22

p1 = Delivery.objects.get(job_name_id=project_id)
job_workspace = "/var/opt/adminset/workspace/{0}/".format(job_name)
log_path = job_workspace + 'logs/'
Expand All @@ -37,10 +44,15 @@ def deploy(job_name, server_list, app_path, source_address, project_id, auth_inf

with open(log_path + log_name, 'ab+') as f:
f.writelines("******STEP: GIT SOURCE CODE******\n\n")

if source_auth:
git_auth = auth_info
else:
git_auth = None
if p1.job_name.source_type == "git":
cmd = git_clone(job_workspace, auth_info, source_address, p1)
cmd = git_clone(job_workspace, git_auth, source_address, p1)
if p1.job_name.source_type == "svn":
cmd = svn_clone(job_workspace, auth_info, source_address, p1)
cmd = svn_clone(job_workspace, git_auth, source_address, p1)
data = cmd_exec(cmd)
p1.bar_data = 30
p1.save()
Expand All @@ -67,30 +79,32 @@ def deploy(job_name, server_list, app_path, source_address, project_id, auth_inf
for server in server_list:
#mkdir app_path
try:
sh.ssh("root@{0}".format(server), "ls {0}".format(app_path))
sh.ssh("-p {0}".format(deploy_port), "{1}@{0}".format(server, username),
"ls {0}".format(app_path))
except:
sh.ssh("root@{0}".format(server), "mkdir -p {0}".format(app_path))
sh.ssh("-p {0}".format(deploy_port), "{1}@{0}".format(server, username),
"mkdir -p {0}".format(app_path))

with open(log_path + log_name, 'ab+') as f:
f.writelines("\n+++rsync code to {0} +++\n".format(server))
if os.path.exists(exclude_file):
cmd = "rsync --progress -raz {4} --exclude-from {3} {0}/code/ {1}:{2}".format(
job_workspace, server, app_path, exclude_file, r_code)
cmd = "rsync -e 'ssh -p {6}' --progress -raz {4} --exclude-from {3} {0}/code/ {5}@{1}:{2}".format(
job_workspace, server, app_path, exclude_file, r_code, username, deploy_port)
else:
cmd = "rsync --progress -raz {3} --exclude '.git' --exclude '.svn' {0}/code/ {1}:{2}".format(
job_workspace, server, app_path, r_code)
cmd = "rsync -e 'ssh -p {5}' --progress -raz {3} --exclude '.git' --exclude '.svn' {0}/code/ {4}@{1}:{2}".format(
job_workspace, server, app_path, r_code, username, deploy_port)
data = cmd_exec(cmd)
with open(log_path + log_name, 'ab+') as f:
f.writelines(cmd)
f.writelines(data)
if p1.shell and not p1.shell_position:
with open(log_path + log_name, 'ab+') as f:
f.writelines("******STEP: SHELL EXECUTE ON REMOTE******\n\n")
cmd = "scp {0} {1}:/tmp".format(deploy_shell, server)
cmd = "scp -P {3} {0} {2}:{1}:/tmp".format(deploy_shell, server, username, deploy_port)
data = cmd_exec(cmd)
with open(log_path + log_name, 'ab+') as f:
f.writelines(data)
cmd = "ssh {1} '/bin/bash /tmp/{0}'".format(deploy_shell_name, server)
cmd = "ssh -P {3} {2}@{1} '/bin/bash /tmp/{0}'".format(deploy_shell_name, server, username, deploy_port)
data = cmd_exec(cmd)
with open(log_path + log_name, 'ab+') as f:
f.writelines(data)
Expand Down

0 comments on commit 1f103ab

Please sign in to comment.