Commit 09376283 authored by 金凯强's avatar 金凯强 🎨

我晕

parent 063cad25
# -*- coding:utf-8 -*-
import json
from optparse import OptionParser
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import pymssql
# data = {
# "pbAPPUpgradeReleaseHistory": [
# {"app_code": "Tool", "app_version": "master", "project_branch": "master", "project_url": "http://baidu.com",
# "project_name": "tools", "operator_email": "huangtianba@huansi.net"}
# ]
# }
#
# print(json.dumps(data, separators=(',', ':')))
# LINUX上 ./db_manage -c mssql+pymssql:// --data '{"pbAPPUpgradeReleaseHistory":[{"project_name":"tools","project_branch":"master","app_code":"Tool","project_url":"http://baidu.com","operator_email":"huangtianba@huansi.net","app_version":"master"}]}'
if __name__ == "__main__":
parser = OptionParser()
parser.add_option('-c', '--connection', help='db connection string')
parser.add_option('-d', '--data', help='data', metavar="FILE")
options, args = parser.parse_args()
con_str = options.connection
data_dict_str = options.data
print(data_dict_str)
data_dict_str = data_dict_str.replace("'", '"')
data_dict = json.loads(data_dict_str)
sql_list = []
for table, table_data_list in data_dict.items():
for table_data in table_data_list:
sql_list.append('''INSERT INTO {}
({})
VALUES
({})'''.format(table, ','.join(table_data.keys()),
','.join(["'" + str(item) + "'" for item in table_data.values()])))
sql_str = '\n'.join(sql_list)
print(sql_str)
engine = create_engine(con_str)
DBSession = sessionmaker(bind=engine)
session = DBSession()
try:
result = session.execute(sql_str)
session.commit()
except Exception as e:
session.rollback()
orig = getattr(e, 'orig')
args = getattr(orig, 'args', ((),))
if isinstance(args[0], tuple):
args = args[0]
error_message = ''
for e_item in args:
if isinstance(e_item, bytes):
try:
e_item = e_item.decode()
except:
e_item = str(e_item)
error_message += str(e_item)
if not error_message:
error_message = str(e)
print('exec sql error:{}'.format(error_message))
finally:
session.close()
# -*- coding:utf-8 -*-
'''
project_name
project_branch
app_code
project_url
operator_email
app_version
'''
import json
import os
import urllib2
class Request():
def post(self, url, data, headers={}):
request = urllib2.Request(url, headers=headers, data=data)
response = urllib2.urlopen(request)
return response
def post_json(self, url, data):
headers = {'Content-Type': 'application/json'}
return self.post(url, data, headers)
class SyncReleaseInfoToDB(object):
def __init__(self):
self.info_dict = self.get_env()
def run(self):
'''
执行同步
:return:
'''
# log_db_connection_str = os.getenv('LOG_DB_CONNECTION_STR', '')
# if not log_db_connection_str.strip():
# print('未配置数据库连接字符串LOG_DB_CONNECTION_STR,退出程序...')
# return
try:
log_data_dict = {
"type": "pbAPPUpgradeReleaseHistory",
"param": "",
"data": {
"pbAPPUpgradeReleaseHistory": [self.info_dict]
}
}
log_data_str = json.dumps(log_data_dict)
gql_url = "http://" + os.getenv('HSGQL_URL', '47.110.145.204:8129') + "/sql_gql/save/?is_logger=false"
response = Request().post_json(gql_url, data=log_data_str)
rs = response.read()
print(rs)
except Exception as e:
print("同步报错:{}".format(str(e)))
def get_env(self):
'''
获取所有环境变量
:return:
'''
app_code = os.getenv('APP_CODE', '')
project_branch = os.getenv('CI_COMMIT_REF_NAME', '')
app_version = os.getenv('APP_VERSION', project_branch)
project_url = os.getenv('CI_PROJECT_URL', '')
project_name = os.getenv('CI_PROJECT_NAME', '')
operator_email = os.getenv('GITLAB_USER_EMAIL', '')
app_name = os.getenv('APP_NAME', '')
ci_pipeline_url = os.getenv('CI_PIPELINE_URL', '')
operator = os.getenv('GITLAB_USER_NAME', '')
commit_message = os.getenv('CI_COMMIT_MESSAGE', '')
info_dict = {
"_action": 1
, "app_code": app_code
, "app_version": app_version
, "project_branch": project_branch
, "project_url": project_url
, "project_name": project_name
, "operator_email": operator_email
, "app_name": app_name
, "operator": operator
, "ci_pipeline_url": ci_pipeline_url
, "commit_message": commit_message
}
return info_dict
if __name__ == "__main__":
SyncReleaseInfoToDB().run()
...@@ -28,7 +28,7 @@ def install_runner(HSCUSCODE=None): ...@@ -28,7 +28,7 @@ def install_runner(HSCUSCODE=None):
with open('/etc/gitlab-runner/config.toml', 'w') as f: with open('/etc/gitlab-runner/config.toml', 'w') as f:
f.write(a) f.write(a)
s1 = os.system("sudo docker stop gitlab-runner and docker rm gitlab-runner") s1 = os.system("sudo docker stop gitlab-runner && docker rm gitlab-runner")
assert s1 == 0, "移除gitlab-runner失败" assert s1 == 0, "移除gitlab-runner失败"
s2 = os.system("sudo docker run --rm -d --name gitlab-runner --restart always \ s2 = os.system("sudo docker run --rm -d --name gitlab-runner --restart always \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment