-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTCpayserver
413 lines (306 loc) · 13.1 KB
/
BTCpayserver
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python3
import os
import wget
import tarfile
import subprocess
import requests
import shutil
import subprocess
def run_apt_update():
update_command = ["sudo", "apt", "update"]
try:
subprocess.run(update_command, check=True)
print("APT package list updated successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
run_apt_update()
import subprocess
def add_postgres_repository():
command = 'sudo sh -c \'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list\''
try:
subprocess.run(command, shell=True, check=True, executable='/bin/bash')
print("PostgreSQL repository added successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
add_postgres_repository()
import subprocess
def add_postgres_key():
command = 'wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -'
try:
subprocess.run(command, shell=True, check=True, executable='/bin/bash')
print("PostgreSQL key added successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
add_postgres_key()
import subprocess
def apt_update():
command = 'sudo apt update'
try:
subprocess.run(command, shell=True, check=True, executable='/bin/bash')
print("APT package list updated successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
apt_update()
import subprocess
def apt_update():
command = 'sudo apt update'
try:
subprocess.run(command, shell=True, check=True, executable='/bin/bash')
print("APT package list updated successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
apt_update()
import subprocess
def install_postgresql():
command = 'sudo apt install postgresql-16 postgresql-client-16'
try:
subprocess.run(command, shell=True, check=True, executable='/bin/bash')
print("PostgreSQL 16 and PostgreSQL client 16 installed successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
def download_and_install_package():
url = "https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb"
download_dir = os.path.expanduser('~/Downloads')
# Use wget to download the package
subprocess.run(["wget", url, "-O", "packages-microsoft-prod.deb"], cwd=download_dir)
# Install the package using dpkg
subprocess.run(["sudo", "dpkg", "-i", "packages-microsoft-prod.deb"], cwd=download_dir)
# Remove the downloaded package file
os.remove(os.path.join(download_dir, "packages-microsoft-prod.deb"))
# Call the function to download and install the package
download_and_install_package()
def update_and_install_packages():
# Update package lists
subprocess.run(["sudo", "apt-get", "update"])
# Install apt-transport-https package
subprocess.run(["sudo", "apt-get", "install", "-y", "apt-transport-https"])
# Update package lists again
subprocess.run(["sudo", "apt-get", "update"])
# Install dotnet-sdk-8.0 package
subprocess.run(["sudo", "apt-get", "install", "-y", "dotnet-sdk-8.0"])
# Call the function to update and install the packages
update_and_install_packages()
chosen_username = "BTCpayusername" # Replace with the desired username
def create_postgres_user(superuser='n', databases='y', more_roles='n'):
# Print the default parameter values
print(f"superuser='{superuser}', databases='{databases}', more_roles='{more_roles}'")
# Print the command to create a new user interactively
print('createuser --pwprompt --interactive')
# Construct the command with additional options
command = f'createuser --interactive --superuser={superuser} --createdb={databases} --no-createrole={more_roles}'
# Run the createuser command with additional options
subprocess.run(["sudo", "-i", "-u", "postgres", command])
def create_btcpayserver_database():
# Print the command to create the btcpayserver database
print(f'createdb -O "{chosen_username}" btcpayserver')
def create_nbxplorer_database():
# Print the command to create the nbxplorer database
print(f'createdb -O "{chosen_username}" nbxplorer')
# Call the functions to perform the actions
create_postgres_user()
create_btcpayserver_database()
create_nbxplorer_database()
def install_postgresql():
# Install PostgreSQL and PostgreSQL-contrib packages
subprocess.run(["sudo", "apt", "install", "-y", "postgresql", "postgresql-contrib"])
# Switch to the postgres user with a new shell session
subprocess.run(["sudo", "-i", "-u", "postgres"])
# Call the function to install PostgreSQL and switch to the postgres user
install_postgresql()
def create_source_directory():
source_dir = os.path.expanduser('~/source')
os.makedirs(source_dir, exist_ok=True)
def clone_nbxplorer_repository():
source_dir = os.path.expanduser('~/source')
os.chdir(source_dir)
subprocess.run(["git", "clone", "https://github.com/dgarage/NBXplorer"])
# Call the function to create the "source" directory
create_source_directory()
# Call the function to clone the NBXplorer repository
clone_nbxplorer_repository()
def remove_dotnet():
subprocess.run(["sudo", "apt", "remove", "dotnet*"])
# Call the function to remove the dotnet package
remove_dotnet()
def remove_netstandard_package():
subprocess.run(["sudo", "apt", "remove", "netstandard*"])
# Call the function to remove the netstandard package
remove_netstandard_package()
def remove_aspnetcore_packages():
subprocess.run(["sudo", "apt", "remove", "aspnetcore*"])
# Call the function to remove the aspnetcore* packages
remove_aspnetcore_packages()
def create_preferences_file():
preferences_dir = '/etc/apt/preferences.d'
preferences_file = os.path.join(preferences_dir, '99microsoft-dotnet.pref')
contents = '''Package: *
Pin: origin "packages.microsoft.com"
Pin-Priority: 1001
'''
subprocess.run(["sudo", "sh", "-c", f'echo "{contents}" > {preferences_file}'])
# Call the function to create the preferences file
create_preferences_file()
def install_dotnet_sdk():
subprocess.run(["sudo", "apt", "install", "dotnet-sdk-8.0"])
# Call the function to install the dotnet-sdk-8.0 package
install_dotnet_sdk()
def build_nbxplorer():
nbxplorer_dir = os.path.expanduser('~/source/NBXplorer')
os.chdir(nbxplorer_dir)
subprocess.run(["./build.sh"])
# Call the function to build NBXplorer
build_nbxplorer()
def create_nbxplorer_directory():
nbxplorer_dir = os.path.expanduser('~/.nbxplorer/Main')
os.makedirs(nbxplorer_dir, exist_ok=True)
# Call the function to create the ~/.nbxplorer/Main directory
create_nbxplorer_directory()
import os
def create_settings_file():
nbxplorer_dir = os.path.expanduser('~/.nbxplorer/Main')
settings_file = os.path.join(nbxplorer_dir, 'settings.config')
with open(settings_file, 'w') as file:
file.write('# Add your settings here\n')
# Call the function to create the settings.config file
create_settings_file()
def create_nbxplorer_directory():
nbxplorer_dir = os.path.expanduser('~/.nbxplorer/Main')
os.makedirs(nbxplorer_dir, exist_ok=True)
# Call the function to create the ~/.nbxplorer/Main directory
create_nbxplorer_directory()
def create_settings_file():
nbxplorer_dir = os.path.expanduser('~/.nbxplorer/Main')
settings_file = os.path.join(nbxplorer_dir, 'settings.config')
rpcusername = "rpc_username"
rpcpassword = "rpc_password"
database_username = "BTCpayusername"
database_password = "BTCpaypassword"
btc_rpc_auth = f"btc.rpc.auth={rpcusername}:{rpcpassword}"
port = 24445
mainnet = 1
postgres = f"postgres=User ID={database_username};Password={database_password};Host=localhost;Port=5432;Database=nbxplorer;"
with open(settings_file, 'w') as file:
file.write(f'{btc_rpc_auth}\n')
file.write(f'port={port}\n')
file.write(f'mainnet={mainnet}\n')
file.write(f'{postgres}\n')
# Call the function to create the settings.config file
create_settings_file()
def create_nbxplorer_service():
ubuntu_username = "humble"
service_config = f'''[Unit]
Description=NBXplorer daemon
Requires=bitcoind.service
After=bitcoind.service
[Service]
ExecStart=/usr/bin/dotnet "/home/{ubuntu_username}/source/NBXplorer/NBXplorer/bin/Release/net8.0/NBXplorer.dll" -c /home/{ubuntu_username}/.nbxplorer/Main/settings.config
User={ubuntu_username}
Group={ubuntu_username}
Type=simple
PIDFile=/run/nbxplorer/nbxplorer.pid
Restart=on-failure
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
PrivateDevices=true
[Install]
WantedBy=multi-user.target
'''
service_file_path = '/etc/systemd/system/nbxplorer.service'
# Use sudo to create the nbxplorer.service file
subprocess.run(["sudo", "sh", "-c", f'echo "{service_config}" > {service_file_path}'])
# Call the function to create the nbxplorer.service file
create_nbxplorer_service()
def enable_nbxplorer_service():
subprocess.run(["sudo", "systemctl", "enable", "nbxplorer.service"])
# Call the function to enable the nbxplorer.service
enable_nbxplorer_service()
def start_nbxplorer_service():
subprocess.run(["sudo", "systemctl", "start", "nbxplorer.service"])
# Call the function to start the nbxplorer.service
start_nbxplorer_service()
# Define the URL of the GitHub repository
github_repo_url = "https://github.com/btcpayserver/btcpayserver.git"
# Define the target directory where you want to clone the repository
source_directory = os.path.expanduser("~/source/")
# Check if the source directory exists, and create it if it doesn't
if not os.path.exists(source_directory):
os.makedirs(source_directory)
# Change the current working directory to the source directory
os.chdir(source_directory)
# Run the 'git clone' command to clone the repository
try:
subprocess.run(["git", "clone", github_repo_url], check=True)
print("BTCPayServer repository cloned successfully.")
# Change the current working directory to the cloned repository
os.chdir("btcpayserver")
# Run the './build.sh' script
try:
subprocess.run(["./build.sh"], check=True)
print("Build script executed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error executing build script: {e}")
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
# Define the target directory
btcpayserver_directory = os.path.expanduser("~/.btcpayserver/Main")
# Define the BTCpayusername and BTCpaypassword
database_username = "BTCpayusername"
database_password = "BTCpaypassword"
# Define the content of the settings.conf file with the predefined variables
settings_content = f"""network=mainnet
port=23001
bind=0.0.0.0
chains=btc
BTC.explorer.url=http://127.0.0.1:24445
#BTC.lightning=type=lnd-rest;server=https://127.0.0.1:8080/;macaroonfilepath=/home/humble/.lnd/data/chain/bitcoin/mainnet/admin.macaroon;certthumbprint=BC5BDE3FEF35EECDA838C4DAFA01FC>
postgres=User ID="{database_username}";Password="{database_password}";Host=localhost;Port=5432;Database=btcpayserver;
"""
# Create the directory if it doesn't exist
if not os.path.exists(btcpayserver_directory):
os.makedirs(btcpayserver_directory)
# Define the path to the settings.conf file
settings_file_path = os.path.join(btcpayserver_directory, "settings.config")
# Write the content to the settings.config file
try:
with open(settings_file_path, "w") as settings_file:
settings_file.write(settings_content)
print(f"settings.config file created successfully at {settings_file_path}")
except Exception as e:
print(f"Error creating settings.conf file: {e}")
def create_btcpayserver_service():
ubuntu_username = "humble" # Replace with your Ubuntu username
service_config = f'''[Unit]
Description=BtcPayServer daemon
Requires=btcpayserver.service
After=nbxplorer.service
[Service]
ExecStart=/usr/bin/dotnet run --no-launch-profile --no-build -c Release -p "/home/{ubuntu_username}/source/btcpayserver/BTCPayServer/BTCPayServer.csproj" -- $@
User="{ubuntu_username}"
Group="{ubuntu_username}"
Type=simple
PIDFile=/run/btcpayserver/btcpayserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
'''
service_file_path = '/etc/systemd/system/btcpayserver.service'
# Use sudo to create the btcpayserver.service file
subprocess.run(["sudo", "sh", "-c", f'echo "{service_config}" > {service_file_path}'])
# Call the function to create the btcpayserver.service file
create_btcpayserver_service()
def enable_btcpayserver_service():
subprocess.run(["sudo", "systemctl", "enable", "btcpayserver.service"])
# Call the function to enable the btcpayserver.service
enable_btcpayserver_service()
def start_btcpayserver_service():
subprocess.run(["sudo", "systemctl", "start", "btcpayserver.service"])
# Call the function to start the btcpayserver.service
start_btcpayserver_service()
if __name__ == "__main__":
install_postgresql()