Esempio n. 1
0
def main():
    help_str = """
    This script is meant to be run manually on test servers, developer desktops, or Jenkins.
    This script supposed to run on python virtualenv from testrunner. Requires root privileges.
    Warning: it removes docker containers, VMs, images, and network configuration.
    """
    parser = ArgumentParser(help_str)

    # Common parameters
    parser.add_argument(
        "-v",
        "--vars",
        dest="yaml_path",
        default="vars.yaml",
        help=
        'path for platform yaml file. Default is vars.yaml. eg: -v myconfig.yaml'
    )
    parser.add_argument(
        "-p",
        "--platform",
        default="openstack",
        choices=["openstack", "vmware", "bare-metal", "libvirt"],
        help="The platform you're targeting. Default is openstack")
    parser.add_argument("-l",
                        "--log-level",
                        dest="log_level",
                        default=None,
                        help="log level",
                        choices=["DEBUG", "INFO", "WARNING", "ERROR"])

    # Sub commands
    commands = parser.add_subparsers(help="command", dest="command")

    cmd_info = commands.add_parser("info", help='ip info')
    cmd_info.set_defaults(func=info)

    cmd_log = commands.add_parser("get_logs", help="gather logs from nodes")
    cmd_log.set_defaults(func=get_logs)

    cmd_cleanup = commands.add_parser("cleanup",
                                      help="cleanup created skuba environment")
    cmd_cleanup.set_defaults(func=cleanup)

    cmd_provision = commands.add_parser(
        "provision",
        help="provision nodes for cluster in "
        "your configured platform e.g: openstack, vmware.")
    cmd_provision.set_defaults(func=provision)
    cmd_provision.add_argument(
        "-m",
        "--master-count",
        dest="master_count",
        type=int,
        default=-1,
        help='number of masters nodes to be deployed. eg: -m 2')
    cmd_provision.add_argument(
        "-w",
        "--worker-count",
        dest="worker_count",
        type=int,
        default=-1,
        help='number of workers nodes to be deployed. eg: -w 2')

    cmd_bootstrap = commands.add_parser("bootstrap",
                                        help="bootstrap k8s cluster with \
                        deployed nodes in your platform")
    cmd_bootstrap.add_argument("-k",
                               "--kubernetes-version",
                               help="kubernetes version",
                               dest="kubernetes_version",
                               default=None)
    cmd_bootstrap.add_argument("-c",
                               "--cloud-provider",
                               action="store_true",
                               help="Use cloud provider integration")
    cmd_bootstrap.set_defaults(func=bootstrap)

    cmd_status = commands.add_parser("status", help="check K8s cluster status")
    cmd_status.set_defaults(func=cluster_status)

    cmd_cluster_upgrade_plan = commands.add_parser("cluster-upgrade-plan",
                                                   help="Cluster upgrade plan")
    cmd_cluster_upgrade_plan.set_defaults(func=cluster_upgrade_plan)

    # common parameters for node commands
    node_args = ArgumentParser(add_help=False)
    node_args.add_argument(
        "-r",
        "--role",
        dest="role",
        choices=["master", "worker"],
        help='role of the node to be added or deleted. eg: --role master')
    node_args.add_argument("-n",
                           "--node",
                           dest="node",
                           type=int,
                           help='node to be added or deleted.  eg: -n 0')

    cmd_join_node = commands.add_parser(
        "join-node",
        parents=[node_args],
        help="add node in k8s cluster with the given role.")
    cmd_join_node.set_defaults(func=join_node)

    cmd_rem_node = commands.add_parser("remove-node",
                                       parents=[node_args],
                                       help="remove node from k8s cluster.")
    cmd_rem_node.set_defaults(func=remove_node)

    cmd_node_upgrade = commands.add_parser(
        "node-upgrade",
        parents=[node_args],
        help="upgrade kubernetes version in node")
    cmd_node_upgrade.add_argument("-a",
                                  "--action",
                                  dest="upgrade_action",
                                  help="action: plan or apply upgrade",
                                  choices=["plan", "apply"])
    cmd_node_upgrade.set_defaults(func=node_upgrade)

    # Start Join Nodes
    cmd_join_nodes = commands.add_parser(
        "join-nodes", help="add node in k8s cluster with the given role.")
    cmd_join_nodes.add_argument(
        "-m",
        "--masters",
        type=int,
        help="Specify how many masters to join. Default is all")
    cmd_join_nodes.add_argument(
        "-w",
        "--workers",
        type=int,
        help="Specify how many workers to join. Default is all")
    cmd_join_nodes.set_defaults(func=join_nodes)
    # End Join Nodes

    ssh_args = ArgumentParser(add_help=False)
    ssh_args.add_argument(
        "-c",
        "--cmd",
        dest="cmd",
        nargs=REMAINDER,
        help=
        "remote command and its arguments. e.g ls -al. Must be last argument for ssh command"
    )
    cmd_ssh = commands.add_parser("ssh",
                                  parents=[node_args, ssh_args],
                                  help="Execute command in node via ssh.")
    cmd_ssh.set_defaults(func=ssh)

    test_args = ArgumentParser(add_help=False)
    test_args.add_argument("-f",
                           "--filter",
                           dest="mark",
                           help="Filter the tests based on markers")
    test_args.add_argument(
        "-j", "--junit", help="Name of the xml file to record the results to.")
    test_args.add_argument("-m",
                           "--module",
                           dest="module",
                           help="folder with the tests")
    test_args.add_argument("-s",
                           "--suite",
                           dest="test_suite",
                           help="test file name")
    test_args.add_argument("-t", "--test", dest="test", help="test to execute")
    test_args.add_argument("-l",
                           "--list",
                           dest="collect",
                           action="store_true",
                           default=False,
                           help="only list tests to be executed")
    test_args.add_argument("-v",
                           "--verbose",
                           dest="verbose",
                           action="store_true",
                           default=False,
                           help="show all output")
    test_args.add_argument(
        "--skip-setup",
        choices=['provisioned', 'bootstrapped', 'deployed'],
        help="Skip the given setup step.\n"
        "'provisioned' For when you have already provisioned the nodes.\n"
        "'bootstrapped' For when you have already bootstrapped the cluster.\n"
        "'deployed' For when you already have a fully deployed cluster.")
    cmd_test = commands.add_parser("test",
                                   parents=[test_args],
                                   help="execute tests")
    cmd_test.set_defaults(func=test)

    options = parser.parse_args()
    try:
        conf = BaseConfig(options.yaml_path)
        Logger.config_logger(conf, level=options.log_level)
        options.conf = conf
        options.func(options)
    except Exception as ex:
        logger.error("Exception executing testrunner command '{}': {}".format(
            options.command, ex, exc_info=True))
        sys.exit(255)
