-
Notifications
You must be signed in to change notification settings - Fork 5
/
menu.py
183 lines (156 loc) · 6.3 KB
/
menu.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import subprocess
import os
import file_encryption
# Function to print colored text to the console
def print_color(text, color):
print(f"\033[1;{color}m{text}\033[0m")
# Function to start Nostpy relay
def start_nostpy_relay():
try:
#success, pass_holder = file_encryption.decrypt_file("./docker_stuff/.env")
#if not success:
# print(
# "Decryption failed, file is not encrypted please encrypt file and rerun command"
# )
# return
os.chdir("./docker_stuff")
file_path = "./postgresql/"
if os.path.exists(file_path):
subprocess.run(
["sudo", "setfacl", "-R", "-m", "u:relay_service:rwx", file_path],
check=True,
)
else:
print("File does not exist. Skipping the command.")
subprocess.run(["ls", "-al"], check=True)
subprocess.run(["groups", "relay_service"], check=True)
subprocess.run(
["sudo", "-u", "relay_service", "docker-compose", "up", "-d"], check=True
)
os.chdir("..")
# Re-encrypt env file to keep it encrypted when not in use
#file_encryption.encrypt_file(filename="./docker_stuff/.env", key=pass_holder)
except subprocess.CalledProcessError as e:
print_color(f"Error occurred: {e}", "31")
except Exception as e:
print(f"Error occurred during decryption: {e}")
return
# Function to destroy all Docker containers and images
def destroy_containers_and_images():
try:
# Change directory to the Docker stuff folder
os.chdir("./docker_stuff")
subprocess.run(
["sudo", "-u", "relay_service", "docker-compose", "down"], check=True
)
# Delete container images by their name
image_names = [
"docker_stuff_nostr_query:latest",
"docker_stuff_event_handler:latest",
"docker_stuff_websocket_handler:latest",
"redis:latest",
"postgres:latest",
"datadog/agent:latest",
]
for image_name in image_names:
subprocess.run(
[
"sudo",
"-u",
"relay_service",
"docker",
"image",
"rm",
"-f",
image_name,
],
check=True,
)
os.chdir("..")
except subprocess.CalledProcessError as e:
print_color(f"Error occurred: {e}", "31")
def stop_containers():
try:
# Change directory to the Docker stuff folder
os.chdir("./docker_stuff")
subprocess.run(
["sudo", "-u", "relay_service", "docker-compose", "down"], check=True
)
os.chdir("..")
except subprocess.CalledProcessError as e:
print_color(f"Error occurred: {e}", "31")
# Function to switch branches
def switch_branches():
try:
branch_name = input("Enter the name of the branch you want to switch to: ")
# Change branch
subprocess.run(["git", "checkout", branch_name], check=True)
except subprocess.CalledProcessError as e:
print_color(f"Error occurred: {e}", "31")
# Function to execute setup.py script
def execute_setup_script():
try:
subprocess.run(["python3", "build_env.py"], check=True)
except subprocess.CalledProcessError as e:
print_color(f"Error occurred: {e}", "31")
def decrypt_env():
while True:
print_color("\n1) Decrypt file", "32")
print_color("2) Encrypt file", "31")
print_color("3) Return to main menu", "33")
option = input(
"\nEnter 1 to decrypt, 2 to encrypt the file, or 3 to return to the main menu: \n"
)
if option == "1":
print_color("Decrypting your .env file", "32")
file_encryption.decrypt_file(encrypted_filename="./docker_stuff/.env")
elif option == "2":
print_color("Encrypting your .env file", "32")
file_encryption.encrypt_file(filename="./docker_stuff/.env")
elif option == "3":
print_color("Returning to main menu", "31")
break
else:
print_color("Invalid option. Please enter either 1, 2, or 3.", "31")
while True:
print_color(
"\n##########################################################################################",
"31",
)
print_color(
""" \n
███╗ ██╗ ██████╗ ███████╗████████╗██████╗ ██╗ ██╗
████╗ ██║██╔═══██╗██╔════╝╚══██╔══╝██╔══██╗╚██╗ ██╔╝
██╔██╗ ██║██║ ██║███████╗ ██║ ██████╔╝ ╚████╔╝
██║╚██╗██║██║ ██║╚════██║ ██║ ██╔═══╝ ╚██╔╝
██║ ╚████║╚██████╔╝███████║ ██║ ██║ ██║
╚═╝ ╚═══╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═╝ ╚═╝
""",
"34",
)
print("\nPlease select an option:\n")
print_color("1) Execute server setup script", "33")
print_color("2) Start Nostpy relay", "32")
print_color("3) Switch branches", "33")
print_color("4) Destroy all docker containers and images", "31")
print_color("5) Decrypt/encrypt .env file to edit", "33")
print_color("6) Stop all containers", "33")
print_color("7) Exit menu", "31")
options = {
"1": execute_setup_script,
"2": start_nostpy_relay,
"3": switch_branches,
"4": destroy_containers_and_images,
"5": decrypt_env,
"6": stop_containers,
"7": lambda: print_color("Exited menu", "31"),
}
try:
choice = input("\nEnter an option number (1-7): ")
if choice in options:
options[choice]()
if choice == "7":
print()
break
except ValueError:
print_color("Invalid choice. Please enter a valid option number.", "31")