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
329b8df5
Commit
329b8df5
authored
Oct 30, 2019
by
金凯强
🎨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
虚拟机配置
parent
e758e67b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
281 additions
and
2 deletions
+281
-2
init.py
init.py
+2
-2
upgrade_handle.py
upgradeImage/upgrade_handle.py
+25
-0
uploadImage.py
upgradeImage/uploadImage.py
+92
-0
util.py
upgradeImage/util.py
+162
-0
No files found.
init.py
View file @
329b8df5
...
@@ -13,10 +13,10 @@ if __name__ == "__main__":
...
@@ -13,10 +13,10 @@ if __name__ == "__main__":
set_docekr_container_proxy
(
ip_and_port
)
set_docekr_container_proxy
(
ip_and_port
)
# 执行一次,设置国内镜像先
# 执行一次,设置国内镜像先
#
install_docker()
install_docker
()
install_runner
(
HSCUSCODE
)
install_runner
(
HSCUSCODE
)
set_gitlab_runner_git_proxy
(
ip_and_port
)
set_gitlab_runner_git_proxy
(
ip_and_port
)
install_docker
()
#
install_docker()
upgradeImage/upgrade_handle.py
0 → 100644
View file @
329b8df5
# -*- coding:utf-8 -*-
from
upgradeImage.util
import
log
from
upgradeImage.uploadImage
import
ImageClass
from
upgradeImage.util
import
SSHConnection
,
ShellManage
if
__name__
==
"__main__"
:
log
.
info
(
'*********************************升级开始*********************************'
)
image_instance
=
ImageClass
()
log
.
info
(
'开始复制docker-compose.yml文件'
)
ShellManage
()
.
exec_shell
(
'cp -f /huansi/gitlab-runner/builds/docker-compose.yml ./docker-compose.yml'
)
log
.
info
(
'成功复制到当前目录下'
)
log
.
info
(
'开始解析需要升级的镜像'
)
image_instance
.
get_deploy_image
(
'docker-compose.yml'
)
log
.
info
(
'开始打包镜像'
)
image_instance
.
save_images
()
log
.
info
(
'复制docker-compose.yml文件到/data/upgrade下'
)
ShellManage
()
.
exec_shell
(
'cp -f ./docker-compose.yml /data/upgrade/docker-compose.yml'
)
log
.
info
(
'*********************************升级结束*********************************'
)
# log.info('开始上传镜像和脚本文件')
# ssh = SSHConnection(host='localhost', port=1111, username='root', pwd='huansi.net')
# ssh.connect()
# log.info('开始在远端服务器执行升级脚本')
# r = ssh.cmd('mkdir jkqjkq')
# log.info('返回结果:\n{}'.format(r))
# log.info('*********************************升级结束*********************************')
upgradeImage/uploadImage.py
0 → 100644
View file @
329b8df5
# -*- coding:utf-8 -*-
import
os
import
re
from
upgradeImage.util
import
FileHelper
,
log
class
ImageClass
():
@
staticmethod
def
get_app_list_from_compose_file
(
compose_content
):
'''
获取app_list
:param compose_content:
:return:
'''
app_list_str
=
re
.
findall
(
'# app_list: ".*"'
,
compose_content
)
if
app_list_str
:
app_list_str
=
app_list_str
[
0
]
else
:
app_list_str
=
""
print
(
'未设置app_list'
)
return
app_list_str
[
len
(
'# app_list: "'
):
-
1
]
def
get_deploy_image
(
self
,
compose_path
):
'''
获取要升级的所有镜像名
:return:
'''
compose_content
=
FileHelper
.
get_file_content
(
compose_path
)
# 获取app_list
app_list_str
=
self
.
get_app_list_from_compose_file
(
compose_content
)
# 获取要升级的镜镜像名称
self
.
deploy_image_list
=
self
.
_get_deploy_image
(
app_list_str
,
compose_content
)
log
.
info
(
'要升级的镜像信息如下:
\n
{}'
.
format
(
self
.
deploy_image_list
))
def
_get_deploy_image
(
self
,
app_list_str
,
compose_content
):
'''
获取所有要升级的镜像名
:param app_list_str:
:return:
'''
# 如果是*,返回所有镜像名
# 如果是空,跳出
if
app_list_str
==
""
:
return
None
deploy_image_list
=
[]
if
app_list_str
==
"*"
:
image_list
=
re
.
findall
(
r'image: (.*)\n'
,
compose_content
)
for
image
in
image_list
:
_l
=
image
.
split
(
':'
)
if
len
(
_l
)
==
1
:
tag
=
'latest'
else
:
tag
=
_l
[
1
]
_image_info
=
'{}:{}'
.
format
(
_l
[
0
]
.
replace
(
'${HUANSI_REGISTRY_URL}'
,
'47.110.145.204:8084'
),
tag
)
deploy_image_list
.
append
(
_image_info
)
else
:
app_list
=
app_list_str
.
split
(
' '
)
for
app
in
app_list
:
image_info
=
re
.
findall
(
r'{}:(.*)\n(.*)image: (.*)\n'
.
format
(
app
),
compose_content
)[
0
][
-
1
:][
0
]
_l
=
image_info
.
split
(
':'
)
if
len
(
_l
)
==
1
:
tag
=
'latest'
else
:
tag
=
_l
[
1
]
_image_info
=
'{}:{}'
.
format
(
_l
[
0
]
.
replace
(
'${HUANSI_REGISTRY_URL}'
,
'47.110.145.204:8084'
),
tag
)
deploy_image_list
.
append
(
_image_info
)
return
deploy_image_list
def
save_images
(
self
):
'''
保存镜像
:return:
'''
if
not
os
.
path
.
exists
(
'/data/upgrade'
):
os
.
mkdir
(
'/data/upgrade'
)
log
.
info
(
'开始生成镜像'
)
for
deploy_image
in
self
.
deploy_image_list
:
if
not
deploy_image
:
continue
print
(
' 生成镜像:{}'
.
format
(
deploy_image
))
file_name
=
deploy_image
.
replace
(
'/'
,
'___'
)
.
replace
(
':'
,
'__'
)
+
'.tar'
_cmd
=
'docker save {} -o /data/upgrade/{}'
.
format
(
deploy_image
,
file_name
)
result
=
os
.
system
(
_cmd
)
if
result
==
1
:
raise
RuntimeError
(
"{}执行出错"
.
format
(
_cmd
))
log
.
info
(
'镜像生成完毕,目录为/data/upgrade'
)
upgradeImage/util.py
0 → 100644
View file @
329b8df5
# -*- coding:utf-8 -*-
import
paramiko
import
logging
import
os
from
logging
import
handlers
class
Logger
(
object
):
def
__init__
(
self
,
filename
,
level
=
'info'
,
when
=
'D'
,
backCount
=
3
,
fmt
=
'[
%(asctime)
s] -
%(levelname)
s:
%(message)
s'
):
self
.
logger
=
logging
.
getLogger
(
filename
)
format_str
=
logging
.
Formatter
(
fmt
)
# 设置日志格式
self
.
logger
.
setLevel
(
level
)
# 设置日志级别
sh
=
logging
.
StreamHandler
()
# 往屏幕上输出
sh
.
setFormatter
(
format_str
)
# 设置屏幕上显示的格式
th
=
handlers
.
TimedRotatingFileHandler
(
filename
=
filename
,
when
=
when
,
backupCount
=
backCount
,
encoding
=
'utf-8'
)
# 往文件里写入#指定间隔时间自动生成文件的处理器
# 实例化TimedRotatingFileHandler
# interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
# S 秒
# M 分
# H 小时、
# D 天、
# W 每星期(interval==0时代表星期一)
# midnight 每天凌晨
th
.
setFormatter
(
format_str
)
# 设置文件里写入的格式
# self.logger.addHandler(sh) # 把对象加到logger里
self
.
logger
.
addHandler
(
th
)
def
error
(
self
,
msg
,
*
args
,
**
kwargs
):
return
self
.
logger
.
error
(
msg
,
*
args
,
**
kwargs
)
def
info
(
self
,
msg
,
*
args
,
**
kwargs
):
return
self
.
logger
.
info
(
msg
,
*
args
,
**
kwargs
)
def
debug
(
self
,
msg
,
*
args
,
**
kwargs
):
return
self
.
logger
.
debug
(
msg
,
*
args
,
**
kwargs
)
def
warning
(
self
,
msg
,
*
args
,
**
kwargs
):
return
self
.
logger
.
warning
(
msg
,
*
args
,
**
kwargs
)
def
critical
(
self
,
msg
,
*
args
,
**
kwargs
):
return
self
.
logger
.
critical
(
msg
,
*
args
,
**
kwargs
)
log
=
Logger
(
os
.
path
.
join
(
os
.
getcwd
(),
'upgrade_log.log'
),
level
=
logging
.
DEBUG
)
class
FileHelper
():
@
staticmethod
def
get_file_content
(
file_name
):
'''
获取文件内容
:param file_name:
:return:
'''
with
open
(
file_name
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
content
=
f
.
read
()
return
content
@
staticmethod
def
set_file_content
(
file_name
,
file_content
):
'''
设置文件内容
:param file_name:
:param file_content:
:return:
'''
with
open
(
file_name
,
'w'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
file_content
)
class
ShellManage
():
def
exec_shell
(
self
,
shell_message
):
'''
执行shell命令
:param sehll_message:
:return:
'''
result
=
os
.
system
(
shell_message
)
log
.
info
(
'执行命令:
\n
{}'
.
format
(
shell_message
))
if
result
==
1
:
raise
RuntimeError
(
'执行【{}】出错'
.
format
(
shell_message
))
def
get_shell_message
(
self
,
shell_message
):
'''
获取shell命令的返回信息
:param shell_message:
:return:
'''
with
os
.
popen
(
shell_message
)
as
f
:
txt
=
f
.
read
()
log
.
info
(
'执行了命令:
\n
{},返回结果为:
\n
{}'
.
format
(
shell_message
,
txt
))
return
txt
def
get_txt
(
self
):
'''
测试
:param file_path:
:return:
'''
with
open
(
'docker images.txt'
,
'r'
)
as
f
:
txt
=
f
.
read
()
return
txt
class
SSHConnection
(
object
):
def
__init__
(
self
,
host
=
None
,
port
=
None
,
username
=
None
,
pwd
=
None
):
self
.
__host
=
host
self
.
__port
=
port
self
.
__username
=
username
self
.
__pwd
=
pwd
self
.
__transport
=
None
self
.
__k
=
None
def
connect
(
self
):
# 创建连接对象
transport
=
paramiko
.
Transport
((
self
.
__host
,
self
.
__port
))
transport
.
connect
(
username
=
self
.
__username
,
password
=
self
.
__pwd
)
self
.
__transport
=
transport
def
close
(
self
):
# 关闭连接
self
.
__transport
.
close
()
def
upload
(
self
,
local_path
,
target_path
):
"""
本地上传文件到服务器
:param local_path: 本地文件地址
:param target_path: 目标文件路径
"""
sftp
=
paramiko
.
SFTPClient
.
from_transport
(
self
.
__transport
)
sftp
.
put
(
local_path
,
target_path
)
def
download
(
self
,
remote_path
,
local_path
):
"""
服务器下载文件到本地
:param remote_path: 服务器文件路径
:param local_path: 本地目标路径
"""
sftp
=
paramiko
.
SFTPClient
.
from_transport
(
self
.
__transport
)
sftp
.
get
(
remote_path
,
local_path
)
def
cmd
(
self
,
command
):
"""
执行指定命令
:param command: 命令
:return:输入,输出,错误
"""
ssh
=
paramiko
.
SSHClient
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
ssh
.
_transport
=
self
.
__transport
# 执行命令,标准输入,输出,错误
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
command
)
# 获取命令结果
outputs
=
stdout
.
read
()
return
outputs
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