2020-03-09 15:38:23 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from pathlib import Path
|
2020-09-29 10:32:45 +00:00
|
|
|
from datetime import datetime
|
2021-07-16 11:12:58 +00:00
|
|
|
from discord_webhook import DiscordWebhook
|
2020-03-09 15:38:23 +00:00
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
import urllib
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import requests
|
|
|
|
import io
|
2020-03-09 15:38:23 +00:00
|
|
|
|
2021-07-16 11:12:58 +00:00
|
|
|
# Load dotenv file if possible.
|
|
|
|
# TODO: change all scripts to use named flags/params
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
env_path = Path(sys.argv[1])
|
|
|
|
load_dotenv(dotenv_path=env_path, override=True)
|
2020-03-09 15:38:23 +00:00
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
# Get the container name as an argument or use "sia" as default.
|
|
|
|
CONTAINER_NAME = "sia"
|
|
|
|
if len(sys.argv) > 2:
|
|
|
|
CONTAINER_NAME = sys.argv[2]
|
|
|
|
|
2021-07-16 11:12:58 +00:00
|
|
|
# sc_precision is the number of hastings per siacoin
|
2022-02-03 17:02:30 +00:00
|
|
|
sc_precision = 10**24
|
2021-07-16 11:12:58 +00:00
|
|
|
|
|
|
|
# Environment variable globals
|
|
|
|
setup_done = False
|
|
|
|
|
2022-03-31 13:23:58 +00:00
|
|
|
|
2022-03-31 09:32:47 +00:00
|
|
|
# get docker container id and return None if container not found
|
|
|
|
def get_docker_container_id(container_name):
|
|
|
|
docker_cmd = "docker ps -q -f name=" + container_name
|
|
|
|
output = subprocess.check_output(docker_cmd, shell=True).decode("utf-8")
|
|
|
|
return None if output == "" else output
|
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
# find out local siad ip by inspecting its docker container
|
2021-07-12 12:48:13 +00:00
|
|
|
def get_docker_container_ip(container_name):
|
2020-09-29 10:32:45 +00:00
|
|
|
ip_regex = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
|
|
|
|
docker_cmd = (
|
|
|
|
"docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "
|
2021-07-12 12:48:13 +00:00
|
|
|
+ container_name
|
2020-09-29 10:32:45 +00:00
|
|
|
)
|
|
|
|
output = subprocess.check_output(docker_cmd, shell=True).decode("utf-8")
|
|
|
|
return ip_regex.findall(output)[0]
|
|
|
|
|
|
|
|
|
2021-07-16 11:12:58 +00:00
|
|
|
# sia deamon local ip address with port
|
|
|
|
api_endpoint = "http://{}:{}".format(
|
|
|
|
get_docker_container_ip(CONTAINER_NAME), os.getenv("API_PORT", "9980")
|
|
|
|
)
|
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
|
2020-09-29 13:42:41 +00:00
|
|
|
# find siad api password by getting it out of the docker container
|
|
|
|
def get_api_password():
|
2022-03-03 15:18:59 +00:00
|
|
|
if os.getenv("SIA_API_PASSWORD"):
|
|
|
|
return os.getenv("SIA_API_PASSWORD")
|
|
|
|
|
2020-09-29 13:42:41 +00:00
|
|
|
api_password_regex = re.compile(r"^\w+$")
|
|
|
|
docker_cmd = "docker exec {} cat /sia-data/apipassword".format(CONTAINER_NAME)
|
|
|
|
output = subprocess.check_output(docker_cmd, shell=True).decode("utf-8")
|
|
|
|
return api_password_regex.findall(output)[0]
|
|
|
|
|
|
|
|
|
2020-03-09 15:38:23 +00:00
|
|
|
def setup():
|
|
|
|
siad.initialize()
|
|
|
|
|
|
|
|
global setup_done
|
|
|
|
setup_done = True
|
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
# send_msg sends the msg to the specified discord channel.
|
|
|
|
# If force_notify is set to true it adds "@here".
|
2021-07-16 11:12:58 +00:00
|
|
|
async def send_msg(msg, force_notify=False, file=None):
|
|
|
|
try:
|
|
|
|
webhook_url = os.getenv("DISCORD_WEBHOOK_URL")
|
|
|
|
webhook_mention_user_id = os.getenv("DISCORD_MENTION_USER_ID")
|
|
|
|
webhook_mention_role_id = os.getenv("DISCORD_MENTION_ROLE_ID")
|
|
|
|
webhook = DiscordWebhook(url=webhook_url, rate_limit_retry=True)
|
|
|
|
|
|
|
|
# Add the portal name.
|
2022-02-16 13:13:37 +00:00
|
|
|
msg = "**{}**: {}".format(os.getenv("SERVER_DOMAIN"), msg)
|
2021-07-16 11:12:58 +00:00
|
|
|
|
|
|
|
if file and isinstance(file, str):
|
|
|
|
is_json = is_json_string(file)
|
|
|
|
content_type = "application/json" if is_json else "text/plain"
|
|
|
|
ext = "json" if is_json else "txt"
|
|
|
|
filename = "{}-{}.{}".format(
|
|
|
|
CONTAINER_NAME, datetime.utcnow().strftime("%Y-%m-%d-%H:%M:%S"), ext
|
|
|
|
)
|
|
|
|
skylink = upload_to_skynet(file, filename, content_type=content_type)
|
|
|
|
if skylink:
|
|
|
|
msg = "{} {}".format(msg, skylink) # append skylink to message
|
|
|
|
else:
|
|
|
|
webhook.add_file(file=io.BytesIO(file.encode()), filename=filename)
|
|
|
|
|
|
|
|
if force_notify and (webhook_mention_user_id or webhook_mention_role_id):
|
|
|
|
webhook.allowed_mentions = {
|
|
|
|
"users": [webhook_mention_user_id],
|
|
|
|
"roles": [webhook_mention_role_id],
|
|
|
|
}
|
|
|
|
msg = "{} /cc".format(msg) # separate message from mentions
|
|
|
|
if webhook_mention_role_id:
|
|
|
|
msg = "{} <@&{}>".format(msg, webhook_mention_role_id)
|
|
|
|
if webhook_mention_user_id:
|
|
|
|
msg = "{} <@{}>".format(msg, webhook_mention_user_id)
|
|
|
|
|
|
|
|
webhook.content = msg
|
|
|
|
webhook.execute()
|
|
|
|
|
|
|
|
print("msg > " + msg) # print message to std output for debugging purposes
|
|
|
|
except:
|
|
|
|
print("Failed to send message!")
|
|
|
|
print(traceback.format_exc())
|
2020-03-09 15:38:23 +00:00
|
|
|
|
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
def upload_to_skynet(contents, filename="file.txt", content_type="text/plain"):
|
|
|
|
files = {"file": (filename, contents, content_type)}
|
|
|
|
res = requests.post("https://siasky.net/skynet/skyfile", files=files)
|
|
|
|
if res.status_code == requests.codes["ok"]:
|
|
|
|
res_json = res.json()
|
|
|
|
return "https://siasky.net/" + res_json["skylink"]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def is_json_string(str):
|
|
|
|
try:
|
|
|
|
json.loads(str)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# siad class provides wrappers for the necessary siad commands.
|
2020-03-09 15:38:23 +00:00
|
|
|
class siad:
|
|
|
|
# initializes values for using the API (password and
|
|
|
|
# user-agent) so that all calls to urllib.request.urlopen have these set.
|
|
|
|
@staticmethod
|
|
|
|
def initialize():
|
|
|
|
# Setup a handler with the API password
|
|
|
|
username = ""
|
|
|
|
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
2020-09-29 13:42:41 +00:00
|
|
|
password_mgr.add_password(None, api_endpoint, username, get_api_password())
|
2020-03-09 15:38:23 +00:00
|
|
|
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
|
|
|
|
|
|
|
|
# Setup an opener with the correct user agent
|
|
|
|
opener = urllib.request.build_opener(handler)
|
2020-09-29 10:32:45 +00:00
|
|
|
opener.addheaders = [("User-agent", "Sia-Agent")]
|
2020-03-09 15:38:23 +00:00
|
|
|
|
|
|
|
# Install the opener.
|
|
|
|
# Now all calls to urllib.request.urlopen use our opener.
|
|
|
|
urllib.request.install_opener(opener)
|
|
|
|
|
|
|
|
# load_json reads the http response and decodes the JSON value
|
|
|
|
@staticmethod
|
|
|
|
def load_json(resp):
|
|
|
|
return json.loads(resp.decode("utf-8"))
|
|
|
|
|
2020-11-27 15:58:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def get(endpoint):
|
|
|
|
if not setup_done:
|
|
|
|
setup()
|
|
|
|
|
|
|
|
resp = urllib.request.urlopen(api_endpoint + endpoint).read()
|
|
|
|
return siad.load_json(resp)
|
|
|
|
|
2020-03-09 15:38:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_wallet():
|
2020-09-29 10:32:45 +00:00
|
|
|
if not setup_done:
|
|
|
|
setup()
|
2020-03-09 15:38:23 +00:00
|
|
|
|
|
|
|
resp = urllib.request.urlopen(api_endpoint + "/wallet").read()
|
|
|
|
return siad.load_json(resp)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_renter():
|
2020-09-29 10:32:45 +00:00
|
|
|
if not setup_done:
|
|
|
|
setup()
|
2020-03-09 15:38:23 +00:00
|
|
|
|
|
|
|
resp = urllib.request.urlopen(api_endpoint + "/renter").read()
|
|
|
|
return siad.load_json(resp)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_renter_contracts():
|
2020-09-29 10:32:45 +00:00
|
|
|
if not setup_done:
|
|
|
|
setup()
|
2020-03-09 15:38:23 +00:00
|
|
|
|
|
|
|
resp = urllib.request.urlopen(api_endpoint + "/renter/contracts").read()
|
|
|
|
return siad.load_json(resp)
|