def StartSwarm(): if SwarmIsInitiated(): print("Swarm is already initiated.") return print("Starting swarm") dockerCommand = "docker swarm init" TerminalTools.ExecuteTerminalCommands([dockerCommand])
def CreateSwarmVolume(volumeName, driver='local', driverOptions=[]): print("Creating volume: {0}, with driver: {1} and driver options: {2}". format(volumeName, driver, driverOptions)) dockerCommand = "docker volume create --driver {0}".format(driver) for driverOption in driverOptions: dockerCommand += " --opt {0}".format(driverOption) dockerCommand += ' {0}'.format(volumeName) TerminalTools.ExecuteTerminalCommands([dockerCommand])
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)
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 DeployStack(composeFile, stackName, environmentVariablesFiles=[], withRegistryAuth=False): for environmentVariablesFile in environmentVariablesFiles: TerminalTools.LoadEnvironmentVariables(environmentVariablesFile) print("Deploying stack: " + stackName) dockerCommand = "docker stack deploy -c " + composeFile if withRegistryAuth: dockerCommand += " --with-registry-auth" dockerCommand += " " + stackName TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def SignNugetPackage(nugetPackage, certificateFilePath, certificatePassword, timestamperUrl, packageOutputSignedFolder, allowMissingCertificateFilePath): verbosity = "quiet" retries = 0 limit = 3 exception = Exception("...") print("") while retries != limit: try: if os.path.exists(certificateFilePath): terminalCommand = 'nuget sign %s -CertificatePath %s -CertificatePassword %s -Timestamper %s -OutputDirectory %s -Verbosity %s' % ( nugetPackage, certificateFilePath, certificatePassword, timestamperUrl, packageOutputSignedFolder, verbosity) # print("Try Sign terminalCommand:{}".format(terminalCommand)) TerminalTools.ExecuteTerminalCommands([terminalCommand], True) print("Signed Package({}) Completed".format(nugetPackage)) return 1 else: if allowMissingCertificateFilePath == False: retries = limit raise Exception( "certificateFilePath not found, not able to sign. Set -allowMissingCertificateFilePath true to allow it." ) print( "Missing certificateFilePath ({}). Copy Package({}) to Output folder({}) instead." .format(certificateFilePath, nugetPackage, packageOutputSignedFolder)) if (os.path.exists(packageOutputSignedFolder)): shutil.copy(nugetPackage, packageOutputSignedFolder) else: os.mkdir(packageOutputSignedFolder, 0o755) shutil.copy(nugetPackage, packageOutputSignedFolder) touch( os.path.join(packageOutputSignedFolder, "_notsigned-missing-certificateFilePath.txt")) return 0 return 1 except Exception as err: exception = err if retries != limit: print(">>> Retry Sign Package({}) {} of {}".format( nugetPackage, 1 + retries, limit)) verbosity = "normal" retries = retries + 1 raise exception
def CreateSwarmNetwork(networkName, encrypted=False, driver='overlay', attachable=True, options=[]): print("Creating network: " + networkName) dockerCommand = "docker network create " dockerCommand += "--driver {0} ".format(driver) if attachable: dockerCommand += "--attachable " if encrypted: dockerCommand += "--opt encrypted " for option in options: dockerCommand += "{0} ".format(option) dockerCommand += networkName TerminalTools.ExecuteTerminalCommands([dockerCommand])
def VerifySignedNugetPackage(nugetPackage): retries = 0 limit = 3 verbosity = "quiet" exception = Exception("...") while retries != limit: try: terminalCommand = 'nuget verify -Signatures %s -Verbosity %s' % ( nugetPackage, verbosity) TerminalTools.ExecuteTerminalCommands([terminalCommand], True) print( "--> Verify Signed Package({}) Succeeded".format(nugetPackage)) return except Exception as err: exception = err if retries != limit: print(">>> Retry Verify Signed Package({}) {} of {}".format( nugetPackage, 1 + retries, limit)) verbosity = "normal" retries = retries + 1 raise exception
def PushImage(imageName): dockerCommand = "docker push " + imageName TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def HandleTerminalCommandsSelection(selection): if COMMAND_KEY in selection: terminalCommands = selection[COMMAND_KEY] TerminalTools.ExecuteTerminalCommands(terminalCommands, True)
def test_ExecuteTerminalCommands(self): pyFile = os.path.join(TestTools.TEST_SAMPLE_FOLDER, 'pythonSnippet.py') cmd = 'python ./{0}'.format(pyFile) TerminalTools.ExecuteTerminalCommands([cmd], True)
def RunImage(imageName, properties=""): dockerCommand = "docker run " + properties + " " + imageName TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def VerifySignedNugetPackage(nugetPackage): terminalCommand = 'nuget verify -Signatures %s' % (nugetPackage) TerminalTools.ExecuteTerminalCommands([terminalCommand], True)
def RemoveSwarmNetwork(networkName): print("Removing network: " + networkName) dockerCommand = "docker network rm " + networkName TerminalTools.ExecuteTerminalCommands([dockerCommand])
def BuildImage(imageName, dockerfile='Dockerfile', context='.'): dockerCommand = "docker build -f " + dockerfile + " -t " + imageName + " " + context TerminalTools.ExecuteTerminalCommands([dockerCommand], True)
def RemoveSwarmVolume(volumeName): print("Removing volume: " + volumeName) dockerCommand = "docker volume rm " + volumeName TerminalTools.ExecuteTerminalCommands([dockerCommand])
def RemoveSwarmConfig(configName): print("Removing config: " + configName) dockerCommand = "docker config rm " + configName TerminalTools.ExecuteTerminalCommands([dockerCommand])
def CreateSwarmConfig(configFile, configName): print("Creating config: " + configName) dockerCommand = "docker config create " + configName + " " + configFile TerminalTools.ExecuteTerminalCommands([dockerCommand])
def RemoveSwarmSecret(secretName): print("Removing secret: " + secretName) dockerCommand = "docker secret rm " + secretName TerminalTools.ExecuteTerminalCommands([dockerCommand])
def CreateSwarmSecret(secretFile, secretName): print("Creating secret: " + secretName) dockerCommand = "docker secret create " + secretName + " " + secretFile TerminalTools.ExecuteTerminalCommands([dockerCommand])
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 RemoveStack(stackName): print("Removing stack: " + stackName) dockerCommand = "docker stack rm " + stackName TerminalTools.ExecuteTerminalCommands([dockerCommand])
def CopyFromContainerToHost(containerName, containerSrc, hostDest): terminalCommand = "docker cp " + \ containerName + ":" + containerSrc + " " + hostDest TerminalTools.ExecuteTerminalCommands([terminalCommand])
def SignNugetPackage(nugetPackage, certificateFilePath, certificatePassword, timestamperUrl, outputDirectory): terminalCommand = 'nuget sign %s -CertificatePath %s -CertificatePassword %s -Timestamper %s -OutputDirectory %s' % ( nugetPackage, certificateFilePath, certificatePassword, timestamperUrl, outputDirectory) TerminalTools.ExecuteTerminalCommands([terminalCommand], True)