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

初始化

parents
Pipeline #12230 failed with stages
# -*- 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__":
from set_env import init_env
init_env()
close_firewalld()
install_docker()
install_docker_compose()
os.system('mkdir /huansi')
os.system('mkdir /huansi/upgrade')
print('*****************************************************')
print('********服务器需要重启,请输入reboot重启服务器*************')
print('*****************************************************')
# -*- coding:utf-8 -*-
import os
from pprint import pprint
def init_env():
if os.path.exists(r"/etc/profile.d/huansi.sh"):
with open(r"/etc/profile.d/huansi.sh", "r") as f:
setting = f.readlines()
print("当前DB配置:\n{}\n".format(''.join([item.replace(r'\n', '') for item in setting])))
while True:
print('=========================设置DB=========================')
print('如果想要退出,按住CTRL+C键然后再按一下Enter键即可退出,如果不知道,想象一下你要复制文本时候的按键')
print('========================================================\n')
HSCUSCODE = str(raw_input("请输入客户代号HSCUSCODE:"))
print(HSCUSCODE)
HSDB_HOST = str(raw_input("请输入数据库主机地址HSDB_HOST:"))
print(HSDB_HOST)
HSDB_PORT = str(raw_input("请输入数据库端口号HSDB_PORT(默认端口1433):"))
print(HSDB_PORT)
HSDB_USER = str(raw_input("请输入数据库用户HSDB_USER:"))
print(HSDB_USER)
HSDB_PASSWORD = str(raw_input("请输入数据库密码HSDB_PASSWORD:"))
print(HSDB_PASSWORD)
HSDB_PASSWORD = HSDB_PASSWORD.replace('$', '\$')
HSDB_NAME = str(raw_input("请输入ERP数据库名称HSDB_NAME(默认HsGmtERP):"))
if not HSDB_NAME:
HSDB_NAME = 'HsGmtERP'
print(HSDB_NAME)
MESDB_NAME = str(raw_input("请输入MES数据库名称MESDB_NAME(默认HsGmtMES):"))
if not MESDB_NAME:
MESDB_NAME = 'HsGmtMES'
print(MESDB_NAME)
HSPASSWORD = "huansi.net"
env = dict(HSCUSCODE=HSCUSCODE,
HSDB_HOST=HSDB_HOST,
HSDB_PORT=HSDB_PORT,
HSDB_USER=HSDB_USER,
HSDB_PASSWORD=HSDB_PASSWORD,
HSDB_NAME=HSDB_NAME,
MESDB_NAME=MESDB_NAME,
HSPASSWORD=HSPASSWORD
)
pprint(env)
flag = raw_input("请检查以上环境变量设置是否正确,确认请输入Y,输入其他,重新设置:")
if str(flag).upper() == 'Y':
with open("/etc/profile.d/huansi.sh", "w") as f:
for k, v in env.items():
f.writelines("export {}={}\n".format(k, v))
print('环境变量设置成功,目标文件为/etc/profile.d/huansi.sh')
# 返回客户编号
return HSCUSCODE
elif flag == "N":
continue
else:
continue
if __name__ == "__main__":
init_env()
print("环境变量设置结束")
#!/usr/bin/env bash
now_time=`date +%Y%m%d_%H%M%S`
python -u load_handle.py > ${now_time}.log
\ No newline at end of file
# -*- coding:utf-8 -*-
import os
import re
if __name__ == '__main__':
with os.popen('ls | grep .tar') as f:
cmd_txt = f.read()
if not cmd_txt:
raise RuntimeError('请检查文件,tar文件为空')
app_name_list = cmd_txt.split('\n')
for app_name in app_name_list:
if not app_name:
continue
print('加载镜像:{}'.format(app_name[:-4].replace('___', "/").replace("__", ":")))
cmd_res = os.system('docker load -i {}'.format(app_name))
if cmd_res == 1:
raise RuntimeError('加载失败')
print('镜像加载完毕')
print('开始升级程序')
with open('docker-compose.yml', 'r') as f:
compose_content = f.read()
app_list_str = re.findall('# app_list: ".*"', compose_content)
if app_list_str:
app_list_str = app_list_str[0]
else:
raise RuntimeError('未设置app_list')
app_str = app_list_str[len('# app_list: "'):-1]
if app_str == '*':
res = os.system('''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
source /etc/profile.d/huansi.sh &&\
docker-compose up -d --force-recreate''')
else:
res = os.system('''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
source /etc/profile.d/huansi.sh &&\
docker-compose up -d --force-recreate --no-deps {}'''.format(app_str))
if res == 1: raise RuntimeError('程序升级失败')
#!/usr/bin/env bash
now_time=`date +%Y%m%d_%H%M%S`
python -u save_handle.py > ${now_time}.log
\ No newline at end of file
# -*- coding:utf-8 -*-
'''
在upgrade_app写入需要升级的镜像,回车隔开,格式为 镜像名:版本号
'''
import os
import re
if __name__ == '__main__':
with open('docker-compose.yml', 'r') as f:
compose_content = f.read()
app_list_str = re.findall('# app_list: ".*"', compose_content)
if app_list_str:
app_list_str = app_list_str[0]
else:
raise RuntimeError('未设置app_list')
app_str = app_list_str[len('# app_list: "'):-1]
app_list = app_str.split(' ')
if not os.path.exists('/data/upgrade'):
os.mkdir('/data/upgrade')
image_list = []
if app_str == '*':
app_list = re.findall(r'image: (.*)\n', compose_content)
for app in app_list:
_l = app.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)
image_list.append(_image_info)
else:
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)
image_list.append(_image_info)
print('开始生成镜像')
for image in image_list:
if not image:
continue
print(' 生成镜像:{}'.format(image))
file_name = image.replace('/', '___').replace(':', '__') + '.tar'
_cmd = 'docker save {} -o /data/upgrade/{}'.format(image, file_name)
result = os.system(_cmd)
if result == 1:
raise RuntimeError("{}执行出错".format(_cmd))
print('镜像生成完毕')
os.system('mv ./docker-compose.yml /data/upgrade/docker-compose.yml')
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