Compare commits
6 commits
6bee27bb0d
...
f5bd881b5d
Author | SHA1 | Date | |
---|---|---|---|
|
f5bd881b5d | ||
|
da373914d6 | ||
|
b771df150e | ||
|
021d2f0aa5 | ||
|
6a6680403f | ||
|
7f59891a4e |
2 changed files with 96 additions and 46 deletions
124
blend
124
blend
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import glob
|
from sys import stdout
|
||||||
import time
|
import time
|
||||||
import shutil
|
import shutil
|
||||||
import socket
|
import socket
|
||||||
|
@ -27,6 +27,7 @@ import getpass
|
||||||
import pexpect
|
import pexpect
|
||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import yaml
|
||||||
|
|
||||||
__version = '2.0.0'
|
__version = '2.0.0'
|
||||||
|
|
||||||
|
@ -83,22 +84,63 @@ def error(err):
|
||||||
print(colors.bold + colors.fg.red + '>> e: ' +
|
print(colors.bold + colors.fg.red + '>> e: ' +
|
||||||
colors.reset + colors.bold + err + colors.reset)
|
colors.reset + colors.bold + err + colors.reset)
|
||||||
|
|
||||||
|
|
||||||
|
def podman_it_remover(cmd: list):
|
||||||
|
'''
|
||||||
|
Removes `-it` from a podman command if stdout is not a tty
|
||||||
|
'''
|
||||||
|
return [x for x in cmd if x != '-it' or stdout.isatty()]
|
||||||
|
|
||||||
# END
|
# END
|
||||||
|
|
||||||
|
# TODO: fix temp paths before committing
|
||||||
|
|
||||||
distro_map = {
|
|
||||||
'arch-linux': 'quay.io/toolbx/arch-toolbox:latest',
|
def image_data_from_dict(dictionary):
|
||||||
'debian': 'quay.io/toolbx-images/debian-toolbox:testing',
|
'''
|
||||||
'fedora-42': 'quay.io/fedora/fedora-toolbox:42',
|
Returns a distro map and aliases from a dict in this format:
|
||||||
'centos': 'quay.io/toolbx-images/centos-toolbox:latest',
|
|
||||||
'ubuntu-22.04': 'quay.io/toolbx/ubuntu-toolbox:22.04',
|
```
|
||||||
'ubuntu-24.04': 'quay.io/toolbx/ubuntu-toolbox:24.04',
|
{'arch-linux': {'image': 'quay.io/toolbx/arch-toolbox:latest', 'aliases': ['arch']}}
|
||||||
}
|
```
|
||||||
|
|
||||||
|
which is loaded in from yaml config files in this format:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
arch-linux:
|
||||||
|
image: "quay.io/toolbx/arch-toolbox:latest"
|
||||||
|
aliases:
|
||||||
|
- arch
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
distro_map = {}
|
||||||
|
aliases = {}
|
||||||
|
|
||||||
|
for distro in dictionary.keys():
|
||||||
|
distro_map[distro] = dictionary[distro]['image']
|
||||||
|
try:
|
||||||
|
aliases[distro] = dictionary[distro]['aliases']
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return (distro_map, aliases)
|
||||||
|
|
||||||
|
|
||||||
|
distro_map, aliases = image_data_from_dict(
|
||||||
|
yaml.safe_load(open('/usr/share/blend/images.yaml')))
|
||||||
|
tmp = image_data_from_dict(yaml.safe_load(open('/system.yaml'))['images'])
|
||||||
|
distro_map += tmp[0]
|
||||||
|
aliases += tmp[1]
|
||||||
|
if os.path.exists(f'{os.getenv('XDG_CONFIG_HOME')}/blend/images.yaml'):
|
||||||
|
tmp = image_data_from_dict(yaml.safe_load(
|
||||||
|
open(f'{os.getenv('XDG_CONFIG_HOME')}/blend/images.yaml')))
|
||||||
|
distro_map += tmp[0]
|
||||||
|
aliases += tmp[1]
|
||||||
|
|
||||||
default_distro = 'arch-linux'
|
default_distro = 'arch-linux'
|
||||||
|
|
||||||
|
|
||||||
def get_distro():
|
def get_image():
|
||||||
try:
|
try:
|
||||||
return distro_map[args.distro]
|
return distro_map[args.distro]
|
||||||
except:
|
except:
|
||||||
|
@ -214,7 +256,7 @@ def core_create_container():
|
||||||
'--userns', 'keep-id',
|
'--userns', 'keep-id',
|
||||||
'--annotation', 'run.oci.keep_original_groups=1'])
|
'--annotation', 'run.oci.keep_original_groups=1'])
|
||||||
|
|
||||||
podman_command.extend([get_distro()])
|
podman_command.extend([get_image()])
|
||||||
|
|
||||||
# User (for init-blend)
|
# User (for init-blend)
|
||||||
podman_command.extend(['--uid', str(os.geteuid())])
|
podman_command.extend(['--uid', str(os.geteuid())])
|
||||||
|
@ -238,20 +280,19 @@ def host_get_output(cmd): return subprocess.run(['bash', '-c', cmd],
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('UTF-8').strip()
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('UTF-8').strip()
|
||||||
|
|
||||||
|
|
||||||
def core_get_retcode(cmd): return subprocess.run(['podman', 'exec', '--user', getpass.getuser(), '-it', args.container_name, 'bash', '-c', cmd],
|
def core_get_retcode(cmd):
|
||||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
|
podman_command = podman_it_remover(['podman', 'exec', '--user', getpass.getuser(), '-it',
|
||||||
|
args.container_name, 'bash', '-c', cmd])
|
||||||
|
return subprocess.run(podman_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
|
||||||
|
|
||||||
|
|
||||||
def core_run_container(cmd):
|
def core_run_container(cmd):
|
||||||
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
||||||
subprocess.call(['podman', 'exec', '--user', getpass.getuser(),
|
subprocess.call(podman_it_remover(['podman', 'exec', '--user', getpass.getuser(),
|
||||||
'-w', os.getcwd(), '-it', args.container_name, 'bash', '-c', cmd])
|
'-w', os.getcwd(), '-it', args.container_name, 'bash', '-c', cmd]))
|
||||||
|
|
||||||
|
|
||||||
def core_install_pkg(pkg):
|
def core_install_pkg(pkg):
|
||||||
if args.distro == 'arch':
|
|
||||||
args.distro = 'arch-linux'
|
|
||||||
|
|
||||||
if args.distro.startswith('fedora-'):
|
if args.distro.startswith('fedora-'):
|
||||||
if args.noconfirm == True:
|
if args.noconfirm == True:
|
||||||
core_run_container(f'sudo dnf -y install {pkg}')
|
core_run_container(f'sudo dnf -y install {pkg}')
|
||||||
|
@ -278,9 +319,6 @@ def core_install_pkg(pkg):
|
||||||
|
|
||||||
|
|
||||||
def core_remove_pkg(pkg):
|
def core_remove_pkg(pkg):
|
||||||
if args.distro == 'arch':
|
|
||||||
args.distro = 'arch-linux'
|
|
||||||
|
|
||||||
if args.distro.startswith('fedora-'):
|
if args.distro.startswith('fedora-'):
|
||||||
if args.noconfirm == True:
|
if args.noconfirm == True:
|
||||||
core_run_container(f'sudo dnf -y remove {pkg}')
|
core_run_container(f'sudo dnf -y remove {pkg}')
|
||||||
|
@ -300,9 +338,6 @@ def core_remove_pkg(pkg):
|
||||||
|
|
||||||
|
|
||||||
def core_search_pkg(pkg):
|
def core_search_pkg(pkg):
|
||||||
if args.distro == 'arch':
|
|
||||||
args.distro = 'arch-linux'
|
|
||||||
|
|
||||||
if args.distro.startswith('fedora-'):
|
if args.distro.startswith('fedora-'):
|
||||||
core_run_container(f'dnf search {pkg}')
|
core_run_container(f'dnf search {pkg}')
|
||||||
elif args.distro == 'arch-linux':
|
elif args.distro == 'arch-linux':
|
||||||
|
@ -314,9 +349,6 @@ def core_search_pkg(pkg):
|
||||||
|
|
||||||
|
|
||||||
def core_show_pkg(pkg):
|
def core_show_pkg(pkg):
|
||||||
if args.distro == 'arch':
|
|
||||||
args.distro = 'arch-linux'
|
|
||||||
|
|
||||||
if args.distro.startswith('fedora-'):
|
if args.distro.startswith('fedora-'):
|
||||||
core_run_container(f'dnf info {pkg}')
|
core_run_container(f'dnf info {pkg}')
|
||||||
elif args.distro == 'arch-linux':
|
elif args.distro == 'arch-linux':
|
||||||
|
@ -371,9 +403,6 @@ def show_blend():
|
||||||
|
|
||||||
|
|
||||||
def sync_blends():
|
def sync_blends():
|
||||||
if args.distro == 'arch':
|
|
||||||
args.distro = 'arch-linux'
|
|
||||||
|
|
||||||
if args.distro.startswith('fedora-'):
|
if args.distro.startswith('fedora-'):
|
||||||
core_run_container(f'dnf makecache')
|
core_run_container(f'dnf makecache')
|
||||||
elif args.distro == 'arch-linux':
|
elif args.distro == 'arch-linux':
|
||||||
|
@ -383,9 +412,6 @@ def sync_blends():
|
||||||
|
|
||||||
|
|
||||||
def update_blends():
|
def update_blends():
|
||||||
if args.distro == 'arch':
|
|
||||||
args.distro = 'arch-linux'
|
|
||||||
|
|
||||||
if args.distro.startswith('fedora-'):
|
if args.distro.startswith('fedora-'):
|
||||||
if args.noconfirm == True:
|
if args.noconfirm == True:
|
||||||
core_run_container(f'sudo dnf -y upgrade')
|
core_run_container(f'sudo dnf -y upgrade')
|
||||||
|
@ -424,30 +450,32 @@ def enter_container():
|
||||||
if not os.environ.get('BLEND_COMMAND'):
|
if not os.environ.get('BLEND_COMMAND'):
|
||||||
if args.pkg == []:
|
if args.pkg == []:
|
||||||
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
||||||
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args,
|
exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args,
|
||||||
'-w', os.getcwd(), '-it', args.container_name, 'bash']))
|
'-w', os.getcwd(), '-it', args.container_name, 'bash'])))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w',
|
exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w',
|
||||||
'/run/host' + os.getcwd(), '-it', args.container_name, 'bash']))
|
'/run/host' + os.getcwd(), '-it', args.container_name, 'bash'])))
|
||||||
else:
|
else:
|
||||||
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
||||||
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args,
|
exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args,
|
||||||
'-w', os.getcwd(), '-it', args.container_name, *args.pkg]))
|
'-w', os.getcwd(), '-it', args.container_name, *args.pkg])))
|
||||||
else:
|
else:
|
||||||
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w',
|
exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w',
|
||||||
'/run/host' + os.getcwd(), '-it', args.container_name, *args.pkg]))
|
'/run/host' + os.getcwd(), '-it', args.container_name, *args.pkg])))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
||||||
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w', os.getcwd(
|
exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w', os.getcwd(), '-it',
|
||||||
), '-it', args.container_name, 'bash', '-c', os.environ.get('BLEND_COMMAND')]))
|
args.container_name, 'bash', '-c', os.environ.get('BLEND_COMMAND')])))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w',
|
exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w',
|
||||||
'/run/host' + os.getcwd(), '-it', args.container_name, 'bash']))
|
'/run/host' + os.getcwd(), '-it', args.container_name, 'bash'])))
|
||||||
|
|
||||||
|
|
||||||
def create_container():
|
def create_container():
|
||||||
for container in args.pkg:
|
for container in args.pkg:
|
||||||
container = 'ubuntu-24.04' if container == 'ubuntu-24.04-lts' else container
|
|
||||||
args.container_name = container
|
args.container_name = container
|
||||||
if container in distro_map.keys() and distro_input == None:
|
if container in distro_map.keys() and distro_input == None:
|
||||||
args.distro = container
|
args.distro = container
|
||||||
|
@ -531,6 +559,10 @@ if len(sys.argv) == 1:
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
args = parser.parse_intermixed_args()
|
args = parser.parse_intermixed_args()
|
||||||
|
if args.distro not in distro_map.keys():
|
||||||
|
for (distro, al) in aliases:
|
||||||
|
if args.distro in al:
|
||||||
|
args.distro = distro
|
||||||
|
|
||||||
command = command_map[args.command]
|
command = command_map[args.command]
|
||||||
|
|
||||||
|
|
18
images.yaml
Normal file
18
images.yaml
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
arch-linux:
|
||||||
|
image: quay.io/toolbx/arch-toolbox:latest
|
||||||
|
aliases:
|
||||||
|
- arch
|
||||||
|
debian:
|
||||||
|
image: quay.io/toolbx-images/debian-toolbox:testing
|
||||||
|
fedora-42:
|
||||||
|
image: quay.io/fedora/fedora-toolbox:42
|
||||||
|
aliases:
|
||||||
|
- fedora
|
||||||
|
centos:
|
||||||
|
image: quay.io/toolbx-images/centos-toolbox:latest
|
||||||
|
ubuntu-22.04:
|
||||||
|
image: quay.io/toolbx/ubuntu-toolbox:22.04
|
||||||
|
ubuntu-24.04:
|
||||||
|
image: quay.io/toolbx/ubuntu-toolbox:24.04
|
||||||
|
aliases:
|
||||||
|
- ubuntu
|
Loading…
Add table
Add a link
Reference in a new issue