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

Initial commit

parents
Pipeline #10363 canceled with stages
from install_docker import install_docker
from install_runner import install_runner
from set_env import init_env
from set_proxy import set_linux_global_proxy, set_docker_service_proxy, set_docekr_container_proxy, \
set_gitlab_runner_git_proxy
if __name__ == "__main__":
HSCUSCODE, ip_and_port = init_env()
set_linux_global_proxy(ip_and_port)
set_docker_service_proxy(ip_and_port)
set_docekr_container_proxy(ip_and_port)
# 执行一次,设置国内镜像先
install_docker()
install_runner(HSCUSCODE)
set_gitlab_runner_git_proxy(ip_and_port)
install_docker()
#coding=utf-8
import os
def install_docker():
# step1 = os.system("wget -qO- https://get.docker.com/ | sh")
# assert step1 == 0, "docker 安装失败"
os.system("sudo mkdir -p /etc/docker")
step2 = os.system("""
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-opt": {
"max-size": "50m",
"max-file": "3"
}
}
EOF
""")
assert step2 == 0, "docker 镜像地址更新失败"
os.system("sudo systemctl daemon-reload")
os.system("sudo systemctl restart docker")
print("docker安装成功")
if __name__ == "__main__":
install_docker()
# coding=utf-8
import os
def install_runner(HSCUSCODE=None):
# 创建配置文件
if not HSCUSCODE:
HSCUSCODE = input("请输入客户代号HSCUSCODE:")
print(HSCUSCODE)
if os.path.exists('/etc/gitlab-runner/config.toml'):
with open('/etc/gitlab-runner/config.toml', 'r') as f:
content = f.read()
if 'huansi.{hscode}'.format(hscode=HSCUSCODE) not in content:
register_huansi_runner(HSCUSCODE)
# if 'gitlab.{hscode}'.format(hscode=HSCUSCODE) not in content:
# register_gitlab_runner(HSCUSCODE)
else:
register_huansi_runner(HSCUSCODE)
# register_gitlab_runner(HSCUSCODE)
os.system("sudo chmod 777 /etc/gitlab-runner/config.toml")
with open('/etc/gitlab-runner/config.toml', 'r') as f:
s = f.read()
a = s.replace(' volumes = ["/cache"]',
'volumes = ["/var/run/docker.sock:/var/run/docker.sock","/etc/profile.d/huansi.sh:/etc/profile.d/huansi.sh" ,"/cache"]')
with open('/etc/gitlab-runner/config.toml', 'w') as f:
f.write(a)
s2 = os.system("sudo docker run -d --name gitlab-runner --restart always \
-v /etc/gitlab-runner:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/profile.d/huansi.sh:/etc/profile.d/huansi.sh \
gitlab/gitlab-runner:latest")
assert s2 == 0, "安装runner失败"
s3 = os.system("sudo docker restart gitlab-runner")
assert s3 == 0, "runner 配置成功"
def register_gitlab_runner(HSCUSCODE):
s4 = os.system("sudo docker run --rm -t -i -v /etc/gitlab-runner:/etc/gitlab-runner gitlab/gitlab-runner register \
-n -u https://gitlab.com/ -r Y1Px-4YpZ8qevi6vCxVY --executor docker --docker-image docker \
--description gitlab.{hscode} \
--tag-list {hscode}".format(hscode=HSCUSCODE))
assert s4 == 0, "gitlab_runner配置失败,如果要重新执行,不要再次执行init.py文件,请直接执行install_docker.py文件"
def register_huansi_runner(HSCUSCODE):
s1 = os.system("sudo docker run --rm -t -i -v /etc/gitlab-runner:/etc/gitlab-runner gitlab/gitlab-runner register \
-n -u http://47.110.145.204:8085/ -r WwpzH4qk19KjvAjEwoTz --executor docker --docker-image docker \
--description huansi.{hscode} \
--tag-list huansi.{hscode}".format(hscode=HSCUSCODE))
assert s1 == 0, "huansi_runner配置失败,如果要重新执行,不要再次执行init.py文件,请直接执行install_docker.py文件"
if os.path.exists('/etc/gitlab-runner'):
install_runner()
print("runner安装成功")
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 = input("请输入客户代号HSCUSCODE:")
print(HSCUSCODE)
HSDB_HOST = input("请输入数据库主机地址HSDB_HOST:")
print(HSDB_HOST)
HSDB_PORT = input("请输入数据库端口号HSDB_PORT(默认端口1433):")
print(HSDB_PORT)
HSDB_USER = input("请输入数据库用户HSDB_USER:")
print(HSDB_USER)
HSDB_PASSWORD = input("请输入数据库密码HSDB_PASSWORD:")
print(HSDB_PASSWORD)
HSDB_NAME = input("请输入ERP数据库名称HSDB_NAME(默认HsGmtERP):")
print(HSDB_NAME)
MESDB_NAME = input("请输入MES数据库名称MESDB_NAME(默认HsGmtMES):")
print(MESDB_NAME)
HSPASSWORD = "huansi.net"
is_proxy = input("是否设置代理,确认请输入Y,输入N不设置代理:")
if is_proxy.upper() == 'Y':
ip_and_port = input("请输入代理的ip地址和端口(列如192.168.1.1:808):").strip()
# CCProxy代理默认端口808
if ":" not in ip_and_port:
ip_and_port = ip_and_port + ":808"
else:
ip_and_port = None
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 = input("请检查以上环境变量设置是否正确,确认请输入Y,输入其他,重新设置:")
if 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, ip_and_port
elif flag == "N":
continue
else:
continue
if __name__ == "__main__":
init_env()
print("环境变量设置结束")
# -*- coding:utf-8 -*-
import json
import os
import re
'''
代理要配置四处
linux 全局代理
docker Service的代理
docker 容器的代理
runner 内部的git代理
'''
def set_docker_service_proxy(ip_and_port):
'''
docker Service的代理
:param ip_and_port:
:return:
'''
if ip_and_port is None:
return
docker_service_path = '/usr/lib/systemd/system/docker.service'
# docker Service的代理
with open(docker_service_path, 'r') as f:
content = f.read()
if 'Environment="HTTP_PROXY=http://' not in content:
content = content.replace('[Service]', '[Service]\nEnvironment="HTTP_PROXY=http://{}"'.format(ip_and_port))
else:
pattern = re.compile(r'(Environment="HTTP_PROXY=http://\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}\")')
r_str = '''Environment="HTTP_PROXY=http://{}"'''.format(ip_and_port)
content = re.sub(pattern, r_str, content)
# 重新写入
with open(docker_service_path, 'w') as f:
f.write(content)
# 重启docker
s = os.system("systemctl daemon-reload && sudo systemctl restart docker")
if s == 0:
print("docker Service的代理配置成功")
else:
print("docker Service的代理配置失败")
def set_linux_global_proxy(ip_and_port):
'''
linux全局代理
:param ip_and_port:
:return:
'''
if ip_and_port is None:
return
proxy_sh_path = '/etc/profile.d/proxy.sh'
with open(proxy_sh_path, 'w') as f:
f.write("export http_proxy=http://{}".format(ip_and_port))
s = os.system("source {}".format(proxy_sh_path))
if s == 0:
print("linux全局代理配置成功")
else:
print("linux全局代理配置失败")
def set_docekr_container_proxy(ip_and_port):
'''
docker 容器的代理
:param ip_and_port:
:return:
'''
if ip_and_port is None:
return
docker_config_path = '/root/.docker/config.json'
if not os.path.exists(docker_config_path):
os.mkdir('/root/.docker')
with open(docker_config_path, 'w') as f:
c_dit = {}
c_dit['proxies'] = {"default": {"httpProxy": "http://{}".format(ip_and_port)}}
c_str = json.dumps(c_dit, indent=4)
f.write(c_str)
else:
with open(docker_config_path, 'r') as f:
content = f.read()
c_dit = json.loads(content)
if c_dit.get('proxies') is None:
c_dit['proxies'] = {"default": {"httpProxy": "http://{}".format(ip_and_port)}}
else:
c_dit['proxies']['default']['httpProxy'] = "http://{}".format(ip_and_port)
c_str = json.dumps(c_dit, indent=4)
with open(docker_config_path, 'w') as f:
f.write(c_str)
print('docker 容器的代理配置成功')
def set_gitlab_runner_git_proxy(ip_and_port):
'''
runner 内部的git代理
:param ip_and_port:
:return:
'''
if ip_and_port is None:
return
gitlab_runner_config_path = '/etc/gitlab-runner/config.toml'
if not os.path.exists(gitlab_runner_config_path):
raise RuntimeError('请先配置gitlab-runner再来执行代理脚本')
with open(gitlab_runner_config_path, 'r') as f:
content = f.read()
if "environment = [\"http_proxy=" in content:
pattern = re.compile(
r'(\[\"http_proxy=http://\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}\", \"HTTP_PROXY=\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}\"\])')
r_str = '''["http_proxy=http://{0}", "HTTP_PROXY={0}"]'''.format(ip_and_port)
content = re.sub(pattern, r_str, content)
else:
r_str = '''executor = "docker"
pre_clone_script = "git config --global http.proxy $HTTP_PROXY"
environment = ["http_proxy=http://{0}", "HTTP_PROXY={0}"]
[runners.custom_build_dir]'''.format(ip_and_port)
content = content.replace('executor = "docker"\n [runners.custom_build_dir]', r_str)
with open(gitlab_runner_config_path, 'w') as f:
f.write(content)
print('gitlab-runner 内部git代理配置成功')
if __name__ == '__main__':
ip_and_port = input("请输入代理的ip地址和端口(列如192.168.1.1:808):").strip()
# CCProxy代理默认端口808
if ":" not in ip_and_port:
ip_and_port = ip_and_port + ":808"
set_gitlab_runner_git_proxy(ip_and_port)
set_linux_global_proxy(ip_and_port)
set_docker_service_proxy(ip_and_port)
set_docekr_container_proxy(ip_and_port)
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