@@ -64,26 +64,48 @@ def GetCompositionCallbacksFromContext(cls, context):
64
64
def GetUUID (cls ):
65
65
return GetDockerSafeUUID ()
66
66
67
- def __init__ (self , context , composeFilesYaml , projectName = None ):
67
+ def __init__ (self , context , composeFilesYaml , projectName = None ,
68
+ force_recreate = True , components = []):
68
69
self .contextHelper = ContextHelper .GetHelper (context = context )
69
70
if not projectName :
70
71
projectName = self .contextHelper .getGuuid ()
71
72
self .projectName = projectName
72
73
self .context = context
73
74
self .containerDataList = []
74
75
self .composeFilesYaml = composeFilesYaml
75
- self .serviceNames = []
76
- self .serviceNames = self ._collectServiceNames ()
76
+
77
77
[callback .composing (self , context ) for callback in Composition .GetCompositionCallbacksFromContext (context )]
78
- self .issueCommand ([ "up" , "--force-recreate" , "-d" ] )
78
+ self .up ( context , force_recreate , components )
79
79
80
80
def _collectServiceNames (self ):
81
81
'First collect the services names.'
82
82
servicesList = [service for service in self .issueCommand (["config" , "--services" ]).splitlines () if "WARNING" not in service ]
83
83
return servicesList
84
84
85
+ def up (self , context , force_recreate = True , components = []):
86
+ self .serviceNames = self ._collectServiceNames ()
87
+ command = ["up" , "-d" ]
88
+ if force_recreate :
89
+ command += ["--force-recreate" ]
90
+ self .issueCommand (command + components )
91
+
92
+ def scale (self , context , serviceName , count = 1 ):
93
+ self .serviceNames = self ._collectServiceNames ()
94
+ command = ["scale" , "%s=%d" % (serviceName , count )]
95
+ self .issueCommand (command )
96
+
97
+ def stop (self , context , components = []):
98
+ self .serviceNames = self ._collectServiceNames ()
99
+ command = ["stop" ]
100
+ self .issueCommand (command , components )
101
+
102
+ def start (self , context , components = []):
103
+ self .serviceNames = self ._collectServiceNames ()
104
+ command = ["start" ]
105
+ self .issueCommand (command , components )
106
+
85
107
def getServiceNames (self ):
86
- return list (self .serviceNames )
108
+ return list (self .serviceNames )
87
109
88
110
def parseComposeFilesArg (self , composeFileArgs ):
89
111
args = [arg for sublist in [["-f" , file ] for file in [file if not os .path .isdir (file ) else os .path .join (file , 'docker-compose.yml' ) for file in composeFileArgs .split ()]] for arg in sublist ]
@@ -120,14 +142,31 @@ def refreshContainerIDs(self):
120
142
def _callCLI (self , argList , expect_success , env ):
121
143
return bdd_test_util .cli_call (argList , expect_success = expect_success , env = env )
122
144
123
- def issueCommand (self , args ):
124
- cmdArgs = self .getFileArgs ()+ args
125
- # output, error, returncode = \
126
- # bdd_test_util.cli_call(["docker-compose"] + cmdArgs, expect_success=True, env=self.getEnv())
145
+ def issueCommand (self , command , components = []):
146
+ componentList = []
147
+ useCompose = True
148
+ for component in components :
149
+ if '_' in component :
150
+ useCompose = False
151
+ componentList .append ("%s_%s" % (self .projectName , component ))
152
+ else :
153
+ break
154
+
155
+ # If we need to perform an operation on a specific container, use
156
+ # docker not docker-compose
157
+ if useCompose :
158
+ cmdArgs = self .getFileArgs ()+ command + components
159
+ cmd = ["docker-compose" ] + cmdArgs
160
+ else :
161
+ cmdArgs = command + componentList
162
+ cmd = ["docker" ] + cmdArgs
163
+
164
+ #print("cmd:", cmd)
127
165
output , error , returncode = \
128
- self ._callCLI (["docker-compose" ] + cmdArgs , expect_success = True , env = self .getEnv ())
166
+ self ._callCLI (cmd , expect_success = True , env = self .getEnv ())
167
+
129
168
# Don't rebuild if ps command
130
- if args [0 ] != "ps" and args [0 ] != "config" :
169
+ if command [0 ] != "ps" and command [0 ] != "config" :
131
170
self .rebuildContainerData ()
132
171
return output
133
172
@@ -162,6 +201,7 @@ def rebuildContainerData(self):
162
201
163
202
def decompose (self ):
164
203
self .issueCommand (["unpause" ])
204
+ self .issueCommand (["down" ])
165
205
self .issueCommand (["kill" ])
166
206
self .issueCommand (["rm" , "-f" ])
167
207
@@ -181,4 +221,3 @@ def decompose(self):
181
221
182
222
# Invoke callbacks
183
223
[callback .decomposing (self , self .context ) for callback in Composition .GetCompositionCallbacksFromContext (self .context )]
184
-
0 commit comments