Beispiel #1
0
    def check_available():
        available_instances = dict()
        for cloud_name, cloud_instance in Cloud._cloud_instances.items():
            if cloud_instance.is_available():
                Log.debug("{0} cloud is available".format(cloud_name))
                available_instances[cloud_name] = cloud_instance
            else:
                Log.warning(
                    "{0} cloud was enabled but did not pass availability tests"
                    .format(cloud_name))

        Cloud._cloud_instances = available_instances
 def collect(self):
     Log.debug('Checking kubectl is installed correctly...')
     response, status = OSCommand.run2("command -v kubectl")
     if status == 0:
         Log.info("Looking good... Found kubectl")
         self.operation = Validator.OPERATION_OK
     else:
         self.operation = Validator.OPERATION_INSTALL
         Log.error(
             "You will need to have kubectl installed on this machine.")
         Log.error(
             "To install kubectl please see: https://kubernetes.io/docs/tasks/tools/install-kubectl/"
         )
Beispiel #3
0
    def install_cloud(self):
        print("")
        Cloud.initialize(self._prompts)
        cloud_names = Cloud.get_cloud_names()

        if len(cloud_names) == 0:
            Log.warning(
                "There are no supported cloud providers found in this bootstrapper application"
            )
            return False

        Log.info(
            "If you are installing in a cloud provider, we can help you create your kubernetes environment.",
            True)
        Log.info(
            "ATTENTION: Cloud Environment installation is provided AS IS with no support.",
            True)
        Log.info(
            "Work with your IT Team to help create kubernetes environments with the security and reliability features that suit your enterprise needs.",
            True)

        create = self._prompts.prompt_boolean(
            "Do you want to create a kubernetes environment in the Cloud?",
            False,
            key_name="CLOUD_ENV")
        if not create:
            Log.info("Not building cloud environment")
            return False

        # Check the availability of each enabled cloud provider
        Cloud.check_available()
        cloud_names = Cloud.get_cloud_names()
        if len(cloud_names) == 0:
            Log.warning(
                "Some clouds were enabled but necessary modules that support these clouds are not available"
            )
            BootstrapBase.exit_application(7)

        choice = self._prompts.prompt_choices("Choose a cloud provider",
                                              Cloud.get_cloud_names(),
                                              key_name="CLOUD_PROVIDER")
        Log.info("Using cloud provider {0}".format(choice))
        self.cloud_instance = Cloud.get_instance(choice)
        Log.debug("Using cloud instance {0}".format(str(self.cloud_instance)))

        Log.info("Building {0} cloud k8s...".format(choice))
        self.cloud_instance.build_cloud()
        Log.info("Created {0} cloud k8s".format(choice))
        self.cloud_created = True
        return True
Beispiel #4
0
 def collect(self):
     Log.debug('Checking for oc (OpenShift CLI) installation...')
     response, status = OSCommand.run2("command -v oc")
     if status == 0:
         Log.info("Looking good... Found oc (OpenShift CLI) installed",
                  True)
         self.operation = Validator.OPERATION_OK
     else:
         self.operation = Validator.OPERATION_NONE
         Log.error(
             "You will need to have oc (OpenShift CLI) installed on this machine."
         )
         Log.error(
             "To install oc please see: https://docs.openshift.com/container-platform/3.11/cli_reference/get_started_cli.html"
         )
    def create_disks_and_attach(self, node):
        # Create a disk list
        disk_list = ""
        for i in range(0, self.disks):
            disk_list += "{0}-disk-{1} ".format(node, i)

        Log.info(
            "Creating and attaching disk(s) for node {0}. One moment...".
            format(node), True)

        result, status = self.invoke_gcloud(
            "compute disks create --size {0}GB --type pd-ssd --project {1} --zone {2} {3}"
            .format(self.block_disk_size, self.project, self.zone, disk_list))
        Log.info("Created {0} disk(s) for node {1}".format(self.disks, node))
        Log.debug(result)

        for i in range(0, self.disks):
            disk_name = "{0}-disk-{1} ".format(node, i)
            result, status = self.invoke_gcloud(
                "compute instances attach-disk {0} --disk {1} --project {2} --zone {3}"
                .format(node, disk_name, self.project, self.zone))
            Log.info("Added disk {0} to node {1}".format(disk_name, node))
            Log.debug(result)

            result, status = self.invoke_gcloud(
                "compute instances set-disk-auto-delete {0} --disk {1} --project {2} --zone {3}"
                .format(node, disk_name, self.project, self.zone))
            Log.info("Set set-disk-auto-delete on disk {0}".format(disk_name))
            Log.debug(result)

        Log.info("Created and attached disk(s) for node {0}".format(node),
                 True)
Beispiel #6
0
    def write_response_file(self):
        if Prompts._instance is None:
            Log.debug(
                'Cannot write a response file prompts are not initialized')
            return

        if self.mode != Prompts.RECORD_MODE:
            Log.debug('Cannot write a response file when not in record mode')
            return

        if self.response_file.closed:
            Log.warn(
                'Cannot write a response file that is not opened. Response file can only be written once'
            )
            return

        try:
            sorted_keys = sorted(self.recorded_options)
            for key in sorted_keys:
                self.response_file.write(
                    '%s=%s%s' % (key, self.recorded_options[key], os.linesep))
        finally:
            self.response_file.close()
