This repository was archived by the owner on Sep 23, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDockerComposeTools.py
More file actions
199 lines (158 loc) · 7.94 KB
/
Copy pathDockerComposeTools.py
File metadata and controls
199 lines (158 loc) · 7.94 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import random
from DockerBuildSystem import TerminalTools, DockerImageTools, YamlTools
def MergeComposeFiles(composeFiles, outputComposeFile):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " config > " + outputComposeFile
TerminalTools.ExecuteTerminalCommands([terminalCommand], True)
def DockerComposeBuild(composeFiles):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " build"
TerminalTools.ExecuteTerminalCommands([terminalCommand], True)
def DockerComposeUp(composeFiles, abortOnContainerExit = True, detached = False):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " up"
if detached:
terminalCommand += " -d"
elif abortOnContainerExit:
terminalCommand += " --abort-on-container-exit"
TerminalTools.ExecuteTerminalCommands([terminalCommand], False)
def DockerComposeDown(composeFiles):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " down"
TerminalTools.ExecuteTerminalCommands([terminalCommand])
def DockerComposeRemove(composeFiles, force = True):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " rm"
if force:
terminalCommand += " -f"
TerminalTools.ExecuteTerminalCommands([terminalCommand])
def DockerComposePush(composeFiles):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " push"
TerminalTools.ExecuteTerminalCommands([terminalCommand], True)
def DockerComposePull(composeFiles, dryRun=False):
terminalCommand = "docker-compose"
terminalCommand += MergeComposeFileToTerminalCommand(composeFiles)
terminalCommand += " pull"
if(dryRun):
print("would have called {}".format(terminalCommand))
else:
TerminalTools.ExecuteTerminalCommands([terminalCommand], True)
def TagImages(composeFile, newTag, dryRun = False):
dockerComposeMap = YamlTools.GetYamlData([composeFile])
for service in dockerComposeMap['services']:
sourceImage = dockerComposeMap['services'][service]['image']
tagIndex = sourceImage.rfind(':')
targetImage = sourceImage[:tagIndex+1] + str(newTag)
if(not dryRun):
DockerImageTools.TagImage(sourceImage, targetImage)
else:
print('Would have tagged image {} as {}'.format(sourceImage, targetImage))
def SaveImages(composeFile, outputFolder):
dockerComposeMap = YamlTools.GetYamlData([composeFile])
if not(os.path.isdir(outputFolder)):
os.makedirs(outputFolder)
for service in dockerComposeMap['services']:
sourceImage = dockerComposeMap['services'][service]['image']
imageName = sourceImage[sourceImage.rfind('/')+1:].replace(':', '-') + '.tar'
outputPath = os.path.join(outputFolder, imageName)
DockerImageTools.SaveImage(sourceImage, outputPath)
def PublishDockerImages(composeFile, dryRun = False):
dockerComposeMap = YamlTools.GetYamlData([composeFile])
for service in dockerComposeMap['services']:
sourceImage = dockerComposeMap['services'][service]['image']
if(not dryRun):
DockerImageTools.PushImage(sourceImage)
else:
print('Would have pushed {}'.format(sourceImage))
def PromoteDockerImages(composeFile, targetTags, sourceFeed = None, targetFeed = None, user = None, password = None, logoutFromFeeds = False, dryRun = False):
userAndPasswordIsGiven = not(user is None or password is None)
if userAndPasswordIsGiven and not(sourceFeed is None):
DockerImageTools.DockerLogin(sourceFeed, user, password, dryRun)
DockerComposePull([composeFile], dryRun)
if userAndPasswordIsGiven and logoutFromFeeds and not(sourceFeed is None):
DockerImageTools.DockerLogout(sourceFeed, dryRun)
if userAndPasswordIsGiven and not(targetFeed is None):
DockerImageTools.DockerLogin(targetFeed, user, password, dryRun)
for tag in targetTags:
PublishDockerImagesWithNewTag(composeFile, tag, sourceFeed, targetFeed, dryRun)
if userAndPasswordIsGiven and logoutFromFeeds and not(targetFeed is None):
DockerImageTools.DockerLogout(targetFeed, dryRun)
def PublishDockerImagesWithNewTag(composeFile, newTag, sourceRepository = None, targetRepository = None, dryRun = False):
dockerComposeMap = YamlTools.GetYamlData([composeFile])
for service in dockerComposeMap['services']:
sourceImage = dockerComposeMap['services'][service]['image']
tagIndex = sourceImage.rfind(':')
targetImage = sourceImage[:tagIndex+1] + str(newTag)
if not(sourceRepository is None or targetRepository is None):
targetImage = targetImage.replace(sourceRepository, targetRepository, 1)
if dryRun:
print("Would have tagged image {} as {}".format(sourceImage, targetImage))
print("Would have pushed image {}".format(targetImage))
else:
DockerImageTools.TagImage(sourceImage, targetImage)
DockerImageTools.PushImage(targetImage)
def ExecuteComposeTests(composeFiles, testContainerNames = None, removeTestContainers = True, buildCompose = True, downCompose = True):
if testContainerNames is None:
TerminalTools.LoadDefaultEnvironmentVariablesFile()
yamlData = YamlTools.GetYamlData(composeFiles)
testContainerNames = GetContainerNames(yamlData)
if buildCompose:
DockerComposeBuild(composeFiles)
DockerComposeUp(composeFiles)
sumExitCodes, sumErrorMsgs = DockerImageTools.VerifyContainerExitCode(testContainerNames)
if downCompose:
DockerComposeDown(composeFiles)
if removeTestContainers:
DockerComposeRemove(composeFiles)
if sumExitCodes > 0:
raise Exception(sumErrorMsgs)
def CreateLocalNetwork(networkName):
print("Creating local network: " + networkName)
dockerCommand = "docker network create --attachable --driver=bridge "
dockerCommand += networkName
TerminalTools.ExecuteTerminalCommands([dockerCommand])
def MergeComposeFileToTerminalCommand(composeFiles):
terminalCommand = ""
for composeFile in composeFiles:
terminalCommand += " -f " + composeFile
return terminalCommand
def AddDigestsToImageTags(yamlData):
for service in yamlData.get('services', []):
if not('image' in yamlData['services'][service]):
continue
imageName = yamlData['services'][service]['image']
imageName = YamlTools.ReplaceEnvironmentVariablesMatches(imageName)
repoDigests = DockerImageTools.GetImageInfo(imageName)['RepoDigests']
if len(repoDigests) > 0:
yamlData['services'][service]['image'] = str(repoDigests[0])
else:
yamlData['services'][service]['image'] = imageName
def AddContainerNames(yamlData, prefix = None, subfix = None):
services = yamlData.get('services', [])
for service in services:
if not ('container_name' in yamlData['services'][service]):
containerName = service
if not(prefix is None):
containerName = prefix + containerName
if not(subfix is None):
containerName = containerName + subfix
else:
random.seed()
randomId = random.randint(0, 1000)
containerName = containerName + "_" + str(randomId)
yamlData['services'][service]['container_name'] = containerName
def GetContainerNames(yamlData):
services = yamlData.get('services', [])
containerNames = []
for service in services:
if 'container_name' in yamlData['services'][service]:
containerNames.append(yamlData['services'][service]['container_name'])
return containerNames