Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140866
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 252
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-27 20:04
文章分类

全部博文(34)

文章存档

2018年(1)

2017年(23)

2016年(10)

我的朋友

分类: 系统运维

2017-05-27 19:54:02

软件环境

  1. jdk1.7.9
  2. maven3+ (设置环境变量), 并且替换setting.xml
    setting.xml
    根据项目替换默认maven里的

  3. python2.7:fabric (使用沙箱环境)
  4. 获取源码:

#创建目录

mkdir /opt/svncode/gitpro/

#获取源码,需要先上传服务器publicKeygitlab账户中

git clone ssh://git地址

  1. jenkins2 (经典安装)

创建自动化脚本

该脚本可以放到任意目录

!/usr/bin/python

from fabric.api import *

from fabric.context_managers import *

from fabric.contrib.console import confirm

from fabric.contrib.files import *

import time,os

i='machtalk@10.112.80.2:22'

env.hosts=[i]

env.roledefs = {

    'all':[i]

}

env.passwords = {

 i:'GVk2qWUTW7kP2hwQ'

}

env.profile='qa'

env.project_basepath='/opt/svncode/gitpro/'

env.project_parent_dir = env.project_basepath+'nextparent/'

env.postfix='-1.0-SNAPSHOT.jar'

env.deploy_basepath='/opt/machtalk/pro/'

env.exec_dir_name = 'exec'

 

def project_update(dir_path):

    with lcd(dir_path):

        local("git fetch origin")

        local("git checkout gome")

        local("git pull origin gome")

 

def project_package(dir_path, project_name):

    with lcd(dir_path):

        local("mvn clean package -Dmaven.test.skip=true -P "+env.profile+","+project_name)

 

def project_init_dir(deploy_path):

    if not exists(env.deploy_basepath):

        run("mkdir -p "+env.deploy_basepath+"logs")

    if not exists("/data/logs"):

        run("sudo mkdir -p /data")

        run("sudo ln -s "+(env.deploy_basepath+"logs")+" /data/logs")

    if not exists(deploy_path):

        run("mkdir -p "+deploy_path+env.exec_dir_name)

        run("mkdir -p "+deploy_path+"bak")

 

def project_put(source_dir, target_dir):

    result = put(source_dir, target_dir)

    if result.failed and not confirm("put file failed, Continue[Y/NO]?"):

        abort("aborting!")

 

def project_port_stop(port):

    with settings(warn_only=True):

        result = run("sudo netstat -lnp|grep "+port+"|awk '{print $7}'|awk -F '/' '{print $1}'")

        if result:

            run("kill "+result)

 

def project_clean(exec_dir):

    run('rm -rf '+exec_dir+'/*')

 

def project_start(exec_dir, jar_name):

    with settings(warn_only=True):

        with cd(exec_dir):

            run("nohup jar -jar "+jar_name+" >dev/null 2>&1 &")

 

def project_deploy(project_name, port):

    deploy_dir = env.deploy_basepath + project_name+'/'

    exec_dir = deploy_dir + env.exec_dir_name

    jar_name =  project_name + env.postfix

    project_init_dir(deploy_dir)

    project_update(env.project_parent_dir)

    project_package(env.project_parent_dir, project_name)

    project_port_stop(port)

    project_clean(exec_dir)

    project_put(env.project_parent_dir+project_name+'/target/'+jar_name, exec_dir+'')

    project_start(exec_dir, jar_name)

  

@task

@roles('all')

def commonservice():

   project_deploy('xcloud-commonservice', '2003')

 

@task

@roles('all')

def personal_openapi():

   project_deploy('xcloud-personal-openapi', '2009')

 

@task

@roles('all')

def apigateway():

   project_deploy('xcloud-apigateway', '2002')

 

@task

@roles('all')

def appservice():

   project_deploy('xcloud-appservice', '2007')

 

@task

@roles('all')

def authcenter():

   project_deploy('xcloud-authcenter', '2008')

 

 

@task

@roles('all')

def deviceservice():

   project_deploy('xcloud-deviceservice', '2005')

 

@task

@roles('all')

def discoveryserver():

   project_deploy('xcloud-discoveryserver', '2000')

 

@task

@roles('all')

def servicemonitor():

   project_deploy('xcloud-servicemonitor', '2001')

 

@task

@roles('all')

def userservice():

   project_deploy('xcloud-userservice', '2004')

  • 验证

fab list

 

#输出结果

Available commands:

 

    apigateway

    appservice

    authcenter

    commonservice

    deviceservice

    discoveryserver

    personal_openapi

    servicemonitor

    userservice

jenkins & fabric脚本管理

以部署 apigateway微服务为例

  • 打开jenkins,选择新建,输入name:apigateway, 选择"构建一个自由风格的软件项目"
  • 选择 构建--Execute shell

cd fabfile.py的目录 && fab apigateway

  • 保存
阅读(2082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~