This commit is contained in:
anki-code 2020-03-13 00:01:45 +03:00
parent b4a1d7f0c4
commit 0a115cfcf1
3 changed files with 36 additions and 7 deletions

View file

@ -21,9 +21,9 @@ usage: xxh [ssh arguments] [user@]host[:port] [xxh arguments] ____
usage: xxh [-p SSH_PORT] [-l SSH_LOGIN] [-i SSH_PRIVATE_KEY] _____ / / __ \ \ / _/
[-o SSH_OPTION -o ...] [+P PASSWORD] [+PP] ___ ( / / / \ \ /
[user@]host[:port] \ \___/ / / /
[+i] [+if] [+s SHELL] [+iff] [+hhr] [+v] [+vv] [+q] ____\ /__/ /
[+i] [+if] [+s SHELL] [+e NAME=VAL +e ...] [+iff] [+hhr] ____\ /__/ /
[+hh HOST_XXH_HOME] [+hf HOST_EXEC_FILE] [+hc HOST_EXEC_CMD] / \________/ /
[+xc XXH_CONFIG] [+lh LOCAL_XXH_HOME] /____________________/
[+xc XXH_CONFIG] [+lh LOCAL_XXH_HOME] [+v] [+vv] [+q] /____________________/
```
There is `~/.xxh/.xxhc` [yaml](https://en.wikipedia.org/wiki/YAML) config to save arguments and reuse it:
@ -31,6 +31,10 @@ There is `~/.xxh/.xxhc` [yaml](https://en.wikipedia.org/wiki/YAML) config to sav
hosts:
myhost: # settings for myhost
-p: 2222 # set special port
+s: xxh-shell-zsh # set zsh shell
# set Oh My Zsh plugin environment:
+e: XXH_ZSH_PLUGIN_OHMYZSH_ZSH_THEME="clean"
+e: XXH_ZSH_PLUGIN_OHMYZSH_PLUGINS="(git docker)"
"company-.*": # for all hosts by regex pattern
+if: # don't asking about install (++install-force)

View file

@ -15,9 +15,12 @@
#hosts:
# myhost: # settings for myhost
# -p: 2222 # set special port
# +s: xxh-shell-zsh # set zsh shell
# # set Oh My Zsh plugin environment:
# +e: XXH_ZSH_PLUGIN_OHMYZSH_ZSH_THEME="clean"
# +e: XXH_ZSH_PLUGIN_OHMYZSH_PLUGINS="(git docker)"
#
# "company-.*": # for all hosts by regex pattern
# +if: # don't asking about install (++install-force)
# +hhr: # remove host xxh home after disconnect (++host-xxh-home-remove)
# +hh: /tmp/.xxh # use special xxh home directory (++host-xxh-home)
#--------------------------------------------------------------------------------------------------
# +hh: /tmp/.xxh # use special xxh home directory (++host-xxh-home)

28
xxh
View file

@ -6,6 +6,7 @@ from sys import exit
from argparse import RawTextHelpFormatter
from urllib.parse import urlparse
from random import randint
from base64 import b64encode
class Xxh:
def __init__(self, package_dir_path, version='0.0.0'):
@ -289,6 +290,24 @@ class Xxh:
return r
def prepare_env_args(self, envs):
env_args=[]
if envs:
for e in envs:
el = e.split('=', 1)
if len(el) != 2:
self.eeprint(f'Wrong environment (expected NAME=VAL): {e}')
if not re.match('^[a-zA-Z_]+$', el[0]):
self.eeprint(f'Wrong environment NAME (expected [a-zA-Z-]): {el[0]}')
val = el[1]
if (val.startswith("'") and val.endswith("'")) or (val.startswith('"') and val.endswith('"')):
val=val[1:-1]
env_args+=['-e', "%s=%s" % ( el[0], b64encode(val.encode()).decode() ) ]
return env_args
def create_xxh_env(self):
home = fp'{self.local_xxh_home}'
if not home.exists():
@ -308,13 +327,14 @@ class Xxh:
argp.add_argument('-l', dest='ssh_login', help="Specifies the user to log in as on the remote machine.")
argp.add_argument('-i', dest='ssh_private_key', help="File from which the identity (private key) for public key authentication is read.")
argp.add_argument('-o', dest='ssh_options', metavar='SSH_OPTION -o ...', action='append', help="SSH options are described in ssh man page. Example: -o Port=22 -o User=snail")
argp.add_argument('+P','++password', help="Password for ssh auth.")
argp.add_argument('+PP','++password-prompt', default=False, action='store_true', help="Enter password manually using prompt.")
argp.add_argument('destination', metavar='[user@]host[:port]', help="Destination may be specified as [ssh://][user@]host[:port] or host from ~/.ssh/config")
argp.add_argument('+i','++install', default=False, action='store_true', help="Install xxh to destination host.")
argp.add_argument('+if','++install-force', default=False, action='store_true', help="Removing the host xxh package and install xxh again.")
argp.add_argument('+iff','++install-force-full', default=False, action='store_true', help="Removing the host xxh home and install xxh again. All installed packages on the host (e.g. pip packages) will be lost.")
argp.add_argument('+xc','++xxh-config', default=self.config_file, help=f"Xxh config file in yaml. Default: " + self.config_file)
argp.add_argument('+P','++password', help="Password for ssh auth.")
argp.add_argument('+PP','++password-prompt', default=False, action='store_true', help="Enter password manually using prompt.")
argp.add_argument('+e','++env', dest='env', metavar='NAME=VAL ...', action='append', help="Setting environment variables")
argp.add_argument('+lh','++local-xxh-home', default=self.local_xxh_home, help=f"Local xxh home path. Default: {self.local_xxh_home}")
argp.add_argument('+hh','++host-xxh-home', default=self.host_xxh_home, help=f"Host xxh home path. Default: {self.host_xxh_home}")
argp.add_argument('+hhr','++host-xxh-home-remove', action='store_true', help=f"Remove xxh home on host after disconnect")
@ -426,6 +446,8 @@ class Xxh:
password = getpass.getpass(f"Enter {username}@{host}'s password: ")
self.password = password
env_args = self.prepare_env_args(opt.env)
opt.install = True if opt.install_force or opt.install_force_full else opt.install
self.local_xxh_home = pf"{opt.local_xxh_home}"
@ -600,7 +622,7 @@ class Xxh:
elif self.verbose:
host_entrypoint_verbose = ['-v', '1']
@(self.sshpass) ssh @(self.ssh_arg_v) @(self.ssh_arguments) @(host) -t bash @(str(host_xxh_home/'xxh/shells'/self.shell/'build/entrypoint.sh')) @(host_execute_file) @(host_execute_command) @(host_entrypoint_verbose)
@(self.sshpass) ssh @(self.ssh_arg_v) @(self.ssh_arguments) @(host) -t bash @(str(host_xxh_home/'xxh/shells'/self.shell/'build/entrypoint.sh')) @(host_execute_file) @(host_execute_command) @(host_entrypoint_verbose) @(env_args)
if opt.host_xxh_home_remove:
if self.verbose: