Use websocket connection instead of fileserver to execute commands from CLI
This commit is contained in:
parent
09f1ad0625
commit
82e6bc5d31
3 changed files with 29 additions and 46 deletions
|
@ -419,26 +419,6 @@ class FileRequest(object):
|
||||||
peer.hashfield.replaceFromString(params["hashfield_raw"])
|
peer.hashfield.replaceFromString(params["hashfield_raw"])
|
||||||
self.response({"ok": "Updated"})
|
self.response({"ok": "Updated"})
|
||||||
|
|
||||||
def actionSiteReload(self, params):
|
|
||||||
if self.connection.ip not in config.ip_local and self.connection.ip != config.ip_external:
|
|
||||||
self.response({"error": "Only local host allowed"})
|
|
||||||
|
|
||||||
site = self.sites.get(params["site"])
|
|
||||||
site.content_manager.loadContent(params["inner_path"], add_bad_files=False)
|
|
||||||
site.storage.verifyFiles(quick_check=True)
|
|
||||||
site.updateWebsocket()
|
|
||||||
|
|
||||||
self.response({"ok": "Reloaded"})
|
|
||||||
|
|
||||||
def actionSitePublish(self, params):
|
|
||||||
if self.connection.ip not in config.ip_local and self.connection.ip != config.ip_external:
|
|
||||||
self.response({"error": "Only local host allowed"})
|
|
||||||
|
|
||||||
site = self.sites.get(params["site"])
|
|
||||||
num = site.publish(limit=8, inner_path=params.get("inner_path", "content.json"), diffs=params.get("diffs", {}))
|
|
||||||
|
|
||||||
self.response({"ok": "Successfuly published to %s peers" % num})
|
|
||||||
|
|
||||||
# Send a simple Pong! answer
|
# Send a simple Pong! answer
|
||||||
def actionPing(self, params):
|
def actionPing(self, params):
|
||||||
self.response("Pong!")
|
self.response("Pong!")
|
||||||
|
|
|
@ -504,6 +504,12 @@ class UiWebsocket(object):
|
||||||
if notification:
|
if notification:
|
||||||
self.response(to, {"error": "Content publish failed."})
|
self.response(to, {"error": "Content publish failed."})
|
||||||
|
|
||||||
|
def actionSiteReload(self, to, inner_path):
|
||||||
|
self.site.content_manager.loadContent(inner_path, add_bad_files=False)
|
||||||
|
self.site.storage.verifyFiles(quick_check=True)
|
||||||
|
self.site.updateWebsocket()
|
||||||
|
return "ok"
|
||||||
|
|
||||||
# Write a file to disk
|
# Write a file to disk
|
||||||
def actionFileWrite(self, to, inner_path, content_base64, ignore_bad_files=False):
|
def actionFileWrite(self, to, inner_path, content_base64, ignore_bad_files=False):
|
||||||
valid_signers = self.site.content_manager.getValidSigners(inner_path)
|
valid_signers = self.site.content_manager.getValidSigners(inner_path)
|
||||||
|
|
49
src/main.py
49
src/main.py
|
@ -154,7 +154,9 @@ class Actions(object):
|
||||||
logging.info("Version: %s r%s, Python %s, Gevent: %s" % (config.version, config.rev, sys.version, gevent.__version__))
|
logging.info("Version: %s r%s, Python %s, Gevent: %s" % (config.version, config.rev, sys.version, gevent.__version__))
|
||||||
|
|
||||||
func = getattr(self, function_name, None)
|
func = getattr(self, function_name, None)
|
||||||
func(**kwargs)
|
back = func(**kwargs)
|
||||||
|
if back:
|
||||||
|
print back
|
||||||
|
|
||||||
# Default action: Start serving UiServer and FileServer
|
# Default action: Start serving UiServer and FileServer
|
||||||
def main(self):
|
def main(self):
|
||||||
|
@ -229,14 +231,13 @@ class Actions(object):
|
||||||
# Not found in users.json, ask from console
|
# Not found in users.json, ask from console
|
||||||
import getpass
|
import getpass
|
||||||
privatekey = getpass.getpass("Private key (input hidden):")
|
privatekey = getpass.getpass("Private key (input hidden):")
|
||||||
diffs = site.content_manager.getDiffs(inner_path)
|
|
||||||
try:
|
try:
|
||||||
succ = site.content_manager.sign(inner_path=inner_path, privatekey=privatekey, update_changed_files=True, remove_missing_optional=remove_missing_optional)
|
succ = site.content_manager.sign(inner_path=inner_path, privatekey=privatekey, update_changed_files=True, remove_missing_optional=remove_missing_optional)
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
logging.error("Sign error: %s" % Debug.formatException(err))
|
logging.error("Sign error: %s" % Debug.formatException(err))
|
||||||
succ = False
|
succ = False
|
||||||
if succ and publish:
|
if succ and publish:
|
||||||
self.sitePublish(address, inner_path=inner_path, diffs=diffs)
|
self.sitePublish(address, inner_path=inner_path)
|
||||||
|
|
||||||
def siteVerify(self, address):
|
def siteVerify(self, address):
|
||||||
import time
|
import time
|
||||||
|
@ -364,7 +365,6 @@ class Actions(object):
|
||||||
site.announce()
|
site.announce()
|
||||||
print site.needFile(inner_path, update=True)
|
print site.needFile(inner_path, update=True)
|
||||||
|
|
||||||
def sitePublish(self, address, peer_ip=None, peer_port=15441, inner_path="content.json", diffs={}):
|
|
||||||
def siteCmd(self, address, cmd, parameters):
|
def siteCmd(self, address, cmd, parameters):
|
||||||
import json
|
import json
|
||||||
from Site import SiteManager
|
from Site import SiteManager
|
||||||
|
@ -384,24 +384,33 @@ class Actions(object):
|
||||||
ws = websocket.create_connection("ws://%s:%s/Websocket?wrapper_key=%s" % (config.ui_ip, config.ui_port, site.settings["wrapper_key"]))
|
ws = websocket.create_connection("ws://%s:%s/Websocket?wrapper_key=%s" % (config.ui_ip, config.ui_port, site.settings["wrapper_key"]))
|
||||||
return ws
|
return ws
|
||||||
|
|
||||||
|
def sitePublish(self, address, peer_ip=None, peer_port=15441, inner_path="content.json"):
|
||||||
global file_server
|
global file_server
|
||||||
from Site import Site
|
from Site import Site
|
||||||
from Site import SiteManager
|
from Site import SiteManager
|
||||||
from File import FileServer # We need fileserver to handle incoming file requests
|
from File import FileServer # We need fileserver to handle incoming file requests
|
||||||
from Peer import Peer
|
from Peer import Peer
|
||||||
SiteManager.site_manager.load()
|
site = SiteManager.site_manager.get(address)
|
||||||
|
|
||||||
logging.info("Loading site...")
|
logging.info("Loading site...")
|
||||||
site = Site(address, allow_create=False)
|
|
||||||
site.settings["serving"] = True # Serving the site even if its disabled
|
site.settings["serving"] = True # Serving the site even if its disabled
|
||||||
|
|
||||||
logging.info("Creating FileServer....")
|
try:
|
||||||
file_server = FileServer()
|
ws = self.getWebsocket(site)
|
||||||
site.connection_server = file_server
|
logging.info("Sending siteReload")
|
||||||
file_server_thread = gevent.spawn(file_server.start, check_sites=False) # Dont check every site integrity
|
self.siteCmd(address, "siteReload", inner_path)
|
||||||
time.sleep(0.001)
|
|
||||||
|
logging.info("Sending sitePublish")
|
||||||
|
self.siteCmd(address, "sitePublish", {"inner_path": inner_path, "sign": False})
|
||||||
|
logging.info("Done.")
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
logging.info("Can't connect to local websocket client: %s" % err)
|
||||||
|
logging.info("Creating FileServer....")
|
||||||
|
file_server = FileServer()
|
||||||
|
site.connection_server = file_server
|
||||||
|
file_server_thread = gevent.spawn(file_server.start, check_sites=False) # Dont check every site integrity
|
||||||
|
time.sleep(0.001)
|
||||||
|
|
||||||
if not file_server_thread.ready():
|
|
||||||
# Started fileserver
|
# Started fileserver
|
||||||
file_server.openport()
|
file_server.openport()
|
||||||
if peer_ip: # Announce ip specificed
|
if peer_ip: # Announce ip specificed
|
||||||
|
@ -409,7 +418,7 @@ class Actions(object):
|
||||||
else: # Just ask the tracker
|
else: # Just ask the tracker
|
||||||
logging.info("Gathering peers from tracker")
|
logging.info("Gathering peers from tracker")
|
||||||
site.announce() # Gather peers
|
site.announce() # Gather peers
|
||||||
published = site.publish(5, inner_path, diffs=diffs) # Push to peers
|
published = site.publish(5, inner_path) # Push to peers
|
||||||
if published > 0:
|
if published > 0:
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
logging.info("Serving files (max 60s)...")
|
logging.info("Serving files (max 60s)...")
|
||||||
|
@ -417,18 +426,6 @@ class Actions(object):
|
||||||
logging.info("Done.")
|
logging.info("Done.")
|
||||||
else:
|
else:
|
||||||
logging.info("No peers found, sitePublish command only works if you already have visitors serving your site")
|
logging.info("No peers found, sitePublish command only works if you already have visitors serving your site")
|
||||||
else:
|
|
||||||
# Already running, notify local client on new content
|
|
||||||
logging.info("Sending siteReload")
|
|
||||||
if config.fileserver_ip == "*":
|
|
||||||
my_peer = Peer("127.0.0.1", config.fileserver_port)
|
|
||||||
else:
|
|
||||||
my_peer = Peer(config.fileserver_ip, config.fileserver_port)
|
|
||||||
|
|
||||||
logging.info(my_peer.request("siteReload", {"site": site.address, "inner_path": inner_path}))
|
|
||||||
logging.info("Sending sitePublish")
|
|
||||||
logging.info(my_peer.request("sitePublish", {"site": site.address, "inner_path": inner_path, "diffs": diffs}))
|
|
||||||
logging.info("Done.")
|
|
||||||
|
|
||||||
# Crypto commands
|
# Crypto commands
|
||||||
def cryptPrivatekeyToAddress(self, privatekey=None):
|
def cryptPrivatekeyToAddress(self, privatekey=None):
|
||||||
|
|
Loading…
Reference in a new issue