Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
#28: Add a :harbor:deployment:ssh task that allows to ssh into a host
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Nov 9, 2020
1 parent 086d2a8 commit 8aa1de1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rkd_harbor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .tasks.service import WaitForServiceTask
from .tasks.listing import ListDefinedServices
from .tasks.deployment.apply import DeploymentTask
from .tasks.deployment.ssh import SSHTask
from .tasks.deployment.syncfiles import UpdateFilesTask
from .tasks.deployment.apply import CreateExampleDeploymentFileTask
from .tasks.deployment.vagrant import ManageVagrantTask
Expand Down Expand Up @@ -84,6 +85,7 @@ def imports():
TaskDeclaration(MaintenanceOnTask()),
TaskDeclaration(MaintenanceOffTask()),
TaskDeclaration(DeploymentTask()),
TaskDeclaration(SSHTask()),
TaskDeclaration(UpdateFilesTask()),
TaskDeclaration(CreateExampleDeploymentFileTask()),
TaskDeclaration(ManageVagrantTask()),
Expand Down
2 changes: 2 additions & 0 deletions src/rkd_harbor/tasks/deployment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from .vault import EncryptVaultTask
from .vault import EnvEncryptTask
from .vault import EditVaultTask
from .ssh import SSHTask


def imports() -> list:
return [
DeploymentTask(),
SSHTask(),
CreateExampleDeploymentFileTask(),
UpdateFilesTask(),
ManageVagrantTask(),
Expand Down
85 changes: 85 additions & 0 deletions src/rkd_harbor/tasks/deployment/ssh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from typing import Dict
from argparse import ArgumentParser
from rkd.api.contract import ExecutionContext
from ...formatting import development_formatting
from ...exception import MissingDeploymentConfigurationError
from .base import BaseDeploymentTask


class SSHTask(BaseDeploymentTask):
"""SSH into a host defined in a deployment configuration"""

def get_name(self) -> str:
return ':ssh'

def get_group_name(self) -> str:
return ':harbor:deployment'

def format_task_name(self, name) -> str:
return development_formatting(name)

def configure_argparse(self, parser: ArgumentParser):
self._add_ask_pass_arguments_to_argparse(parser)
self._add_vault_arguments_to_argparse(parser)

parser.add_argument('--group', help='Node group eg. "production"', required=True)
parser.add_argument('--num', help='Node number from given group, defaults to "0"', default="0")
parser.add_argument('--print-password', help='Print user password', action='store_true')

def get_declared_envs(self) -> Dict[str, str]:
envs = super().get_declared_envs()
envs['VAULT_PASSWORDS'] = ''

return envs

def run(self, context: ExecutionContext) -> bool:
self._preserve_vault_parameters_for_usage_in_inner_tasks(context)

node_group = context.get_arg('--group')
node_num = int(context.get_arg('--num'))
should_print_password = context.get_arg('--print-password')

try:
config = self.get_config()

if not self._validate(node_group, node_num, config):
return False

if should_print_password:
self._print_password(node)

return self._ssh(node)

except MissingDeploymentConfigurationError as e:
self.io().error_msg(str(e))
return False

def _validate(self, node_group: str, node_num: int, config: dict):
if node_group not in config['nodes'].keys():
self.io().error_msg('Node group "{}" not found'.format(node_group))
return False

try:
node = config['nodes'][node_group][node_num]
except KeyError:
self.io().error_msg('Node group "{}" does not have node of number #{}'.format(node_group, node_num))
return False

def _print_password(self, node: dict):
if 'sudo_password' in node:
self.io().info_msg('>> SUDO password is: "{}"'.format(node['sudo_password']))
return

if 'password' in node:
self.io().info_msg('>> User password is: "{}"'.format(node['password']))
return

def _ssh(self, node: dict) -> bool:
ssh_cmd = 'ssh {}@{} -p {}'.format(node['user'], node['host'], node['port'])

if 'private_key' in node:
ssh_cmd += ' -i {}'.format(node['private_key'])

self.sh(ssh_cmd)

return True

0 comments on commit 8aa1de1

Please sign in to comment.