Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
I
install.api
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
install.api
Commits
0eae11bf
Commit
0eae11bf
authored
Mar 24, 2020
by
金凯强
🎨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
应用修改和远程DB配置
parent
3d348e2f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
137 additions
and
3 deletions
+137
-3
api_test.py
api_test.py
+12
-1
conncetion_api.py
app/conncetion/conncetion_api.py
+16
-0
conncetion_service.py
app/conncetion/conncetion_service.py
+63
-0
upgarde_api.py
app/upgrade/upgarde_api.py
+7
-0
upgrade_service.py
app/upgrade/upgrade_service.py
+39
-2
No files found.
api_test.py
View file @
0eae11bf
...
...
@@ -33,11 +33,22 @@ if __name__ == '__main__':
# # 设置远程服务器连接
# res = requests.post('http://localhost:5000/connection/remote_server/', json=json_data)
# print(res.json())
#
# # 获取远程服务器连接
# res = requests.get('http://localhost:5000/connection/remote_server/')
# print(res.json())
# json_data = {"project_no":"jkq_test","db_ip":"192.168.0.1"
# ,"db_port":"22","db_user":"hhhh","db_password":"2222","mes_db_name":"4324324","tiip_db_name":"tiiip"}
#
# # 设置远程服务器DB连接
# res = requests.post('http://localhost:5000/connection/remote_db/', json=json_data)
# print(res.json())
#
# # 获取远程服务器DB连接
# res = requests.get('http://localhost:5000/connection/remote_db/')
# print(res.json())
# # 测试端口是否开放
# res = requests.get('http://localhost:5000/connection/test_port/23002/')
# print(res.json())
...
...
app/conncetion/conncetion_api.py
View file @
0eae11bf
...
...
@@ -50,6 +50,22 @@ class ConnectionAPI(ApiController):
'''
return
ConnectionService
()
.
set_remote_server_info
(
request
.
json
)
@
api
(
'remote_db'
)
def
post_set_remote_db_info
(
self
):
'''
设置远程服务器数据库配置
:return:
'''
return
ConnectionService
()
.
set_remote_db_info
(
request
.
get_json
())
@
api
(
'remote_db'
)
def
get_remote_db_info
(
self
):
'''
获取远程服务器数据库配置
:return:
'''
return
ConnectionService
()
.
get_remote_db_info
()
@
api
(
'test_port/<int:port>'
)
def
get_test_port
(
self
,
port
):
'''
...
...
app/conncetion/conncetion_service.py
View file @
0eae11bf
...
...
@@ -213,3 +213,66 @@ values
raise
HSException
(
f
'{host_ip}:{port}连接超时'
)
except
Exception
as
e
:
raise
HSException
(
f
'{host_ip}:{port}连接失败'
)
def
set_remote_db_info
(
self
,
param
):
'''
设置远程服务器数据库配置
:param param:
:return:
'''
'''project_no,db_ip,db_port,db_user,db_password,mes_db_name,tiip_db_name'''
import
os
from
static_file
import
profile_dir
remote_db_info_path
=
os
.
path
.
join
(
profile_dir
,
'huansi_server.sh'
)
# 写入
with
open
(
remote_db_info_path
,
"w"
)
as
f
:
for
k
,
v
in
param
.
items
():
k
=
self
.
math_name
(
k
)
if
k
in
[
'ID'
,
'CREATE_TIME'
]:
continue
f
.
writelines
(
"export {}={}
\n
"
.
format
(
k
,
v
))
# mes的数据库写两次,兼容之前的
if
k
==
'HSDB_NAME'
:
f
.
writelines
(
"export {}={}
\n
"
.
format
(
'MESDB_NAME'
,
v
))
return
{
'message'
:
"保存成功"
}
def
get_remote_db_info
(
self
):
'''
获取远程服务器数据库配置
:return:
'''
import
os
from
static_file
import
profile_dir
remote_db_info_path
=
os
.
path
.
join
(
profile_dir
,
'huansi_server.sh'
)
if
not
os
.
path
.
exists
(
remote_db_info_path
):
return
{}
with
open
(
remote_db_info_path
,
"r"
)
as
f
:
data
=
f
.
read
()
data
=
data
.
replace
(
'export '
,
''
)
data_item_list
=
data
.
split
(
'
\n
'
)
data_dict
=
{}
for
data_item
in
data_item_list
:
_l
=
data_item
.
split
(
'='
)
if
_l
[
0
]
==
'HSCUSCODE'
:
data_dict
[
'project_no'
]
=
_l
[
1
]
if
_l
[
0
]
==
'HSDB_HOST'
:
data_dict
[
'db_ip'
]
=
_l
[
1
]
if
_l
[
0
]
==
'HSDB_PORT'
:
data_dict
[
'db_port'
]
=
_l
[
1
]
if
_l
[
0
]
==
'HSDB_USER'
:
data_dict
[
'db_user'
]
=
_l
[
1
]
if
_l
[
0
]
==
'HSDB_PASSWORD'
:
data_dict
[
'db_password'
]
=
_l
[
1
]
if
_l
[
0
]
==
'HSDB_NAME'
:
data_dict
[
'mes_db_name'
]
=
_l
[
1
]
if
_l
[
0
]
==
'TIIP_DB_NAME'
:
data_dict
[
'tiip_db_name'
]
=
_l
[
1
]
return
data_dict
app/upgrade/upgarde_api.py
View file @
0eae11bf
...
...
@@ -14,6 +14,13 @@ class UpgradeAPI(ApiController):
3、导出安装包
4、导入安装包升级(不紧急)
'''
@
api
(
'apply'
)
def
get_apply_db_setting
(
self
):
'''
应用数据库配置
:return:
'''
return
UpgradeService
()
.
apply_db_setting
()
@
api
(
'rollback/<int:log_id>'
)
def
get_rollback_upgrade
(
self
,
log_id
):
...
...
app/upgrade/upgrade_service.py
View file @
0eae11bf
...
...
@@ -118,6 +118,11 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
g
.
language
=
'cn'
upgrade_no
=
self
.
get_upgrade_no_by_log_id
(
log_id
)
# 校验远程服务器db有没有配置
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
profile_dir
,
'huansi_server.sh'
)):
logger
.
info
(
'远程升级失败'
)
raise
HSException
(
'服务器DB未配置,请先配置'
)
# 找到服务器ip
# 上传app包到服务器
remote_server_info
=
ConnectionService
()
.
get_remote_server_info
()
...
...
@@ -151,7 +156,8 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
ssh
.
upload
(
path
,
f
'/huansi/upgrade/{files}'
)
ssh
.
upload
(
os
.
path
.
join
(
system_file_dir
,
'load.sh'
),
'/huansi/upgrade/load.sh'
)
ssh
.
upload
(
os
.
path
.
join
(
profile_dir
,
'huansi.sh'
),
'/huansi/upgrade/huansi.sh'
)
# 传入服务器配置的DB
ssh
.
upload
(
os
.
path
.
join
(
profile_dir
,
'huansi_server.sh'
),
'/huansi/upgrade/huansi.sh'
)
ssh
.
upload
(
os
.
path
.
join
(
system_file_dir
,
'load_handle.py'
),
'/huansi/upgrade/load_handle.py'
)
out
,
err
=
ssh
.
exec_command
(
"sh /huansi/upgrade/load.sh"
)
...
...
@@ -473,7 +479,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
return
deploy_image_list
def
get_upgrade_no_by_log_id
(
self
,
log_id
):
def
get_upgrade_no_by_log_id
(
self
,
log_id
=
None
):
'''
获取升级编号
:param log_id:
...
...
@@ -507,3 +513,34 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
logger
.
info
(
'验证远端服务器是否安装docker日志:{}'
.
format
(
err
))
logger
.
info
(
'远程升级失败'
)
raise
HSException
(
'远端服务器未安装docker,请先安装后再升级'
)
def
apply_db_setting
(
self
):
'''
应用数据库db配置
:return:
'''
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
profile_dir
,
'huansi.sh'
)):
raise
HSException
(
'请先配置数据库信息'
)
query_app_sql
=
'select * from app_upgrade_log where default_version=1'
with
db_driver
as
session
:
app_info
=
session
.
retrive_sql
(
query_app_sql
)
if
not
app_info
:
raise
HSException
(
'未查询到本机有升级信息,不用应用修改'
)
else
:
upgrade_no
=
app_info
[
'upgrade_no'
]
project_info
=
ConnectionService
()
.
get_project_info
()
host_ip
=
project_info
[
'host_ip'
]
# 调用本机的docker-compose
ssh_conenct
=
SSHConnect
(
user_name
=
'root'
,
password
=
'huansi.net'
,
host_port
=
'1111'
,
host_ip
=
host_ip
)
with
ssh_conenct
as
ssh
:
out
,
err
=
ssh
.
exec_command
(
f
'''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&
\
source /etc/profile.d/huansi.sh &&
\
docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.yml -p deploy up -d --force-recreate'''
)
logger
.
info
(
f
'应用操作信息:{out},err:{err}'
)
logger
.
info
(
"应用修改成功"
)
\ No newline at end of file
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