sitePublish --recursive option

This commit is contained in:
caryoscelus 2023-07-20 16:29:04 +00:00
parent 8700351f33
commit c09b5e16c9
2 changed files with 44 additions and 28 deletions

View file

@ -309,6 +309,8 @@ class Config(object):
default=15441, nargs='?') default=15441, nargs='?')
action.add_argument('--inner_path', help='Content.json you want to publish (default: content.json)', action.add_argument('--inner_path', help='Content.json you want to publish (default: content.json)',
default="content.json", metavar="inner_path") default="content.json", metavar="inner_path")
action.add_argument('--recursive', help="Whether to publish all of site's content.json. "
"Overrides --inner_path. (default: false)", action='store_true', dest='recursive')
# SiteVerify # SiteVerify
action = self.subparsers.add_parser("siteVerify", help='Verify site files using sha512: address') action = self.subparsers.add_parser("siteVerify", help='Verify site files using sha512: address')

View file

@ -422,47 +422,61 @@ class Actions(object):
ws = websocket.create_connection(ws_address) ws = websocket.create_connection(ws_address)
return ws return ws
def sitePublish(self, address, peer_ip=None, peer_port=15441, inner_path="content.json"): def sitePublish(self, address, peer_ip=None, peer_port=15441, inner_path="content.json", recursive=False):
global file_server
from Site.Site import Site
from Site import SiteManager from Site import SiteManager
from File import FileServer # We need fileserver to handle incoming file requests
from Peer import Peer
file_server = FileServer()
site = SiteManager.site_manager.get(address)
logging.info("Loading site...") logging.info("Loading site...")
site = SiteManager.site_manager.get(address)
site.settings["serving"] = True # Serving the site even if its disabled site.settings["serving"] = True # Serving the site even if its disabled
if not recursive:
inner_paths = [inner_path]
else:
inner_paths = list(site.content_manager.contents.keys())
try: try:
ws = self.getWebsocket(site) ws = self.getWebsocket(site)
except Exception as err:
self.sitePublishFallback(site, peer_ip, peer_port, inner_paths, err)
else:
logging.info("Sending siteReload") logging.info("Sending siteReload")
self.siteCmd(address, "siteReload", inner_path) self.siteCmd(address, "siteReload", inner_path)
logging.info("Sending sitePublish") for inner_path in inner_paths:
self.siteCmd(address, "sitePublish", {"inner_path": inner_path, "sign": False}) logging.info(f"Sending sitePublish for {inner_path}")
self.siteCmd(address, "sitePublish", {"inner_path": inner_path, "sign": False})
logging.info("Done.") logging.info("Done.")
ws.close()
except Exception as err: def sitePublishFallback(self, site, peer_ip, peer_port, inner_paths, err):
logging.info("Can't connect to local websocket client: %s" % err) if err is not None:
logging.info("Creating FileServer....") logging.info(f"Can't connect to local websocket client: {err}")
file_server_thread = gevent.spawn(file_server.start, check_sites=False) # Dont check every site integrity logging.info("Publish using fallback mechanism. "
time.sleep(0.001) "Note that there might be not enough time for peer discovery, "
"but you can specify target peer on command line.")
logging.info("Creating FileServer....")
file_server_thread = gevent.spawn(file_server.start, check_sites=False) # Dont check every site integrity
time.sleep(0.001)
# Started fileserver # Started fileserver
file_server.portCheck() file_server.portCheck()
if peer_ip: # Announce ip specificed if peer_ip: # Announce ip specificed
site.addPeer(peer_ip, peer_port) site.addPeer(peer_ip, peer_port)
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
for inner_path in inner_paths:
published = site.publish(5, inner_path) # Push to peers published = site.publish(5, inner_path) # Push to peers
if published > 0:
time.sleep(3) if published > 0:
logging.info("Serving files (max 60s)...") time.sleep(3)
gevent.joinall([file_server_thread], timeout=60) logging.info("Serving files (max 60s)...")
logging.info("Done.") gevent.joinall([file_server_thread], timeout=60)
else: logging.info("Done.")
logging.info("No peers found, sitePublish command only works if you already have visitors serving your site") else:
logging.info("No peers found, sitePublish command only works if you already have visitors serving your site")
# Crypto commands # Crypto commands
def cryptPrivatekeyToAddress(self, privatekey=None): def cryptPrivatekeyToAddress(self, privatekey=None):