Exemple #1
0
def deploy_vnetwork(url, username, password):
    tree = ET.parse("XML/network.xml")
    root = tree.getroot()
    for child in root:
        if child.tag == str("name"):
            child.text = input(
                "Enter the name of the network to be deployed: ")
            tree.write("XML/network.xml")
        if child.tag == str("bridge"):
            child.text = input(
                "Enter the name of the bridge to be associated with the network.\n"
                "This should be the bridge that was previously created: ")
            tree.write("XML/network.xml")
            contents = open("XML/network.xml").read()
            print(contents)
    uri, header, post_data = nfvis_urns.post("networks", url, format="xml")
    code, response = nfvis_calls.post(username,
                                      password,
                                      uri,
                                      header,
                                      xml_data=contents)
    print("\n%s \nAPI Status Code: %i\n" % (uri, code))
    if code != 201:
        cprint("Network deployment failed\n", "red")
    else:
        cprint("Network deployment successful\n", "green")
Exemple #2
0
def scp_file(nfvis, username, password, s_file, d_file):
    try:
        conn = netmiko.ConnectHandler(host=nfvis,
                                      port=22222,
                                      device_type="linux",
                                      username=username,
                                      password=password,
                                      timeout=10)
        scp_conn = netmiko.SCPConn(conn)
        print("#" * 20)
        cprint("Beginning SCP file transfer...\nPlease wait...", "green")
        scp_conn.scp_transfer_file(s_file, d_file)
        print("#" * 20)
        cprint("SCP file transfer complete.\n", "green")
        conn.disconnect()
        image = s_file.split("/")
        name = image[-1]
        payload = "<image><name>" + name + "</name>" \
                  "<src>" + d_file + "</src></image>"
        print(payload)
        url = "https://" + nfvis
        uri, header, post_data = nfvis_urns.post("images", url, format="xml")
        code, response = nfvis_calls.post(username,
                                          password,
                                          uri,
                                          header,
                                          xml_data=payload)
        print("\n%s \nAPI Status Code: %i\n" % (uri, code))

        if code != 201:
            cprint("Bridge deployment failed\n", "red")
        else:
            cprint("Bridge deployment successful\n", "green")

    except netmiko.NetMikoTimeoutException:
        cprint(
            "\nFailed: SSH session timed out. Check network connectivity and try again.\n",
            "red")
    except netmiko.NetMikoAuthenticationException:
        cprint("\nFailed: Authentication failed.\n", "red")
    except FileNotFoundError:
        cprint("Invalid source file name entered. Please try again.", "red")
    except scp.SCPException:
        cprint("Invalid destination file name entered. Please try again.",
               "red")
Exemple #3
0
def deploy_bridge(url, username, password):
    tree = ET.parse("XML/bridge.xml")
    root = tree.getroot()
    for child in root:
        child.text = input("Enter the name of the bridge to be deployed: ")
    tree.write("XML/bridge.xml")
    contents = open("XML/bridge.xml").read()
    print(contents)
    uri, header, post_data = nfvis_urns.post("bridges", url, format="xml")
    code, response = nfvis_calls.post(username,
                                      password,
                                      uri,
                                      header,
                                      xml_data=contents)
    print("\n%s \nAPI Status Code: %i\n" % (uri, code))

    if code != 201:
        cprint("Bridge deployment failed\n", "red")
    else:
        cprint("Bridge deployment successful\n", "green")
Exemple #4
0
def cli(args):
    if len(sys.argv) == 7:
        method, name_ip, username, password, s_file, d_file = (
            sys.argv[1],
            sys.argv[2],
            sys.argv[3],
            sys.argv[4],
            sys.argv[5],
            sys.argv[6],
        )
    elif len(sys.argv) == 5:
        method, key, name_ip, setting = (
            sys.argv[1],
            sys.argv[2],
            sys.argv[3],
            sys.argv[4],
        )
    elif len(sys.argv) == 2:
        method = sys.argv[1]
        if method is "h":
            print(
                "Non-Interactive mode supports the following arguments:\n"
                "\nArgument 1:\n"
                "g = get\n"
                "p = post\n"
                "d = delete\n"
                "\nArgument 2:\n"
                "\nGet Method:\n"
                "platform-detail, bridges, networks, deployments, flavors, images\n"
                "\nPost Method:\n"
                "bridges, networks, deployments\n"
                "\nDelete Method:\n"
                "bridges, networks, deployments\n"
                "\nArgument 3:\n"
                "Enter the IP address of the NFVIS system. Alternatively, substitute the \n"
                "key word 'bulk' instead of the IP address to run API calls on all devices\n"
                "in the creds.json file.\n"
                "\nArgument 4:\n"
                "When using the post method a .xml template is required for configuration\n"
                "of the bridge, network, or VNF. Examples of the templates can be found\n"
                "in the XML folder.\n"
                "\nWhen using the delete method argument 4 is the name of the bridge,"
                "\nnetwork, or VNF that is to be deleted."
                "\nTo retrieve information about the system use the get method.\n"
                "To delete an existing bridge, network, or VNF use the delete method.\n"
                "To deploy a new bridge, network, or VNF use the post method.\n"
                "\nWhen using method g, p, or d this program will check the given IP address\n"
                "against the creds.json file. If the IP address is present, then the credentials\n"
                "in the creds.json file will be used. If the key work 'bulk' is used instead of\n"
                "an IP address then the API call will be run on all devices in the creds.json file.\n"
                "\nExamples:\n"
                "Get Method - CNAT.py g networks 10.10.10.10\n"
                "Post Method - CNAT.py P deployments 10.10.10.10 ASAv_ENCS.xml\n"
                "Delete Method - CNAT.py d deployments 10.10.10.10 ASAv\n"
                "\nTo upload an image to NFVIS the 's' method can be used. This requires 6 arguments.\n"
                "Method, IP Address, username, password, source file, and destination file\n"
                "")
            sys.exit()
        else:
            cprint("Invalid option entered. Use sys.argv 'h' for help.", "red")
            sys.exit()
    else:
        method, key, name_ip = (sys.argv[1], sys.argv[2], sys.argv[3])
    if "creds.json" not in listdir():
        username = input("Username: "******"creds.json", "w") as f:
            json.dump(creds, f)
    with open("creds.json", "r") as f:
        creds = json.load(f)

    if name_ip not in creds.keys():
        if name_ip != "bulk":
            username = input("Username: "******"creds.json", "w") as f:
                json.dump(creds, f)
            ip_list = [name_ip]
        else:
            ip_list = list(creds.keys())
    else:
        ip_list = [name_ip]

    if method is "g":
        for i in ip_list:
            url = "https://%s" % i
            username, password = (list(creds[i].keys())[0],
                                  list(creds[i].values())[0])
            uri, header = nfvis_urns.get(key, url)
            code, response_json = nfvis_calls.get(username, password, uri,
                                                  header)
            cprint(
                "API Response Code: %i\n\nRequest URI: %s\n\nJSON Reponse:\n\n%s\n\n"
                % (code, uri, response_json), "green")
            response_parser(response_json)
    if method is "p":
        for i in ip_list:
            url = "https://%s" % i
            username, password = (list(creds[i].keys())[0],
                                  list(creds[i].values())[0])
            uri, header, post_data = nfvis_urns.post(key, url, format="xml")
            with open(setting) as f:
                contents = f.read()
            code, response = nfvis_calls.post(username,
                                              password,
                                              uri,
                                              header,
                                              xml_data=contents)
            cprint(
                "API Response Code: %i\n\nRequest URI: %s\n\nJSON Reponse:\n\n%s\n\n"
                % (code, uri, response), "green")
            if code == 201:
                cprint("Deployment successful", "green")
            else:
                cprint("Deployment failed", "red")
    if method is "d":
        for i in ip_list:
            url = "https://%s" % i
            username, password = (list(creds[i].keys())[0],
                                  list(creds[i].values())[0])
            uri, header, = nfvis_urns.delete(key,
                                             url,
                                             vnf=setting,
                                             bridge=setting,
                                             network=setting)
            code, response = nfvis_calls.delete(username, password, uri,
                                                header)
            cprint(
                "API Response Code: %i\n\nRequest URI: %s\n\nJSON Reponse:\n\n%s\n\n"
                % (code, uri, response), "green")
            if code == 204:
                cprint("Deletion successful", "green")
            else:
                cprint("Deletion failed"), "red"
    if method is "s":
        for i in ip_list:
            nfvis = key
            s_file = name_ip
            d_file = setting
            username, password = (list(creds[i].keys())[0],
                                  list(creds[i].values())[0])
            scp_file(nfvis, username, password, s_file, d_file)
Exemple #5
0
def deploy_vnf(url, username, password):
    tree = ET.parse("XML/vnf.xml")
    root = tree.getroot()
    for child in root.findall("./name"):
        child.text = input("Enter a name for the deployment: ")
        tree.write("XML/vnf.xml")

    for child in root.findall("./vm_group/name"):
        child.text = input("Enter a name for the VNF: ")
        tree.write("XML/vnf.xml")

    uri, header = nfvis_urns.get("images", url)
    code, response_json = nfvis_calls.get(username, password, uri, header)
    print("API Response Code: %i :\n%s" % (code, uri))
    if code == 401:
        cprint("\nAuthentication Failed to Device", "red")
    else:
        print()
    try:
        for i in response_json["vmlc:images"]["image"]:
            print(i["name"] + "\n")
    except Exception as e:
        if code == 204:
            print("There are no images on this device.\n")
            return
        else:
            print(repr(e))

    for child in root.iter("image"):
        child.text = input(
            "Enter the name of the image to be deployed.\nImage must exist on the system: "
        )
        tree.write("XML/vnf.xml")

    uri, header = nfvis_urns.get("flavors", url)
    code, response_json = nfvis_calls.get(username, password, uri, header)
    print("API Response Code: %i :\n%s" % (code, uri))
    if code == 401:
        cprint("\nAuthentication Failed to Device", "red")
    else:
        print()
    try:
        for i in response_json["vmlc:flavors"]["flavor"]:
            print(i["name"] + "\n")
    except Exception as e:
        if code == 204:
            print("There are no flavors on this device.\n")
            return
        else:
            print(repr(e))

    for child in root.iter("flavor"):
        child.text = input(
            "Enter the VNF flavor\nFlavor must exist on the system: ")
        tree.write("XML/vnf.xml")

    uri, header = nfvis_urns.get("networks", url)
    code, response_json = nfvis_calls.get(username, password, uri, header)
    print("API Response Code: %i :\n%s" % (code, uri))

    if code == 401:
        cprint("Authentication Failed to Device", "red")
        sys.exit()
    else:
        print("Currently Deployed Virtual Switches on NFVIS: \n")
    try:
        [
            print(i["name"] + "\n")
            for i in response_json["network:networks"]["network"]
        ]
    except Exception as e:
        print(repr(e))

    for child in root.findall(
            "./vm_group/interfaces/interface/network[@id='1']"):
        if child.tag == str("network"):
            child.text = input(
                "Enter the name of the network to connect to nicid 1: ")
            tree.write("XML/vnf.xml")
        else:
            continue

    for child in root.findall(
            "./vm_group/interfaces/interface/network[@id='2']"):
        if child.tag == str("network"):
            child.text = input(
                "Enter the name of the network to connect to nicid 2: ")
            tree.write("XML/vnf.xml")
        else:
            continue

    for child in root.findall(
            "./vm_group/interfaces/interface/network[@id='3']"):
        if child.tag == str("network"):
            child.text = input(
                "Enter the name of the network to connect to nicid 3: ")
            tree.write("XML/vnf.xml")
        else:
            continue

    for child in root.findall(
            "./vm_group/interfaces/interface/port_forwarding/port/"):
        if child.tag == str("vnf_port"):
            child.text = input(
                "Enter the VNF port to forward. Usually port 22: ")
            tree.write("XML/vnf.xml")

    for child in root.findall(
            "./vm_group/interfaces/interface/port_forwarding/port/external_port_range/"
    ):
        if child.tag == str("start"):
            child.text = input(
                "Enter the first port in the range for external port forwarding: "
            )
            tree.write("XML/vnf.xml")
        elif child.tag == str("end"):
            child.text = input(
                "Enter the last port in the range for external port forwarding: "
            )
            tree.write("XML/vnf.xml")
        else:
            continue

    contents = open("XML/vnf.xml").read()
    print(contents, "\n")

    uri, header, post_data = nfvis_urns.post("deployments", url, format="xml")
    code, response = nfvis_calls.post(username,
                                      password,
                                      uri,
                                      header,
                                      xml_data=contents)
    print("\n%s \nAPI Status Code: %i\n" % (uri, code))
    if code != 201:
        cprint("VNF deployment failed\n", "red")
    else:
        cprint("VNF deployment successful\n", "green")