Commit 73bbb518 authored by James Coyle's avatar James Coyle

improvements for Windows

added auto db updater
parent a135d92c
......@@ -11,12 +11,14 @@ stages:
- set-env
- deploy
- cleanup
- db
set-pypirc:
stage: set-env
when: always
script:
- pip3 install crate
- rm -rf ~/.pypirc
- echo "[distutils]" >> ~/.pypirc
- echo "index-servers=" >> ~/.pypirc
......@@ -69,3 +71,12 @@ cleanup_pypirc:
- rm -rf ~/.pypirc
tags:
- storjapi
update_db:
stage: db
only:
- master
script:
- python3 update-db.py
tags:
- storjapi
......@@ -26,7 +26,8 @@ setup(name='storjstatus',
entry_points={
'console_scripts': [
'storjstatus-register=storjstatus:register',
'storjstatus-send=storjstatus:send'
'storjstatus-send=storjstatus:send',
'storjstatus-version=storjstatus:version'
]
}
)
......@@ -8,3 +8,6 @@ def send():
def register():
storjstatus_register.init_register()
def version():
print("StorjStatus Client: " + storjstatus_common.get_version())
......@@ -7,6 +7,7 @@ import logging
CONFIGFILE = '/etc/storjstatus/config.json'
CONFIGREG = 'Software\StorjStatus\StorjStatusClient\config'
APIENDPOINT = 'https://www.storjstatus.com/api/'
log = None
......@@ -40,6 +41,14 @@ def setup_logger():
log = ssLog
def get_os_type():
if (os.name == "posix"):
return ""
elif (os.name == "nt"):
return "win"
else:
return "x"
def cleanup_json(json):
json = re.sub(r'(?<!https:)//.*', '', json, flags = re.MULTILINE)
json = json.strip().replace('\r', '').replace('\n', '')
......@@ -48,6 +57,20 @@ def cleanup_json(json):
def check_strojshare():
if get_os_type() == "win":
return check_strojshare_win()
elif get_os_type() == "linux":
return check_strojshare_linux()
def check_strojshare_win():
result = subprocess_result(['storjshare', '-V'])
print(result[0].decode('utf-8').strip())
return "OK", result[0].decode('utf-8').strip()
def check_strojshare_linux():
result = subprocess_result(['which', 'storjshare'])
if 'storjshare' in result[0].decode('utf-8'):
try:
......
......@@ -4,8 +4,10 @@ import argparse
import getpass
import json
import os
from os.path import expanduser
import requests
import socket
import ctypes
from crontab import CronTab
from os import scandir
from random import randint
......@@ -48,34 +50,34 @@ def init_register():
name = name or guess_hostname()
else:
name = args.arg_name
print('Using server name :' + name)
print('Using server name :' + name + '.')
if not args.arg_config_dir:
config_dir = input('Enter your Storjshare config directory [' + guess_config_dir() + ']: ')
config_dir = config_dir or guess_config_dir()
else:
config_dir = args.arg_config_dir
print('Using config directory :' + config_dir)
print('Using config directory :' + config_dir + '.')
# Final check on vars
if not email or len(email) < 5:
print_error('Email address is invalid')
print_error('Email address is invalid.')
if not password or len(password) < 6:
print_error('Password is invalid')
print_error('Password is invalid.')
if not name or len(name) < 3:
print_error('Server name must be at least 3 characters')
print_error('Server name must be at least 3 characters.')
if not config_dir or len(config_dir) < 1:
print_error('Config Directory is invalid')
print_error('Config Directory is invalid.')
if not os.path.isdir(config_dir):
print_error('Config Directory does not exist or is not a directory')
print_error('Config Directory does not exist or is not a directory.')
path, dirs, files = os.walk(config_dir).__next__()
if (len(files) < 1):
print_error('Unable to find any files in the Config Directory')
print_error('Unable to find any files in the Config Directory.')
# Get api creds
key, secret = api_creds(email, password)
......@@ -89,24 +91,38 @@ def init_register():
cron_job()
print()
print("Setup complete. You will see your statistics appear on your dashboard over the next hour")
print("Setup complete. You will see your statistics appear on your dashboard over the next 30 mins.")
def checks():
global FORCE
if os.geteuid() != 0:
print_error('Please run this script with root privileges')
if storjstatus_common.get_os_type() == "x":
print_error('Unsupported Os type.')
try:
if os.geteuid() != 0:
print_error('Please run this script with root privileges.')
except AttributeError:
pass
try:
if ctypes.windll.shell32.IsUserAnAdmin() != 1:
print_error('Please run this script with Administrator privileges.')
except AttributeError:
print_error('Error checking user access level.')
if FORCE == True:
print("Forcing regeneration of config and crontab. Note cron times may change.")
elif os.path.isfile(storjstatus_common.CONFIGFILE):
print_error('Server config file already exists')
print_error('Server config file already exists.')
exit(1)
# Check strojshare exists
code, result = storjstatus_common.check_strojshare()
try:
code, result = storjstatus_common.check_strojshare()
except FileNotFoundError:
print_error("Unable to find the Storj Share Daemon. Please ensure it's on your PATH variable.")
if code != "OK":
print_error(result, False)
......@@ -114,8 +130,8 @@ def checks():
def header():
print('####################################################')
print(' StorjStatus Server Registration')
print('# Version: ' + storjstatus_common.get_version())
print(' StorjStatus Server Registration')
print(' Version: ' + storjstatus_common.get_version())
print('####################################################')
print()
......@@ -133,17 +149,29 @@ def cmdargs():
def guess_config_dir():
user = getpass.getuser()
if user == 'root':
return '/root/.config/storjshare/configs'
else:
return '/home/' + user + '/.config/storjshare/configs'
if storjstatus_common.get_os_type() == "win":
return expanduser("~") + '.storjshare\configs'
elif storjstatus_common.get_os_type() == "linux":
if user == 'root':
return '/root/.config/storjshare/configs'
else:
return '/home/' + user + '/.config/storjshare/configs'
def guess_hostname():
return socket.gethostname()
def save_settings(api_key, api_secret, server_guid, storj_config):
if storjstatus_common.get_os_type() == "win":
save_settings_win(api_key, api_secret, server_guid, storj_config)
elif storjstatus_common.get_os_type() == "linux":
save_settings_linux(api_key, api_secret, server_guid, storj_config)
def save_settings_linux(api_key, api_secret, server_guid, storj_config):
settings = {
'api_key': api_key,
'api_secret': api_secret,
......@@ -167,6 +195,20 @@ def save_settings(api_key, api_secret, server_guid, storj_config):
settings_file.close()
def save_settings_win(api_key, api_secret, server_guid, storj_config):
try:
import winreg
local_machine = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
config = winreg.OpenKey(local_machine, storjstatus_common.CONFIGREG)
winreg.SetValueEx(config, "api_key", 0, winreg.REG_SZ, api_key)
winreg.SetValueEx(config, "api_secret", 0, winreg.REG_SZ, api_secret)
winreg.SetValueEx(config, "server_guid", 0, winreg.REG_SZ, server_guid)
winreg.SetValueEx(config, "storj_config", 0, winreg.REG_SZ, storj_config)
except AttributeError:
print_error("There was a problem writing to the Windows Registry")
def api_creds(email, password):
json_request = {
'email': email,
......@@ -203,6 +245,16 @@ def server_guid(key, secret, name):
def cron_job():
if storjstatus_common.get_os_type() == "win":
print()
print('************* Windows scheduling unsupported ************')
print()
print('Please set up a manual schedule for "storjstatus-send" every 15 minutes.')
elif storjstatus_common.get_os_type() == "linux":
cron_job_linux()
def cron_job_linux():
result = storjstatus_common.subprocess_result(['which', 'storjstatus-send'])
if 'storjstatus-send' in result[0].decode('utf-8'):
......
......@@ -166,9 +166,28 @@ def config_json():
exit(1)
def load_settings():
if storjstatus_common.get_os_type() == "win":
return load_settings_win()
elif storjstatus_common.get_os_type() == "linux":
return load_settings_linux()
def load_settings():
def load_settings_win():
try:
import winreg
local_machine = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
config = winreg.OpenKey(local_machine, storjstatus_common.CONFIGREG)
APIKEY = winreg.QueryValueEx(config, 'api_key')[0]
APISECRET = winreg.QueryValueEx(config, 'api_secret')[0]
SERVERGUID = winreg.QueryValueEx(config, 'server_guid')[0]
STORJCONFIG = winreg.QueryValueEx(config, 'storj_config')[0]
except AttributeError:
print_error('Error parsing Windows registry. Did you run storjstatus-register?')
def load_settings_linux():
global APIKEY
global APISECRET
global SERVERGUID
......
__version__ = '0.2.6'
__version__ = '0.3.0'
import os
import setuptools
from crate.client import connect
def main():
exec(open('storjstatus/version.py').read())
print("Updating database to version " + __VERSION__)
connection = connect('data2.jamescoyle.net:4200')
cursor = connection.cursor()
cursor.execute("UPDATE storj.settings SET setting_value = '" + __VERSION__ + "' WHERE setting_name = 'storjstatus-client-version' ")
if __name__ == "__main__":
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment