Skip to content

Commit

Permalink
Merge pull request #82 from Native-Planet/nallux_dev
Browse files Browse the repository at this point in the history
Beta-3.5.0
  • Loading branch information
nallux-dozryl authored Dec 16, 2022
2 parents c669156 + 1c80580 commit 2bf30bc
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 136 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Native Planet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,32 @@ This downloads the appropriate service file for you init system and the groundse
sudo wget -O - only.groundseg.app | bash
```

### For Windows

- Make sure you are running Windows 10 version 2004 or higher.

- Open the Command Prompt App from the Start Menu.

- Run the `wsl.exe --install` command.

- Reboot your machine.

- You will be prompted for a user name and password from the Command Prompt.

- Open the Ubuntu app from the Start Menu.

- Run the `sudo apt-get update && sudo apt-get upgrade -y` command.

- Once the installation is complete, run `sudo wget -O - get.groundseg.app | bash`.

## Edge Branch Installation (Unstable)

1. Modify `"updateUrl"` in `/opt/nativeplanet/groundseg/settings/system.json` to `https://version.infra.native.computer/version_edge.csv`
2. `sudo systemctl restart groundseg`

## Development and Building From Source
(Outdated -- New instructions coming soon)
1. Clone this repository
2. `export HOST_HOSTNAME=$(hostname)`
3. Run `sudo -E docker-compose up --build` in the root directory of the repository.

Coming Soon

## Removing GroundSeg (Uninstall)

Expand Down
17 changes: 0 additions & 17 deletions api/Dockerfile

This file was deleted.

118 changes: 76 additions & 42 deletions api/groundseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,72 @@
from flask import Flask, jsonify, request, make_response
from flask_cors import CORS
from werkzeug.utils import secure_filename

from utils import Log
from orchestrator import Orchestrator
from updater_docker import WatchtowerDocker

# Stop and remove legacy containers
os.system('docker rm -f groundseg_api groundseg_webui')
# Create flask app
app = Flask(__name__)
CORS(app, supports_credentials=True)

# Announce
Log.log_groundseg("---------- Starting GroundSeg ----------")
Log.log_groundseg("----------- Urbit is love <3 -----------")

# Load GroundSeg
orchestrator = Orchestrator("/opt/nativeplanet/groundseg/settings/system.json")

app = Flask(__name__)

CORS(app, supports_credentials=True)

# Binary Updater
def check_bin_updates():
print("Binary updater thread started", file=sys.stderr)

Log.log_groundseg("Binary updater thread started")
cur_hash = orchestrator.config['binHash']

while True:

try:
new_name, new_hash, dl_url = requests.get(orchestrator.config['updateUrl']).text.split('\n')[0].split(',')[0:3]
new_name, new_hash, dl_url = requests.get(
orchestrator.config['updateUrl']
).text.split('\n')[0].split(',')[0:3]

if orchestrator.config['updateMode'] == 'auto' and cur_hash != new_hash:
print(f"Latest version: {new_name}", file=sys.stderr)
print("Downloading new groundseg binary", file=sys.stderr)
Log.log_groundseg(f"Latest version: {new_name}")
Log.log_groundseg("Downloading new groundseg binary")
urllib.request.urlretrieve(dl_url, f"{orchestrator.config['CFG_DIR']}/groundseg_new")

print("Removing old groundseg binary", file=sys.stderr)
os.remove(f"{orchestrator.config['CFG_DIR']}/groundseg")
Log.log_groundseg("Removing old groundseg binary")
try:
os.remove(f"{orchestrator.config['CFG_DIR']}/groundseg")
except:
pass

time.sleep(3)

print("Renaming new groundseg binary", file=sys.stderr)
Log.log_groundseg("Renaming new groundseg binary")
os.rename(f"{orchestrator.config['CFG_DIR']}/groundseg_new",
f"{orchestrator.config['CFG_DIR']}/groundseg")

time.sleep(2)
print("Setting launch permissions for new binary", file=sys.stderr)
Log.log_groundseg("Setting launch permissions for new binary")
os.system(f"chmod +x {orchestrator.config['CFG_DIR']}/groundseg")

time.sleep(1)

print("Restarting groundseg...", file=sys.stderr)
Log.log_groundseg("Restarting groundseg...")
if sys.platform == "darwin":
os.system("launchctl load /Library/LaunchDaemons/io.nativeplanet.groundseg.plist")
else:
os.system("systemctl restart groundseg")

