Clear out buildah wrapper public API and debug levels

This commit is contained in:
jpic 2020-02-15 21:23:17 +01:00
parent 2513c65433
commit a3a81eaca5
7 changed files with 68 additions and 61 deletions

View File

@ -104,6 +104,8 @@ class Action:
return Output(*args, **kwargs) return Output(*args, **kwargs)
async def __call__(self, *args, **kwargs): async def __call__(self, *args, **kwargs):
self.call_args = args
self.call_kwargs = kwargs
self.output = self.output_factory(*args, **kwargs) self.output = self.output_factory(*args, **kwargs)
self.output_start() self.output_start()
self.status = 'running' self.status = 'running'

View File

@ -6,6 +6,7 @@ from shlax import *
class GitLabCIConfig(Script): class GitLabCIConfig(Script):
async def call(self, *args, write=True, **kwargs): async def call(self, *args, write=True, **kwargs):
output = yaml.dump(self.kwargs) output = yaml.dump(self.kwargs)
if kwargs['debug'] is True:
self.output(output) self.output(output)
if write: if write:
with open('.gitlab-ci.yml', 'w+') as f: with open('.gitlab-ci.yml', 'w+') as f:

View File

@ -17,7 +17,7 @@ class Output:
def colorize(self, code, content): def colorize(self, code, content):
return self.color(code) + content + self.color() return self.color(code) + content + self.color()
def __init__(self, prefix=None, regexps=None, debug=True, write=None, flush=None): def __init__(self, prefix=None, regexps=None, debug=False, write=None, flush=None):
self.prefix = prefix self.prefix = prefix
self.debug = debug self.debug = debug
self.prefix_length = 0 self.prefix_length = 0

View File

@ -54,7 +54,7 @@ class Proc:
""" """
test = False test = False
def __init__(self, *args, prefix=None, raises=True, debug=True, output=None): def __init__(self, *args, prefix=None, raises=True, debug=None, output=None):
self.debug = debug if not self.test else False self.debug = debug if not self.test else False
self.output = output or Output() self.output = output or Output()
self.cmd = ' '.join(args) self.cmd = ' '.join(args)

View File

@ -31,54 +31,3 @@ class Script(Action):
async def call(self, *args, **kwargs): async def call(self, *args, **kwargs):
for action in self.actions: for action in self.actions:
await action(*args, **kwargs) await action(*args, **kwargs)
def shargs(self, *args, **kwargs):
user = kwargs.pop('user', None)
kwargs['debug'] = True
args = [str(arg) for arg in args if args is not None]
if args and ' ' in args[0]:
if len(args) == 1:
args = ['sh', '-euc', args[0]]
else:
args = ['sh', '-euc'] + list(args)
if user == 'root':
args = ['sudo'] + args
elif user:
args = ['sudo', '-u', user] + args
if self.parent:
return self.parent.shargs(*args, **kwargs)
else:
return args, kwargs
async def exec(self, *args, **kwargs):
args, kwargs = self.shargs(*args, **kwargs)
proc = await Proc(*args, **kwargs)()
if kwargs.get('wait', True):
await proc.wait()
return proc
async def rexec(self, *args, **kwargs):
kwargs['user'] = 'root'
return await self.exec(*args, **kwargs)
async def env(self, name):
return (await self.exec('echo $' + name)).out
async def which(self, *cmd):
"""
Return the first path to the cmd in the container.
If cmd argument is a list then it will try all commands.
"""
for path in (await self.env('PATH')).split(':'):
for c in cmd:
p = os.path.join(self.root, path[1:], c)
if os.path.exists(p):
return p[len(str(self.root)):]
async def copy(self, *args):
args = ['cp', '-ra'] + list(args)
return await self.exec(*args)

View File

@ -86,12 +86,16 @@ class Buildah(Localhost):
if os.path.exists(p): if os.path.exists(p):
return p[len(str(self.mnt)):] return p[len(str(self.mnt)):]
@property def is_wrapper(self):
def _compatible(self): return (
return Proc.test or os.getuid() == 0 or getattr(self.parent, 'parent', None) Proc.test
or os.getuid() == 0
or getattr(self.parent, 'parent', None)
)
async def call(self, *args, **kwargs): async def call(self, *args, **kwargs):
if self._compatible: if not self.is_wrapper():
self.ctr = (await self.exec('buildah', 'from', self.base, buildah=False)).out self.ctr = (await self.exec('buildah', 'from', self.base, buildah=False)).out
self.mnt = Path((await self.exec('buildah', 'mount', self.ctr, buildah=False)).out) self.mnt = Path((await self.exec('buildah', 'mount', self.ctr, buildah=False)).out)
result = await super().call(*args, **kwargs) result = await super().call(*args, **kwargs)
@ -103,13 +107,13 @@ class Buildah(Localhost):
argv = [ argv = [
'buildah', 'unshare', 'buildah', 'unshare',
sys.argv[0], # current script location sys.argv[0], # current script location
cli.shlaxfile.path, # current shlaxfile location
] ]
if debug is True: if debug is True:
argv.append('-d') argv.append('-d')
elif isinstance(debug, str): elif isinstance(debug, str) and debug:
argv.append('-d=' + debug) argv.append('-d=' + debug)
argv += [ argv += [
cli.shlaxfile.path,
cli.parser.command.name, # script name ? cli.parser.command.name, # script name ?
] ]
self.output(' '.join(argv), 'EXECUTION', flush=True) self.output(' '.join(argv), 'EXECUTION', flush=True)
@ -158,7 +162,7 @@ class Buildah(Localhost):
await self.exec('podman', 'push', f'{self.image.repository}:{tag}', buildah=False) await self.exec('podman', 'push', f'{self.image.repository}:{tag}', buildah=False)
async def clean(self, *args, **kwargs): async def clean(self, *args, **kwargs):
if not self._compatible: if self.is_wrapper():
return return
for src, dst in self.mounts.items(): for src, dst in self.mounts.items():

View File

@ -7,3 +7,54 @@ from ..strategies.script import Script
class Localhost(Script): class Localhost(Script):
root = '/' root = '/'
def shargs(self, *args, **kwargs):
user = kwargs.pop('user', None)
args = [str(arg) for arg in args if args is not None]
if args and ' ' in args[0]:
if len(args) == 1:
args = ['sh', '-euc', args[0]]
else:
args = ['sh', '-euc'] + list(args)
if user == 'root':
args = ['sudo'] + args
elif user:
args = ['sudo', '-u', user] + args
if self.parent:
return self.parent.shargs(*args, **kwargs)
else:
return args, kwargs
async def exec(self, *args, **kwargs):
kwargs.setdefault('debug', self.call_kwargs.get('debug', False))
args, kwargs = self.shargs(*args, **kwargs)
proc = await Proc(*args, **kwargs)()
if kwargs.get('wait', True):
await proc.wait()
return proc
async def rexec(self, *args, **kwargs):
kwargs['user'] = 'root'
return await self.exec(*args, **kwargs)
async def env(self, name):
return (await self.exec('echo $' + name)).out
async def which(self, *cmd):
"""
Return the first path to the cmd in the container.
If cmd argument is a list then it will try all commands.
"""
for path in (await self.env('PATH')).split(':'):
for c in cmd:
p = os.path.join(self.root, path[1:], c)
if os.path.exists(p):
return p[len(str(self.root)):]
async def copy(self, *args):
args = ['cp', '-ra'] + list(args)
return await self.exec(*args)