def index(self, path=None, alias=None, log_level=0, checks=True, accept_new_host_keys=True): # check specific. if self.specific: if alias == None: alias = self.alias # checks. if path == None: return r3sponse.error(f"Define parameter: path.") path = gfp.clean(path) # check encryption activated. if not encryption.activated: return r3sponse.error("The encryption requires to be activated.") # remote. if alias != None: # checks. if checks: # check alias. response = aliases.check(alias) if not response["success"]: return response # check passphrase. if CONFIG["aliases"][alias]["smartcard"] in [ True, "true", "True" ]: response = encryption.encryption.decrypt( CONFIG["aliases"][alias]["passphrase"]) else: response = encryption.encryption.decrypt( CONFIG["aliases"][alias]["passphrase"]) if not response["success"]: return response passphrase = response.decrypted.decode() # tests. response = agent.add( private_key=CONFIG["aliases"][alias]["private_key"], passphrase=passphrase) if not response["success"]: return response response = ssh.utils.test_ssht00ls( alias=alias, accept_new_host_keys=accept_new_host_keys) if not response["success"]: return response response = ssh.utils.test_path( alias=alias, path=path, accept_new_host_keys=accept_new_host_keys) if not response["success"]: return response # index. return self.utils.execute( command= f"""printf 'yes' | ssh {DEFAULT_SSH_OPTIONS} {alias} ' export IPINFO_API_KEY="{IPINFO_API_KEY}" && python3 /usr/local/lib/ssht00ls/classes/ssync/index.py --path {path} --json --non-interactive --no-checks ' """, serialize=True, log_level=log_level, ) # local. else: if checks: if not Files.exists(path): return r3sponse.error(f"Path [{path}] does not exist.") elif not os.path.isdir(path): return r3sponse.error(f"Path [{path}] is not a directory.") # handler. dict = self.utils.index(path) return r3sponse.success( f"Successfully indexed {len(dict)} files from directory [{path}].", { "index": dict, })
def push( self, # the local path. path=None, # the ssht00ls alias. alias=None, # the remote path. remote=None, # exlude subpaths (list) (leave None to use default). exclude=None, # path is directory boolean (leave None to parse automatically). directory=True, empty_directory=False, # update deleted files. delete=False, # forced mode. forced=False, # version control. safe=False, # accept new hosts keys. accept_new_host_keys=True, # checks. checks=True, check_base=True, # log level. log_level=0, # get the command in str. command=False, ): # check specific. if self.specific: if alias == None: alias = self.alias # check encryption activated. if not encryption.activated: return r3sponse.error("The encryption requires to be activated.") if checks: if self.specific: try: self.activated except: self.activated = False if not self.specific or not self.activated: if CONFIG["aliases"][alias]["smartcard"] in [ True, "true", "True" ]: response = encryption.decrypt( CONFIG["aliases"][alias]["passphrase"]) else: response = encryption.decrypt( CONFIG["aliases"][alias]["passphrase"]) if not response["success"]: return response passphrase = response.decrypted.decode() response = aliases.check(alias) if not response["success"]: return response response = agent.add( private_key=CONFIG["aliases"][alias]["private_key"], passphrase=passphrase) if not response["success"]: return response if self.specific: self.activated = True return self.utils.push( # the local path. path=path, # the ssht00ls alias. alias=alias, # the remote path. remote=remote, # exlude subpaths (list) (leave None to use default). exclude=exclude, # path is directory boolean (leave None to parse automatically). directory=directory, empty_directory=empty_directory, # update deleted files. delete=delete, # forced mode. forced=forced, # version control. safe=safe, # accept new hosts keys. accept_new_host_keys=accept_new_host_keys, # checks. checks=checks, check_base=check_base, # log level. log_level=log_level, # get the command in str. command=command, )
def index(self, path=None, alias=None, log_level=dev0s.defaults.options.log_level, checks=True, accept_new_host_keys=True): # check specific. if self.specific: if alias == None: alias = self.alias # checks. if path == None: return dev0s.response.error(f"Define parameter: path.") path = gfp.clean(path) # check encryption activated. if not ssht00ls_agent.activated: return dev0s.response.error( f"The {ssht00ls_agent.id} encryption requires to be activated." ) # remote. if alias != None: # checks. if checks: # check alias. response = aliases.check(alias) if not response["success"]: return response # check passphrase. if CONFIG["aliases"][alias]["smartcard"] in [ True, "true", "True" ]: passphrase = CONFIG["aliases"][alias]["pin"] else: passphrase = CONFIG["aliases"][alias]["passphrase"] if passphrase not in ["", "none", "None", None]: response = ssht00ls_agent.encryption.decrypt() if not response["success"]: return response passphrase = response.decrypted.decode() else: passphrase = None # tests. response = agent.add( private_key=CONFIG["aliases"][alias]["private_key"], passphrase=passphrase) if not response["success"]: return response response = ssh.utils.test_ssht00ls( alias=alias, accept_new_host_keys=accept_new_host_keys) if not response["success"]: return response response = ssh.utils.test_path( alias=alias, path=path, accept_new_host_keys=accept_new_host_keys) if not response["success"]: return response # index. response = self.utils.execute( command= f"""printf 'yes' | ssh {DEFAULT_SSH_OPTIONS} {alias} ' export IPINFO_API_KEY="{IPINFO_API_KEY}" && python3 /usr/local/lib/ssht00ls/classes/ssync/index.py --path {path} --json --non-interactive --no-checks ' """, serialize=True, log_level=log_level, input={ "Are you sure you want to continue connecting": Boolean(accept_new_host_keys).string(true="yes", false="no"), }, optional=True, ) if not response.success: return dev0s.response.error( f"Failed to connect with {alias}, error: {response.error}") else: return response # local. else: if checks: if not Files.exists(path): return dev0s.response.error( f"Path [{path}] does not exist.") elif not os.path.isdir(path): return dev0s.response.error( f"Path [{path}] is not a directory.") # handler. dict = self.utils.index(path) return dev0s.response.success( f"Successfully indexed {len(dict)} files from directory [{path}].", { "index": dict, })
def run(self): # logs. if self.log_level >= 0: loader = dev0s.console.Loader( f"Checking daemon {self.id}", interactive=dev0s.defaults.options.interactive) # checks. status = str(cache.get(id=self.cache_path, group="daemons")) if not running(self.cache_path): if self.log_level >= 0: loader.stop(success=False) self.crash( f"ssht00ls daemon ({self.id}): Path [{self.path}] is not running (status: {status})." ) if self.log_level >= 0: loader.mark(f"Checking mountpoint {self.path}") if not Files.exists(self.path): if self.log_level >= 0: loader.stop(success=False) self.crash( f"ssht00ls daemon ({self.id}): Path [{self.path}] does not exist." ) if not os.path.isdir(self.path): if self.log_level >= 0: loader.stop(success=False) self.crash( f"ssht00ls daemon ({self.id}): Path [{self.path}] is not a directory." ) # check alias. if self.log_level >= 0: loader.mark(f"Checking alias {self.alias}") response = aliases.check(self.alias) if not response["success"]: if self.log_level >= 0: loader.stop(success=False) self.crash(response=response) # get index. if self.log_level >= 0: loader.mark( f"Indexing [{self.path}] & [{self.alias}:{self.remote}].") response = self.index(short=True) if not response["success"]: if self.log_level >= 0: loader.stop(success=False) self.crash(response=response) # start success. if self.log_level >= 0: loader.mark(f"Starting daemon {self.id}") loader.stop() # start. timestamp_count = 0 while True: # check stop command. status = str(cache.get(id=self.cache_path, group="daemons")) if "*stop*" in status or "*unmounting*" in status: break # if file no longer exists stop daemon. if status == "none" or not Files.exists(self.path): self.crash( f"ssht00ls daemon ({self.id}): Mounted directory [{self.path}] from [{self.alias}:{self.remote}] no longer exists." ) # sync. response = self.sync(attempts=3, delay=[3, 5, 10]) if self.log_level >= 1: dev0s.response.log(response=response) if not response["success"]: self.crash( f"ssht00ls daemon ({self.id}) encountered an error while synchronizing: {response.error}", unmount=False) # set timestamp. do = False if self.sleeptime >= 1: do = True elif timestamp_count >= 1 / self.sleeptime: do = True timestamp_count = 0 timestamp_count += 1 if do: status = str(cache.get(id=self.cache_path, group="daemons")) if "*running*" in status: cache.set( id=self.cache_path, group="daemons", data=f"*running* (timestamp={Date().seconds_timestamp})" ) # sleep. time.sleep(self.sleeptime) # stop. self.stop()
def pull( self, # the local path. path=None, # the ssht00ls alias. alias=None, # the remote path. remote=None, # exlude subpaths (list) (leave None to exclude none). exclude=[], # path is directory boolean (leave None to parse automatically). directory=True, empty_directory=False, # update deleted files. delete=False, # forced mode. forced=False, # version control. safe=False, # accept new hosts keys. accept_new_host_keys=True, # checks. checks=True, # log level. log_level=dev0s.defaults.options.log_level, # get the command in str. command=False, ): # check specific. if self.specific: if alias == None: alias = self.alias # check encryption activated. if not ssht00ls_agent.activated: return dev0s.response.error( f"The {ssht00ls_agent.id} encryption requires to be activated." ) if checks: if self.specific: try: self.activated except: self.activated = False if not self.specific or not self.activated: response = aliases.check(alias) if not response["success"]: return response response = agent.check( public_key=CONFIG["aliases"][alias]["public_key"]) if not response.success: if "is not added to the" not in response.error: return response else: if CONFIG["aliases"][alias]["smartcard"] in [ True, "true", "True" ]: passphrase = CONFIG["aliases"][alias]["pin"] else: passphrase = CONFIG["aliases"][alias]["passphrase"] if passphrase not in [None, "None", "none", "", False]: response = ssht00ls_agent.encryption.decrypt( passphrase) if not response["success"]: return response passphrase = response.decrypted.decode() else: passphrase = None response = agent.add(private_key=CONFIG["aliases"] [alias]["private_key"], passphrase=passphrase) if not response["success"]: return response if self.specific: self.activated = True return self.utils.pull( # the local path. path=path, # the ssht00ls alias. alias=alias, # the remote path. remote=remote, # exlude subpaths (list) (leave None to use default). exclude=exclude, # path is directory boolean (leave None to parse automatically). directory=directory, empty_directory=empty_directory, # update deleted files. delete=delete, # forced mode. forced=forced, # version control. safe=safe, # accept new hosts keys. accept_new_host_keys=accept_new_host_keys, # checks. checks=checks, # log level. log_level=log_level, # get the command in str. command=command, )