def listClusterInfo(dClusterId=None): psResult = dockerDriver.ps() if dClusterId is None: clusterIds = [] for container in psResult: clusterName, _ = _parseContainerName(container.name) if clusterName is not None and clusterName not in clusterIds: clusterIds.append(clusterName) print "cluster Ids" print "\n".join(clusterIds) return for container in psResult: clusterName, _ = _parseContainerName(container.name) if clusterName == dClusterId: ip = dockerDriver.getContainerIpAddress(container.name) print ip
def create(clusterConfigFilePath, overrideClusterId): ''' return: { "dns": "172.17.0.2", "hosts": "172.17.0.2 master\n172.17.0.3 slave1\n172.17.04 slave2" } ''' dnsServerAddress = None hosts = "" with open(clusterConfigFilePath, "r") as conffile: conf = conffile.read() try: clusterConfig = json.loads(conf) except ValueError as e: print "Given cluster config json file " + clusterConfigFilePath + " is invalid " print e.message return 1 # docker build if Dockerfile is specified clusterConfig = _flattenDockerfile(clusterConfig) clusterConfig = _flattenHostname(clusterConfig) if overrideClusterId != None: clusterConfig["id"] = overrideClusterId # Append DNS dnsNode = { "hostname" : "dclouddns", "imageName" : REPO_DNS_BASE, "cmd" : "service sshd start && tail -f /var/log/yum.log" } clusterConfig["nodes"].insert(0, dnsNode) for i in range(len(clusterConfig["nodes"])): # The first iteration is for DNS node = clusterConfig["nodes"][i] container_name = _generateContainerName(clusterConfig["id"], node["hostname"]) cmd = ["docker", "run" , "-d" # daemon , "--privileged"] # DNS cmd.append("--dns") if i == 0: cmd.append("127.0.0.1") # localhost else: cmd.append(dnsServerAddress) if "dns" in clusterConfig: for dnsIp in clusterConfig["dns"]: cmd.append("--dns") cmd.append(dnsIp) if "domain" in clusterConfig: cmd.append("--dns-search") cmd.append(clusterConfig["domain"]) fqdn = node["hostname"] + "." + clusterConfig["domain"] cmd.append("--name") #cmd.append(container_name) cmd.append(fqdn) cmd.append("-h") cmd.append(fqdn) if "volumes" in node: for volumn in node["volumes"]: cmd.append("-v") cmd.append(volumn) cmd.append(node["imageName"]) cmd.append("bash") cmd.append("-c") cmd.append(node["cmd"]) print "executing: " + ' '.join(cmd) subprocess.call(cmd) ip = dockerDriver.getContainerIpAddress(container_name) if i == 0: dnsServerAddress = ip hosts += ip + " " + fqdn + " " + node["hostname"] + "\n" print "dnsServerAddress: " + dnsServerAddress if(not ssh.connection_check(dnsServerAddress, "root", "changeme")): print "**** ERROR ****" print "ssh connection to root@" + dnsServerAddress + " could not be established" return 1 ssh.exec_command2(dnsServerAddress, "root", "changeme", "echo '" + hosts + "' > /etc/dcloud/dnsmasq/hosts") ssh.exec_command2(dnsServerAddress, "root", "changeme", "service dnsmasq restart") print "hosts:" print hosts result = RunResult() result.dns = dnsServerAddress result.hosts = hosts return 0