-
Notifications
You must be signed in to change notification settings - Fork 0
/
DockerMempool
287 lines (235 loc) · 9.45 KB
/
DockerMempool
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
import os
import wget
import tarfile
import subprocess
import requests
import shutil
# Install Docker
docker_install_command = ["sudo", "apt", "install", "docker"]
subprocess.run(docker_install_command, check=True)
# Install Docker Compose
docker_compose_install_command = ["sudo", "apt", "install", "docker-compose"]
subprocess.run(docker_compose_install_command, check=True)
# Get the current user's username
current_user = os.getlogin()
# Construct the command to add the current user to the Docker group
add_user_to_docker_command = ["sudo", "usermod", "-aG", "docker", current_user]
# Run the command
subprocess.run(add_user_to_docker_command, check=True)
# Make docker-compose executable
docker_compose_chmod_command = ["sudo", "chmod", "+x", "/usr/bin/docker-compose"]
subprocess.run(docker_compose_chmod_command, check=True)
docker_compose_path = "/usr/bin/docker-compose"
if os.access(docker_compose_path, os.X_OK):
print("Docker Compose is executable.")
else:
print("Docker Compose is not executable.")
# Define the Git repository URL
repository_url = "https://github.com/mempool/mempool.git"
# Directory where you want to clone the repository
clone_directory = "mempool"
def clone_repository(url, directory):
try:
# Run the git clone command
subprocess.run(["git", "clone", url, directory], check=True)
print(f"Repository cloned successfully to {directory}")
except subprocess.CalledProcessError as e:
print(f"Error cloning the repository: {e}")
if __name__ == "__main__":
clone_repository(repository_url, clone_directory)
# Define file path, IP address, RPC username, and RPC password
file_path = "/home/humble/mempool/docker/docker-compose.yml"
ipaddress = "ip_address"
rpcusername = "rpc_username"
rpcpassword = "rpc_password"
# Delete the file with sudo privileges
delete_command = ["sudo", "rm", "-f", file_path]
try:
subprocess.run(delete_command, check=True)
print(f"File {file_path} deleted successfully.")
except subprocess.CalledProcessError as e:
print(f"Error deleting file {file_path}: {e}")
# Write the file content to the specified path with sudo privileges
file_content = f'''\
version: "3.7"
services:
web:
environment:
FRONTEND_HTTP_PORT: "8080"
BACKEND_MAINNET_HTTP_HOST: "api"
image: mempool/frontend:latest
user: "1000:1000"
restart: always
stop_grace_period: 1m
command: "./wait-for db:3306 --timeout=720 -- nginx -g 'daemon off;'"
ports:
- 4080:8080
api:
environment:
MEMPOOL_BACKEND: "fulcrum"
CORE_RPC_HOST: "{ipaddress}"
CORE_RPC_PORT: "8332"
CORE_RPC_USERNAME: "{rpcusername}"
CORE_RPC_PASSWORD: "{rpcpassword}"
DATABASE_ENABLED: "true"
DATABASE_HOST: "db"
DATABASE_DATABASE: "mempool"
DATABASE_USERNAME: "mempool"
DATABASE_PASSWORD: "mempool"
STATISTICS_ENABLED: "true"
image: mempool/backend:latest
user: "1000:1000"
restart: always
stop_grace_period: 1m
command: "./wait-for-it.sh db:3306 --timeout=720 --strict -- ./start.sh"
volumes:
- ./data:/backend/cache
db:
environment:
MYSQL_DATABASE: "mempool"
MYSQL_USER: "mempool"
MYSQL_PASSWORD: "mempool"
MYSQL_ROOT_PASSWORD: "admin"
image: mariadb:10.5.8
user: "1000:1000"
restart: always
stop_grace_period: 1m
volumes:
- ./mysql/data:/var/lib/mysql
'''
# Write the file content to the specified path with sudo privileges
write_command = ["sudo", "tee", file_path]
subprocess.run(write_command, input=file_content.encode(), check=True)
print(f"docker-compose.yml file created at: {file_path}")
# Change current working directory
docker_directory = "/home/humble/mempool/docker/"
os.chdir(docker_directory)
# Command to execute
docker_compose_up_command = ["sudo", "docker-compose", "up"]
# Command to enable the Docker service
enable_docker_command = 'sudo systemctl enable docker'
# Execute the enable Docker command using subprocess
try:
subprocess.run(enable_docker_command, shell=True, check=True)
print("Docker service enabled successfully.")
except subprocess.CalledProcessError as e:
print(f"Error enabling Docker service: {e}")
# Command to update the package list
update_command = "sudo apt-get update"
# Execute the command using subprocess
try:
subprocess.run(update_command, shell=True, check=True)
print("Package list updated successfully.")
except subprocess.CalledProcessError as e:
print(f"Error updating package list: {e}")
# Command to install the specified packages
install_command = "sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common unzip"
# Execute the command using subprocess
try:
subprocess.run(install_command, shell=True, check=True)
print("Packages installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error installing packages: {e}")
# Command to remove Docker-related packages
remove_command = "sudo apt-get remove -y docker docker-engine docker.io containerd runc"
# Execute the command using subprocess
try:
subprocess.run(remove_command, shell=True, check=True)
print("Docker-related packages removed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error removing Docker-related packages: {e}")
# Command to download the Docker GPG key and add it to apt-key
add_gpg_key_command = "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -"
# Execute the command using subprocess
try:
subprocess.run(add_gpg_key_command, shell=True, check=True)
print("Docker GPG key added successfully.")
except subprocess.CalledProcessError as e:
print(f"Error adding Docker GPG key: {e}")
# Command to add the Docker repository
add_repository_command = 'sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"'
# Execute the command using subprocess
try:
subprocess.run(add_repository_command, shell=True, check=True)
print("Docker repository added successfully.")
except subprocess.CalledProcessError as e:
print(f"Error adding Docker repository: {e}")
# Command to update the package list
update_command = "sudo apt-get update"
# Command to install Docker-related packages
install_docker_command = "sudo apt-get install -y docker-ce docker-ce-cli containerd.io"
# Execute the update command using subprocess
try:
subprocess.run(update_command, shell=True, check=True)
print("Package list updated successfully.")
except subprocess.CalledProcessError as e:
print(f"Error updating package list: {e}")
# Execute the install Docker command using subprocess
try:
subprocess.run(install_docker_command, shell=True, check=True)
print("Docker-related packages installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error installing Docker-related packages: {e}")
# Command to download and install Docker Compose
install_compose_command = (
'sudo curl -L "https://github.com/docker/compose/releases/download/2.24.0/'
'docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose'
)
# Execute the install Docker Compose command using subprocess
try:
subprocess.run(install_compose_command, shell=True, check=True)
print("Docker Compose installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error installing Docker Compose: {e}")
# Command to download and install Docker Compose
install_compose_command = (
'sudo curl -L "https://github.com/docker/compose/releases/download/2.24.0/'
'docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && '
'sudo chmod +x /usr/local/bin/docker-compose'
)
# Execute the install Docker Compose command using subprocess
try:
subprocess.run(install_compose_command, shell=True, check=True)
print("Docker Compose installed and made executable successfully.")
except subprocess.CalledProcessError as e:
print(f"Error installing Docker Compose: {e}")
import subprocess
def create_mempool_service(system_username):
file_path = "/etc/systemd/system/mempool.service"
content = '''\
[Unit]
Description=Mempool Docker Compose Stack
Requires=docker.service
After=docker.service
[Service]
ExecStart=/usr/bin/docker-compose up
ExecStop=/usr/bin/docker-compose down
WorkingDirectory=/home/humble/mempool/docker/
Restart=always
[Install]
WantedBy=multi-user.target
'''
# Write the content to a temporary file
temp_file_path = "/tmp/mempool.service"
with open(temp_file_path, 'w') as file:
file.write(content)
# Use sudo to move the temporary file to the target directory
move_command = ["sudo", "mv", temp_file_path, file_path]
subprocess.run(move_command, check=True)
# Set the correct ownership and permissions for the service file
chown_command = ["sudo", "chown", "root:root", file_path]
subprocess.run(chown_command, check=True)
chmod_command = ["sudo", "chmod", "644", file_path]
subprocess.run(chmod_command, check=True)
# Reload systemd configuration
reload_command = ["sudo", "systemctl", "daemon-reload"]
subprocess.run(reload_command, check=True)
# Enable the mempool service
enable_command = ["sudo", "systemctl", "enable", "mempool.service"]
subprocess.run(enable_command, check=True)
# Start the mempool service
start_command = ["sudo", "systemctl", "start", "mempool.service"]
subprocess.run(start_command, check=True)
if __name__ == "__main__":
create_mempool_service("humble") # Replace with your system username