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 1 commit
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
38 changes: 38 additions & 0 deletions docky/cmd/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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 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()
PierrickBrun marked this conversation as resolved.
Show resolved Hide resolved
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)
PierrickBrun marked this conversation as resolved.
Show resolved Hide resolved
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)
1 change: 1 addition & 0 deletions docky/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import api
from . import generator
from . import project
from . import remote
25 changes: 25 additions & 0 deletions docky/common/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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 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}")