1
0
mirror of https://github.com/gryf/boxpy.git synced 2025-12-20 14:17:57 +01:00

Added ssh command

This commit is contained in:
2021-06-05 17:54:14 +02:00
parent 3f8dec1f2d
commit 81da9d7c13
2 changed files with 62 additions and 10 deletions

View File

@@ -40,7 +40,7 @@ your operating system, or by using virtualenv for Python requirements, i.e.:
$ . .venv/bin/activate $ . .venv/bin/activate
(.venv) $ pip install requirements.txt (.venv) $ pip install requirements.txt
than you can issue: then you can issue:
.. code:: shell-session .. code:: shell-session
@@ -53,8 +53,33 @@ or simply link it somewhere in the path:
$ ln -s /path/to/box.py ~/bin/boxpy $ ln -s /path/to/box.py ~/bin/boxpy
$ chmod +x ~/bin/boxpy $ chmod +x ~/bin/boxpy
and now you can issue some command. There are four command for simple managing and now you can issue some command. For example, to spin up a VM with Ubuntu
VMs, maybe some other will be available in the future. Who knows. 18.04 with one CPU, 2GB of memory and 10GB of disk:
.. code:: shell-session
$ boxpy create --version 18.04 myvm
note, that Ubuntu is default distribution you don't need to specify
``--distro`` nor ``--version`` it will pick up latest LTS version. Now, let's
recreate it with 20.04:
.. code:: shell-session
$ boxpy rebuild --version 20.04 myvm
or recreate it with Fedora and add additional CPU:
.. code:: shell-session
$ boxpy rebuild --distro fedora --version 34 --cpu 2 myvm
now, let's connect to the VM using either ssh command, which is printed out at
as last ``boxpy`` output line, or simply by using ssh boxpy command:
.. code:: shell-session
$ boxpy ssh myvm
For your convenience there is a bash completion for each command, so you can For your convenience there is a bash completion for each command, so you can
use it ad-hoc, or place on your ``.bashrc`` or whatever: use it ad-hoc, or place on your ``.bashrc`` or whatever:
@@ -63,14 +88,17 @@ use it ad-hoc, or place on your ``.bashrc`` or whatever:
$ source <(boxpy completion bash) $ source <(boxpy completion bash)
currently there are four commands available: Currently, following commands are available:
- ``list`` - for quickly listing all/running VMs - ``list`` - for quickly listing all/running VMs
- ``destroy`` - that is probably obvious one - ``destroy`` - that is probably obvious one
- ``create`` and ``rebuild`` - ``create`` - create new VM
- ``rebuild`` - recreate specified VM
- ``ssh`` - connect to the VM using ssh
- ``completion`` - as described above
The latter two accepts several options besides required vm name. You can All of the commands have a range of options, and can be examined it by using
examine it by using ``--help``. ``--help``.
What is more interesting though, is the fact, that you can pass your own What is more interesting though, is the fact, that you can pass your own
`cloud-init`_ yaml file, so that VM can be provisioned in easy way. `cloud-init`_ yaml file, so that VM can be provisioned in easy way.

30
box.py
View File

@@ -119,7 +119,7 @@ _boxpy() {
fi fi
fi fi
opts="create destroy rebuild list completion" opts="create destroy rebuild list completion ssh"
if [[ ${cur} == "-q" || ${cur} == "-v" || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == "-q" || ${cur} == "-v" || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0 return 0
@@ -172,6 +172,11 @@ _boxpy() {
_get_excluded_items "${items[@]}" _get_excluded_items "${items[@]}"
COMPREPLY=( $(compgen -W "$result" -- ${cur}) ) COMPREPLY=( $(compgen -W "$result" -- ${cur}) )
;; ;;
ssh)
if [[ ${prev} == ${cmd} ]]; then
_vms_comp vms
fi
;;
esac esac
} }
@@ -255,7 +260,7 @@ class Config:
setattr(self, attr, vm_info[attr]) setattr(self, attr, vm_info[attr])
# next, grab the cloud config file # next, grab the cloud config file
if 'config' in args: if 'config' in args and args.config:
self.user_data = os.path.abspath(args.config) self.user_data = os.path.abspath(args.config)
else: else:
self.user_data = vm_info.get('user_data') self.user_data = vm_info.get('user_data')
@@ -855,7 +860,7 @@ def vmcreate(args, conf=None):
vbox.poweron() vbox.poweron()
print('You can access your VM by issuing:') print('You can access your VM by issuing:')
print(f'ssh -p {conf.port} -i {conf.ssh_key_path[:-4]} ' print(f'ssh -p {conf.port} -i {conf.ssh_key_path[:-4]} '
f'{DISTROS[args.distro]["username"]}@localhost') f'{DISTROS[conf.distro]["username"]}@localhost')
return 0 return 0
@@ -894,6 +899,21 @@ def shell_completion(args):
return 0 return 0
def connect(args):
vbox = VBoxManage(args.name)
conf = Config(args, vbox)
try:
subprocess.call(['ssh', '-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-i', conf.ssh_key_path[:-4],
f'ssh://{DISTROS[conf.distro]["username"]}'
f'@localhost:{conf.port}'])
except subprocess.CalledProcessError:
return None
return 0
def main(): def main():
parser = argparse.ArgumentParser(description="Automate deployment and " parser = argparse.ArgumentParser(description="Automate deployment and "
"maintenance of Ubuntu VMs using " "maintenance of Ubuntu VMs using "
@@ -963,6 +983,10 @@ def main():
help="pick shell to generate completions for") help="pick shell to generate completions for")
completion.set_defaults(func=shell_completion) completion.set_defaults(func=shell_completion)
ssh = subparsers.add_parser('ssh', help='Connect to the machine via SSH')
ssh.add_argument('name', help='name or UUID of the VM')
ssh.set_defaults(func=connect)
args = parser.parse_args() args = parser.parse_args()
if hasattr(args, 'func'): if hasattr(args, 'func'):