gitlab代码库切分支脚本

#!/usr/bin/env python
#coding:utf-8
import sys,getopt
import requests
import time
import sys
reload(sys)
sys.setdefaultencoding(“utf-8”)

dict={}

GITLAB_URL = ‘http://gitlab.xxx.com/api/v3/’
GITLAB_PRIVATE_TOKEN = ‘xxxxx’
NOW=time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime())
def project_list():
url = GITLAB_URL + ‘/projects?private_token=’ + GITLAB_PRIVATE_TOKEN + ‘&per_page=2000’
#yourparams={‘search’: ‘trip-service’}
#yourparams={}
#r = requests.get(url,params=yourparams)
r = requests.get(url)
data = r.json()
for i in data:
id = i[u’id’]
name = i[u’name’]
dict[name]=id
#print(dict)
return dict

def project_query(project_name):
url = GITLAB_URL + ‘projects/search/’ + project_name
headers = {‘PRIVATE-TOKEN’: GITLAB_PRIVATE_TOKEN}
r = requests.get(url,headers = headers)
data = r.json()
for i in data:
id = i[u’id’]
name = i[u’name’]
if name == project_name:
break
return id

“””
def project_query(project_name):
url = GITLAB_URL + ‘/projects?private_token=’ + GITLAB_PRIVATE_TOKEN + ‘&per_page=2000’
yourparams={‘search’: project_name}
#yourparams={}
r = requests.get(url,params=yourparams)
data = r.json()
for i in data:
id = i[u’id’]
name = i[u’name’]
return id
“””

def protect_branch(project_id,branch_name):
url = GITLAB_URL + ‘projects/’ + project_id + ‘/repository/branches/’ + branch_name + ‘/protect’
print(url)
headers = {‘PRIVATE-TOKEN’: GITLAB_PRIVATE_TOKEN}
r = requests.put(url,headers = headers)
print(r.text)

def unprotect_branch(project_id,branch_name):
url = GITLAB_URL + ‘projects/’ + project_id + ‘/repository/branches/’ + branch_name + ‘/unprotect’
headers = {‘PRIVATE-TOKEN’: GITLAB_PRIVATE_TOKEN}
r = requests.put(url,headers = headers)
print(r.text)

def create_branch(project_id,newbranch_name,from_branch):
url = GITLAB_URL + ‘projects/’ + project_id + ‘/repository/branches?branch_name=’ + newbranch_name + ‘&ref=’ + from_branch
headers = {‘PRIVATE-TOKEN’: GITLAB_PRIVATE_TOKEN}
r = requests.post(url,headers = headers)
print(r.text)

def delete_branch(project_id,newbranch_name):
url = GITLAB_URL + ‘projects/’ + project_id + ‘/repository/branches/’ + newbranch_name
print url
headers = {‘PRIVATE-TOKEN’: GITLAB_PRIVATE_TOKEN}
r = requests.delete(url,headers = headers)
print(r.text)

def usage():
print ‘\033[1;32;40m’
print ‘*’ * 50
print ‘*COMMAND:\t’, ‘python gitlab.py args’
print ‘*HELP:\t’, ‘python gitlab.py –help’
print ‘*VERSION:\t’, ‘V0.0.1’
print ‘*’ * 50
print ‘\033[0m’
print ‘\n’ \
‘eg: python gitlab.py -t project_list \n’ \
‘eg: python gitlab.py -t create_branch -p -b -f \n’ \
‘eg: python gitlab.py -t create_branch -p -b \n’ \
‘eg: python gitlab.py -t protect_branch -p -f \n’ \
‘eg: python gitlab.py -t unprotect_branch -p -f \n’ \
‘\n’ \
‘ -t command type\n’ \
‘ -p gitlab project id\n’ \
‘ -b create new branch name\n’ \
‘ -f new branch from branch\n’ \

def main():
command_type = ”
project_name = ”
newbranch_name = ”
from_branch = ”

if len(sys.argv) < 2: usage() try: opts, args = getopt.getopt(sys.argv[1:], "h:t:p:b:f:", ["help", "type=","project_id=","branch=","from="]) except getopt.GetoptError as err: # print help information and exit: print str(err) # will print something like "option -a not recognized" usage() sys.exit(2) for name, value in opts: if name in ('-h','--help'): usage() elif name in ("-t", "--type"): command_type = value elif name in ("-p", "--project_name"): project_name = value #project_id=str(dict[project_name]) elif name in ("-b", "--branch_name"): newbranch_name = value elif name in ("-f", "--from_branch"): from_branch = value else: assert False, "unhandled option" dict=project_list() try: #project_id=str(dict[project_name]) project_id=str(project_query(project_name)) except: pass if command_type == "project_list": print(dict) elif command_type == "create_branch": if project_id != "" and newbranch_name != "" and from_branch != "": print(project_id,newbranch_name,from_branch) create_branch(project_id,newbranch_name,from_branch) else: print "please input -p project_name -b newbranch_name -f from_branch" elif command_type == "delete_branch": if project_id != "" and newbranch_name != "": delete_branch(project_id,newbranch_name) else: print "please input -p project_name -b newbranch_name" elif command_type == "protect_branch": if project_id != "" and from_branch != "": protect_branch(project_id,from_branch) else: print "please input -p project_name -f branch_name" elif command_type == "unprotect_branch": if project_id != "" and from_branch != "": unprotect_branch(project_id,from_branch) else: print "please input -p unproject_name -f branch_name" if __name__ == "__main__": main() 以上是脚本,下面是jenkins中的命令 /opt/env-2.7/bin/python gitlab.py -t unprotect_branch -p $project -f $branch /opt/env-2.7/bin/python gitlab.py -t delete_branch -p $project -b $branch /opt/env-2.7/bin/python gitlab.py -t create_branch -p $project -b $branch -f $from /opt/env-2.7/bin/python gitlab.py -t protect_branch -p $project -f $branch

gerrit代码库切分支脚本

curl –digest –user admin:lZ6wrtsCoPMGLRdFjoTkthOywkLlY8JpJuFSxxx -X DELETE http://x.x.x.x:8081/a/projects/$1/branches/$3

sleep 5

curl –digest –user admin:lZ6wrtsCoPMGLRdFjoTkthOywkLlY8JpJuFSxxx http://x.x.x.x:8081/a/projects/$1/branches/$2 > $2.txt

sed -i ‘1d’ $2.txt
sed -i ‘2d’ $2.txt
sed -i ‘3d’ $2.txt
sed -i ‘s/,//g’ $2.txt

curl –digest –user admin:lZ6wrtsCoPMGLRdFjoTkthOywkLlY8JpJuFSxxx -X PUT -d@$2.txt –header “Content-Type: application/json;charset=UTF-8” http://x.x.x.x:8081/a/projects/$1/branches/$3

以上是shell脚本,下面是jenkins中的命令:
/root/branch_manager/recreate_branch.sh Cloud%2F$ProjectName ww_daily ww_qa

从某一台机器上跳转

网络从一台机器上跳转到另外一台机器采用haproxy的方法,配置文件如下:
#———————————————————————
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#———————————————————————

#———————————————————————
# Global settings
#———————————————————————
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the ‘-r’ option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#———————————————————————
# common defaults that all the ‘listen’ and ‘backend’ sections will
# use if not designated in their block
#———————————————————————

defaults
mode tcp
log global
# option httplog
option dontlognull
option http-server-close
# option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

listen gerrit
mode tcp
bind :29418
rate-limit sessions 300
server gerrit 10.25.xx.xx:29418

listen gerrit_http
mode tcp
bind :8081
rate-limit sessions 300
server gerrit 10.25.xx.xx:8080

listen wiki
mode tcp
bind :8080
rate-limit sessions 300
server maven 10.25.xx.xx:8080

listen rms
mode tcp
bind :80
rate-limit sessions 300
server maven 10.25.xx.xx:8082

listen ldap
mode tcp
bind :389
rate-limit sessions 300
server ldap 10.25.xx.xx:389

listen mysql
mode tcp
bind :3306
rate-limit sessions 300
server mysql xx.xx.xx.xx:80