def ssl_app(tmpdir_factory, kube_ns):
    """Partially instantiate a JupyterHub instance to generate ssl certificates

    Generates ssl certificates on the host,
    which will then be staged

    This is not a fully instantiated Hub,
    but it will have internal_ssl-related attributes such as
    .internal_trust_bundles and .internal_certs_location initialized.
    """
    tmpdir = tmpdir_factory.mktemp("ssl")
    tmpdir.chdir()
    config = Config()
    config.JupyterHub.internal_ssl = True
    tmpdir.mkdir("internal-ssl")
    # use relative path for ssl certs
    config.JupyterHub.internal_certs_location = "internal-ssl"
    config.JupyterHub.trusted_alt_names = [
        "DNS:hub-ssl",
        f"DNS:hub-ssl.{kube_ns}",
        f"DNS:hub-ssl.{kube_ns}.svc",
        f"DNS:hub-ssl.{kube_ns}.svc.cluster.local",
    ]
    app = JupyterHub(config=config)
    app.init_internal_ssl()
    return app
Exemplo n.º 2
0
def ssl_app(tmpdir_factory):
    tmpdir = tmpdir_factory.mktemp("ssl")
    tmpdir.chdir()
    config = Config()
    config.JupyterHub.internal_ssl = True
    tmpdir.mkdir("internal-ssl")
    # use relative
    config.JupyterHub.internal_certs_location = "internal-ssl"
    app = JupyterHub(config=config)
    app.init_internal_ssl()
    return app
Exemplo n.º 3
0
from alembic import context
from sqlalchemy import engine_from_config
from sqlalchemy import pool

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if 'jupyterhub' in sys.modules:
    from traitlets.config import MultipleInstanceError
    from jupyterhub.app import JupyterHub

    app = None
    if JupyterHub.initialized():
        try:
            app = JupyterHub.instance()
        except MultipleInstanceError:
            # could have been another Application
            pass
    if app is not None:
        alembic_logger = logging.getLogger('alembic')
        alembic_logger.propagate = True
        alembic_logger.parent = app.log
    else:
        fileConfig(config.config_file_name, disable_existing_loggers=False)
else:
    fileConfig(config.config_file_name)

# add your model's MetaData object here for 'autogenerate' support
Exemplo n.º 4
0
pygments_style = 'sphinx'
todo_include_todos = False

# Set the default role so we can use `foo` instead of ``foo``
default_role = 'literal'

# -- Config -------------------------------------------------------------
from jupyterhub.app import JupyterHub
from docutils import nodes
from sphinx.directives.other import SphinxDirective
from contextlib import redirect_stdout
from io import StringIO

# create a temp instance of JupyterHub just to get the output of the generate-config
# and help --all commands.
jupyterhub_app = JupyterHub()


class ConfigDirective(SphinxDirective):
    """Generate the configuration file output for use in the documentation."""

    has_content = False
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = False
    option_spec = {}

    def run(self):
        # The generated configuration file for this version
        generated_config = jupyterhub_app.generate_config_file()
        # post-process output
 def _config_default(self):
     # load application config by default
     if JupyterHub.initialized():
         return JupyterHub.instance().config
     else:
         return Config()
