代码仅作编写python远程批量部署代码参考,无实际用途
git.py
from __future__ import with_statement
from fabric.api import local, lcd, settings, task
from fabric.contrib.console import confirm
from os import path, getenv, listdir
PROJECT_HOME = getenv('PROJECT_HOME')
GIT_REMOTE = getenv('GIT_REMOTE') or 'internal'
GIT_BRANCH = getenv('GIT_BRANCH') or 'master'
@task
def pull(proj, remote = GIT_REMOTE, branch = GIT_BRANCH):
with settings(warn_only = True):
with lcd(proj):
local('echo ********** BOF %s **********' % proj)
@task
def push(proj, remote = GIT_REMOTE, branch = GIT_BRANCH):
with settings(warn_only = True):
with lcd(proj):
local('echo ********** BOF %s **********' % proj)
@task
def sync(proj, from_repo, to_repo, branch = GIT_BRANCH):
with lcd(proj):
result = local('git remote', capture = True)
if result.find(from_repo) < 0:
local('echo "Git remote named %s not exists"' % from_repo)
return
if result.find(to_repo) < 0:
local('echo "Git remote named %s not exists"' % to_repo)
return
local('git pull %s %s' % (from_repo, branch))
local('git push %s %s' % (to_repo, branch))
@task
def release(proj, version, remote = GIT_REMOTE):
print('Start to release %s %s from %s ...' % (proj, version, remote))
with lcd(proj):
local('git tag %s' % version)
@task
def unrelease(proj, version, remote = GIT_REMOTE):
print('Start to unrelease %s %s from %s ...' % (proj, version, remote))
with lcd(proj):
local('git tag -d %s' % version)
local('git push %s :refs/tags/%s' % (remote, version))
project.py
from __future__ import with_statement
from fabric.api import local, lcd, settings, task, env, run, cd
from fabric.contrib.console import confirm
import paramiko
from datetime import datetime
from os import path, getenv, listdir
from git import *
xxxx = getenv('xxxx') or '/usr/local'
app_date = datetime.now().strftime('%Y%m%d%H%M%S')
SERVER_USER = getenv('SERVER_USER')
SERVER_USER_PASSWORD = getenv('SERVER_USER_PASSWORD')
SERVER_ADDRESS = getenv('SERVER_ADDRESS')
SERVER_PORT = int(getenv('SERVER_PORT') or 22)
env.colorize_errors = True
if SERVER_USER:
env.user = SERVER_USER
if SERVER_USER_PASSWORD:
env.password = SERVER_USER_PASSWORD
if SERVER_ADDRESS:
env.hosts = SERVER_ADDRESS
PROJECTS_HOME = getenv('PROJECTS_HOME')
DEP_PROJECTS = []
if getenv('DEP_PROJECTS'):
DEP_PROJECTS = getenv('DEP_PROJECTS').split(":")
PROJECT_NAME = getenv('PROJECT_NAME')
PROJECT_VERSION = getenv('PROJECT_VERSION')
PROJECT_HOME = '%s/%s' % (PROJECTS_HOME, PROJECT_NAME)
@task
def update_deps(skip_test = False):
opts = ''
if skip_test:
opts = '-Dmaven.test.skip'
for p in DEP_PROJECTS:
project_path = '%s/%s' % (PROJECTS_HOME, p)
with lcd(project_path):
pull(project_path)
local('mvn clean')
@task
def clean(skip_deps = False):
if not skip_deps:
for p in DEP_PROJECTS:
project_path = '%s/%s' % (PROJECTS_HOME, p)
with lcd(project_path):
local('mvn clean')
local('mvn clean')
@task
def compile(skip_dep_tests = False, skip_update_deps = False):
if not skip_update_deps:
update_deps(skip_dep_tests)
clean(skip_deps = False)
pull(PROJECT_HOME)
with lcd(PROJECT_HOME):
local('mvn compile')
@task
def test(skip_dep_tests = False, skip_update_deps = False):
compile(skip_dep_tests, skip_update_deps)
with lcd(PROJECT_HOME):
local('mvn test')
@task
def mvn_install(skip_test = False, skip_update_deps = False):
compile(skip_test, skip_update_deps)
install_cmd = 'mvn install'
if skip_test:
install_cmd = '%s -Dmaven.test.skip' % install_cmd
with lcd(PROJECT_HOME):
local(install_cmd)
@task
def package(skip_test = False, skip_update_deps = False, make_self_run = True):
mvn_install(skip_test, skip_update_deps)
with lcd(PROJECT_HOME):
if make_self_run:
local('PROJECT_HOME=%s ./src/main/scripts/package.sh' % PROJECT_HOME)
@task
def deploy(skip_test = False, skip_update_deps = False):
package(skip_test, skip_update_deps)
installer = '%s-installer' % PROJECT_NAME
sftp = _create_sftp_client()
print '%s/target/%s' %(PROJECT_HOME, installer)
sftp.put('%s/target/%s' % (PROJECT_HOME, installer), '/usr/local/pkg/%s' % installer)
with cd('/usr/local/pkg'):
run('sh ./%s' % installer)
@task
def undeploy():
with cd('%s/%s' % (xxxx, PROJECT_NAME)):
run('sh ./bin/uninstall.sh')
@task
def deploy_sec(skip_test = False, skip_update_deps = False):
package(skip_test, skip_update_deps,False)
if PROJECT_NAME in ('xxxx','xxxx','xxxx','xxxx','xxxx'):
installer='%s-%s.war'%(PROJECT_NAME,PROJECT_VERSION)
sftp= _create_sftp_client()
print '%s/target/%s' %(PROJECT_HOME,installer)
sftp.put('%s/target/%s' % (PROJECT_HOME, installer), '/usr/local/pkg/%s' % installer)
with cd('/usr/local/pkg'):
run('mkdir %s/app/%s/releases/%s'% (xxxx,PROJECT_NAME,app_date))
run('unzip -d %s/app/%s/releases/%s %s'% (xxxx,PROJECT_NAME,app_date,installer))
run('rm -f %s' % installer)
with cd('%s/app/%s/'%(xxxx,PROJECT_NAME)):
run('rm -rf current')
run('ln -s releases/%s current'%app_date)
run('find . -name xxxx* | xargs rm -f')
else:
installer = '%s-%s-bin.tar.gz' % (PROJECT_NAME, PROJECT_VERSION)
sftp = _create_sftp_client()
print '%s/distribution/target/%s' % (PROJECT_HOME, installer)
sftp.put('%s/distribution/target/%s' % (PROJECT_HOME, installer), '/usr/local/pkg/%s' % installer)
with cd('/usr/local/pkg'):
run('tar xf %s'%installer)
with cd('%s/app/%s/'%(xxxx,PROJECT_NAME)):
run('service %s stop'%PROJECT_NAME)
def _create_sftp_client():
t = paramiko.Transport((SERVER_ADDRESS, SERVER_PORT))
t.connect(username = SERVER_USER, password = SERVER_USER_PASSWORD)
return paramiko.SFTPClient.from_transport(t)