-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_domain_column.py
More file actions
executable file
·140 lines (119 loc) · 3.83 KB
/
remove_domain_column.py
File metadata and controls
executable file
·140 lines (119 loc) · 3.83 KB
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
137
138
139
140
#!/usr/bin/env python3
import subprocess
import sys
# Your 'Nginx Proxy Manager' container name here
CONTAINER_NAME = "npm"
# Try to import MySQL driver
try:
import mysql.connector
except ImportError:
print("The package 'python3-mysql.connector' is required but not installed.")
print("Please install it using your package manager, e.g., 'apt install python3-mysql.connector'")
sys.exit(1)
# Command execution helper
def run_command(cmd):
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return result.stdout.strip(), result.stderr.strip(), result.returncode
# Checks if Docker container exists
def container_exists(name):
stdout, stderr, rc = run_command(["docker", "ps", "-a", "--format", "{{.Names}}"])
if rc != 0:
print(f"Error in querying Docker containers: {stderr}")
sys.exit(1)
return name in stdout.splitlines()
# Reads environment variables from inside the container.
def get_env_from_container(name):
stdout, stderr, rc = run_command(["docker", "exec", name, "env"])
if rc != 0:
print(f"Error reading environment variables from container '{name}': {stderr}")
sys.exit(1)
env = {}
for line in stdout.splitlines():
if "=" in line:
key, value = line.split("=", 1)
env[key] = value
return env
# Main function
def main():
# STEP 1: check container existence
if not container_exists(CONTAINER_NAME):
print(f"Container '{CONTAINER_NAME}' does not exist.")
sys.exit(1)
# STEP 2: get environment variables from container
env = get_env_from_container(CONTAINER_NAME)
# STEP 2.1: validate required env vars
required_vars = [
"DB_MYSQL_HOST",
"DB_MYSQL_PORT",
"DB_MYSQL_NAME",
"DB_MYSQL_USER",
"DB_MYSQL_PASSWORD",
]
missing = [v for v in required_vars if v not in env]
if missing:
print("Missing environment variables in container:")
print("\n".join(missing))
sys.exit(1)
# STEP 2.2: extract env vars
db_host = env["DB_MYSQL_HOST"]
db_port = int(env["DB_MYSQL_PORT"])
db_name = env["DB_MYSQL_NAME"]
db_user = env["DB_MYSQL_USER"]
db_password = env["DB_MYSQL_PASSWORD"]
# STEP 3: Try to connect to the database
try:
conn = mysql.connector.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
database=db_name,
ssl_disabled=True,
)
except mysql.connector.Error as err:
print(f"DB connection error: {err}")
sys.exit(1)
cursor = conn.cursor()
# STEP 4: Check if 'domain' column exists
try:
# STEP 4.1: Check column existence
cursor.execute(
"""
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = %s
AND TABLE_NAME = 'access_list_client'
AND COLUMN_NAME = 'domain'
""", (db_name,)
)
exists = cursor.fetchone()[0] > 0
# STEP 4.2: Skip if column does not exist
if not exists:
print("Column 'domain' does not exist, nothing to do.")
return
# STEP 4.3: Remove the 'domain' column
print("Removing column 'domain'...")
try:
cursor.execute(
"""
ALTER TABLE access_list_client
DROP COLUMN domain
"""
)
conn.commit()
print("Column 'domain' successfully removed.")
except mysql.connector.Error as err:
print(f"Error removing column: {err}")
sys.exit(1)
# STEP 5: Cleanup
finally:
cursor.close()
conn.close()
# Run main function
if __name__ == "__main__":
main()