2023-08-15 16:29:47 -05:00
|
|
|
#!/usr/bin/python3
|
2023-02-11 06:03:29 -06:00
|
|
|
# Copyright (C) 2023 Rudra Saraswat
|
2023-05-02 13:32:57 -05:00
|
|
|
#
|
2023-02-11 06:03:29 -06:00
|
|
|
# This file is part of blend.
|
2023-05-02 13:32:57 -05:00
|
|
|
#
|
2023-02-11 06:03:29 -06:00
|
|
|
# blend is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2023-05-02 13:32:57 -05:00
|
|
|
#
|
2023-02-11 06:03:29 -06:00
|
|
|
# blend is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2023-05-02 13:32:57 -05:00
|
|
|
#
|
2023-02-11 06:03:29 -06:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with blend. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
|
|
|
import time
|
2023-01-18 11:36:57 -06:00
|
|
|
import shutil
|
2023-02-11 06:03:29 -06:00
|
|
|
import socket
|
2023-05-02 13:32:57 -05:00
|
|
|
import getpass
|
2023-02-11 06:03:29 -06:00
|
|
|
import pexpect
|
2023-01-18 11:36:57 -06:00
|
|
|
import argparse
|
|
|
|
import subprocess
|
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
__version = '2.0.0'
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
# Colors
|
|
|
|
|
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
class colors:
|
|
|
|
reset = '\033[0m'
|
|
|
|
bold = '\033[01m'
|
|
|
|
disable = '\033[02m'
|
|
|
|
underline = '\033[04m'
|
|
|
|
reverse = '\033[07m'
|
|
|
|
strikethrough = '\033[09m'
|
|
|
|
invisible = '\033[08m'
|
|
|
|
|
|
|
|
class fg:
|
|
|
|
black = '\033[30m'
|
|
|
|
red = '\033[31m'
|
|
|
|
green = '\033[32m'
|
|
|
|
orange = '\033[33m'
|
|
|
|
blue = '\033[34m'
|
|
|
|
purple = '\033[35m'
|
|
|
|
cyan = '\033[36m'
|
|
|
|
lightgrey = '\033[37m'
|
|
|
|
darkgrey = '\033[90m'
|
|
|
|
lightred = '\033[91m'
|
|
|
|
lightgreen = '\033[92m'
|
|
|
|
yellow = '\033[93m'
|
|
|
|
lightblue = '\033[94m'
|
|
|
|
pink = '\033[95m'
|
|
|
|
lightcyan = '\033[96m'
|
|
|
|
|
|
|
|
class bg:
|
|
|
|
black = '\033[40m'
|
|
|
|
red = '\033[41m'
|
|
|
|
green = '\033[42m'
|
|
|
|
orange = '\033[43m'
|
|
|
|
blue = '\033[44m'
|
|
|
|
purple = '\033[45m'
|
|
|
|
cyan = '\033[46m'
|
|
|
|
lightgrey = '\033[47m'
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
# END
|
|
|
|
|
|
|
|
# Helper functions
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
def info(msg):
|
2023-05-02 13:32:57 -05:00
|
|
|
print(colors.bold + colors.fg.cyan + '>> i: ' +
|
|
|
|
colors.reset + colors.bold + msg + colors.reset)
|
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
def error(err):
|
2023-05-02 13:32:57 -05:00
|
|
|
print(colors.bold + colors.fg.red + '>> e: ' +
|
|
|
|
colors.reset + colors.bold + err + colors.reset)
|
|
|
|
|
|
|
|
# END
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
distro_map = {
|
|
|
|
'arch': 'docker.io/library/archlinux',
|
2023-05-17 00:02:22 -05:00
|
|
|
'almalinux-9': 'quay.io/almalinux/almalinux:9',
|
2023-05-16 13:05:16 -05:00
|
|
|
'crystal-linux': 'registry.getcryst.al/crystal/misc/docker:latest',
|
2023-05-16 12:42:52 -05:00
|
|
|
'debian': 'docker.io/library/debian:latest',
|
2023-06-04 10:24:34 -05:00
|
|
|
'fedora-38': 'docker.io/library/fedora:38',
|
2023-05-18 10:56:34 -05:00
|
|
|
'kali-linux': 'docker.io/kalilinux/kali-rolling',
|
2023-05-16 13:05:16 -05:00
|
|
|
'neurodebian-bookworm': 'docker.io/library/neurodebian:nd120',
|
2023-05-18 10:56:34 -05:00
|
|
|
'rocky-linux': 'docker.io/rockylinux/rockylinux:9',
|
2023-02-11 06:03:29 -06:00
|
|
|
'ubuntu-22.04': 'docker.io/library/ubuntu:22.04',
|
2023-05-08 02:40:29 -05:00
|
|
|
'ubuntu-23.04': 'docker.io/library/ubuntu:23.04'
|
2023-01-18 11:36:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
default_distro = 'arch'
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def get_distro():
|
|
|
|
try:
|
|
|
|
return distro_map[args.distro]
|
|
|
|
except:
|
|
|
|
error(f"{args.distro} isn't supported by blend.")
|
|
|
|
exit()
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def list_containers():
|
2023-01-18 11:36:57 -06:00
|
|
|
_list = subprocess.run(['podman', 'ps', '-a', '--no-trunc', '--size', '--format',
|
2023-05-02 13:32:57 -05:00
|
|
|
'{{.Names}}:{{.Mounts}}'], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
|
2023-01-18 11:36:57 -06:00
|
|
|
if len(_list) == 0:
|
|
|
|
info('No containers. Create one by installing a package (`blend install hello`), or manually create one (`blend create-container arch`).')
|
2023-01-19 03:01:12 -06:00
|
|
|
else:
|
|
|
|
info('List of containers:')
|
2023-02-11 06:03:29 -06:00
|
|
|
for i, container in enumerate(_list.splitlines(keepends=False)):
|
|
|
|
if 'blend' in container.split(':')[1]:
|
|
|
|
print(f"{colors.bold}{i}.{colors.reset} {container.split(':')[0]}")
|
2023-01-18 11:36:57 -06:00
|
|
|
return False
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def check_container(name):
|
2023-01-18 11:36:57 -06:00
|
|
|
_list = subprocess.run(['podman', 'ps', '-a', '--no-trunc', '--size', '--format',
|
2023-05-02 13:32:57 -05:00
|
|
|
'{{.Names}}:{{.Mounts}}'], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
|
2023-01-18 11:36:57 -06:00
|
|
|
for container in _list.splitlines(keepends=False):
|
2023-04-10 23:54:31 -05:00
|
|
|
if ('blend' in container.split(':')[1] or 'distrobox' in container.split(':')[1]) and name.strip() == container.split(':')[0]:
|
2023-05-02 13:32:57 -05:00
|
|
|
return True
|
2023-01-18 11:36:57 -06:00
|
|
|
return False
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
|
|
|
def check_container_status(name):
|
2023-02-11 06:03:29 -06:00
|
|
|
return host_get_output("podman inspect --type container " + name + " --format \"{{.State.Status}}\"")
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2024-02-09 04:27:30 -06:00
|
|
|
def core_start_container(name, new_container=False):
|
2023-05-02 13:32:57 -05:00
|
|
|
subprocess.call(['podman', 'start', name],
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
2023-02-11 06:03:29 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
start_time = time.time() - 1000 # workaround
|
2023-02-11 06:03:29 -06:00
|
|
|
if check_container_status(name) != 'running':
|
|
|
|
print('')
|
|
|
|
error('the entry point failed to run; try again later')
|
|
|
|
info("here are the container's logs:")
|
|
|
|
subprocess.call(['podman', 'logs', '--since', str(start_time), name])
|
|
|
|
exit(1)
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
logproc = pexpect.spawn(
|
|
|
|
'podman', args=['logs', '-f', '--since', str(start_time), name], timeout=300)
|
2023-02-11 06:03:29 -06:00
|
|
|
logproc.logfile_read = sys.stdout.buffer
|
|
|
|
|
2023-04-17 02:26:58 -05:00
|
|
|
logproc.expect('Completed container setup.')
|
2023-02-11 06:03:29 -06:00
|
|
|
logproc.terminate()
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def core_create_container():
|
2023-01-18 11:36:57 -06:00
|
|
|
name = args.container_name
|
|
|
|
distro = args.distro
|
|
|
|
info(f'creating container {name}, using {distro}')
|
2023-02-11 06:03:29 -06:00
|
|
|
|
2024-02-09 04:27:30 -06:00
|
|
|
if check_container(name):
|
|
|
|
error(f'container {name} already exists')
|
|
|
|
exit(1)
|
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
podman_command = []
|
|
|
|
|
|
|
|
# Basic stuff
|
|
|
|
podman_command.extend(['podman', 'create', '--name', name])
|
|
|
|
podman_command.extend(['--hostname', name + '.' + socket.gethostname()])
|
|
|
|
podman_command.extend(['--privileged', '--ipc', 'host'])
|
|
|
|
podman_command.extend(['--network', 'host'])
|
|
|
|
podman_command.extend(['--security-opt', 'label=disable'])
|
|
|
|
podman_command.extend(['--user', 'root:root', '--pid', 'host'])
|
2023-05-02 13:32:57 -05:00
|
|
|
# identify as blend container
|
|
|
|
podman_command.extend(['--label', 'manager=blend'])
|
2023-02-11 06:03:29 -06:00
|
|
|
|
|
|
|
# Env variables
|
|
|
|
podman_command.extend(['--env', 'HOME=' + os.path.expanduser('~')])
|
2023-05-02 13:32:57 -05:00
|
|
|
podman_command.extend(['--env', 'CONTAINER_NAME=' + name])
|
2023-02-11 06:03:29 -06:00
|
|
|
|
|
|
|
# Volumes
|
|
|
|
podman_command.extend(['--volume', '/:/run/host:rslave'])
|
|
|
|
podman_command.extend(['--volume', '/tmp:/tmp:rslave'])
|
2023-05-02 13:32:57 -05:00
|
|
|
podman_command.extend(
|
|
|
|
['--volume', f"{os.path.expanduser('~')}:{os.path.expanduser('~')}:rslave"])
|
|
|
|
podman_command.extend(
|
|
|
|
['--volume', f"/run/user/{os.geteuid()}:/run/user/{os.geteuid()}:rslave"])
|
2023-02-11 06:03:29 -06:00
|
|
|
|
|
|
|
# Volumes (config files)
|
|
|
|
podman_command.extend(['--volume', f"/etc/hosts:/etc/hosts:ro"])
|
|
|
|
podman_command.extend(['--volume', f"/etc/localtime:/etc/localtime:ro"])
|
2023-05-02 13:32:57 -05:00
|
|
|
podman_command.extend(
|
|
|
|
['--volume', f"/etc/resolv.conf:/etc/resolv.conf:ro"])
|
2023-02-11 06:03:29 -06:00
|
|
|
|
|
|
|
# Volumes (files and tools)
|
|
|
|
podman_command.extend(['--volume', '/usr/bin/init-blend:/usr/bin/init-blend:ro',
|
2023-05-02 13:32:57 -05:00
|
|
|
'--entrypoint', '/usr/bin/init-blend']) # our entrypoint
|
|
|
|
# and the tool to run commands on the host
|
|
|
|
podman_command.extend(
|
|
|
|
['--volume', '/usr/bin/host-blend:/usr/bin/host-blend:ro'])
|
2023-02-11 06:03:29 -06:00
|
|
|
podman_command.extend(['--volume', '/var/log/journal'])
|
|
|
|
|
|
|
|
podman_command.extend(['--mount', 'type=devpts,destination=/dev/pts',
|
|
|
|
'--userns', 'keep-id',
|
|
|
|
'--annotation', 'run.oci.keep_original_groups=1'])
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
podman_command.extend([get_distro()])
|
|
|
|
|
|
|
|
# User (for init-blend)
|
|
|
|
podman_command.extend(['--uid', str(os.geteuid())])
|
|
|
|
podman_command.extend(['--group', str(os.getgid())])
|
|
|
|
podman_command.extend(['--username', getpass.getuser()])
|
|
|
|
podman_command.extend(['--home', os.path.expanduser('~')])
|
|
|
|
|
|
|
|
ret = subprocess.run(podman_command).returncode
|
|
|
|
if ret != 0:
|
2023-01-18 11:36:57 -06:00
|
|
|
error(f'failed to create container {name}')
|
|
|
|
exit(1)
|
2023-02-11 06:03:29 -06:00
|
|
|
|
|
|
|
core_start_container(name)
|
|
|
|
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
def core_get_output(cmd): return subprocess.run(
|
|
|
|
cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('UTF-8').strip()
|
|
|
|
|
|
|
|
|
|
|
|
def host_get_output(cmd): return subprocess.run(['bash', '-c', cmd],
|
|
|
|
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],
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def core_run_container(cmd):
|
2023-05-02 13:32:57 -05:00
|
|
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
|
|
|
subprocess.call(['podman', 'exec', '--user', getpass.getuser(),
|
|
|
|
'-w', os.getcwd(), '-it', args.container_name, 'bash', '-c', cmd])
|
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def core_install_pkg(pkg):
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.distro == 'fedora-rawhide':
|
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo dnf -y install {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo dnf install {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro == 'arch':
|
2023-02-11 06:03:29 -06:00
|
|
|
if core_get_retcode('[ -f /usr/bin/yay ]') != 0:
|
|
|
|
core_run_container('sudo pacman -Sy')
|
2023-05-02 13:32:57 -05:00
|
|
|
core_run_container(
|
|
|
|
'sudo pacman --noconfirm -Syu --needed git base-devel')
|
|
|
|
core_run_container(
|
|
|
|
'TEMP_DIR="$(mktemp -d)"; cd "${TEMP_DIR}"; git clone https://aur.archlinux.org/yay.git; cd yay; makepkg --noconfirm -si; rm -rf "${TEMP_DIR}"')
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay -Sy')
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay --noconfirm -Syu {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay -Syu {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro.startswith('ubuntu-'):
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get update')
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get install -y {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get install {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def core_remove_pkg(pkg):
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.distro == 'fedora-rawhide':
|
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo dnf -y remove {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo dnf remove {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro == 'arch':
|
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo pacman --noconfirm -Rcns {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo pacman -Rcns {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro.startswith('ubuntu-'):
|
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get purge -y {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get purge {pkg}')
|
|
|
|
core_run_container(f'sudo apt-get autoremove --purge -y {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def core_search_pkg(pkg):
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.distro == 'fedora-rawhide':
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'dnf search {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro == 'arch':
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay -Sy')
|
|
|
|
core_run_container(f'yay {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro.startswith('ubuntu-'):
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get update')
|
|
|
|
core_run_container(f'apt-cache search {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
def core_show_pkg(pkg):
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.distro == 'fedora-rawhide':
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'dnf info {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro == 'arch':
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay -Sy')
|
|
|
|
core_run_container(f'yay -Si {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro.startswith('ubuntu-'):
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get update')
|
|
|
|
core_run_container(f'apt-cache show {pkg}')
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def install_blend():
|
|
|
|
if len(args.pkg) == 0:
|
|
|
|
error('no packages to install')
|
|
|
|
|
|
|
|
for pkg in args.pkg:
|
|
|
|
info(f'installing blend {pkg}')
|
2023-02-11 06:03:29 -06:00
|
|
|
if not check_container(args.container_name):
|
|
|
|
core_create_container()
|
|
|
|
core_install_pkg(pkg)
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def remove_blend():
|
|
|
|
if len(args.pkg) == 0:
|
|
|
|
error('no packages to remove')
|
|
|
|
|
|
|
|
for pkg in args.pkg:
|
|
|
|
info(f'removing blend {pkg}')
|
2023-02-11 06:03:29 -06:00
|
|
|
if not check_container(args.container_name):
|
2023-01-18 11:36:57 -06:00
|
|
|
error(f"container {args.container_name} doesn't exist")
|
2023-02-11 06:03:29 -06:00
|
|
|
core_remove_pkg(pkg)
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def search_blend():
|
|
|
|
if len(args.pkg) == 0:
|
|
|
|
error('no packages to search for')
|
|
|
|
|
|
|
|
for pkg in args.pkg:
|
2023-02-11 06:03:29 -06:00
|
|
|
if not check_container(args.container_name):
|
2023-01-18 11:36:57 -06:00
|
|
|
error(f"container {args.container_name} doesn't exist")
|
2023-02-11 06:03:29 -06:00
|
|
|
core_search_pkg(pkg)
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def show_blend():
|
|
|
|
if len(args.pkg) == 0:
|
|
|
|
error('no packages to show')
|
|
|
|
|
|
|
|
for pkg in args.pkg:
|
|
|
|
info(f'info about blend {pkg}')
|
2023-02-11 06:03:29 -06:00
|
|
|
if not check_container(args.container_name):
|
2023-01-18 11:36:57 -06:00
|
|
|
error(f"container {args.container_name} doesn't exist")
|
2023-02-11 06:03:29 -06:00
|
|
|
core_show_pkg(pkg)
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def sync_blends():
|
|
|
|
if args.distro == 'fedora-rawhide':
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'dnf makecache')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro == 'arch':
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay -Syy')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro.startswith('ubuntu-'):
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get update')
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def update_blends():
|
|
|
|
if args.distro == 'fedora-rawhide':
|
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo dnf -y upgrade')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo dnf upgrade')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro == 'arch':
|
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay --noconfirm')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'yay')
|
2023-01-18 11:36:57 -06:00
|
|
|
elif args.distro.startswith('ubuntu-'):
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get update')
|
2023-01-18 11:36:57 -06:00
|
|
|
if args.noconfirm == True:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get dist-upgrade -y')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
core_run_container(f'sudo apt-get dist-upgrade')
|
2023-01-18 11:36:57 -06:00
|
|
|
else:
|
|
|
|
error(f'distribution {args.distro} is not supported')
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def enter_container():
|
2024-02-09 04:27:30 -06:00
|
|
|
if check_container_status(args.container_name) != 'running':
|
|
|
|
core_start_container(args.container_name)
|
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
podman_args = ['--env', 'LC_ALL=C.UTF-8']
|
2023-02-11 06:03:29 -06:00
|
|
|
sudo = []
|
|
|
|
if os.environ.get('SUDO_USER') == None:
|
|
|
|
podman_args = ['--user', getpass.getuser()]
|
|
|
|
else:
|
2023-05-02 13:32:57 -05:00
|
|
|
sudo = ['sudo', '-u', os.environ.get(
|
|
|
|
'SUDO_USER'), f'PATH={os.path.expanduser("~/.local/share/bin/blend_bin")}:/usr/bin']
|
2023-02-11 06:03:29 -06:00
|
|
|
for name, val in os.environ.items():
|
2023-05-02 13:32:57 -05:00
|
|
|
if name not in ['LANG', 'LC_CTYPE', 'LC_ALL', 'PATH', 'HOST', 'HOSTNAME', 'SHELL'] and not name.startswith('_'):
|
2023-02-11 06:03:29 -06:00
|
|
|
podman_args.append('--env')
|
|
|
|
podman_args.append(name + '=' + val)
|
2023-01-19 03:01:12 -06:00
|
|
|
if os.environ.get('BLEND_COMMAND') == None or os.environ.get('BLEND_COMMAND') == '':
|
2023-02-11 06:03:29 -06:00
|
|
|
if args.pkg == []:
|
|
|
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
2023-05-02 13:32:57 -05:00
|
|
|
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args,
|
|
|
|
'-w', os.getcwd(), '-it', args.container_name, 'bash']))
|
2023-02-11 06:03:29 -06:00
|
|
|
else:
|
2023-05-02 13:32:57 -05:00
|
|
|
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w',
|
|
|
|
'/run/host' + os.getcwd(), '-it', args.container_name, 'bash']))
|
2023-02-11 06:03:29 -06:00
|
|
|
else:
|
|
|
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
2023-05-02 13:32:57 -05:00
|
|
|
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args,
|
|
|
|
'-w', os.getcwd(), '-it', args.container_name, *args.pkg]))
|
2023-02-11 06:03:29 -06:00
|
|
|
else:
|
2023-05-02 13:32:57 -05:00
|
|
|
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w',
|
|
|
|
'/run/host' + os.getcwd(), '-it', args.container_name, *args.pkg]))
|
2023-01-19 03:01:12 -06:00
|
|
|
else:
|
2023-02-11 06:03:29 -06:00
|
|
|
if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'):
|
2023-05-02 13:32:57 -05:00
|
|
|
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w', os.getcwd(
|
|
|
|
), '-it', args.container_name, 'bash', '-c', os.environ.get('BLEND_COMMAND')]))
|
2023-02-11 06:03:29 -06:00
|
|
|
else:
|
2023-05-02 13:32:57 -05:00
|
|
|
exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w',
|
|
|
|
'/run/host' + os.getcwd(), '-it', args.container_name, 'bash']))
|
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
def create_container():
|
|
|
|
for container in args.pkg:
|
|
|
|
args.container_name = container
|
2023-01-26 04:18:52 -06:00
|
|
|
if container in distro_map.keys() and distro_input == None:
|
2023-01-27 08:54:49 -06:00
|
|
|
args.distro = container
|
2023-02-11 06:03:29 -06:00
|
|
|
core_create_container()
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
def remove_container():
|
|
|
|
for container in args.pkg:
|
|
|
|
info(f'removing container {container}')
|
2023-05-02 13:32:57 -05:00
|
|
|
subprocess.run(['podman', 'stop', container],
|
|
|
|
stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
|
|
|
|
subprocess.run(['podman', 'rm', '-f', container],
|
|
|
|
stdout=subprocess.DEVNULL)
|
|
|
|
for bin in os.listdir(os.path.expanduser('~/.local/bin/blend_bin')):
|
|
|
|
if bin.endswith(f'.{container}'):
|
|
|
|
os.remove(os.path.join(os.path.expanduser('~/.local/bin/blend_bin'), bin))
|
2023-05-08 02:40:29 -05:00
|
|
|
for app in os.listdir(os.path.expanduser('~/.local/share/applications')):
|
|
|
|
if app.startswith(f'blend;{container};'):
|
|
|
|
os.remove(os.path.join(os.path.expanduser('~/.local/share/applications'), app))
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
def start_containers():
|
|
|
|
_list = subprocess.run(['podman', 'ps', '-a', '--no-trunc', '--size', '--format',
|
2023-05-02 13:32:57 -05:00
|
|
|
'{{.Names}}:{{.Mounts}}'], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
|
2023-01-18 11:36:57 -06:00
|
|
|
if len(_list) == 0:
|
2023-02-11 06:03:29 -06:00
|
|
|
info('No containers. Create one by installing a package (`blend install hello`), or manually create one (`blend create-container -d arch`).')
|
2023-01-18 11:36:57 -06:00
|
|
|
for container in _list.splitlines(keepends=False):
|
|
|
|
container = container.split(':')[0]
|
|
|
|
info(f'starting container {container}')
|
2023-02-11 06:03:29 -06:00
|
|
|
subprocess.call(['podman', 'start', container])
|
2023-01-18 11:36:57 -06:00
|
|
|
|
2023-05-02 13:32:57 -05:00
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
if shutil.which('podman') is None:
|
|
|
|
error("podman isn't installed, which is a hard dep")
|
2023-01-18 11:36:57 -06:00
|
|
|
exit(1)
|
|
|
|
|
2023-02-11 06:03:29 -06:00
|
|
|
if os.geteuid() == 0 and os.environ['BLEND_ALLOW_ROOT'] == None:
|
2023-01-18 11:36:57 -06:00
|
|
|
error("do not run as root")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
description = f'''
|
|
|
|
{colors.bold}{colors.fg.purple}Version:{colors.reset} {__version}{colors.bold}
|
|
|
|
|
2024-02-09 04:27:30 -06:00
|
|
|
Use the 'blendOS Settings' app to create and manage Linux containers, Android apps and for system configuration.
|
2023-04-17 02:26:58 -05:00
|
|
|
|
|
|
|
You can install and submit web apps from the Web Store.
|
2023-01-18 11:36:57 -06:00
|
|
|
'''
|
|
|
|
|
|
|
|
epilog = f'''
|
|
|
|
{colors.bold}Made with {colors.fg.red}\u2764{colors.reset}{colors.bold} by Rudra Saraswat.{colors.reset}
|
|
|
|
'''
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=description, usage=argparse.SUPPRESS,
|
|
|
|
epilog=epilog, formatter_class=argparse.RawTextHelpFormatter)
|
2023-05-02 13:32:57 -05:00
|
|
|
command_map = {'enter': enter_container,
|
|
|
|
'exec': enter_container,
|
|
|
|
'create-container': core_create_container,
|
|
|
|
'remove-container': remove_container,
|
|
|
|
'list-containers': list_containers,
|
|
|
|
'start-containers': start_containers,
|
|
|
|
'sync': sync_blends,
|
|
|
|
'help': 'help',
|
|
|
|
'version': 'version'}
|
|
|
|
parser.add_argument('command', choices=command_map.keys(),
|
|
|
|
help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument('pkg', action='store', type=str,
|
|
|
|
nargs='*', help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument('-cn', '--container-name', action='store',
|
|
|
|
nargs=1, metavar='CONTAINER NAME', help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument('-y', '--noconfirm',
|
|
|
|
action='store_true', help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument('-d', '--distro', action='store', nargs=1,
|
|
|
|
metavar='DISTRO', help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument('-v', '--version', action='version',
|
|
|
|
version=f'%(prog)s {__version}', help=argparse.SUPPRESS)
|
2023-01-18 11:36:57 -06:00
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
parser.print_help()
|
|
|
|
exit()
|
|
|
|
|
|
|
|
args = parser.parse_intermixed_args()
|
|
|
|
|
2023-01-19 03:01:12 -06:00
|
|
|
command = command_map[args.command]
|
|
|
|
|
2023-01-18 11:36:57 -06:00
|
|
|
distro_input = args.distro
|
|
|
|
args.distro = default_distro if args.distro == None else args.distro[0]
|
|
|
|
cn_input = args.container_name
|
|
|
|
args.container_name = args.distro if args.container_name == None else args.container_name[0]
|
|
|
|
|
|
|
|
if command == 'help':
|
|
|
|
parser.print_help()
|
|
|
|
elif command == 'version':
|
|
|
|
parser.parse_args(['--version'])
|
|
|
|
else:
|
2023-01-19 03:01:12 -06:00
|
|
|
command()
|