Commit c5034271 authored by tarak.li's avatar tarak.li

添加商城多次部署处理逻辑

parent 88928a50
......@@ -40,12 +40,7 @@ def login(path, username, password, host):
resp = session.get(url)
# print "同意使用協議"
# url = "http://{}".format(host)
# print(url)
# resp = session.get("http://{}".format(host))
# print(resp.text)
resp = session.get("http://{}/soft".format(host))
# resp = session.post("http://121.196.33.88:8888/site?action=GetPHPVersion", data={"action": "GetPHPVersion"})
cookie_token = session.cookies.get_dict().get("request_token", "")
http_token = between(resp.text, 'token="', '"').encode()
......@@ -59,35 +54,27 @@ def add_site(site, host):
url = "http://{}/site?action=AddSite".format(host)
user = 'sql{}'.format(site.replace(".", "_"))
webname = json.dumps({"domain": site, "domainlist": [], "count": 1})
print(webname)
# webname_1=&ps=121.196.33.88&path=%2Fwww%2Fwwwroot%2F121.196.33.88&datauser=sql121_196_33_8&datapassword=3ZsfeZeTdf&version=56&port=80&webname={"domain":"121.196.33.88","domainlist":[],"count":1}&ftp=false&sql=true&address=localhost&codeing=utf8&version=56
# data = """webname_1=121.196.33.88&ps=121.196.33.88&path=%2Fwww%2Fwwwroot%2F121.196.33.88&datauser=admin&datapassword=admin&version=56&port=80&webname={"domain":"121.196.33.88","domainlist":[],"count":1}&ftp=false&sql=true&address=localhost&codeing=utf8&version=56"""
data = {"webname_1": site, "ps": site, "path": "/www/wwwroot/{}".format(site),
"datauser": user, "datapassword": "123456", "version": "56", "port": "80",
"webname": webname, "ftp": "false", "sql": "true",
"address": "localhost", "codeing": "utf8"}
# UxHerzgeXvveWb9zRw7PlsmMfZEe6TWfwBXAkazAXmi3Cstp
# cookie_token = session.cookies.get_dict().get("request_token", "")
# http_token = between(resp.text, 'token="', '"')
# session.headers.update({"x-cookie-token": cookie_token, "x-http-token": http_token})
resp = session.post(url, data=data)
# {"status": false, "msg": "\u60a8\u6dfb\u52a0\u7684\u7ad9\u70b9\u5df2\u5b58\u5728!"}
print(resp.text)
# 正常添加返回
# {"ftpStatus": false, "databaseUser": "sql121_196_33_88", "databaseStatus": true, "databasePass": "123456", "siteId": 6, "siteStatus": true}
# 异常添加返回
# {"status": false, "msg": "\u60a8\u6dfb\u52a0\u7684\u7ad9\u70b9\u5df2\u5b58\u5728!"}
return True
def del_site(ip, site):
url = "http://{}/data?action=getData".format(ip)
# tojs=site.get_list&table=sites&limit=15&p=1&search=&order=id desc&type=-1
data = {"tojs": "site.get_list", "table": "sites", "limit": 15, "p": 1, "search": "", "order": "id desc",
"type": -1}
print(url)
resp = session.post(url, data)
print(resp.json())
for item in resp.json().get("data", []):
if item.get("name") == site:
if item.get("name") == site or not site:
print("删除站点:{}".format(item.get("name", "")))
url = "http://{}/site?action=DeleteSite".format(ip)
# data = "id=1&webname=site"
data = {"database": 1, "path": 1, "webname": site, "id": item.get("id")}
......@@ -96,16 +83,11 @@ def del_site(ip, site):
def install_dbshop(site, host):
# http://121.196.33.88:8888/deployment?action=SetupPackage
# dname=DBShop&site_name=121.196.33.88&php_version=56
url = "http://{}/deployment?action=GetList".format(host)
data = {"type": 0}
session.post(url, data)
# http://121.196.33.88:8888/deployment?action=SetupPackage
url = "http://{}/deployment?action=SetupPackage".format(host)
# data = "dname=DBShop&site_name=121.196.33.88&php_version=56"
# dname=DBShop&site_name=121.196.33.88&php_version=56
data = {"dname": "DBShop", "site_name": site, "php_version": "56"}
i = 0
while True:
......@@ -120,7 +102,6 @@ def install_dbshop(site, host):
break
time.sleep(10)
# {"status": true, "msg": {"db_config": "", "run_path": "/", "php_versions": "54,55,56,70,71,72,73", "admin_username": "", "success_url": "/install", "chmod": [], "remove_file": [], "php_ext": ["fileinfo"], "admin_password": ""}}
# {"status": true, "msg": {"db_config": "", "run_path": "/", "php_versions": "54,55,56,70,71,72,73", "admin_username": "", "success_url": "/install", "chmod": [], "remove_file": [], "php_ext": ["fileinfo"], "admin_password": ""}}
# http://121.196.33.88:8888/deployment?action=GetSpeed
# {"pre": 0, "total": 0, "speed": 0, "name": "\u5b89\u88c5\u5fc5\u8981\u7684PHP\u6269\u5c55", "used": 0}
......@@ -138,7 +119,6 @@ def install_dbshop(site, host):
def mysql_root_pwd(ip):
url = "http://{}/data?action=getKey".format(ip)
# 獲取密碼 table=config&key=mysql_root&id=1
data = {"table": "config", "key": "mysql_root", "id": "1"}
resp = session.post(url, data)
pwd = resp.text
......@@ -147,10 +127,9 @@ def mysql_root_pwd(ip):
def shop_config(host, site, webname="DBShop电子商务系统"):
# http://121.196.33.88/install
pwd = mysql_root_pwd(host)
host = host.replace(":8888", "")
print(host)
# 等待时间让网页进行加载
time.sleep(10)
session.get("http://{}/install".format(host))
session.get("http://{}/install/Install/installStep/step_1".format(host))
......@@ -197,115 +176,165 @@ def check_shop(site):
resp = requests.get(url)
resp.encoding = "utf-8"
code = resp.status_code
print("check_shop", resp.content)
# print("check_shop", resp.content)
return code == 200 and '用户中心' in resp.content and '登录' in resp.content
except:
code = 0
return False
def update_nginx(ip, site):
# /www/server/panel/vhost/nginx/121.196.33.88.conf
url = "http://{}/files?action=GetFileBody".format(ip)
data = {"path": "/www/server/panel/vhost/nginx/{}.conf".format(site)}
def get_domain(ip, id):
url = "http://{}/data?action=getData".format(ip)
data = {"table": "domain", "list": True, "search": id}
resp = session.post(url, data)
print("nginx", resp.text)
body = r"""server
{
listen 80;
server_name %(site)s;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/%(site)s;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-56.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/%(site)s.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
access_log /www/wwwlogs/%(site)s.log;
error_log /www/wwwlogs/%(site)s.error.log;
}
server {
#===> 监听端口
listen 80;
print("get_domain", [i.get("name", "") for i in resp.json()], resp.text)
return [i.get("name", "") for i in resp.json()]
# 编码
charset utf-8;
#===> 域名可以有多个,用空格隔开,本地测试先修改/etc/hosts,xxxx表示伪装层域名
server_name %(site)s;
def check_all_domain(ip):
url = "http://{}/data?action=getData".format(ip)
# tojs=site.get_list&table=sites&limit=15&p=1&search=&order=id desc&type=-1
data = {"tojs": "site.get_list", "table": "sites", "limit": 15, "p": 1, "search": "", "order": "id desc",
"type": -1}
# 支持读取非nginx标准的用户自定义header
underscores_in_headers on;
resp = session.post(url, data)
is_exists_shop_id = 0
name = ""
for item in resp.json().get("data", []):
id = item.get("id", 0)
name = item.get("name", "")
for domain in get_domain(ip, id):
if check_shop(domain):
is_exists_shop_id = id
# 定义本虚拟主机的访问日志
access_log logs/pay_access.log combined buffer=1k;
error_log logs/pay_error.log info;
print("{} 已存在商城 {}".format(domain, name))
break
location = /favicon.ico {
log_not_found off;
access_log off;
}
if is_exists_shop_id:
break
location /robots.txt {
return 200 'User-agent: *\nDisallow: /';
}
return is_exists_shop_id, name
resolver 8.8.8.8;
location /proxy_dynamic {
rewrite ^/proxy_dynamic/(https?)/([^/]+)/(\d+)/(.*) /$4 break;
proxy_pass $1://$2:$3;
proxy_set_header Host $2;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_ssl_session_reuse off;
}
# 收银台地址
location / {
proxy_pass https://cashier.paycoat.com;
proxy_set_header Referer $http_referer;
#proxy_set_header Host $http_host;
proxy_buffers 256 4k;
def sub_domain(site):
if site.count('.') == 1:
pay_site = 'pay.' + site
elif site.count('.') == 2:
pay_site = 'pay.' + site.split(".", 1)[1]
else:
pay_site = site
return pay_site
def nginx_conf(domain, sub_domain, site):
body = r"""server
{
listen 80;
server_name %(site)s;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/%(path)s;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-56.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/%(path)s.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
access_log /www/wwwlogs/%(path)s.log;
error_log /www/wwwlogs/%(path)s.error.log;
}
}""" % {"site": site}
source_body = resp.text
server {
#===> 监听端口
listen 80;
# 编码
charset utf-8;
#===> 域名可以有多个,用空格隔开,本地测试先修改/etc/hosts,xxxx表示伪装层域名
server_name %(pay_site)s;
# 支持读取非nginx标准的用户自定义header
underscores_in_headers on;
# 定义本虚拟主机的访问日志
access_log logs/pay_access.log combined buffer=1k;
error_log logs/pay_error.log info;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location /robots.txt {
return 200 'User-agent: *\nDisallow: /';
}
resolver 8.8.8.8;
location /proxy_dynamic {
rewrite ^/proxy_dynamic/(https?)/([^/]+)/(\d+)/(.*) /$4 break;
proxy_pass $1://$2:$3;
proxy_set_header Host $2;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_ssl_session_reuse off;
}
# 收银台地址
location / {
proxy_pass https://cashier.paycoat.com;
proxy_set_header Referer $http_referer;
#proxy_set_header Host $http_host;
proxy_buffers 256 4k;
}
}""" % {"site": domain.encode(), "pay_site": sub_domain.encode(), "path": site.encode()}
return body
def update_nginx(ip, site):
# /www/server/panel/vhost/nginx/121.196.33.88.conf
url = "http://{}/files?action=GetFileBody".format(ip)
data = {"path": "/www/server/panel/vhost/nginx/{}.conf".format(site)}
resp = session.post(url, data)
print("nginx", resp.text)
pay_site = sub_domain(site)
body = nginx_conf(site, pay_site, site)
url = "http://{}/files?action=SaveFileBody".format(ip)
# encoding: utf-8
data = {"data": body, "path": "/www/server/panel/vhost/nginx/{}.conf".format(site), "encoding": "utf-8"}
......@@ -315,7 +344,9 @@ server {
def install_shop(ip, site, webname):
del_site(ip, site)
# del_site(ip, site)
# 删除所有站点
del_site(ip, "")
add_site(site, ip)
print("添加站点成功")
install_dbshop(site, ip)
......@@ -332,14 +363,53 @@ def add_site_domain(id, ip, site, webname):
data = {"table": "domain", "list": True, "search": id}
resp = session.post(url, data)
# domain=121.196.33.88&webname=www.gingboutrade.com&id=13
url = "http://{}/site?action=AddDomain".format(ip)
data = {"domain": site, "webname": webname, "id": id}
session.post(url, data)
print(resp.text)
resp = session.post(url, data)
print(resp.content)
return
def add_nginx(ip, id, site):
sites = get_domain(ip, id)
url = "http://{}/files?action=GetFileBody".format(ip)
data = {"path": "/www/server/panel/vhost/nginx/{}.conf".format(site)}
resp = session.post(url, data)
print("nginx", resp.text)
main_domain = ' '.join(sites)
pay_domain = ' '.join([sub_domain(domain) for domain in sites])
print("domain", main_domain, "pay_domain", pay_domain)
body = nginx_conf(main_domain, pay_domain, site)
print("body", body)
url = "http://{}/files?action=SaveFileBody".format(ip)
# encoding: utf-8
data = {"data": body, "path": "/www/server/panel/vhost/nginx/{}.conf".format(site), "encoding": "utf-8"}
resp = session.post(url, data=data)
print("nginx save", resp.text)
return True
def run(ip, path, user, pwd, site, webname):
print(login(path, user, pwd, ip))
print("完成宝塔登录配置")
if check_shop(site):
print("商城已存在")
return
shop_id, name = check_all_domain(ip)
if shop_id:
print("商城已存在", shop_id, "name", name)
add_site_domain(shop_id, ip, site, webname)
add_nginx(ip, shop_id, name)
return
install_shop(ip, site, webname)
def main():
parser = argparse.ArgumentParser()
# parser.add_argument("action", choices=['start', 'stop', 'restart'])
......@@ -356,23 +426,12 @@ def main():
print(args.path, args.user, args.pwd, args.site)
path = args.path or ''
print(login(path, args.user, args.pwd, ip))
print("完成宝塔登录配置")
if check_shop(args.site):
print("商城已存在")
return
install_shop(ip, args.site, args.webname)
run(ip, path, args.user, args.pwd, args.site, args.webname)
if __name__ == '__main__':
main()
# run("121.196.33.88:8888", path='e7393047', pwd='93395d57', site='www.gingboutrade.com', user='oag8vyv9',
# webname='测试')
# check_shop("121.196.33.88")
# http://121.196.33.88:8888/b950f1a0
# login("b950f1a0", "wuuqmrmz", "68d64b2a", "121.196.33.88:8888")
# update_nginx("121.196.33.88:8888", "121.196.33.88")
# http://121.196.33.88:8888/c21e863d
# d3ixnjie 57eda37a
# s = "121.196.33.88".replace('.', '_')
# print s
......@@ -68,8 +68,6 @@ def read_local_ip():
def update_local_proxy(conf, local):
proxy = conf.get("Proxy", [])
proxys = [item.get("name", "") for item in proxy]
print("proxys", proxys)
print(local)
_local = []
for k, v in local.items():
if k not in proxys and len(v.split(":")) == 2:
......
......@@ -85,6 +85,9 @@ def auto_install_bt(ip, username, password, site, webname, host_id=0):
print("更新商城信息")
update_shop_info(site, webname, host_id, status=1, remark="商城部署成功", url=site)
if "一键配置商城信息失败" in result:
update_shop_info(site, webname, host_id, status=3, remark="商城部署失败,请重试", url=site)
except AuthenticationException as err:
update_shop_info(site, webname, host_id, status=3, remark="连接失败,请检查远程信息")
finally:
......@@ -108,6 +111,6 @@ def main():
if __name__ == '__main__':
# push_file('121.196.33.88', 'root', 'Aa123456')
# auto_install_bt('121.196.33.88', 'root', 'Aa123456', "www.gingboutrade.com", "测试商城")
auto_install_bt('121.196.33.88', 'root', 'Aa123456', "www.gingboutrade.com", "测试商城")
# update_shop_info(site="www.test.com", webname="测试商城1", test="")
main()
# main()
......@@ -135,7 +135,7 @@ def free_host():
db = get_db()
sql = """select a.id as id
from tp_remote_host a LEFT join tp_remote_account_host_bind b on a.id = b.host_id
where b.id is null"""
where b.id is null and os_type=1"""
if host_id_lock:
not_id = ' and a.id not in ({})'.format(','.join([str(id) for id in host_id_lock.keys()]))
......
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