Skip to content

Commit

Permalink
Cleanup for v0.8 (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
UTXOnly authored Feb 24, 2024
1 parent e354c33 commit c5464ba
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 73 deletions.
25 changes: 15 additions & 10 deletions ReadME.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

![Pylint_score](./pylint.svg)

[![Ubuntu Build](http://54.144.142.244:8080/buildStatus/icon?job=nostpy-build-containers-docker-compose-up&subject=Ubuntu%20Build)](http://54.144.142.244:8080/job/nostpy-build-containers-docker-compose-up/)

[![Ubuntu Build](http://54.144.142.244:8080/buildStatus/icon?job=nostpy-build-containers-docker-compose-up/branch/optimize&subject=Dev%20Branch)](http://54.144.142.244:8080/job/nostpy-build-containers-docker-compose-up/)


A simple and easy to deploy nostr relay using `asyncio` & `websockets` to server Nostr clients
A purely Python, easy to deploy nostr relay using `asyncio` & `websockets` to server Nostr clients

## Description

![Image 2023-09-15 at 9 53 46 AM](https://github.com/UTXOnly/nost-py/assets/49233513/724cfbeb-03a0-4d10-b0d1-6b638ac153c4)



A containerized Python relay paried with a Postgres databse, reachable via a NGINX reverse proxy. This has been tested on [Coracle](https://coracle.social), [Iris.to](https://Iris.to) and [Snort.social](https://Snort.social) clients and works for the NIPS listed below.
A containerized Python relay paried with a Postgres databse, reachable via a NGINX reverse proxy. This has been tested on [Nostrudel](https://nostrudel.ninja/), [Iris.to](https://Iris.to) and [Snort.social](https://Snort.social) clients and works for the NIPS listed below.

Numerous branches in development, trying to improve performance, reliability and ease of use. The Datadog branch deploys a Datadog agent container to collect logs, metrics and traces to better observe application performance.

Expand Down Expand Up @@ -45,6 +40,9 @@ DD_API_KEY=<YOUR_DATADOG_API_KEY>
DOMAIN_NAME=<YOUR_DOMAIN_NAME>
HEX_PUBKEY=<YOUR_HEX_PUBLIC_KEY_FOR_NIP_11>
CONTACT=<YOUR_EMAIL_OR_NPUB>
ENV_FILE_PATH=./docker_stuff/.env
NGINX_FILE_PATH=/etc/nginx/sites-available/default
VERSION=v0.8
```

Expand All @@ -58,12 +56,18 @@ python3 menu.py

This will bring up the menu below and you can control the program from there!

**Usage notes**
* Option 1 `Execute server setup script` needs to be run to create the `relay_service` user and set proper file permissions
* After creating the `.env` that file is meant to stay encrypted(encrypted suring `Execute server script`), option2 `Start Nostpy relay` will not run unless the file is encypted
* You can encrypt/decrypt the file with option 5



![Image 2023-09-15 at 8 44 45 AM](https://github.com/UTXOnly/nost-py/assets/49233513/ee40d91c-2e6a-48a8-a0a8-c14e25e8ff07)


![image](https://github.com/UTXOnly/nost-py/assets/49233513/c970f4a8-8af3-4b23-a6fe-3fc9bac49ec0)
![Screenshot from 2024-02-23 21-15-49](https://github.com/UTXOnly/nost-py/assets/49233513/2119a053-3ebf-42b5-a996-2ccb87651c9e)



### Install demo
Expand All @@ -83,14 +87,15 @@ This is relay is actively worked on, it's is only a proof of concept right now.
- [x] NIP-01: Basic protocol flow description
- [x] NIP-02: Contact list and petnames
- [x] NIP-04: Encrypted Direct Message
- [] NIP-09: Event deletion
- [x] NIP-09: Event deletion
- [x] NIP-11: Relay information document
- [] NIP-11a: Relay Information Document Extensions
- [] NIP-12: Generic tag queries
- [] NIP-13: Proof of Work
- [x] NIP-15: End of Stored Events Notice
- [x] NIP-16: Event Treatment
- [x] NIP-25: Reactions
- [x] NIP-50: Search Capability
- [x] NIP-99: Classified Listings

### Contributing

Expand Down
32 changes: 23 additions & 9 deletions build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

try:
subprocess.run(["sudo", "apt-get", "update", "-y"], check=True)
subprocess.run(["sudo", "apt-get", "install", "-y", "docker-compose", "nginx", "certbot", "python3.10-venv", "python3-certbot-nginx", "acl"], check=True)

subprocess.run(['python3', '-m', 'venv', 'snmpenv'], check=True)

subprocess.run(
[
"sudo",
"apt-get",
"install",
"-y",
"docker-compose",
"nginx",
"certbot",
"python3.10-venv",
"python3-certbot-nginx",
"acl",
],
check=True,
)

subprocess.run(["python3", "-m", "venv", "snmpenv"], check=True)

# Activate the virtual environment and run subsequent commands within it (runs prepare server script in venv)
activate_cmd = '. snmpenv/bin/activate && '
activate_cmd = ". snmpenv/bin/activate && "
commands = [
'pip install --upgrade pip',
'pip install -r requirements.txt',
'python prepare_server.py'
"pip install --upgrade pip",
"pip install -r requirements.txt",
"python prepare_server.py",
]
for cmd in commands:
subprocess.run(['sudo', 'bash', '-c', activate_cmd + cmd], check=True)
subprocess.run(["sudo", "bash", "-c", activate_cmd + cmd], check=True)

except subprocess.CalledProcessError as e:
print(f"An error occurred while executing the command: {e}")
12 changes: 12 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## v0.8

**Enhancements**
* Completed feature request [#37 [FR] Support for NIP-50 (search capability) for both content and tags](https://github.com/UTXOnly/nost-py/issues/37)
* Support for [NIP-50](https://github.com/nostr-protocol/nips/blob/master/50.md#nip-50) search queries
* Searches `tags` and `content` fields
* Compatible with searching [NIP-99 Classified Listings](https://github.com/nostr-protocol/nips/blob/master/99.md)

**Bug fixes**
* Fixed [#39 [BUG] Escape characters included in tag query](https://github.com/UTXOnly/nost-py/issues/39)
* Fixed [#40 unbounded query limit](https://github.com/UTXOnly/nost-py/issues/40)

## v0.7
**Enhancements**
* Re-added redis cache to serve frequent queries faster
Expand Down
2 changes: 1 addition & 1 deletion docker_stuff/.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ HEX_PUBKEY=<YOUR_HEX_PUBKEY>
CONTACT=[email protected]
ENV_FILE_PATH=./docker_stuff/.env
NGINX_FILE_PATH=/etc/nginx/sites-available/default
VERSION=v0.6
VERSION=v0.8
96 changes: 58 additions & 38 deletions menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,35 @@
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")
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)
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)
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:
Expand All @@ -37,12 +45,15 @@ def start_nostpy_relay():
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)
subprocess.run(
["sudo", "-u", "relay_service", "docker-compose", "down"], check=True
)

# Delete container images by their name
image_names = [
Expand All @@ -55,20 +66,36 @@ def destroy_containers_and_images():
]

for image_name in image_names:
subprocess.run(["sudo", "-u", "relay_service", "docker", "image", "rm", "-f", image_name], check=True)
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)
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:
Expand All @@ -79,20 +106,24 @@ def switch_branches():
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")

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")
Expand All @@ -105,46 +136,36 @@ def decrypt_env():
else:
print_color("Invalid option. Please enter either 1, 2, or 3.", "31")


def setup_dbm():

#try:
# pkg_resources.get_distribution('psycopg2-binary')
# print("psycopg2-binary is already installed")
#except pkg_resources.DistributionNotFound:
# try:
# subprocess.run(["pip3", "install", "psycopg2-binary==2.9.1"], check=True)
# print("psycopg2-binary successfully installed")
# except subprocess.CalledProcessError as e:
# print(f"Error occurred while installing psycopg2-binary: {e}")

try:
#subprocess.run(["python3", "./docker_stuff/dbm_setup.py"], check=True)
subprocess.run(['python3', '-m', 'venv', 'snmpenv'], check=True)
subprocess.run(["python3", "-m", "venv", "snmpenv"], check=True)

activate_cmd = '. snmpenv/bin/activate && '
commands = [
'python ./docker_stuff/dbm_setup.py'
]
activate_cmd = ". snmpenv/bin/activate && "
commands = ["python ./docker_stuff/dbm_setup.py"]
for cmd in commands:
subprocess.run(['bash', '-c', activate_cmd + cmd], check=True)
subprocess.run(["bash", "-c", activate_cmd + cmd], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while running dbm_setup.py: {e}")





while True:
print_color("\n##########################################################################################", "31")
print_color(""" \n
print_color(
"\n##########################################################################################",
"31",
)
print_color(
""" \n
███╗ ██╗ ██████╗ ███████╗████████╗██████╗ ██╗ ██╗
████╗ ██║██╔═══██╗██╔════╝╚══██╔══╝██╔══██╗╚██╗ ██╔╝
██╔██╗ ██║██║ ██║███████╗ ██║ ██████╔╝ ╚████╔╝
██║╚██╗██║██║ ██║╚════██║ ██║ ██╔═══╝ ╚██╔╝
██║ ╚████║╚██████╔╝███████║ ██║ ██║ ██║
╚═╝ ╚═══╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═╝ ╚═╝
""" , "34")
""",
"34",
)
print("\nPlease select an option:\n")
print_color("1) Execute server setup script", "33")
print_color("2) Start Nostpy relay", "32")
Expand All @@ -155,7 +176,6 @@ def setup_dbm():
print_color("7) Setup database monitoring", "32")
print_color("8) Exit menu", "31")


options = {
"1": execute_setup_script,
"2": start_nostpy_relay,
Expand All @@ -164,9 +184,9 @@ def setup_dbm():
"5": decrypt_env,
"6": stop_containers,
"7": setup_dbm,
"8": lambda: print_color("Exited menu", "31")
"8": lambda: print_color("Exited menu", "31"),
}

try:
choice = input("\nEnter an option number (1-8): ")
if choice in options:
Expand Down
Loading

0 comments on commit c5464ba

Please sign in to comment.