-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodmantest.py
116 lines (92 loc) · 4.47 KB
/
podmantest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from podman import PodmanClient
from subprocess import Popen
import subprocess
#------
import os
import tarfile
client = PodmanClient(base_url="unix:///run/podman/podman.sock")
c = client.containers.get("test")
help(client.containers)
c.stop()
c.wait()
arr = []
client.containers.run('cont2image',detach=True,auto_remove=True,name="myhaproxy")
client.containers.run()
client.containers.run("testcontainer",detach=True,)
client.containers.create("testcontainer",None,)
client.containers.create()
#if c.status == 'running':
# print(c.attrs['NetworkSettings']['Networks']['podman']['IPAddress'])
l = [c.name for c in client.containers.list()]
c = client.containers.get("testcontainer")
#if c.status == 'running':
# print(c.attrs['NetworkSettings']['Networks']['podman']['IPAddress'])
c.stop()
#--------------------------------------------- run HAproxy
client.containers.run('cont2image',detach=True,auto_remove=True,name="myhaproxy")
#
#for container in client.containers.list(all=True, filters={"name":"star_with_"}): instead of name ancestor for image
#--------------------------------------------- run webserver container
code_mount = Mount(
source=str(repo_root.absolute()),
target=target_dir,
type='bind',
)
client.containers.run('testcontainer',detach=True,auto_remove=True,name="mycontainer",mounts=[Mount(str('/objects'), str('srv/objects'), type="bind")])
#-------------------------------------------- list running containers IP
from podman import PodmanClient
client = PodmanClient(base_url="unix:///run/podman/podman.sock")
for c in client.containers.list():
c = client.containers.get(c.name)
if c.status == 'running':
print(c.attrs['NetworkSettings']['Networks']['podman']['IPAddress'])
#-------------------------------------------------------------- list running containers ip in an image
from podman import PodmanClient
client = PodmanClient(base_url="unix:///run/podman/podman.sock")
for c in client.containers.list(filters={'ancestor':''}):
c = client.containers.get(c.name)
print(c.image)
if c.image == "localhost/cont2image":
if c.status == 'running':
print(c.attrs['NetworkSettings']['Networks']['podman']['IPAddress'])
#-------------------------------------------------------- run webserver image with mounting
command='''podman run -v /srv/objects:/objects --rm -d --name mycontainer testcontainer'''
process=Popen(command,shell=True,stdout=subprocess.PIPE)
result=process.communicate()
print(result)
#-------------------------------------------------------- run webserver image with mounting API Final
client.containers.run('testcontainer',detach=True,auto_remove=True,name="mycontainer",volumes={'objecttt':{'bind':"/objects"}}) # objecttt is the host directory
#-------------------------------------------------------- list running containers ip in an image to update haproxy.conf Final
for c in client.containers.list(filters={'ancestor':'testcontainer'}): #all = true for both running and non running
x = client.containers.get(c.name)
print(c.image)
if x.status == 'running':
print(x.attrs['NetworkSettings']['Networks']['podman']['IPAddress'])
# IPcont = x.attrs['NetworkSettings']['Networks']['podman']['IPAddress']
#---------------------------------------------------------- stop container Final and take the ip to delete from haproxy.conf
for c in client.containers.list(filters={'ancestor':'testcontainer'}):
x = client.containers.get(c.name)
print (x)
if x.status == "running":
x.stop()
IPcont = x.attrs['NetworkSettings']['Networks']['podman']['IPAddress']
print("container", x , "has stopped" , 'with IP addres', IPcont)
#----------------------------------------------------------------------------------------- copy file first try
command='''podman cp foozi.txt myhaproxy:/etc/haproxy/foozi.txt'''
process=Popen(command,shell=True,stdout=subprocess.PIPE)
result=process.communicate()
print(result)
#------------------------------------------------------------------------ copy file 2nd try
def copy_to(src, dst):
name, dst = dst.split(':')
container = client.containers.get(name)
os.chdir(os.path.dirname(src))
srcname = os.path.basename(src)
tar = tarfile.open(src + '.tar', mode='w')
try:
tar.add(srcname)
finally:
tar.close()
data = open(src + '.tar', 'rb').read()
container.put_archive(os.path.dirname(dst), data)
# usage copy_to('/local/foo.txt', 'my-container:/tmp/foo.txt')