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
a14f5621
Commit
a14f5621
authored
Mar 21, 2020
by
金凯强
🎨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增远程安装docker功能
parent
c5bec666
Pipeline
#21278
passed with stage
in 58 seconds
Changes
6
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
182 additions
and
148 deletions
+182
-148
conncetion_service.py
app/conncetion/conncetion_service.py
+0
-1
install_service.py
app/install/install_service.py
+41
-143
upgrade_service.py
app/upgrade/upgrade_service.py
+7
-4
docker-19.03.4.tgz
static_file/system_file/docker-19.03.4.tgz
+0
-0
docker-compose-Linux-x86_64
static_file/system_file/docker-compose-Linux-x86_64
+0
-0
install_docker.py
static_file/system_file/install_docker.py
+134
-0
No files found.
app/conncetion/conncetion_service.py
View file @
a14f5621
...
...
@@ -3,7 +3,6 @@ from _socket import timeout
from
huansi_utils.db.db
import
new_id
from
huansi_utils.exception.exception
import
HSException
from
huansi_utils.server.service_uc
import
HSBaseUCService
from
sqlalchemy
import
create_engine
from
sqlalchemy.orm
import
sessionmaker
...
...
app/install/install_service.py
View file @
a14f5621
...
...
@@ -9,6 +9,7 @@ from huansi_utils.server.service_uc import HSBaseUCService
from
app.conncetion.conncetion_service
import
ConnectionService
from
app.utils.ssh_tools
import
SSHConnect
from
static_file
import
system_file_dir
class
InstallService
(
HSBaseUCService
):
...
...
@@ -33,6 +34,7 @@ class InstallService(HSBaseUCService):
conncetion_service
=
ConnectionService
()
project_info
=
conncetion_service
.
get_project_info
()
if
not
project_info
:
logger
.
info
(
'runner安装失败'
)
raise
HSException
(
'项目信息未查到,请先配置'
)
project_code
=
project_info
[
'project_no'
]
self
.
install_huansi_runner
(
project_code
)
...
...
@@ -49,6 +51,7 @@ class InstallService(HSBaseUCService):
--description huansi.{hscode}
\
--tag-list huansi.{hscode}"
.
format
(
hscode
=
HSCUSCODE
))
if
s1
!=
0
:
logger
.
info
(
'runner安装失败'
)
raise
HSException
(
'gitlab-runner安装失败'
)
self
.
_edit_runner_setting
()
self
.
_remove_old_runner_container
()
...
...
@@ -126,6 +129,7 @@ class InstallService(HSBaseUCService):
gitlab/gitlab-runner:latest"
)
if
s
!=
0
:
logger
.
info
(
'runner安装失败'
)
raise
HSException
(
'启动runner失败'
)
logger
.
info
(
'重新启动runner成功'
)
...
...
@@ -139,8 +143,21 @@ class InstallService(HSBaseUCService):
安装远端服务器的docker
:return:
'''
t
=
Thread
(
target
=
self
.
_install_remote_service_docker
)
t
.
start
()
return
{
"message"
:
"开始安装docker"
}
def
_install_remote_service_docker
(
self
):
from
flask_app
import
global_app
with
global_app
.
app_context
():
from
flask
import
g
g
.
user
=
{}
g
.
language
=
'cn'
remote_server_info
=
ConnectionService
()
.
get_remote_server_info
()
if
not
remote_server_info
:
logger
.
info
(
'dokcer安装失败'
)
raise
HSException
(
'远端服务器信息未查到,请先配置'
)
ssh_conenct
=
SSHConnect
(
host_ip
=
remote_server_info
[
'server_ip'
],
...
...
@@ -149,139 +166,20 @@ class InstallService(HSBaseUCService):
password
=
remote_server_info
[
'server_password'
])
with
ssh_conenct
as
ssh
:
InstallDocker
(
ssh
)
.
run
()
docker_service
=
'''[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target'''
class
InstallDocker
(
object
):
def
__init__
(
self
,
ssh
:
SSHConnect
):
self
.
ssh
=
ssh
def
run
(
self
):
self
.
_close_firewalld
()
self
.
_install_docker
()
self
.
_install_docker_compose
()
self
.
ssh
.
exec_command
(
'sudo mkdir -p /huansi && sudo mkdir -p /huansi/upgrade'
)
print
(
'*****************************************************'
)
print
(
'********服务器需要重启,请输入reboot重启服务器*************'
)
print
(
'*****************************************************'
)
def
_close_firewalld
(
self
):
print
(
'关闭防火墙...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'systemctl stop firewalld && systemctl disable firewalld'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'关闭防火墙失败'
)
def
_install_docker_compose
(
self
):
print
(
'修改/etc/selinux/config下的SELINUX属性'
)
out
,
err
=
self
.
ssh
.
exec_command
(
"sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config"
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'修改/etc/selinux/config下的SELINUX属性失败'
)
print
(
'移动文件至/usr/local/bin/docker-compose...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'移动文件至/usr/local/bin/docker-compose失败'
)
print
(
'添加可执行权限...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo chmod +x /usr/local/bin/docker-compose'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'添加可执行权限失败'
)
print
(
'查看docker-compose版本...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'docker-compose -v'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'查看docker-compose版本失败'
)
def
_install_docker
(
self
):
print
(
'解压tar包...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo tar -xvf docker-19.03.4.tgz'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'解压tar包失败'
)
print
(
'将docker目录移到/usr/bin目录下...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo cp docker/* /usr/bin/'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'将docker目录移到/usr/bin目录下失败'
)
print
(
'在/etc/systemd/system/目录下创建docekr.service...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
f
"""sudo tee /etc/systemd/system/docker.service <<-'EOF'
{docker_service}
EOF"""
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'在/etc/systemd/system/目录下创建docekr.service失败'
)
print
(
'添加文件权限...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo chmod +x /etc/systemd/system/docker.service'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'添加文件权限失败'
)
print
(
'新增配置文件/etc/docker/daemon.json...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
"""sudo mkdir -p /etc/docker && sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors":["https://m6wlkecl.mirror.aliyuncs.com"],
"insecure-registries": ["http://47.110.145.204:8084","http://183.134.73.2:8084"],
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
}
}
EOF"""
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'新增配置文件/etc/docker/daemon.json失败'
)
print
(
'重新加载配置文件...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo systemctl daemon-reload'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'重新加载配置文件失败'
)
print
(
'启动docker...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo systemctl start docker'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'启动docker失败'
)
print
(
'设置开机自启...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'sudo systemctl enable docker.service'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'设置开机自启失败'
)
print
(
'查看docker版本...'
)
out
,
err
=
self
.
ssh
.
exec_command
(
'docker -v'
)
print
(
out
)
if
err
:
raise
RuntimeError
(
'查看docker版本失败'
)
# 先上传必要的文件
logger
.
info
(
'上传文件docker-19.03.4.tgz'
)
ssh
.
upload
(
os
.
path
.
join
(
system_file_dir
,
'docker-19.03.4.tgz'
),
'/docker-19.03.4.tgz'
)
logger
.
info
(
'上传文件docker-compose-Linux-x86_64'
)
ssh
.
upload
(
os
.
path
.
join
(
system_file_dir
,
'docker-compose-Linux-x86_64'
),
'/docker-compose-Linux-x86_64'
)
logger
.
info
(
'上传文件install_docker.py'
)
ssh
.
upload
(
os
.
path
.
join
(
system_file_dir
,
'install_docker.py'
),
'/install_docker.py'
)
# 执行命令
logger
.
info
(
'执行安装命令'
)
out
,
err
=
ssh
.
exec_command
(
'cd / && python install_docker.py'
)
logger
.
info
(
f
'执行安装命令信息:{out},错误信息:{err}'
)
logger
.
info
(
'执行重启命令'
)
out2
,
err2
=
ssh
.
exec_command
(
'reboot'
)
logger
.
info
(
f
'执行重启命令信息:{out2},错误信息:{err2}'
)
logger
.
info
(
f
'执行成功,静等服务器重启。。。'
)
logger
.
info
(
'dokcer安装成功'
)
app/upgrade/upgrade_service.py
View file @
a14f5621
...
...
@@ -117,12 +117,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
)
self
.
_package_images
(
upgrade_no
)
# 找到服务器ip
# 上传app包到服务器
remote_server_info
=
ConnectionService
()
.
get_remote_server_info
()
if
not
remote_server_info
:
logger
.
info
(
'远程升级失败'
)
raise
HSException
(
'远端服务器信息未查到,请先配置'
)
ssh_conenct
=
SSHConnect
(
host_ip
=
remote_server_info
[
'server_ip'
],
...
...
@@ -139,6 +138,9 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
# 验证远端服务器有没有按照docker
self
.
validate_remote_server_install_docker
(
ssh
)
# 打包镜像
self
.
_package_images
(
upgrade_no
)
for
files
in
os
.
listdir
(
upgrade_back_up_dir
):
path
=
os
.
path
.
join
(
upgrade_back_up_dir
,
files
)
# 隐藏文件不复制
...
...
@@ -154,7 +156,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
out
,
err
=
ssh
.
exec_command
(
"sh /huansi/upgrade/load.sh"
)
logger
.
info
(
f
'远程升级信息:{out},远程错误信息:{out}'
)
logger
.
info
(
f
'远程升级
完成
'
)
logger
.
info
(
f
'远程升级
成功
'
)
return
{
'message'
:
'升级完成'
,
out
:
err
}
...
...
@@ -166,7 +168,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
t
=
Thread
(
target
=
self
.
_remote_upgrade
,
args
=
(
log_id
,))
t
.
start
()
return
{
"message"
:
"开始
安装runner
"
}
return
{
"message"
:
"开始
远程升级
"
}
def
remote_upgrade_step1
(
self
):
'''
...
...
@@ -498,4 +500,5 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
if
'Docker version'
not
in
out
or
err
:
logger
.
info
(
'验证远端服务器是否安装docker日志:{}'
.
format
(
err
))
logger
.
info
(
'远程升级失败'
)
raise
HSException
(
'远端服务器未安装docker,请先安装后再升级'
)
static_file/system_file/docker-19.03.4.tgz
0 → 100644
View file @
a14f5621
File added
static_file/system_file/docker-compose-Linux-x86_64
0 → 100644
View file @
a14f5621
File added
static_file/system_file/install_docker.py
0 → 100644
View file @
a14f5621
# -*- coding:utf-8 -*-
import
os
docker_service
=
'''[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target'''
def
install_docker
():
'''
安装docker
:return:
'''
print
(
'解压tar包...'
)
res
=
os
.
system
(
'sudo tar -xvf docker-19.03.4.tgz'
)
if
res
==
1
:
raise
RuntimeError
(
'解压tar包失败'
)
print
(
'将docker目录移到/usr/bin目录下...'
)
res
=
os
.
system
(
'sudo cp docker/* /usr/bin/'
)
if
res
==
1
:
raise
RuntimeError
(
'将docker目录移到/usr/bin目录下失败'
)
print
(
'在/etc/systemd/system/目录下创建docekr.service...'
)
with
open
(
'/etc/systemd/system/docker.service'
,
'w'
)
as
f
:
f
.
write
(
docker_service
)
print
(
'添加文件权限...'
)
res
=
os
.
system
(
'sudo chmod +x /etc/systemd/system/docker.service'
)
if
res
==
1
:
raise
RuntimeError
(
'添加文件权限失败'
)
print
(
'新增配置文件/etc/docker/daemon.json...'
)
if
not
os
.
path
.
exists
(
'/etc/docker'
):
os
.
mkdir
(
'/etc/docker'
)
with
open
(
'/etc/docker/daemon.json'
,
'w'
)
as
f
:
f
.
write
(
'''{
"registry-mirrors":["https://m6wlkecl.mirror.aliyuncs.com"],
"insecure-registries": ["http://47.110.145.204:8084","http://183.134.73.2:8084"],
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
}
}'''
)
print
(
'重新加载配置文件...'
)
res
=
os
.
system
(
'sudo systemctl daemon-reload'
)
if
res
==
1
:
raise
RuntimeError
(
'重新加载配置文件失败'
)
print
(
'启动docker...'
)
res
=
os
.
system
(
'sudo systemctl start docker'
)
if
res
==
1
:
raise
RuntimeError
(
'启动docker失败'
)
print
(
'设置开机自启...'
)
res
=
os
.
system
(
'sudo systemctl enable docker.service'
)
if
res
==
1
:
raise
RuntimeError
(
'设置开机自启失败'
)
print
(
'查看docker版本...'
)
res
=
os
.
system
(
'docker -v'
)
if
res
==
1
:
raise
RuntimeError
(
'查看docker版本失败'
)
def
install_docker_compose
():
'''
安装docker_compose
:return:
'''
print
(
'修改/etc/selinux/config下的SELINUX属性'
)
with
open
(
'/etc/selinux/config'
,
'r'
)
as
f
:
config
=
f
.
read
()
config
=
config
.
replace
(
'SELINUX=enforcing'
,
'SELINUX=disabled'
)
with
open
(
'/etc/selinux/config'
,
'w'
)
as
f
:
f
.
write
(
config
)
print
(
'移动文件至/usr/local/bin/docker-compose...'
)
res
=
os
.
system
(
'sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose'
)
if
res
==
1
:
raise
RuntimeError
(
'移动文件至/usr/local/bin/docker-compose失败'
)
print
(
'添加可执行权限...'
)
res
=
os
.
system
(
'sudo chmod +x /usr/local/bin/docker-compose'
)
if
res
==
1
:
raise
RuntimeError
(
'添加可执行权限失败'
)
print
(
'查看docker-compose版本...'
)
res
=
os
.
system
(
'docker-compose -v'
)
if
res
==
1
:
raise
RuntimeError
(
'查看docker-compose版本失败'
)
def
close_firewalld
():
'''
关闭防火墙
:return:syste
'''
print
(
'关闭防火墙...'
)
res
=
os
.
system
(
'''systemctl stop firewalld && systemctl disable firewalld'''
)
if
res
==
1
:
raise
RuntimeError
(
'关闭防火墙失败'
)
if
__name__
==
"__main__"
:
close_firewalld
()
install_docker
()
install_docker_compose
()
os
.
system
(
'mkdir /huansi'
)
os
.
system
(
'mkdir /huansi/upgrade'
)
print
(
'*****************************************************'
)
print
(
'********服务器需要重启,请输入reboot重启服务器*************'
)
print
(
'*****************************************************'
)
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