Added ssh target

This commit is contained in:
jpic 2020-06-01 15:41:22 +02:00
parent 879bb6f79e
commit 3b6a4f3fc4
4 changed files with 34 additions and 2 deletions

View File

@ -67,6 +67,9 @@ class Packages:
return os.path.join(os.getenv('HOME'), '.cache') return os.path.join(os.getenv('HOME'), '.cache')
async def update(self, target): async def update(self, target):
if not target.islocal:
return await target.rexec(self.cmds['update'])
# run pkgmgr_setup functions ie. apk_setup # run pkgmgr_setup functions ie. apk_setup
cachedir = await getattr(self, self.mgr + '_setup')(target) cachedir = await getattr(self, self.mgr + '_setup')(target)

View File

@ -28,6 +28,12 @@ class TargetArgument(cli2.Argument):
super().__init__(cmd, param, doc=self.__doc__, default=Target()) super().__init__(cmd, param, doc=self.__doc__, default=Target())
self.alias = ['target', 't'] self.alias = ['target', 't']
def cast(self, value):
from shlax.targets.ssh import Ssh
if '@' in value:
user, host = value.split('@')
return Ssh(host=host, user=user)
class Command(cli2.Command): class Command(cli2.Command):
def setargs(self): def setargs(self):
@ -45,10 +51,16 @@ class Command(cli2.Command):
class ActionCommand(cli2.Command): class ActionCommand(cli2.Command):
def setargs(self):
super().setargs()
self['target'] = TargetArgument(
self,
inspect.Parameter('target', inspect.Parameter.KEYWORD_ONLY),
)
def call(self, *args, **kwargs): def call(self, *args, **kwargs):
self.target = self.target(*args, **kwargs) self.target = self.target(*args, **kwargs)
from shlax.targets.base import Target return super().call(self['target'].value)
return super().call(Target())
class ConsoleScript(Group): class ConsoleScript(Group):

View File

@ -17,6 +17,7 @@ class Target:
self.output = Output() self.output = Output()
self.parent = None self.parent = None
self.root = root or os.getcwd() self.root = root or os.getcwd()
self.islocal = getattr(self, 'islocal', True)
def __str__(self): def __str__(self):
return 'localhost' return 'localhost'

16
shlax/targets/ssh.py Normal file
View File

@ -0,0 +1,16 @@
from .base import Target
class Ssh(Target):
def __init__(self, *actions, host, user=None):
self.host = host
self.user = user
self.islocal = False
super().__init__(*actions)
async def exec(self, *args, user=None, **kwargs):
_args = ['ssh', self.host]
if user == 'root':
_args += ['sudo']
_args += [' '.join([str(a) for a in args])]
return await self.parent.exec(*_args, **kwargs)