From f3232e7c44e61ec942a95615a20c306b39e64f49 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Thu, 27 Jun 2019 11:45:36 +0200 Subject: [PATCH 1/2] Add tljh_post_install hook --- .../plugins/simplest/tljh_simplest.py | 23 ++++++++++++++++++- integration-tests/test_simplest_plugin.py | 9 ++++++++ tljh/hooks.py | 10 ++++++++ tljh/installer.py | 3 +++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/integration-tests/plugins/simplest/tljh_simplest.py b/integration-tests/plugins/simplest/tljh_simplest.py index 4e03f3c..6253bad 100644 --- a/integration-tests/plugins/simplest/tljh_simplest.py +++ b/integration-tests/plugins/simplest/tljh_simplest.py @@ -1,6 +1,9 @@ """ Simplest plugin that exercises all the hooks """ +from textwrap import dedent + +from tljh import systemd from tljh.hooks import hookimpl @@ -39,4 +42,22 @@ def tljh_config_post_install(config): @hookimpl def tljh_custom_jupyterhub_config(c): - c.JupyterHub.authenticator_class = 'tmpauthenticator.TmpAuthenticator' \ No newline at end of file + c.JupyterHub.authenticator_class = 'tmpauthenticator.TmpAuthenticator' + + +@hookimpl +def tljh_post_install(): + post_install_service = dedent(""" + [Unit] + Description=Post Install Test Service + + [Service] + ExecStart=ls + + [Install] + WantedBy=multi-user.target + """) + service = "post-install-test.service" + systemd.install_unit(service, post_install_service) + systemd.enable_service(service) + systemd.reload_daemon() diff --git a/integration-tests/test_simplest_plugin.py b/integration-tests/test_simplest_plugin.py index 37d17c4..b740857 100644 --- a/integration-tests/test_simplest_plugin.py +++ b/integration-tests/test_simplest_plugin.py @@ -6,6 +6,7 @@ import requests import os import subprocess from tljh.config import CONFIG_FILE, USER_ENV_PREFIX, HUB_ENV_PREFIX +from tljh.systemd import check_service_enabled yaml = YAML(typ='rt') @@ -54,6 +55,7 @@ def test_config_hook(): assert data['simplest_plugin']['present'] + def test_jupyterhub_config_hook(): """ Test that tmpauthenticator is enabled by our custom config plugin @@ -61,3 +63,10 @@ def test_jupyterhub_config_hook(): resp = requests.get('http://localhost/hub/tmplogin', allow_redirects=False) assert resp.status_code == 302 assert resp.headers['Location'] == '/hub/spawn' + + +def test_post_install_hook(): + """ + Test that the post-install-test systemd service is enabled + """ + assert check_service_enabled("post-install-test") diff --git a/tljh/hooks.py b/tljh/hooks.py index 8ab8d13..d59f246 100644 --- a/tljh/hooks.py +++ b/tljh/hooks.py @@ -58,4 +58,14 @@ def tljh_config_post_install(config): be the serialized contents of config, so try to not overwrite anything the user might have explicitly set. """ + pass + +@hookspec +def tljh_post_install(): + """ + Post install script to be executed after installation + and after all the other hooks. + + This can be arbitrary Python code. + """ pass \ No newline at end of file diff --git a/tljh/installer.py b/tljh/installer.py index 3a4ea93..05055e2 100644 --- a/tljh/installer.py +++ b/tljh/installer.py @@ -405,6 +405,9 @@ def run_plugin_actions(plugin_manager, plugins): )) conda.ensure_pip_packages(USER_ENV_PREFIX, user_pip_packages) + # Custom post install actions + hook.tljh_post_install() + def ensure_config_yaml(plugin_manager): """ From b96a760aee1c39038ea743d5f3c540dd6ceed5b4 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Tue, 2 Jul 2019 12:53:06 +0200 Subject: [PATCH 2/2] Simplify test for the tljh_post_install hook --- .../plugins/simplest/tljh_simplest.py | 19 ++----------------- integration-tests/test_simplest_plugin.py | 8 +++++--- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/integration-tests/plugins/simplest/tljh_simplest.py b/integration-tests/plugins/simplest/tljh_simplest.py index 6253bad..3c6a3f7 100644 --- a/integration-tests/plugins/simplest/tljh_simplest.py +++ b/integration-tests/plugins/simplest/tljh_simplest.py @@ -1,9 +1,6 @@ """ Simplest plugin that exercises all the hooks """ -from textwrap import dedent - -from tljh import systemd from tljh.hooks import hookimpl @@ -47,17 +44,5 @@ def tljh_custom_jupyterhub_config(c): @hookimpl def tljh_post_install(): - post_install_service = dedent(""" - [Unit] - Description=Post Install Test Service - - [Service] - ExecStart=ls - - [Install] - WantedBy=multi-user.target - """) - service = "post-install-test.service" - systemd.install_unit(service, post_install_service) - systemd.enable_service(service) - systemd.reload_daemon() + with open('test_post_install', 'w') as f: + f.write('123456789') diff --git a/integration-tests/test_simplest_plugin.py b/integration-tests/test_simplest_plugin.py index b740857..bb07a83 100644 --- a/integration-tests/test_simplest_plugin.py +++ b/integration-tests/test_simplest_plugin.py @@ -6,7 +6,6 @@ import requests import os import subprocess from tljh.config import CONFIG_FILE, USER_ENV_PREFIX, HUB_ENV_PREFIX -from tljh.systemd import check_service_enabled yaml = YAML(typ='rt') @@ -67,6 +66,9 @@ def test_jupyterhub_config_hook(): def test_post_install_hook(): """ - Test that the post-install-test systemd service is enabled + Test that the test_post_install file has the correct content """ - assert check_service_enabled("post-install-test") + with open("test_post_install") as f: + content = f.read() + + assert content == "123456789"