diff --git a/shlax/actions/packages.py b/shlax/actions/packages.py index 814da6a..43615bd 100644 --- a/shlax/actions/packages.py +++ b/shlax/actions/packages.py @@ -67,6 +67,9 @@ class Packages: return os.path.join(os.getenv('HOME'), '.cache') async def update(self, target): + if not target.islocal: + return await target.rexec(self.cmds['update']) + # run pkgmgr_setup functions ie. apk_setup cachedir = await getattr(self, self.mgr + '_setup')(target) diff --git a/shlax/cli.py b/shlax/cli.py index a40b741..4fac0fb 100644 --- a/shlax/cli.py +++ b/shlax/cli.py @@ -28,6 +28,12 @@ class TargetArgument(cli2.Argument): super().__init__(cmd, param, doc=self.__doc__, default=Target()) 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): def setargs(self): @@ -45,10 +51,16 @@ class Command(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): self.target = self.target(*args, **kwargs) - from shlax.targets.base import Target - return super().call(Target()) + return super().call(self['target'].value) class ConsoleScript(Group): diff --git a/shlax/targets/base.py b/shlax/targets/base.py index 0b555b3..1e8a843 100644 --- a/shlax/targets/base.py +++ b/shlax/targets/base.py @@ -17,6 +17,7 @@ class Target: self.output = Output() self.parent = None self.root = root or os.getcwd() + self.islocal = getattr(self, 'islocal', True) def __str__(self): return 'localhost' diff --git a/shlax/targets/ssh.py b/shlax/targets/ssh.py new file mode 100644 index 0000000..d3dd707 --- /dev/null +++ b/shlax/targets/ssh.py @@ -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)