def kill(self): """ check which worker is executing and then kill the worker :return: """ j.shell() self.check_ready()
def install(self): """ kosmos 'j.tools.pyinstaller.install()' :return: """ j.shell()
def find(self, **kwargs): """ :param kwargs: e.g. color="red",... :return: list of the children objects found """ res = [] for key, item in self._children.items(): match = True if isinstance(item, j.baseclasses.object_config): for key, val in kwargs.items(): print( "need to check in properties of schema to see if we can check" ) j.shell() elif isinstance(item, j.baseclasses.object): for key, val in kwargs.items(): if item._hasattr(key): if val != getattr(item, key): match = False else: raise j.exceptions.Value( "could not find for prop:%s, did not exist in %s" % (key, self._key)) else: raise j.exceptions.JSBUG( "only support jsx objects in _children") if match: res.append(item) return res
def _schema_property_add_if_needed(self, schema, done=[]): """ recursive walks over schema properties (multiple levels) if a sub property is a complex type by itself, then we need to make sure we remember the schema's also in BCDB :param schema: :return: """ for prop in schema.properties: if prop.jumpscaletype.NAME == "list" and isinstance( prop.jumpscaletype.SUBTYPE, j.data.types._jsxobject): # now we know that there is a subtype, we need to store it in the bcdb as well s = prop.jumpscaletype.SUBTYPE._schema if not j.data.schema.exists(url=s.url): # should be there lets see why not j.shell() # now see if more subtypes if s._md5 not in done: done.append(s._md5) done = self._schema_property_add_if_needed(s) elif prop.jumpscaletype.NAME == "jsxobject": s = prop.jumpscaletype._schema if s.url not in j.data.schema.schemas_loaded: # should be there lets see why not j.shell() # now see if more subtypes if s._md5 not in done: done.append(s._md5) done = self._schema_property_add_if_needed(s) return done
def main(self): """ to run: kosmos 'j.data.schema.test(name="corex")' --debug """ j.servers.corex.default.check() corex = j.servers.corex.default.client self.http.executor = "corex" self.http.corex_client_name = corex.name self.http.timeout = 5 self.http.delete() self.http.cmd_start = "python3 -m http.server" # starts on port 8000 self.http.executor = "corex" self.http.corex_client_name = corex.name self.http.start() self.http.monitor.ports = 8000 j.shell() self.http.stop() self.http.delete() return "OK"
def main(self): """ to run: js_shell 'j.tools.logger.test(name="base")' """ j.shell()
def tail(self): """ follow everything coming in in relation to the config mgmt done :return: """ j.shell()
def test(self, install=False): """ kosmos 'j.tools.legal_contracts.test(install=False)' :return: """ if install: self.install() testdir = "/tmp/legaldocs/" j.sal.fs.createDir(testdir) path = j.clients.git.getContentPathFromURLorPath( "https://github.com/threefoldfoundation/info_legal/tree/master/HR/dutch" ) logo_path = j.clients.git.getContentPathFromURLorPath( "https://github.com/threefoldfoundation/info_legal/tree/master/images/threefold_logo.png" ) doc = self.doc_get("%s/test_legal.pdf" % testdir) j.shell() w p = j.tools.prefab.local url = "https://media.wired.com/photos/598e35994ab8482c0d6946e0/master/w_628,c_limit/phonepicutres-TA.jpg" j.shell()
def table_add(self, md_part): """ returns table which needs to be manipulated """ j.shell() self.parts.append(t) return t
def server(self): def start(): cmd = "/sandbox/bin/js_mux start" j.sal.process.execute(cmd, die=True) time.sleep(0.1) if self._server is None: rc, out, err = j.core.tools.execute("tmux ls", die=False) if rc > 0: if err.find("No such file or directory") != -1: start() rc, out, err = j.core.tools.execute("tmux ls", die=False) if err.find("no server running") != -1: start() rc, out, err = j.core.tools.execute("tmux ls", die=False) if rc > 0: raise RuntimeError("could not execute tmux ls\n%s" % err) if out.strip().count("\n") > 0: j.shell() raise RuntimeError("found too many tmux sessions, there should only be 1") rc, out, err = j.sal.process.execute("tmux -f /sandbox/cfg/.tmux.conf has-session -t main", die=False) if rc > 0: j.shell() raise RuntimeError("did not find tmux session -t main") self._server = tmuxp.Server() time.sleep(1) self._log_info("tmux server is running") return self._server
def test(self): """ js_shell 'j.tools.tbot.test()' """ bot = self.get() print(bot.node) j.shell()
def sshclients_add(self, sshclients=None): """ :param sshclients: name of sshclient, sshclient or list of sshclient or names :return: """ assert sshclients from Jumpscale.clients.ssh.SSHClientBase import SSHClientBase if j.data.types.list.check(sshclients): for item in sshclients: self.sshclients_add(item) return elif isinstance(sshclients, SSHClientBase): cl = sshclients elif isinstance(sshclients, str): name = sshclients if not j.clients.ssh.exists(name=name): raise j.exceptions.Base( "cannot find sshclient:%s for syncer:%s" % (name, self)) cl = j.clients.ssh.get(name=name) else: j.shell() raise j.exceptions.Base( "only support name of sshclient or the sshclient instance itself" ) if cl.name not in self.sshclients: self.sshclients[cl.name] = cl if not cl.name in self.sshclient_names: self.sshclient_names.append(cl.name) self.save()
def test_generation(self): p = j.tools.prefab.local url = "https://media.wired.com/photos/598e35994ab8482c0d6946e0/master/w_628,c_limit/phonepicutres-TA.jpg" p.network.tools.download(url, to="/tmp/reportlab/image.jpg", overwrite=False, retry=3) from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet from reportlab.rl_config import defaultPageSize from reportlab.lib.units import inch PAGE_HEIGHT = defaultPageSize[1] PAGE_WIDTH = defaultPageSize[0] styles = getSampleStyleSheet() Title = "Hello world" pageinfo = "platypus example" def myFirstPage(canvas, doc): canvas.saveState() canvas.setFont("Times-Bold", 16) canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 108, Title) canvas.setFont("Times-Roman", 9) canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo) canvas.restoreState() dr = self._getdrawing() j.shell()
def docker_environment(self, delete=True): """ kosmos 'j.servers.threebot.docker_environment(delete=True)' kosmos 'j.servers.threebot.docker_environment(delete=False)' will create a main container with jummpscale & 3bot will start wireguard connection on OSX will start threebot :return: """ docker = self._docker_jumpscale_get(name="3bot", delete=delete) if j.core.myenv.platform() != "linux": # only need to use wireguard if on osx or windows (windows not implemented) docker.execute("source /sandbox/env.sh;jsx wireguard" ) # get the wireguard started docker.wireguard.connect() self._log_info("check we can reach the container") assert j.sal.nettools.waitConnectionTest(docker.config.ipaddr, 22, timeout=30) self._log_info("start the threebot server") docker.execute( "source /sandbox/env.sh;kosmos 'j.servers.threebot.start(packages_add=True)'" ) j.shell()
def replace_in_txt(self, txt): if len(self.source) < 4: j.shell() raise j.exceptions.Base("something wrong with original source") txt = txt.replace(self.source, self.markdown) # self.source = self.markdown #there is a new source now return txt
def test(self): """ kosmos 'j.data.dict_editor.test()' :return: """ import copy config = copy.copy(j.core.myenv.config) # to make sure we don't edit the jumpscale config file e = j.data.dict_editor.get(config) e.DIR_TEMP = "/tmp_new" assert config["DIR_TEMP"] == "/tmp_new" schema = """ @url = despiegk.test2 llist2 = "" (LS) nr = 4 date_start = 0 (D) description = "" cost_estimate = 0.0 #this is a comment llist = [] enum = "red,green,blue" (E) #first one specified is the default one @url = despiegk.test3 llist = [] description = "" """ s = j.data.schema.get_from_text(schema_text=schema) o = s.new() j.shell()
def _authorized(self, cmd_obj, user_session): """ checks if the current session is authorized to access the requested command Notes: * system actor is always public, it will be used to retrieve the client metadata even before authentication * threebot_id should be in the session otherwise it can only access the public methods only :return: (True, None) if authorized else (False, reason) """ if user_session.admin: return True, None elif cmd_obj.rights.public: return True, None elif not user_session.threebot_id: return False, "No user is authenticated on this session and the command isn't public" else: j.shell() w # TODO: needs to be reimplemented (despiegk) # user = j.data.bcdb.system.user.find(threebot_id=user_session.threebot_id) # if not user: # return False, f"Couldn't find user with threebot_id {threebot_id}" # return actor_obj.acl.rights_check(userids=[user[0].id], rights=[cmd_name]), None return False, "Command not found"
def main(self): """ kosmos -p 'j.servers.gedis.test("threebot_redis_registration_encryption")' """ cl = self._threebot_client_default._redis # is a client to a threebot server (in this test its local) # to make example complete, make sure we use json on our connection cl.execute_command("config_format", "json") """ on server: the user_session can be authenticated and data can be verified, when verified it means the data was encrypted and signed so the actor knows for sure that the source is real and data is correct user_session.authenticated user_session.data_verified """ # login wih me seed = j.data.idgenerator.generateGUID( ) # any seed works, the more random the more secure signature = j.data.nacl.default.sign_hex( seed) # this is just std signing on nacl and hexifly it assert len(signature) == 128 # authentication is done by means of threebotid and a random seed # which is signed with the threebot (is the client) private key res = cl.execute_command("auth", j.tools.threebot.me.default.tid, seed, signature) assert res == b"OK" error = False try: res = cl.execute_command("auth", j.tools.threebot.me.default.tid, seed + "a", signature) except: error = True assert error # lets now ask the threebot remote to load the right package using nothing else than redis # if there would be no authentication it would fail args = {} args["name"] = "ibiza_test" args["path"] = j.core.tools.text_replace( "{DIR_BASE}/code/github/threefoldtech/jumpscaleX_threebot/ThreeBotPackages/examples/ibiza" ) data_return_json = cl.execute_command( "default.package_manager.package_add", j.data.serializers.json.dumps(args)) j.shell() #### LETS NOW TEST WITH ENCRYPTION # a threebotme is a local threebot definition, it holds your pubkey, ... # this returns 2 test nacl sessions & threebot definiions for a fake client & threebotserver nacl_client, nacl_server, threebot_me_client, threebot_me_server = j.tools.threebot.test_register_nacl_threebots( ) j.shell() print("**DONE**")
def test(self, manual=False): """ js_shell 'j.servers.digitalme.test()' js_shell 'j.servers.digitalme.test(manual=True)' :param manual means the server is run manually using e.g. js_shell 'j.servers.digitalme.start()' """ admincl = j.servers.zdb.start_test_instance(destroydata=True) cl = admincl.namespace_new("test", secret="1234") if manual: namespace = "system" # if server manually started can use this secret = "1234" gedisclient = j.clients.gedis.configure(namespace, namespace=namespace, port=8001, secret=secret, host="localhost") else: # gclient will be gedis client gedisclient = self.start(addr=cl.addr, port=cl.port, namespace=cl.nsname, secret=cl.secret, background=True) # ns=gedisclient.core.namespaces() j.shell()
def _capnp_schema_text(self): tpath = "%s/templates/schema.capnp" % self._dirpath # j.shell() _capnp_schema_text = j.tools.jinja2.template_render(path=tpath, reload=False, obj=self, objForHash=self._md5) if "@0x" not in _capnp_schema_text: j.shell() return _capnp_schema_text
def service_manage(self): """ get the service to basically run in memory, means checking the monitoring, make the model reality... :return: """ if self._running is None: self.q_in = queue.Queue() self.q_out = queue.Queue() self.task_id_current = 0 self.greenlet_task = spawn(self._main) # spawn(self._main) for method in inspect.getmembers(self, predicate=inspect.ismethod): j.shell() w mname = method[0] print("iterate over method:%s" % mname) if mname.startswith("monitor"): if mname in ["monitor_running"]: continue print("found monitor: %s" % mname) method = getattr(self, mname) self.monitors[mname[8:]] = spawn(method) else: if mname.startswith("action_"): self._stateobj_get( mname) # make sure the action object exists
def decrypt(self, data, binary=False): if isinstance(data, str): j.shell() r = self._box.decrypt(data) if not binary: return r.decode("utf-8") else: return r
def gslides(self): """ kosmos 'j.builder.apps.digitalme.gslides()' google slides option :return: """ j.shell() "google-api-python-client,google-auth-httplib2,google-auth-oauthlib"
def start(self, path, background=False): """ :param path: to file of dash :param background: :return: """ j.shell()
def exists(self, name): if name in self._children: if not name in self._config: j.shell() assert name in self._config return True return name in self._config
def kosmos(name="3bot", target="auto", configdir=None): j = jumpscale_get(die=True) j.application.interactive = True n = j.data.nacl.get(load=False) # important to make sure private key is loaded if n.load(die=False) is False: n.configure() j.application.bcdb_system # needed to make sure we have bcdb running, needed for code completion j.shell(loc=False, locals_=locals(), globals_=globals())
def test(self): """ js_shell 'j.clients.currencylayer.test()' """ r = j.clients.currencylayer._Find() j.shell() self._log_info(self.cur2usd) assert "aed" in self.cur2usd
def generate(self, reset=False): """ Generate ssh key :param reset: if True, then delete old ssh key from dir, defaults to False :type reset: bool, optional """ # TODO: use SSHAgent in installtools j.shell()
def test_redis_kvs(self): """ js_shell 'j.tools.memusagetest.test_redis_kvs()' """ memusage_start = j.application.getMemoryUsage() print("MEM USAGE START:%s" % memusage_start) j.clients.redis.core_stop() r_classic = j.clients.redis.core_get() s = r_classic.register_script("%s/test.lua" % self._dirpath) rest = r.evalsha(s.sha, 2, "aaa", "bbb") j.shell() r = j.data.bcdb.redis pids = j.sal.process.getPidsByFilter("redis-server") assert len(pids) == 1 p = j.sal.process.getProcessObject(pids[0]) info = p.memory_full_info() redis_mem_start = info.uss j.tools.timer.start("memusage") from nacl.hash import blake2b from nacl import encoding from pyblake2 import blake2b nritems = 100000 for item in range(nritems): hash = blake2b(str(item).encode(), digest_size=8).digest() # can do 900k per second try: r.execute("HSET", hash[0:2], hash[2:], b"aaaa") except: j.shell() e j.tools.timer.stop(nritems) memusage_stop = j.application.getMemoryUsage() print("MEM USAGE STOP:%s" % memusage_stop) print("MEM USAGE difference in KB:%s" % (memusage_stop - memusage_start)) # p=j.sal.process.getProcessObject(pids[0]) info = p.memory_full_info() redis_mem_stop = info.uss print("REDIS MEM USAGE difference in KB:%s" % ((redis_mem_stop - redis_mem_start) / 1024))
def test_explorer(self): """ kosmos -p 'j.servers.threebot.test_explorer()' :return: """ j.servers.threebot.default.start(background=True) j.shell()