except Exception as e:
print(e, file=sys.stderr)
Log.log_groundseg(e)

time.sleep(90)


# Get updated Anchor information every 12 hours
def anchor_information():
print("Anchor information thread started", file=sys.stderr)
Log.log_groundseg("Anchor information thread started")
while True:
response = None
if orchestrator.config['wgRegistered']:
Expand All @@ -84,27 +91,61 @@ def anchor_information():
headers=headers).json()

orchestrator.anchor_config = response
print(response, file=sys.stderr)
Log.log_groundseg(response)
time.sleep(60 * 60 * 12)

except Exception as e:
print(e, file=sys.stderr)
Log.log_groundseg(e)
time.sleep(60)
else:
time.sleep(60)

# Constantly update system information
def sys_monitor():
print("System monitor thread started", file=sys.stderr)
Log.log_groundseg("System monitor thread started")
error = False
while True:
orchestrator._ram = psutil.virtual_memory().percent
orchestrator._cpu = psutil.cpu_percent(1)
orchestrator._core_temp = psutil.sensors_temperatures()['coretemp'][0].current
orchestrator._disk = shutil.disk_usage("/")
if error:
Log.log_groundseg("System monitor error, 5 second timeout")
time.sleep(5)
error = False

# RAM info
try:
orchestrator._ram = psutil.virtual_memory().percent
except Exception as e:
orchestrator._ram = 0.0
Log.log_groundseg(e)
error = True

# CPU info
try:
orchestrator._cpu = psutil.cpu_percent(1)
except Exception as e:
orchestrator._cpu = 0.0
Log.log_groundseg(e)
error = True

# CPU Temp info
try:
orchestrator._core_temp = psutil.sensors_temperatures()['coretemp'][0].current
except Exception as e:
orchestrator._core_temp = 0.0
Log.log_groundseg(e)
error = True

# Disk info
try:
orchestrator._disk = shutil.disk_usage("/")
except Exception as e:
orchestrator._disk = [0,0,0]
Log.log_groundseg(e)
error = True


# Checks if a meld is due, runs meld
def meld_loop():
print("Meld thread started", file=sys.stderr)
Log.log_groundseg("Meld thread started")
while True:
copied = orchestrator._urbits
for p in list(copied):
Expand All @@ -120,24 +161,16 @@ def meld_loop():

time.sleep(30)

# Start binary updater thread
threading.Thread(target=check_bin_updates).start()

# Start system monitoring on a new thread
threading.Thread(target=sys_monitor).start()

# Start meld loop on a new thread
threading.Thread(target=meld_loop).start()

# Start anchor information loop on a new thread
threading.Thread(target=anchor_information).start()
threading.Thread(target=check_bin_updates).start() # Start binary updater thread
threading.Thread(target=sys_monitor).start() # Start system monitoring on a new thread
threading.Thread(target=meld_loop).start() # Start meld loop on a new thread
threading.Thread(target=anchor_information).start() # Start anchor information loop on a new thread

#
# Endpoints
#

# Get all urbits

# Check if cookie is valid
@app.route("/cookies", methods=['GET'])
def check_cookies():
sessionid = request.args.get('sessionid')
Expand All @@ -147,6 +180,7 @@ def check_cookies():

return jsonify(404)

# Get all urbits
@app.route("/urbits", methods=['GET'])
def all_urbits():
sessionid = request.args.get('sessionid')
Expand Down Expand Up @@ -265,9 +299,9 @@ def pier_upload():
if current_chunk == 0:
try:
os.remove(save_path)
print("Cleaning up old files", file=sys.stderr)
Log.log_groundseg("Cleaning up old files")
except:
print("Directory is clear", file=sys.stderr)
Log.log_groundseg("Directory is clear")
pass

if os.path.exists(save_path) and current_chunk == 0:
Expand All @@ -280,7 +314,7 @@ def pier_upload():
f.seek(int(request.form['dzchunkbyteoffset']))
f.write(file.stream.read())
except Exception as e:
print(e,file=sys.stderr)
Log.log_groundseg(e,file=sys.stderr)
orchestrator._watchtower = WatchtowerDocker(orchestrator.config['updateMode'])
return jsonify("Can't write file")

Expand Down
Loading

0 comments on commit 2bf30bc

Please sign in to comment.