Esempio n. 2
0
def main():
    help_str = """
    This script is meant to be run manually on test servers, developer desktops, or Jenkins.
    This script supposed to run on python virtualenv from testrunner. Requires root privileges.
    Warning: it removes docker containers, VMs, images, and network configuration.
    """
    parser = ArgumentParser(help_str)

    # Common parameters
    parser.add_argument("-v", "--vars", dest="yaml_path", default="vars.yaml",
                        help='path for platform yaml file. Default is vars.yaml. eg: -v myconfig.yaml')
    parser.add_argument("-p", "--platform",
                        default="openstack",
                        choices=["openstack", "vmware",
                                 "bare-metal", "libvirt"],
                        help="The platform you're targeting. Default is openstack")
    parser.add_argument("-l", "--log-level", dest="log_level", default=None, help="log level",
                        choices=["DEBUG", "INFO", "WARNING", "ERROR"])
    parser.add_argument("-c", "--print-conf", dest="print_conf", action="store_true",
                        help="prints the configuration")

    # Sub commands
    commands = parser.add_subparsers(help="command", dest="command")

    cmd_info = commands.add_parser("info", help='ip info')
    cmd_info.set_defaults(func=info)

    cmd_config = commands.add_parser("config", help='prints configuration to log')
    cmd_config.set_defaults(func=config)

    cmd_log = commands.add_parser("get_logs", help="gather logs from nodes")
    cmd_log.set_defaults(func=get_logs)

    cmd_cleanup = commands.add_parser(
        "cleanup", help="cleanup created skuba environment")
    cmd_cleanup.set_defaults(func=cleanup)

    cmd_provision = commands.add_parser("provision", help="provision nodes for cluster in "
                                                          "your configured platform e.g: openstack, vmware.")
    cmd_provision.set_defaults(func=provision)
    cmd_provision.add_argument("-m", "--master-count", dest="master_count", type=int, default=-1,
                               help='number of masters nodes to be deployed. eg: -m 2')
    cmd_provision.add_argument("-w", "--worker-count", dest="worker_count", type=int, default=-1,
                               help='number of workers nodes to be deployed. eg: -w 2')

    # common parameters for cluster bootstrap
    bootstrap_args = ArgumentParser(add_help=False)
    bootstrap_args.add_argument("-k", "--kubernetes-version", help="kubernetes version",
                                dest="kubernetes_version", default=None)
    bootstrap_args.add_argument("-c", "--cloud-provider", action="store_true",
                                help="Use cloud provider integration")
    bootstrap_args.add_argument("-t", "--timeout", type=int, default=300,
                                help="timeout for waiting a node to become ready (seconds)")
    bootstrap_args.add_argument("-m", "--registry-mirror", metavar=('R', 'M'),
                                help="Add to the registry R a mirror M."
                                     " If an image is available at the mirror"
                                     " it will be preferred, otherwise the"
                                     " image in the original registry is used."
                                     " This argument can be used multiple"
                                     " times, then mirrors will be tried in"
                                     " that order."
                                     " Example: --registry-mirror registry.example.com/path test-registry.example.com/path",
                                nargs=2, action='append')

    cmd_bootstrap = commands.add_parser(
        "bootstrap", parents=[bootstrap_args], help="bootstrap k8s cluster")
    cmd_bootstrap.set_defaults(func=bootstrap)

    cmd_deploy = commands.add_parser(
        "deploy", parents=[bootstrap_args],
        help="initializes, bootstrap and join all nodes k8s")
    cmd_deploy.set_defaults(func=deploy)

    cmd_status = commands.add_parser("status", help="check K8s cluster status")
    cmd_status.set_defaults(func=cluster_status)

    cmd_cluster_upgrade = commands.add_parser(
        "cluster-upgrade", help="Cluster upgrade")
    cmd_cluster_upgrade.add_argument(
        "-a", "--action", dest="upgrade_action", choices=["plan", "localconfig"])
    cmd_cluster_upgrade.set_defaults(func=cluster_upgrade)

    # common parameters for node commands
    node_args = ArgumentParser(add_help=False)
    node_args.add_argument(
        "-r", "--role", dest="role", choices=["master", "worker"],
        help='role of the node to be added or deleted. eg: --role master')
    node_args.add_argument(
        "-n", "--node", dest="node", type=int,
        help='node to be added or deleted.  eg: -n 0')
    # End of common parameters

    cmd_join_node = commands.add_parser(
        "join-node", parents=[node_args],
        help="add node in k8s cluster with the given role.")
    cmd_join_node.add_argument(
        "-t", "--timeout", type=int, default=180,
        help="timeout for waiting the node to become ready (seconds)")
    cmd_join_node.set_defaults(func=join_node)

    cmd_rem_node = commands.add_parser("remove-node", parents=[node_args],
                                       help="remove node from k8s cluster.")
    cmd_rem_node.set_defaults(func=remove_node)

    cmd_node_upgrade = commands.add_parser("node-upgrade", parents=[node_args],
                                           help="upgrade kubernetes version in node")
    cmd_node_upgrade.add_argument("-a", "--action", dest="upgrade_action",
                                  help="action: plan or apply upgrade", choices=["plan", "apply"])
    cmd_node_upgrade.add_argument("-t", "--timeout", type=int, default=180,
                                help="timeout for waiting the nodes to become ready after upgrade (seconds)")
    cmd_node_upgrade.set_defaults(func=node_upgrade)

    cmd_check_node = commands.add_parser(
        "check-node", parents=[node_args], help="check node health")
    cmd_check_node.add_argument("-c", "--check", dest="checks", nargs='+',
                                help="check to be executed (multiple checks can be specified")
    cmd_check_node.add_argument("-s", "--stage", dest="stage",
                                help="only execute checks that apply to this stage")
    cmd_check_node.set_defaults(func=node_check)

    # Start Join Nodes
    cmd_join_nodes = commands.add_parser("join-nodes",
                                         help="add multiple provisioned nodes k8s.")
    cmd_join_nodes.add_argument("-m", "--masters", type=int,
                                help="Specify how many masters to join. Default is all")
    cmd_join_nodes.add_argument("-w", "--workers", type=int,
                                help="Specify how many workers to join. Default is all")
    cmd_join_nodes.add_argument("-t", "--timeout", type=int, default=180,
                                help="timeout for waiting the master nodes to become ready (seconds)")
    cmd_join_nodes.set_defaults(func=join_nodes)
    # End Join Nodes

    ssh_args = ArgumentParser(add_help=False)
    ssh_args.add_argument("-c", "--cmd", dest="cmd", nargs=REMAINDER, help="remote command and its arguments. e.g ls -al. Must be last argument for ssh command")
    cmd_ssh = commands.add_parser("ssh", parents=[node_args, ssh_args], help="Execute command in node via ssh.")
    cmd_ssh.set_defaults(func=ssh)

    test_args = ArgumentParser(add_help=False)
    test_args.add_argument("-f", "--filter", dest="mark", help="Filter the tests based on markers")
    test_args.add_argument("-j", "--junit", help="Name of the xml file to record the results to.")
    test_args.add_argument("-m", "--module", dest="module", help="folder with the tests")
    test_args.add_argument("-s", "--suite", dest="test_suite", help="test file name")
    test_args.add_argument("-t", "--test", dest="test", help="test to execute")
    test_args.add_argument("-l", "--list", dest="collect", action="store_true", default=False,
                           help="only list tests to be executed")
    test_args.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False,
                           help="show all output from testrunner libraries")
    test_args.add_argument("--skip-setup",
                           choices=['provisioned', 'bootstrapped', 'deployed'],
                           help="Skip the given setup step.\n"
                                "'provisioned' For when you have already provisioned the nodes.\n"
                                "'bootstrapped' For when you have already bootstrapped the cluster.\n"
                                "'deployed' For when you already have a fully deployed cluster.")
    test_args.add_argument("--traceback", default="short", choices=['long', 'short', 'line', 'no'],
                           help="level of detail in traceback for test failure")
    cmd_test = commands.add_parser(
        "test", parents=[test_args], help="execute tests")
    cmd_test.set_defaults(func=test)

    cmd_inhibit_kured = commands.add_parser(
        "inhibit_kured", help="Prevent kured to reboot nodes")
    cmd_inhibit_kured.set_defaults(func=inhibit_kured)

    cmd_check_cluster = commands.add_parser(
        "check-cluster", help="check cluster health")
    cmd_check_cluster.add_argument(
        "-c", "--check", dest="checks", nargs='+',
        help="check to be executed (multiple checks can be specified")
    cmd_check_cluster.add_argument(
        "-s", "--stage", dest="stage",
        help="only execute checks that apply to this stage")
    cmd_check_cluster.set_defaults(func=cluster_check)

    options = parser.parse_args()
    try:
        conf = BaseConfig(options.yaml_path)
        Logger.config_logger(conf, level=options.log_level)
        options.conf = conf
        if options.print_conf:
            out = io.StringIO()
            BaseConfig.print(conf, out=out)
            logger.debug(f'Configuration\n{out.getvalue()}')

        options.func(options)
    except SystemExit as ex:
        if ex.code > 0:
            logger.error(f'Command {options.command} ended with error code {ex.code}')
            sys.exit(ex.code)
    except Exception as ex:
        logger.error(f'Exception {ex} executing command {options.command}', exc_info=True)
        sys.exit(255)

    sys.exit(0)