Beispiel #7
0
    def collect(self):
        Log.debug("Getting Python information...")

        python_major, python_version = self._get_python_version()
        Log.info("Python version: {0}".format(python_version))

        self.results[Validator.FOUND] = True
        self.results[Validator.VERSION] = python_version

        if python_major == 2:
            pmin = PythonValidator.PYTHON2_MIN
            pmax = PythonValidator.PYTHON2_MAX
        elif python_major == 3:
            pmin = PythonValidator.PYTHON3_MIN
            pmax = PythonValidator.PYTHON3_MAX

            if python_version <= PythonValidator.PYTHON3_ERROR_MAX:
                Log.error("The virtual environments created with your python version {0} are incompatible. "
                          "Please use Python 3.3 or greater".format(python_version))
                self.operation = Validator.OPERATION_INSTALL
                return
        else:
            Log.error("The major Python version '{0}' is not supported; Only version 2 and 3 supported".format(python_major))
            self.operation = Validator.OPERATION_TOO_NEW
            return

        expected = "Expected versions between {0} and {1} or between {2} and {3}"\
            .format(PythonValidator.PYTHON2_MIN, PythonValidator.PYTHON2_MAX,
                    PythonValidator.PYTHON3_MIN, PythonValidator.PYTHON3_MAX)

        if python_version > pmax or python_version < pmin:
            Log.warning("The Python version on this system is {0}. {1}"
                      .format(python_version, expected))
            self.operation = Validator.OPERATION_WARNING
        else:
            Log.debug("The Python version on this system is compatible")
            self.operation = Validator.OPERATION_OK
Beispiel #8
0
    def run2(statements,
             username=None,
             use_nohup=False,
             out_file=None,
             in_background=False,
             users_env=False):
        if isinstance(statements, str):
            statements = [statements]

        responses = ''
        status = 0

        for statement in statements:
            new_statement = ''
            if use_nohup:
                new_statement += 'nohup '
            if username is not None:
                new_statement += 'su '
                if users_env:
                    new_statement += '- '
                new_statement += username + ' -c \'' + statement + '\''
            else:
                new_statement += statement

            if in_background:
                if use_nohup and out_file is not None:
                    new_statement += ' > ' + out_file + ' 2>&1'
                else:
                    new_statement += ' &>/dev/null'
                new_statement += ' &'

            Log.debug('RUN: %s' % new_statement)

            process = subprocess.Popen('%s 2>&1' % new_statement,
                                       shell=True,
                                       stdout=subprocess.PIPE)
            response = process.stdout.read()
            # process.wait will only return None if the process hasn't terminated. We don't
            # need to check for None here
            status = process.wait()

            if len(response) == 0:
                response = '<no response>'
            else:
                # Python 3 returns byes or bytearray from the read() above
                if not isinstance(response, str) and isinstance(
                        response, (bytes, bytearray)):
                    response = response.decode("UTF-8")
            Log.debug('STATUS: %s' % str(status))
            Log.debug('RESPONSE: %s' % response)

            responses += response

            if status != 0:
                break

        return responses, status
Beispiel #9
0
    def initialize(prompts):
        Cloud.prompts = prompts
        Cloud._clouds = list()
        Cloud._cloud_instances = dict()
        files = os.listdir(Cloud.DIR)

        Log.info("Initializing cloud support. One moment please...")

        for afile in files:
            full_file = os.path.join(Cloud.DIR, afile)
            if not full_file.endswith(".py"):
                Log.debug(
                    "Not inspecting: {0} because it is not a py file".format(
                        full_file))
                continue
            if not os.path.isfile(full_file):
                Log.debug(
                    "Not inspecting: {0} because it is not a file".format(
                        full_file))
                continue
            if afile == "cloud.py" or afile == "__init__.py":
                Log.debug(
                    "Not inspecting: {0} because it is not a file".format(
                        full_file))
                continue

            module_name = full_file[:full_file.index(".")]
            file_module = imp.load_source(module_name,
                                          os.path.join(Cloud.DIR, full_file))
            class_members = inspect.getmembers(file_module, inspect.isclass)
            Log.debug("class_members for file_module {0} is: {1}".format(
                str(file_module), str(class_members)))

            for clazz in class_members:
                # if the class is of the correct subclass add it to the list of tests
                if not issubclass(clazz[1], Cloud) or clazz[
                        1] == Cloud or clazz[1] in Cloud._clouds:
                    continue

                Cloud._clouds.append(clazz[1])
                instance = clazz[1]()
                name = instance.get_name()
                if instance.is_enabled():
                    Cloud._cloud_instances[name] = instance
                    Log.debug(
                        "Cloud {0} was added to list because it is enabled".
                        format(name))
                else:
                    Log.debug(
                        "Cloud {0} was not added to list because it is not enabled"
                        .format(name))

        Log.debug("There were {0} cloud providers found".format(
            len(Cloud._clouds)))