This repository was archived by the owner on Sep 23, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDockerImageTools.py
More file actions
140 lines (100 loc) · 4.75 KB
/
Copy pathDockerImageTools.py
File metadata and controls
140 lines (100 loc) · 4.75 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from DockerBuildSystem import TerminalTools
import re
import json
def BuildImage(imageName, dockerfile = 'Dockerfile', context = '.'):
dockerCommand = "docker build -f " + dockerfile + " -t " + imageName + " " + context
TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def RunImage(imageName, properties = ""):
dockerCommand = "docker run " + properties + " " + imageName
TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def PullImage(imageName):
dockerCommand = "docker pull " + imageName
TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def PushImage(imageName):
dockerCommand = "docker push " + imageName
TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def TagImage(sourceImage, targetImage):
dockerCommand = "docker tag " + sourceImage + " " + targetImage
TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def SaveImage(imageName, outputPath):
dockerCommand = "docker save -o " + outputPath + " " + imageName
TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def GetContainerExitCode(containerName):
terminalCommand = "docker inspect " + containerName + " --format='{{.State.ExitCode}}'"
output = TerminalTools.ExecuteTerminalCommandAndGetOutput(terminalCommand)
exitCode = TerminalTools.GetNumbersFromString(output)[0]
return int(exitCode)
def GetContainerRunningCode(containerName):
terminalCommand = "docker inspect " + \
containerName + " --format='{{.State.Running}}'"
output = TerminalTools.ExecuteTerminalCommandAndGetOutput(terminalCommand)
running = bool(re.search('true', str(output).lower()))
return running
def CopyFromContainerToHost(containerName, containerSrc, hostDest):
terminalCommand = "docker cp " + \
containerName + ":" + containerSrc + " " + hostDest
TerminalTools.ExecuteTerminalCommands([terminalCommand])
def GetImageRepoDigest(imageName):
terminalCommand = "docker inspect --format=\"{{index .RepoDigests 0}}\" " + imageName
repoDigest = str(TerminalTools.ExecuteTerminalCommandAndGetOutput(terminalCommand).decode("utf-8")).replace('\n', '')
return repoDigest
def GetImageLabels(imageName):
jsonInfo = GetImageInfo(imageName)
labels = jsonInfo['ContainerConfig']['Labels']
return labels
def GetImageLabel(imageName, labelKey):
labels = GetImageLabels(imageName)
if labelKey in labels:
return labels[labelKey]
return '<no value>'
def CheckImageLabelExists(imageName, labelKey):
labelValue = GetImageLabel(imageName, labelKey)
return not(labelValue == '<no value>')
def GetImageId(imageName):
terminalCommand = "docker inspect --format=\"{{.Id}}\" " + imageName
imageId = str(TerminalTools.ExecuteTerminalCommandAndGetOutput(terminalCommand).decode("utf-8")).replace('\n', '')
return imageId
def GetImageInfo(imageName):
terminalCommand = "docker inspect " + imageName
info = str(TerminalTools.ExecuteTerminalCommandAndGetOutput(terminalCommand).decode("utf-8"))
jsonInfo = json.loads(info)[0]
return jsonInfo
def GetContainerInfo(containerName):
return GetImageInfo(containerName)
def GetLogsFromContainer(containerName):
terminalCommand = 'docker logs {0}'.format(containerName)
logs = str(TerminalTools.ExecuteTerminalCommandAndGetOutput(terminalCommand, includeErrorOutput=True).decode("utf-8"))
return logs
def VerifyContainerExitCode(containerNames, assertExitCodes = False):
sumExitCodes = 0
sumErrorMsgs = ""
for containerName in containerNames:
exitCode = GetContainerExitCode(containerName)
sumExitCodes += exitCode
if exitCode > 0:
errorMsg = "Container '" + containerName + "' FAILED!\r\n"
sumErrorMsgs += errorMsg
print(errorMsg)
else:
print(containerName + " container finished with success.\r\n")
if sumExitCodes > 0 and assertExitCodes:
raise Exception(sumErrorMsgs)
return sumExitCodes, sumErrorMsgs
def DockerLogin(server, userName, password, dryRun=False):
if(dryRun):
print("Would have logged in to {0} with user {1}".format(server, userName))
else:
terminalCommand = 'docker login {0} -u {1} -p {2}'.format(server, userName, password)
TerminalTools.ExecuteTerminalCommands(
terminalCommands=[terminalCommand],
raiseExceptionWithErrorCode=True,
printCommand=False)
def DockerLogout(server, dryRun=False):
if(dryRun):
print("Would have logged out of {0}".format(server))
else:
terminalCommand = 'docker logout {0}'.format(server)
TerminalTools.ExecuteTerminalCommands(
terminalCommands=[terminalCommand],
raiseExceptionWithErrorCode=True,
printCommand=False)