From e7a3f819de157f1172b66e28a5361567a7174d24 Mon Sep 17 00:00:00 2001 From: mx Date: Tue, 3 May 2022 15:20:27 +0700 Subject: [PATCH] add deploy script --- .gitignore | 1 + fabfile.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 fabfile.py diff --git a/.gitignore b/.gitignore index a6c2e39..10ca09d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ Homestead.yaml npm-debug.log yarn-error.log .env +*.tar.gz diff --git a/fabfile.py b/fabfile.py new file mode 100644 index 0000000..e8eb41b --- /dev/null +++ b/fabfile.py @@ -0,0 +1,57 @@ +""" +Deploy Tool + +usage: + +1. `fab deploy` 上传 DIRS 中指定的目录/文件 +2. `fab deploy -p=xxx` 上传 xxx +3. `fab deploy -p='xxx yyy zzz' 上传 xxx, yyy, zzz + +PS. 提前配置服务器地址,密码 + +其他功能自行修改 + +""" + +from os.path import exists, join +from fabric import task,Connection + +FILE_TAR = 'coinwind.tar.gz' +DIRS = ('app', 'config', 'resources') +UPLOAD_PATH = '~' +DEPLOY_PATH = '/www/coinwind' + + +def run(c, cmd): + return c.run(cmd, hide=True) + + +@task +def tar(c): + c.run('rm -f {}'.format(FILE_TAR)) + run(c, 'tar czf {} {}'.format(FILE_TAR, ' '.join(DIRS))) + # print(ret) + + +@task +def ptar(c, p): + # fallback + if not p: + return tar(c) + + cmd = 'tar -czf {} {}'.format(FILE_TAR, p) + run(c, cmd) + + +@task +def deploy(c, p=''): + ptar(c, p) + + with Connection(host='154.82.76.95', user='root', connect_kwargs={'password': 'jkR^2!2Nr&D%lwm9'}) as r: + with r.cd(UPLOAD_PATH): + r.put(FILE_TAR) + run(r, 'tar -xzf {} -C {}'.format(FILE_TAR, DEPLOY_PATH)) + run(r, 'rm {}'.format(FILE_TAR)) + run(r, 'chown -R nginx:nginx {}'.format(DEPLOY_PATH)) + run(r, 'chmod 600 {}'.format(join(DEPLOY_PATH, 'config/*'))) + run(r, 'chmod 600 {}'.format(join(DEPLOY_PATH, '.env'))) \ No newline at end of file