Exemplo n.º 6
0
    def submit_batch_script(self):
        subvars = self.get_req_subvars()
        cmd = self.batch_submit_cmd.format(**subvars)
        self.log.info("\nsubvars:%s\n" % pprint.pformat(subvars))

        for v in subvars['keepvars'].split(","):
            if os.getenv(v):
                subvars[v] = os.getenv(v)

        self.log.info("\nsubvars after keepvar :%s\n" %
                      pprint.pformat(subvars))

        #SKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKkkk
        template_directory = '%s/' % self.authenticator.job_template_dir
        template_to_find = "%s/%s -*.template" % (template_directory,
                                                  subvars['input'])
        self.log.info('template_to_find: /%s/' % template_to_find)

        templates = glob.glob(template_to_find)

        template = templates[0]

        self.log.info("template found %s" % template)

        script = "".join(open(template, "r").readlines())

        self.log.info('template content: \n' + '=' * 80 + '\n' + script +
                      '\n' + '=' * 80 + '\n')

        # replace all the __tag;...;..;;...__ and __tag__ pattern with the value
        # chosen in the GUI

        for k in subvars.keys():
            v = subvars[k]
            script = re.sub(r"__%s;.+__" % k, v, script)
            script = re.sub(r"__%s__" % k, v, script)

        self.log.info('content after __tag__ replacememnt: \n' \
                      + '='*80 + '\n' + script + '\n' + '='*80 + '\n')

        self.log.info('user_options: %s' % self.user_options)

        # initialize the keepvar environment variable

        # find the right spot in the script (just before the first command

        script_env_set = ""
        env_done = False

        for l in script.split("\n"):
            if len(l) == 0:
                script_env_set = script_env_set + l + "\n"
            elif l[0] == "#" or len(l.strip()) == 0 or env_done:
                script_env_set = script_env_set + l + "\n"
            else:
                vars = "#" * 80 + \
                       "\n# Script generated from template\n#\t%s" % os.path.abspath(template) + \
                       "\n" +"#" * 80 + \
                       "\n# Environment configuration to be forwarded...." +\
                       "\n" +"#" * 80 + "\n\n"
                for v in subvars['keepvars'].split(","):
                    if os.getenv(v):
                        vars = vars + "export %s=%s\n" % (v, os.getenv(v))
                vars = vars + \
                       "export USER=__USER__" + \
                       "\nexport KSLHUB_ROOT=__KSLHUB_ROOT__" +\
                       "\n. $KSLHUB_ROOT/kslhub_init_env.sh" +\
                       "\n\n" +\
                       "#" * 80 + "\n# Setting connection with the hub\n" +\
                       "#" * 80

                script_env_set = script_env_set + vars

                # adding token and secret credential

                script_env_set = script_env_set + \
                "\nexport JUPYTERHUB_BASE_URL=/" +\
                "\nexport JUPYTERHUB_CLIENT_ID=jupyterhub-user-__USER__" +\
                "\nexport JUPYTERHUB_API_TOKEN=__TOKEN__" +\
                "\nexport JUPYTERHUB_API_URL=http://__HOST__:__PORT__/hub/api" +\
                "\nexport JUPYTERHUB_USER=__USER__" +\
                "\nexport JUPYTERHUB_OAUTH_CALLBACK_URL=/user/__USER__/oauth_callback" +\
                "\nexport JUPYTERHUB_HOST=" +\
                "\nexport JUPYTERHUB_SERVICE_PREFIX=/user/__USER__/" +\
                "\nexport JUPYTER_RUNTIME_DIR=__KSLHUB_ROOT__/runtime" +\
                "\nexport CONFIGPROXY_AUTH_TOKEN=__SECRET__" +\
                "\n\n"+ "#" * 80 + "\n" +\
                "# Original script" +\
                "\n"+ "#" * 80 + "\n" +\
                l + "\n"

                env_done = True

        script = script_env_set
        self.log.info('content after env addition: \n' \
                      + '='*80 + '\n' + script + '\n' + '='*80 + '\n')

        # computing the token related to user

        hub = JupyterHub(parent=self)
        hub.load_config_file(hub.config_file)
        hub.init_db()

        def init_users():
            loop = asyncio.new_event_loop()
            loop.run_until_complete(hub.init_users())

        ThreadPoolExecutor(1).submit(init_users).result()
        user = orm.User.find(hub.db, self.user.name)
        self.log.info("user : %s" % pprint.pformat(self.user))
        if user is None:
            self.log.info("No such user: %s" % self.user.name, file=sys.stderr)
            self.exit(1)
        token = user.new_api_token(note="command-line generated")
        print(token)

        self.log.info("token = !!!%s!!! " % token)

        script = script.replace("__TOKEN__", token)

        client_id = 'jupyterhub-user-%s' % quote(self.user.name)
        oauth_client = (hub.db.query(
            orm.OAuthClient).filter_by(identifier=client_id).first())
        self.log.info("secret = !!!%s!!! " % oauth_client.secret)

        script = script.replace("__SECRET__", oauth_client.secret)
        script = script.replace("__USER__", self.user.name)
        # SKKKKKKKK to be fixed!!!
        script = script.replace("__HOST__", self.proxy_ip)
        script = script.replace("__PORT__", str(self.proxy_port))
        script = script.replace("__NOTEBOOK_PORT__",
                                str(self.user.server.port))
        script = script.replace("__PYTHONPATH__", os.getenv("PYTHONPATH", ""))
        script = script.replace("__KSLHUB_ROOT__",
                                os.getenv("KSLHUB_ROOT", "."))
        script = script.replace("__USER__", os.getenv("USER", "$USER"))
        script = script.replace("__JOB_DIR__",
                                os.getenv("KSLHUB_ROOT", ".") + "/jobs/")


        self.log.info('content after token replacement: \n' \
                      + '='*80 + '\n' + script + '\n' + '='*80 + '\n')

        subvars['cmd'] = self.cmd_formatted_for_batch()
        self.log.info("cmd formatted:%s" % subvars['cmd'])
        script = script.replace("__CONNECT__", subvars['cmd'])

        if hasattr(self, 'user_options'):
            subvars['user_options'] = self.user_options

        #script = self.batch_script.format(**subvars)
        #self.log.debug('Spawner submitting job using ' + cmd)
        #self.log.debug('Spawner submitted script:\n' + script)

        # set all

        f = open("%s/jobs/%s_last_script" % (kslhub_root, self.user.name), "w")
        f.write(script)
        f.close()

        if script.find("#SBATCH -v") > -1:
            # need to export environment variable by dynamically computing their absolute values
            vars = "#" * 80 + "\n# Environment variable to be forwarded....\n"
            for v in subvars['keepvars'].split(","):
                if os.getenv(v):
                    vars = vars + "export %s=%s\n " % (v, os.getenv(v))
            vars = vars + "#" * 80 + "\n"

            script = script.replace("#SBATCH -v", "###########SBATCH -v")
            #script = script.replace("\njupyterhub-singleuser","\n"+vars+"\njupyterhub-singleuser  --config=/home/kortass/JUPYTER/jupyterhub_config.py")
            script = script.replace("\njupyterhub-singleuser","\n"+vars+\
                                    "\nwhich jupyterhub-singleuser " +\
                                    "\njupyterhub-singleuser ")
            #script = script.replace('--notebook-dir="/home/kortass/JUPYTER/NOTEBOOKS"','')

        os.system("env > %s/jobs/%s_last_script_env" %
                  (kslhub_root, self.user.name))

        f = open(
            "%s/jobs/%s_last_script_modified" % (kslhub_root, self.user.name),
            "w")
        f.write(script)
        f.close()

        self.log.info('Job content: \n' + '=' * 80 + '\n' + script + '\n' +
                      '=' * 80 + '\n')

        # self.log.info('Spawner modified script:\n' + script)
        #SKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKkkk
        print("[AUTHENT] in submit_batch_script user: %s or |||%s|| ot type %s " %\
                  (self.user,pprint.pformat(self.user),type(self.user)))

        user = self.user
        user_str = ("%s" % user)
        if user_str.find('<User(') > -1:
            user = user_str[6:].split(' ')[0]

        out = yield run_command(cmd,
                                input=script,
                                env=self.get_env(),
                                user=user)
        try:
            self.log.info('Job submitted. cmd: ' + cmd + ' output: ' + out)
            self.job_id = self.parse_job_id(out)
            f = open(
                "%s/jobs/%s_%s.job" %
                (kslhub_root, self.user.name, self.job_id), "w")
            f.write(script)
            f.close()

        except:
            self.log.error('Job submission failed with exit code %s ' % out)
            self.job_id = ''
        return self.job_id
Exemplo n.º 7
0
 def _app(cfg: Config) -> JupyterHub:
     hub = JupyterHub(config=cfg)
     hub.tornado_settings = {"foo": "bar"}
     hub.init_hub()
     hub.base_url = "/mytenant"
     return hub
Exemplo n.º 8
0
from alembic import context
from sqlalchemy import engine_from_config
from sqlalchemy import pool

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if 'jupyterhub' in sys.modules:
    from traitlets.config import MultipleInstanceError
    from jupyterhub.app import JupyterHub

    app = None
    if JupyterHub.initialized():
        try:
            app = JupyterHub.instance()
        except MultipleInstanceError:
            # could have been another Application
            pass
    if app is not None:
        alembic_logger = logging.getLogger('alembic')
        alembic_logger.propagate = True
        alembic_logger.parent = app.log
    else:
        fileConfig(config.config_file_name)
else:
    fileConfig(config.config_file_name)

# add your model's MetaData object here for 'autogenerate' support
Exemplo n.º 9
0
def app(config, tmpdir):
    tmpdir.chdir()
    app = JupyterHub(config=config)
    return app