-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployWordpress.py
136 lines (119 loc) · 4.04 KB
/
deployWordpress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from kubernetes import client, config as k8s_config
import yaml
import sys
import requests
import subprocess
import mysql.connector
import configparser
# home path
home_path = '/home/tommygood/telegram_bot'
# config
config = configparser.ConfigParser()
config.read(home_path + '/config.ini')
token = config["env"]["bot_token"]
# ip address
ip = config["env"]["host"]
# token
token = config["env"]["bot_token"]
# db
db_user = config["db"]["user"]
db_password = config["db"]["password"]
db_host = config["db"]["host"]
db_port = config["db"]["port"]
db = config["db"]["database"]
# config path
config_path = config["env"]["k8s_config"]
# kubctl path
kubectl_path = config["env"]["kubectl_path"]
k8s_config.load_kube_config(config_file=config_path)
app_name = sys.argv[1]
namespace = sys.argv[2]
replicas = sys.argv[3]
try:
# load yaml
with open(f"{home_path}/wp_deploy.yaml","r") as file:
yaml_file = yaml.safe_load(file)
yaml_file["metadata"]["name"] = app_name
yaml_file["spec"]["selector"]["matchLabels"]["app"] = app_name
yaml_file["spec"]["template"]["metadata"]["labels"]["app"] = app_name
yaml_file["spec"]["replicas"] = int(replicas)
# create deployment
v1 = client.AppsV1Api()
v1.create_namespaced_deployment(
body = yaml_file,
namespace = namespace
)
# 已使用的NodePort
v1 = client.CoreV1Api()
services = v1.list_service_for_all_namespaces().items
used_ports = []
for service in services:
if service.spec.type == 'NodePort' and service.spec.ports:
for port in service.spec.ports:
if port.node_port is not None:
used_ports.append(port.node_port)
# 找未使用的NodePort
port = 30000
while port <= 32767:
if port not in used_ports:
break
port += 1
if port > 32767: # port超過可用範圍
# 刪除deployment
command = f"{kubectl_path} delete deployment {app_name}"
output = subprocess.check_output(command, shell=True, text=True)
conn = mysql.connector.connect(
user = db_user,
password = db_password,
host = db_host,
port = db_port,
database = db
)
cur = conn.cursor()
sql = "delete from all_wordpress where app_name = %s;"
cur.execute(sql,(app_name,))
conn.commit()
chat_id = sys.argv[4]
message = "wordpress建置失敗,因為nodePort數量不夠!"
url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={message}"
requests.get(url)
else:
with open(f"{home_path}/wp_service.yaml","r") as file:
yaml_file = yaml.safe_load(file)
yaml_file["metadata"]["name"] = "wordpress-service"+app_name
yaml_file["spec"]["selector"]["app"] = app_name
yaml_file["spec"]["ports"][0]["nodePort"] = port
# create service
v1 = client.CoreV1Api()
v1.create_namespaced_service(
body=yaml_file,
namespace = namespace
)
# 使用者 id
chat_id = sys.argv[4]
# 訊息
message = "WordPress建置完成!\n網址為 "+ip+":"+str(port)
url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={message}"
requests.get(url)
except Exception as e :
try:
# 刪除deployment
command = f"{kubectl_path} delete deployment {app_name}"
output = subprocess.check_output(command, shell=True, text=True)
except:
pass
chat_id = sys.argv[4]
message = "WordPress建置失敗!\n請重新建置!\n" + str(e)
url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={message}"
requests.get(url)
conn = mysql.connector.connect(
user = db_user,
password = db_password,
host = db_host,
port = db_port,
database = db
)
cur = conn.cursor()
sql = "delete from all_wordpress where app_name = %s;"
cur.execute(sql,(app_name,))
conn.commit()