Skip to content

Commit

Permalink
FAB-1438: Add up, down, scale to compose util
Browse files Browse the repository at this point in the history
This updates the compose util to allow one to
start, up, stop, and scale a container based on a docker
service. It also allows for manipulating a specific
container.

Change-Id: I424c64d0ea65d9a00344163cd839404117e0b807
Signed-off-by: Latitia M Haskins <latitia.haskins@gmail.com>
  • Loading branch information
lhaskins committed Feb 22, 2017
1 parent 008ee85 commit 81cd41b
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions bddtests/steps/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,48 @@ def GetCompositionCallbacksFromContext(cls, context):
def GetUUID(cls):
return GetDockerSafeUUID()

def __init__(self, context, composeFilesYaml, projectName = None):
def __init__(self, context, composeFilesYaml, projectName = None,
force_recreate = True, components = []):
self.contextHelper = ContextHelper.GetHelper(context=context)
if not projectName:
projectName = self.contextHelper.getGuuid()
self.projectName = projectName
self.context = context
self.containerDataList = []
self.composeFilesYaml = composeFilesYaml
self.serviceNames = []
self.serviceNames = self._collectServiceNames()

[callback.composing(self, context) for callback in Composition.GetCompositionCallbacksFromContext(context)]
self.issueCommand(["up", "--force-recreate", "-d"])
self.up(context, force_recreate, components)

def _collectServiceNames(self):
'First collect the services names.'
servicesList = [service for service in self.issueCommand(["config", "--services"]).splitlines() if "WARNING" not in service]
return servicesList

def up(self, context, force_recreate=True, components=[]):
self.serviceNames = self._collectServiceNames()
command = ["up", "-d"]
if force_recreate:
command += ["--force-recreate"]
self.issueCommand(command + components)

def scale(self, context, serviceName, count=1):
self.serviceNames = self._collectServiceNames()
command = ["scale", "%s=%d" %(serviceName, count)]
self.issueCommand(command)

def stop(self, context, components=[]):
self.serviceNames = self._collectServiceNames()
command = ["stop"]
self.issueCommand(command, components)

def start(self, context, components=[]):
self.serviceNames = self._collectServiceNames()
command = ["start"]
self.issueCommand(command, components)

def getServiceNames(self):
return list(self.serviceNames)
return list(self.serviceNames)

def parseComposeFilesArg(self, composeFileArgs):
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]
Expand Down Expand Up @@ -120,14 +142,31 @@ def refreshContainerIDs(self):
def _callCLI(self, argList, expect_success, env):
return bdd_test_util.cli_call(argList, expect_success=expect_success, env=env)

def issueCommand(self, args):
cmdArgs = self.getFileArgs()+ args
# output, error, returncode = \
# bdd_test_util.cli_call(["docker-compose"] + cmdArgs, expect_success=True, env=self.getEnv())
def issueCommand(self, command, components=[]):
componentList = []
useCompose = True
for component in components:
if '_' in component:
useCompose = False
componentList.append("%s_%s" % (self.projectName, component))
else:
break

# If we need to perform an operation on a specific container, use
# docker not docker-compose
if useCompose:
cmdArgs = self.getFileArgs()+ command + components
cmd = ["docker-compose"] + cmdArgs
else:
cmdArgs = command + componentList
cmd = ["docker"] + cmdArgs

#print("cmd:", cmd)
output, error, returncode = \
self._callCLI(["docker-compose"] + cmdArgs, expect_success=True, env=self.getEnv())
self._callCLI(cmd, expect_success=True, env=self.getEnv())

# Don't rebuild if ps command
if args[0] !="ps" and args[0] !="config":
if command[0] !="ps" and command[0] !="config":
self.rebuildContainerData()
return output

Expand Down Expand Up @@ -162,6 +201,7 @@ def rebuildContainerData(self):

def decompose(self):
self.issueCommand(["unpause"])
self.issueCommand(["down"])
self.issueCommand(["kill"])
self.issueCommand(["rm", "-f"])

Expand All @@ -181,4 +221,3 @@ def decompose(self):

# Invoke callbacks
[callback.decomposing(self, self.context) for callback in Composition.GetCompositionCallbacksFromContext(self.context)]

0 comments on commit 81cd41b

Please sign in to comment.