From 9a37d54efdfa4783638ed24f90e4bc06f4fb7e57 Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Mon, 2 May 2022 17:58:22 +0200 Subject: [PATCH 1/3] [ADD] docky remote init --- docky/cmd/__init__.py | 3 ++- docky/cmd/remote.py | 38 ++++++++++++++++++++++++++++++++++++++ docky/common/__init__.py | 1 + docky/common/remote.py | 25 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 docky/cmd/remote.py create mode 100644 docky/common/remote.py diff --git a/docky/cmd/__init__.py b/docky/cmd/__init__.py index 67e063b..aa82f9b 100644 --- a/docky/cmd/__init__.py +++ b/docky/cmd/__init__.py @@ -4,4 +4,5 @@ from . import forward from . import run_open from . import kill -from . import init \ No newline at end of file +from . import init +from . import remote diff --git a/docky/cmd/remote.py b/docky/cmd/remote.py new file mode 100644 index 0000000..abda9cf --- /dev/null +++ b/docky/cmd/remote.py @@ -0,0 +1,38 @@ +# Copyright 2022 Akretion (https://www.akretion.com). +# @author Pierrick Brun +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import os +from plumbum import cli +from plumbum.cli.terminal import ask, prompt + +from .base import Docky + +from ..common.remote import RemoteVM +from ..common.gitlab import Gitlab + + +@Docky.subcommand("remote") +class DockyRemote(cli.Application): + """Handle remote env""" + +@DockyRemote.subcommand("init") +class DockyRemote(cli.Application): + """Init remote repository""" + + def main(self, *args, **kwargs): + project_url = os.popen("git remote get-url origin").readline() + project_url = project_url.replace("\n", "") + project_url = project_url.replace("ssh://git@", "https://") + project_url = project_url.replace(":10022/", "/") + project_url = project_url.replace(".git", "") + project_name = project_url.rsplit("/")[-1] + vm_name = prompt( + "What is the name of the VM ?", default=project_name) + project_access_token = prompt( + f"\n\n{project_url}/-/settings/access_tokens\n"\ + "Please create the Project Access Token\n"\ + "Then paste it here (read_repository & read_registry needed)") + dir_name = prompt( + "In which directory do you want to clone the project in the VM ?", default=project_name) + RemoteVM(vm_name).clone(project_url, project_access_token, dir_name) diff --git a/docky/common/__init__.py b/docky/common/__init__.py index 64cacda..2a2e5a8 100644 --- a/docky/common/__init__.py +++ b/docky/common/__init__.py @@ -3,3 +3,4 @@ from . import api from . import generator from . import project +from . import remote diff --git a/docky/common/remote.py b/docky/common/remote.py new file mode 100644 index 0000000..7e9a955 --- /dev/null +++ b/docky/common/remote.py @@ -0,0 +1,25 @@ +# Copyright 2022 Akretion (https://www.akretion.com). +# @author Pierrick Brun +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import os +from plumbum.cli.terminal import ask, prompt + + +TELEPORT_PROXY = "teleport.akretion.com:443" + +class RemoteVM(object): + + def __init__(self, name): + super().__init__() + self.name = name + + def _remote_exec(self, command): + remote_command = f"tsh --proxy={TELEPORT_PROXY} ssh app@{self.name} bash -c '{command}'" + output = os.popen(remote_command).readlines() + echo(output) + return + + def clone(self, project_url, project_access_token, dir_name): + project_url_with_token = project_url.replace("https://", f"https://oauth2:{project_access_token}@") + self._remote_exec(f"git clone {project_url_with_token} {dir_name}") From 60172416c0bfabb536d8c0f55aa13d15d693338a Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Tue, 3 May 2022 14:04:31 +0200 Subject: [PATCH 2/3] fixup! [ADD] docky remote init --- docky/cmd/remote.py | 4 ++-- docky/common/remote.py | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docky/cmd/remote.py b/docky/cmd/remote.py index abda9cf..908dea8 100644 --- a/docky/cmd/remote.py +++ b/docky/cmd/remote.py @@ -5,11 +5,11 @@ import os from plumbum import cli from plumbum.cli.terminal import ask, prompt +from plumbum.cmd import git from .base import Docky from ..common.remote import RemoteVM -from ..common.gitlab import Gitlab @Docky.subcommand("remote") @@ -21,7 +21,7 @@ class DockyRemote(cli.Application): """Init remote repository""" def main(self, *args, **kwargs): - project_url = os.popen("git remote get-url origin").readline() + project_url = git["remote", "get-url", "origin"]() project_url = project_url.replace("\n", "") project_url = project_url.replace("ssh://git@", "https://") project_url = project_url.replace(":10022/", "/") diff --git a/docky/common/remote.py b/docky/common/remote.py index 7e9a955..199b56f 100644 --- a/docky/common/remote.py +++ b/docky/common/remote.py @@ -4,6 +4,9 @@ import os from plumbum.cli.terminal import ask, prompt +from plumbum.cmd import tsh + +from ..common.api import logger TELEPORT_PROXY = "teleport.akretion.com:443" @@ -15,10 +18,9 @@ def __init__(self, name): self.name = name def _remote_exec(self, command): - remote_command = f"tsh --proxy={TELEPORT_PROXY} ssh app@{self.name} bash -c '{command}'" - output = os.popen(remote_command).readlines() - echo(output) - return + remote_command = tsh["--proxy", TELEPORT_PROXY, "ssh", f"app@{self.name}", "bash", "-c", f"'{command}'"] + logger.info(str(remote_command)) + return remote_command() def clone(self, project_url, project_access_token, dir_name): project_url_with_token = project_url.replace("https://", f"https://oauth2:{project_access_token}@") From cbd715022928032fa28147a8795c73204e6bcfc6 Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Wed, 4 May 2022 19:17:58 +0200 Subject: [PATCH 3/3] fixup! fixup! [ADD] docky remote init --- docky/cmd/remote.py | 90 +++++++++++++++++++++++++++++----------- docky/common/__init__.py | 1 - docky/common/remote.py | 27 ------------ 3 files changed, 66 insertions(+), 52 deletions(-) delete mode 100644 docky/common/remote.py diff --git a/docky/cmd/remote.py b/docky/cmd/remote.py index 908dea8..536cf2d 100644 --- a/docky/cmd/remote.py +++ b/docky/cmd/remote.py @@ -2,37 +2,79 @@ # @author Pierrick Brun # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -import os +import yaml from plumbum import cli -from plumbum.cli.terminal import ask, prompt -from plumbum.cmd import git +from plumbum.cmd import echo, tsh -from .base import Docky +from .base import Docky, DockySubNoProject -from ..common.remote import RemoteVM + +TELEPORT_PROXY = "teleport.akretion.com:443" @Docky.subcommand("remote") -class DockyRemote(cli.Application): +class DockyRemote(DockySubNoProject): + + def _main(self, *args): + pass + + +class DockyRemoteSub(cli.Application): """Handle remote env""" + _cmd = None -@DockyRemote.subcommand("init") -class DockyRemote(cli.Application): - """Init remote repository""" + def _run(self, *args, **kwargs): + self.parent._run(*args, **kwargs) + + def _init_remote(self): + with cli.Config(".deploy.conf") as conf: + self.vm_name = conf.get("default.vm") + self.dir_name = conf.get("default.dir") + self.repo_url = conf.get("default.repo_url") + repo_access_token = conf.get("default.repo_access_token") + self.repo_url_with_token = self.repo_url.replace("https://", f"https://oauth2:{repo_access_token}@") + if self._cmd: + self._cmd = f"cd {self.dir_name} && docker-compose " + self._cmd def main(self, *args, **kwargs): - project_url = git["remote", "get-url", "origin"]() - project_url = project_url.replace("\n", "") - project_url = project_url.replace("ssh://git@", "https://") - project_url = project_url.replace(":10022/", "/") - project_url = project_url.replace(".git", "") - project_name = project_url.rsplit("/")[-1] - vm_name = prompt( - "What is the name of the VM ?", default=project_name) - project_access_token = prompt( - f"\n\n{project_url}/-/settings/access_tokens\n"\ - "Please create the Project Access Token\n"\ - "Then paste it here (read_repository & read_registry needed)") - dir_name = prompt( - "In which directory do you want to clone the project in the VM ?", default=project_name) - RemoteVM(vm_name).clone(project_url, project_access_token, dir_name) + self._init_remote() + self._main(*args, **kwargs) + + def _main(self, *args): + cmd = ["--proxy", TELEPORT_PROXY, "ssh", f"app@{self.vm_name}", "bash", "-c"] + cmd.append(f"'{self._cmd}'") + return self._run(tsh[cmd]) + + +@DockyRemote.subcommand("clone") +class DockyRemoteClone(DockyRemoteSub): + """Clone remote repository""" + + def _init_remote(self): + super()._init_remote() + self._cmd = f"git clone {self.repo_url_with_token} {self.dir_name}" + +@DockyRemote.subcommand("pull") +class DockyRemotePull(DockyRemoteSub): + """Pull docker images""" + _cmd = "pull" + +@DockyRemote.subcommand("up") +class DockyRemoteUp(DockyRemoteSub): + """make remote project up""" + _cmd = "up -d" + +@DockyRemote.subcommand("down") +class DockyRemoteUp(DockyRemoteSub): + """make remote project down""" + _cmd = "down" + +@DockyRemote.subcommand("logs") +class DockyRemoteLogs(DockyRemoteSub): + """View output from remote containers""" + _cmd = "logs -f --tail=100" + +@DockyRemote.subcommand("restart") +class DockyRemoteRestart(DockyRemoteSub): + """Restart remote service""" + _cmd = "restart" diff --git a/docky/common/__init__.py b/docky/common/__init__.py index 2a2e5a8..64cacda 100644 --- a/docky/common/__init__.py +++ b/docky/common/__init__.py @@ -3,4 +3,3 @@ from . import api from . import generator from . import project -from . import remote diff --git a/docky/common/remote.py b/docky/common/remote.py deleted file mode 100644 index 199b56f..0000000 --- a/docky/common/remote.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2022 Akretion (https://www.akretion.com). -# @author Pierrick Brun -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -import os -from plumbum.cli.terminal import ask, prompt -from plumbum.cmd import tsh - -from ..common.api import logger - - -TELEPORT_PROXY = "teleport.akretion.com:443" - -class RemoteVM(object): - - def __init__(self, name): - super().__init__() - self.name = name - - def _remote_exec(self, command): - remote_command = tsh["--proxy", TELEPORT_PROXY, "ssh", f"app@{self.name}", "bash", "-c", f"'{command}'"] - logger.info(str(remote_command)) - return remote_command() - - def clone(self, project_url, project_access_token, dir_name): - project_url_with_token = project_url.replace("https://", f"https://oauth2:{project_access_token}@") - self._remote_exec(f"git clone {project_url_with_token} {dir_name}")