Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
D
deploy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Script
deploy
Commits
09376283
Commit
09376283
authored
Feb 24, 2020
by
金凯强
🎨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
我晕
parent
063cad25
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
168 additions
and
1 deletion
+168
-1
db_manage.py
db_connect_test/db_manage.py
+72
-0
sync_release_info_to_db.py
db_connect_test/sync_release_info_to_db.py
+95
-0
install_runner.py
install_runner.py
+1
-1
No files found.
db_connect_test/db_manage.py
0 → 100644
View file @
09376283
# -*- 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
()
db_connect_test/sync_release_info_to_db.py
0 → 100644
View file @
09376283
# -*- 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
()
install_runner.py
View file @
09376283
...
@@ -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
\
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment