def set_role(fuel_ip): fr = open(ROLE_CONF) for node in fr.readlines(): node_mac = ":".join(node.split(":")[0:6])[1:-1] node_roles = node.split(":")[-1][1:-2] if node_mac in nodes: cmd = "fuel --env %s node set --node %s --role %s" % ( env, node_mac, node_roles) print cmd print ssh(fuel_ip, cmd=cmd)
def remote_cmd(fuel_ip, node, cmd): def ssh_tunnel(jump_host, remote_host, bind_port): def outter_warpper(func): def wrapper(*args, **kwargs): with SSHTunnelForwarder( (jump_host, 22), ssh_username="******", ssh_password="******", remote_bind_address=(remote_host, 22), local_bind_address=("0.0.0.0", bind_port)) as tunnel: return func(*args, **kwargs) return wrapper return outter_warpper @ssh_tunnel(jump_host=fuel_ip, remote_host=node, bind_port=10022) def ssh(ip_addr="127.0.0.1", cmd=""): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) key_path = fuel_remote.get_ssh_key(fuel_ip) pkey = paramiko.RSAKey.from_private_key_file(key_path) ssh.connect(hostname=ip_addr, port=10022, username="******", pkey=pkey) stdin, stdout, stderr = ssh.exec_command(cmd) out = stdout.read() err = stderr.read() if err: print err return out res = ssh(cmd=cmd) # print res return res
def get_nodes(): cmd = "fuel nodes" res = fuel_remote.ssh(fuel_ip, cmd=cmd) for i in range(2, len(res)): nodes["node-" + res[i].split("|")[0].strip()] = Nodes( name="node-" + res[i].split("|")[0].strip(), roles=res[i].split("|")[6].rstrip().split(","))
def setup(): # ssh("10.121.137.12", # "fuel env -c --name mengph --rel 1 --mode ha --network-mode neutron --net-segment-type vlan --storage ceph") res = ssh(fuel_ip, cmd=cmd_debug) while len(nodes.keys()) < 5: for i in range(2, len(res)): nodes[res[i].split("|")[5].strip()] = res[i].split("|")[0].rstrip() # print nodes env = ssh( "10.121.137.12", "fuel env -c --name mengph --rel 1 --mode ha --network-mode neutron --net-segment-type vlan --storage ceph" ) if env: left = env[0].find("id") right = env[0].find(",") return env[0][left + 3:right]
def get_nodes(): '''thie function login the fuel host to get all host information of env, and return a dict. the key was the host name and the value was a node obj''' cmd = "fuel nodes" res = fuel_remote.ssh(fuel_ip, cmd=cmd) for i in range(2, len(res)): if res[i].split("|")[1].strip() == "ready": nodes["node-" + res[i].split("|")[0].strip()] = Nodes( name="node-" + res[i].split("|")[0].strip(), roles=res[i].split("|")[6].rstrip().split(","))
def get_nodes(): cmd = "fuel nodes" res = ssh(fuel_ip, cmd=cmd) for i in range(2, len(res)): nodes["node-" + res[i].split("|")[0].strip()] = res[i].split( "|")[6].rstrip().split(",")
fun="upload") def conf_env(): O_CONF = "/home/mengph/env_12/home/mengph/network_2.yaml" N_CONF = "/home/mengph/env_12/N_network.yaml" OUT_PUT = "/home/mengph/env_12/" + "/network_R.yaml" sync_network(O_CONF, N_CONF, OUT_PUT) os.popen( "cp /home/mengph/env_12/home/mengph/settings_2.yaml /home/mengph/env_12/settings_R.yaml" ) if __name__ == '__main__': env = setup() scp(fuel_ip, dst_path="/root/", src_path="home/mengph/rearch/fuel_config.py", fun="upload") ssh(fuel_ip, "python /root/fuel_config.py download_settings " + str(env)) copy_new_conf_to_loacl(fuel_ip, nodes, mode="settings") conf_env() copy_refresh_conf_to_fuel(fuel_ip, mode="settings") ssh(fuel_ip, "python /root/fuel_config.py upload_settings " + str(env)) set_role(fuel_ip) ssh(fuel_ip, "python /root/fuel_config.py download_nodes " + str(env)) copy_new_conf_to_loacl(fuel_ip, nodes, mode="nodes") conf_nodes() copy_refresh_conf_to_fuel(fuel_ip, mode="nodes") ssh(fuel_ip, "python /root/fuel_config.py upload_nodes " + str(env))