Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_045_raises_no_ctnrs(self):
global ident, pod
with self.assertRaises(podman.NoContainersInPod):
pod.start()
with self.assertRaises(podman.NoContainersInPod):
pod.restart()
with self.assertRaises(podman.NoContainerRunning):
next(pod.stats())
with self.assertRaises(podman.ErrorOccurred):
pod.top()
def commit(self):
"""Create image from container."""
try:
try:
ctnr = self.client.containers.get(self._args.container[0])
except podman.ContainerNotFound as e:
sys.stdout.flush()
print(
'Container {} not found.'.format(e.name),
file=sys.stderr,
flush=True)
return 1
else:
ident = ctnr.commit(self.opts['image'][0], **self.opts)
print(ident)
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
return 0
def kill(self):
"""Signal provided pods."""
idents = None if self._args.all else self._args.pod
pods = query_pods(self.client.pods, idents)
for pod in pods:
try:
pod.kill(self._args.signal)
print(pod.id)
except podman.PodNotFound as ex:
print(
'Pod "{}" not found.'.format(ex.name),
file=sys.stderr,
flush=True)
except podman.ErrorOccurred as e:
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
return 0
def stop(self):
"""Stop pod and container(s)."""
idents = None if self._args.all else self._args.pod
pods = query_pods(self.client.pods, idents)
for pod in pods:
try:
pod.stop()
except podman.ErrorOccurred as ex:
print(
'{}'.format(ex.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
return 0
def restart(self):
"""Restart pod and container(s)."""
idents = None if self._args.all else self._args.pod
pods = query_pods(self.client.pods, idents)
for pod in pods:
try:
pod.restart()
print(pod.id)
except podman.PodNotFound as ex:
print(
'Pod "{}" not found.'.format(ex.name),
file=sys.stderr,
flush=True)
except podman.ErrorOccurred as ex:
print(
'{}'.format(ex.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
return 0
if self._args.type in ('all', 'container'):
obj = self._get_container(ident)
if obj is None and self._args.type in ('all', 'image'):
obj = self._get_image(ident)
if obj is None:
if self._args.type == 'container':
msg = 'Container "{}" not found'.format(ident)
elif self._args.type == 'image':
msg = 'Image "{}" not found'.format(ident)
else:
msg = 'Object "{}" not found'.format(ident)
print(msg, file=sys.stderr, flush=True)
else:
output.update(obj._asdict())
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
else:
print(json.dumps(output, indent=2))
def pull(self):
"""Retrieve image."""
for ident in self._args.targets:
try:
self.client.images.pull(ident)
print(ident)
except podman.ImageNotFound as e:
sys.stdout.flush()
print(
'Image {} not found.'.format(e.name),
file=sys.stderr,
flush=True)
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
def export(self):
"""Create tarball from container filesystem."""
try:
try:
ctnr = self.client.containers.get(self._args.container[0])
except podman.ContainerNotFound as e:
sys.stdout.flush()
print(
'Container {} not found.'.format(e.name),
file=sys.stderr,
flush=True)
return 1
else:
ctnr.export(self._args.output[0])
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
return 0
def info(self):
"""Report on Podman Service."""
try:
info = self.client.system.info()
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)
return 1
else:
if self._args.format == 'json':
print(json.dumps(info._asdict(), indent=2), flush=True)
else:
print(
yaml.dump(
dict(info._asdict()),
canonical=False,
default_flow_style=False),
flush=True)
def restart(self):
"""Restart container(s)."""
try:
for ident in self._args.targets:
try:
ctnr = self.client.containers.get(ident)
logging.debug('Restarting Container %s', ctnr.id)
ctnr.restart(timeout=self._args.timeout)
print(ident)
except podman.ContainerNotFound as e:
sys.stdout.flush()
print(
'Container {} not found.'.format(e.name),
file=sys.stderr,
flush=True)
except podman.ErrorOccurred as e:
sys.stdout.flush()
print(
'{}'.format(e.reason).capitalize(),
file=sys.stderr,
flush=True)