-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotateimage.py
More file actions
executable file
·103 lines (79 loc) · 2.98 KB
/
Copy pathrotateimage.py
File metadata and controls
executable file
·103 lines (79 loc) · 2.98 KB
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
#!/usr/bin/env python3
import subprocess as sp
import os
# IMAGE = sp.run(["podman", "images", "--name", "eauth"])
# ["podman", "ps", '--format"{{.IMAGE}}"']
# PRUNE = sp.run(["podman", "rm", f"{IMAGE}"])
# CONSTANTS
CONTAINERNAME = ["podman", "ps", '--format"{{.Names}}"']
IMAGENAME = "gcr.io/datadoghq/synthetics-private-location-worker:latest"
PODMANIMAGELISTER = ["podman", "image", "ls"]
IMAGEREMOVER = sp.run(["podman", "image", "rm", IMAGENAME])
PODMANCONTAINERLIST = sp.run(["podman", "ps"])
PULLCOMMAND = [
"podman",
"run",
"--rm",
"-v",
"$PWD/worker-config-e_auth-1dfda7758e6eb02738989ddc03348e62.json:/etc/datadog/synthetics-check-runner.json",
"gcr.io/datadoghq/synthetics-private-location-worker:latest",
]
# FUNCTIONS
def podmanPuller() -> None:
podmanPull = sp.Popen(
PULLCOMMAND, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, text=True
)
podmanPull.communicate()
if podmanPull.returncode not in (0, 125):
print(f"Error pulling new private location image: {podmanPull.returncode},")
print("image pull successful:")
sp.run(PODMANIMAGELISTER)
# TODO:test then convert to systemctl stop container
def containerStopper(containerName: str) -> None:
# containerstop = [("systemctl", "stop", {containerName})]
containerstop = ["podman", "stop", containerName]
runner = sp.Popen(
containerstop, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, text=True
)
if runner.returncode != 0:
print(f"Error stopping container: {containerName}")
print(f"Removed: {containerName}")
def imageRemover(imageName: str) -> None:
containerstop = ["podman", "rm", imageName]
runner = sp.Popen(
containerstop, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, text=True
)
if runner.returncode != 0:
print(f"Error removing image {imageName}")
print(f"removed {imageName}")
# STOP CONTAINERS WITH DATED IMAGE
for i in range(PODMANCONTAINERLIST):
if IMAGENAME in i:
containerStopper(i)
# REMOVE DATED IMAGES
# DIRECTORY MANAGEMENT
os.chdir("/etc/datadog/")
print("Now in", os.getcwd())
print("Pulling Image")
# NOTE: TO STREAM OUTPUT WHILE PULLING IMAGE
# def podmanRunner():
# podmanPull = sp.Popen(PULL_COMMAND, stdout=sp.PIPE, stderr=sp.STDOUT)
# for line in iter(podmanPull.stdout.readline, b""):
# print(line.decode(), end="")
# podmanPull.stdout.close()
# return_code = podmanPull.wait()
# if return_code != 0:
# print(
# f"Error pulling new private location image with return code: {return_code}"
# )
podmanPuller()
def podmanServiceCreate() -> None:
podmanPull = sp.Popen(PULLCOMMAND, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
podmanPull.communicate()
if podmanPull.returncode not in (0, 125):
print(f"Error pulling new private location image: {podmanPull.returncode},")
print("image pull successful:")
sp.run(PODMANIMAGELISTER)
# create service
# if __name__ == "__main__":
# podmanRunner()