Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] docky remote init #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docky/cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
from . import forward
from . import run_open
from . import kill
from . import init
from . import init
from . import remote
80 changes: 80 additions & 0 deletions docky/cmd/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2022 Akretion (https://www.akretion.com).
# @author Pierrick Brun <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import yaml
from plumbum import cli
from plumbum.cmd import echo, tsh

from .base import Docky, DockySubNoProject


TELEPORT_PROXY = "teleport.akretion.com:443"


@Docky.subcommand("remote")
class DockyRemote(DockySubNoProject):

def _main(self, *args):
pass


class DockyRemoteSub(cli.Application):
"""Handle remote env"""
_cmd = None

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):
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"