def discover_preinstalled_resources(spec, hosts, logger): """Poor man's discovery mechanism. Based on the list in preinstalled_resource_checks, we check for resources and adjust the spec (in-place) as needed. TODO: make this work multi-node. Need to thnk about how we invoke the install check function on each host. """ for host in hosts: if host["id"] != "master-host": continue # Only works for master node today! for entry in preinstalled_resource_checks: concrete_inst_l = \ query_install_spec(spec, inside=host["id"], name=entry["resource_key"]["name"], version=entry["resource_key"]["version"]) has_trigger = False for key in entry["trigger_resource_keys"]: t_l = query_install_spec(spec, inside=host["id"], name=key["name"], version=key["version"]) if len(t_l)>0: has_trigger = True break assert len(concrete_inst_l)<=1 has_concrete = (len(concrete_inst_l)==1) if (not has_concrete) and (not has_trigger): continue # done messing with this entry is_installed = entry["install_check_function"]() if is_installed: logger.debug("Discovered that %s %s is pre-installed in host %s" % (entry["resource_key"]["name"], entry["resource_key"]["version"], host["id"])) else: logger.debug("Discovered that %s %s is NOT pre-installed in host %s" % (entry["resource_key"]["name"], entry["resource_key"]["version"], host["id"])) continue # nothing to do if has_concrete: concrete_inst = concrete_inst_l[0] if not concrete_inst.has_key("properties"): concrete_inst["properties"] = {} concrete_inst["properties"]["installed"] = True else: concrete_inst = { "key": entry["resource_key"], "id": "_%s_%s" % (mangle_resource_key(entry["resource_key"]), host["id"]), "properties": {"installed":True}, "inside":{ "id": host["id"], "key":host["key"], "port_mapping":{"host":"host"} } } spec.append(concrete_inst)
def restore(self, backup_to_directory, package): p = self.ctx.props r = self.ctx.r script = os.path.join( os.path.join(backup_to_directory, fileutils.mangle_resource_key(self.metadata.key)), "mysql_dump_%s.sql" % p.config_port.NAME) r(check_file_exists, script) self.install(package) r(mysql_utils.run_mysql_script, "root", self._get_admin_password(), script, database=p.config_port.NAME) logger.debug("Successfully restored database %s" % p.config_port.NAME)
def backup(self, backup_to_directory, compress=True): p = self.ctx.props r = self.ctx.r mysql_backup_dir = \ os.path.join(backup_to_directory, fileutils.mangle_resource_key(self.metadata.key)) r(ensure_dir_exists, mysql_backup_dir) need_to_stop = self._ensure_mysql_running() r(mysql_utils.dump_database, p.config_port.NAME, os.path.join(mysql_backup_dir, "mysql_dump_%s.sql" % p.config_port.NAME), self._get_admin_password()) if need_to_stop: # leave the server in the same state we started it r(mysql_utils.run_mysqladmin, p.input_ports.mysql_admin, self._get_admin_password(), ["stop"])
def _get_metadata_filename(self): return os.path.join(self._get_metadata_filedir(), fileutils.mangle_resource_key(self.metadata.key) + ".json")