mirror of
https://github.com/gryf/boxpy.git
synced 2026-02-02 22:25:53 +01:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5f0def3103 | |||
| 43eabb9947 | |||
| 55416db13d | |||
| 86a5655025 | |||
| 259d11e409 |
117
box.py
117
box.py
@@ -18,11 +18,12 @@ import requests
|
||||
import yaml
|
||||
|
||||
|
||||
__version__ = "1.3"
|
||||
__version__ = "1.9.2"
|
||||
|
||||
CACHE_DIR = os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
|
||||
CLOUD_IMAGE = "ci.iso"
|
||||
FEDORA_RELEASE_MAP = {'32': '1.6', '33': '1.2', '34': '1.2'}
|
||||
FEDORA_RELEASE_MAP = {'32': '1.6', '33': '1.2', '34': '1.2', '35': '1.2',
|
||||
'36': '1.5', '37': '1.7', '38': '1.6'}
|
||||
DEBIAN_CODENAME_MAP = {'12': 'bookworm', '11': 'bullseye', '10': 'buster'}
|
||||
TYPE_MAP = {'HardDisk': 'disk', 'DVD': 'dvd', 'Floppy': 'floppy'}
|
||||
DISTRO_MAP = {'ubuntu': 'Ubuntu', 'fedora': 'Fedora',
|
||||
@@ -137,13 +138,37 @@ _boxpy() {
|
||||
if [[ ${prev} == ${cmd} ]]; then
|
||||
COMPREPLY=( $(compgen -W "bash" -- ${cur}) )
|
||||
fi
|
||||
;;
|
||||
start)
|
||||
items=(--type)
|
||||
if [[ ${prev} == ${cmd} ]]; then
|
||||
if [[ ${cmd} = "start" ]]; then
|
||||
_vms_comp vms
|
||||
else
|
||||
COMPREPLY=( $(compgen -W "${items[*]}" -- ${cur}) )
|
||||
fi
|
||||
else
|
||||
_get_excluded_items "${items[@]}"
|
||||
COMPREPLY=( $(compgen -W "$result" -- ${cur}) )
|
||||
|
||||
case "${prev}" in
|
||||
--type)
|
||||
COMPREPLY=( $(compgen -W "gui headless sdl separate" \
|
||||
-- ${cur}) )
|
||||
;;
|
||||
--*)
|
||||
COMPREPLY=( )
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
;;
|
||||
create|rebuild)
|
||||
items=(--cpus --disable-nested --disk-size --default-user --distro
|
||||
--forwarding --image --key --memory --hostname --port --config
|
||||
--version --type)
|
||||
if [[ ${prev} == ${cmd} ]]; then
|
||||
if [[ ${cmd} = "rebuild" ]]; then
|
||||
if [[ ${cmd} = "rebuild" || ${cmd} == "start" ]]; then
|
||||
_vms_comp vms
|
||||
else
|
||||
COMPREPLY=( $(compgen -W "${items[*]}" -- ${cur}) )
|
||||
@@ -195,11 +220,6 @@ _boxpy() {
|
||||
_vms_comp runningvms
|
||||
fi
|
||||
;;
|
||||
start)
|
||||
if [[ ${prev} == ${cmd} ]]; then
|
||||
_vms_comp vms
|
||||
fi
|
||||
;;
|
||||
stop)
|
||||
if [[ ${prev} == ${cmd} ]]; then
|
||||
_vms_comp runningvms
|
||||
@@ -587,14 +607,47 @@ class Config:
|
||||
|
||||
|
||||
class OsTypes:
|
||||
def ubuntu(conf):
|
||||
return "Ubuntu%s_64" % conf.version.replace('.', '_')
|
||||
def __init__(self, conf):
|
||||
self._conf = conf
|
||||
self._ostypes = []
|
||||
|
||||
def fedora(conf):
|
||||
self._gather_os_types()
|
||||
|
||||
def _gather_os_types(self):
|
||||
out = Run(['vboxmanage', 'list', 'ostypes']).stdout
|
||||
for line in out.split('\n'):
|
||||
if not line.startswith('ID:'):
|
||||
continue
|
||||
self._ostypes.append(line.split(':')[1].strip())
|
||||
|
||||
def ubuntu(self):
|
||||
lts = ''
|
||||
major, minor = [int(x) for x in self._conf.version.split('.')]
|
||||
|
||||
if major % 2 == 0 and minor == 4:
|
||||
lts = '_LTS'
|
||||
name = "Ubuntu%s%s_64" % (major, lts)
|
||||
|
||||
if name not in self._ostypes:
|
||||
return 'Ubuntu_64'
|
||||
|
||||
return name
|
||||
|
||||
def fedora(self):
|
||||
return "Fedora_64"
|
||||
|
||||
def debian(conf):
|
||||
return "Debian%s_64" % conf.version
|
||||
def debian(self):
|
||||
name = "Debian%s_64" % self._conf.version
|
||||
if name not in self._ostypes:
|
||||
return 'Debian_64'
|
||||
|
||||
return name
|
||||
|
||||
def get(self):
|
||||
if not hasattr(self, self._conf.distro):
|
||||
return "Linux_64"
|
||||
|
||||
return getattr(self, self._conf.distro)()
|
||||
|
||||
|
||||
class VBoxManage:
|
||||
@@ -779,12 +832,8 @@ class VBoxManage:
|
||||
'--nic1', 'nat',
|
||||
'--natpf1', f'boxpyssh,tcp,,{port},,22',
|
||||
'--graphicscontroller', 'vmsvga',
|
||||
'--vram', '16']
|
||||
|
||||
if hasattr(OsTypes, conf.distro):
|
||||
cmd.extend(['--ostype', getattr(OsTypes, conf.distro)(conf)])
|
||||
else:
|
||||
cmd.extend(['--ostype', 'Linux_64'])
|
||||
'--vram', '16',
|
||||
'--ostype', OsTypes(conf).get()]
|
||||
|
||||
for count, (hostport, vmport) in enumerate(conf.forwarding.items(),
|
||||
start=1):
|
||||
@@ -1155,17 +1204,17 @@ DISTROS = {'ubuntu': {'username': 'ubuntu',
|
||||
'realname': 'fedora',
|
||||
'img_class': Fedora,
|
||||
'amd64': 'x86_64',
|
||||
'default_version': '34'},
|
||||
'default_version': '38'},
|
||||
'centos': {'username': 'centos',
|
||||
'realname': 'centos',
|
||||
'img_class': CentosStream,
|
||||
'amd64': 'x86_64',
|
||||
'default_version': '8'},
|
||||
'default_version': '9'},
|
||||
'debian': {'username': 'debian',
|
||||
'realname': 'debian',
|
||||
'img_class': Debian,
|
||||
'amd64': 'amd64',
|
||||
'default_version': '11'}}
|
||||
'default_version': '12'}}
|
||||
|
||||
|
||||
def get_image_object(vbox, version, image='ubuntu', arch='amd64'):
|
||||
@@ -1320,11 +1369,18 @@ def vmcreate(args, conf=None):
|
||||
f'Check output in debug mode.')
|
||||
time.sleep(3)
|
||||
counter += 1
|
||||
if counter == 8 and conf.distro == 'debian':
|
||||
|
||||
# TODO: there is something odd with debian cloud images prior
|
||||
# to 12 (bookworm), as on first run system crashes. In that
|
||||
# case after ~20 seconds there should already be panic, reset
|
||||
# machine as a workaround. Remove this after debian 12
|
||||
# stabilization later this year.
|
||||
if (counter == 8 and conf.distro == 'debian'
|
||||
and conf.version != '12'):
|
||||
LOG.debug('Resetting `%s`, due to the issue with kernel '
|
||||
'panic on Debian %s the first run', conf.name,
|
||||
conf.version)
|
||||
counter += 1
|
||||
# there is something odd with debian cloud images, as they
|
||||
# segfault on first run. after ~20 seconds there should
|
||||
# already be panic, reset machine should help
|
||||
vbox.poweroff()
|
||||
time.sleep(3)
|
||||
vbox.poweron(args.type)
|
||||
@@ -1549,7 +1605,7 @@ def connect(args):
|
||||
return Run(cmd, False).returncode
|
||||
|
||||
|
||||
def _set_vmstate(name, state):
|
||||
def _set_vmstate(name, state, guitype=None):
|
||||
|
||||
vbox = VBoxManage(name)
|
||||
if not vbox.get_vm_info():
|
||||
@@ -1565,13 +1621,13 @@ def _set_vmstate(name, state):
|
||||
return
|
||||
|
||||
if state == "start":
|
||||
vbox.poweron()
|
||||
vbox.poweron(guitype)
|
||||
else:
|
||||
vbox.acpipowerbutton()
|
||||
|
||||
|
||||
def vmstart(args):
|
||||
_set_vmstate(args.name, 'start')
|
||||
_set_vmstate(args.name, 'start', args.type)
|
||||
|
||||
|
||||
def vmstop(args):
|
||||
@@ -1686,6 +1742,9 @@ def main():
|
||||
|
||||
start = subparsers.add_parser('start', help='start VM')
|
||||
start.add_argument('name', help='name or UUID of the VM')
|
||||
start.add_argument('-t', '--type', default='headless',
|
||||
help="VM run type, headless by default.",
|
||||
choices=['gui', 'headless', 'sdl', 'separate'])
|
||||
start.set_defaults(func=vmstart)
|
||||
|
||||
stop = subparsers.add_parser('stop', help='stop VM')
|
||||
|
||||
Reference in New Issue
Block a user