def testGetTemplate(self, run_mock): configService = Config("/fake/path") template = configService.getTemplate("mysql.yml") targetTemplate = "my mysql fake sample" self.assertEqual(template, targetTemplate) configService = Config() template = configService.getTemplate("mysql.yml") self.assertEqual(template, targetTemplate)
def searchDump(self, backupPath, listServices): """ This class search service where to perform a dump before to backup them and grab all setting to perform backup :param backupPath: The path where to store the dump :param listServices: The list of all services provider by Rancher API :type backupPath: str :type listServices: list :return dict The list of service where to perform dump and docker command line associated to them. """ if backupPath is None or backupPath == "": raise KeyError("backupPath must be provided") if isinstance(listServices, list) is False: raise KeyError("listServices must be a list") logger.debug("backupPath: %s", backupPath) logger.debug("listServices: %s", listServices) configService = Config() index = configService.getIndex() listDump = [] for service in listServices: for name, setting in index.iteritems(): if re.search(setting['regex'], service['launchConfig']['imageUuid']): logger.info("Found '%s/%s' to do dumping" % (service['stack']['name'], service['name'])) template = configService.getTemplate(setting['template']) env = Environment() template = env.from_string(template) context = {} # Get environment variables if 'environment' in service['launchConfig']: context["env"] = service['launchConfig']['environment'] # Get IP for instance in service['instances']: if instance['state'] == "running": context["ip"] = instance['primaryIpAddress'] logger.debug("Found IP %s", context["ip"]) break # Get Taget backup context["target_dir"] = backupPath + "/" + service[ 'stack']['name'] + "/" + service['name'] setting = yaml.load(template.render(context)) setting["service"] = service setting["target_dir"] = context["target_dir"] if "environments" not in setting: setting["environments"] = [] if "image" not in setting: setting["image"] = service['launchConfig']['imageUuid'] listDump.append(setting) break logger.debug(listDump) return listDump