示例#1
0
    def ocm_cli_install(self):
        """Installs ocm cli if not installed"""
        if not self._is_ocmcli_installed():
            log.info("Installing ocm cli...")
            cmd = "sudo curl -Lo /bin/ocm {}".format(self.ocm_cli_binary_url)
            ret = execute_command(cmd)
            if ret is None:
                log.info("Failed to download ocm cli binary")
                sys.exit(1)

            cmd = "sudo chmod +x /bin/ocm"
            ret = execute_command(cmd)
            if ret is None:
                log.info("Failed to give execute permission to ocm cli binary")
                sys.exit(1)
def get_prometheus_token(project):
    """
    Get prometheus token for the cluster.
    """
    cmd = "oc sa get-token prometheus -n {}".format(project)
    prometheus_token = execute_command(cmd)
    return prometheus_token.strip("\n")
示例#3
0
    def create_group(self, group_name):
        """Creates new group"""

        cmd = "oc adm groups new {}".format(group_name)
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to add group " "{}".format(group_name))
示例#4
0
    def create_idp(self):
        """Creates Identity Provider"""

        if self.idp_type == "htpasswd":
            cmd = ("ocm create idp -c {} -t {} -n {} --username {} "
                   "--password {}".format(self.cluster_name, self.idp_type,
                                          self.idp_name,
                                          self.htpasswd_cluster_admin,
                                          self.htpasswd_cluster_password))
            log.info("CMD: {}".format(cmd))
            ret = execute_command(cmd)
            if ret is None:
                log.info("Failed to add identity provider of "
                         "type {}".format(self.idp_type))
            self.add_user_to_group()

        elif (self.idp_type == "ldap"):
            ldap_yaml_file = (os.path.abspath(os.path.dirname(__file__)) +
                              "/../../../configs/templates/ldap/ldap.yaml")
            cmd = "oc apply -f {}".format(ldap_yaml_file)
            log.info("CMD: {}".format(cmd))
            ret = execute_command(cmd)
            if ret is None:
                log.info("Failed to deploy openldap application")
                sys.exit(1)

            replace_vars = {
                "LDAP_URL": self.ldap_url,
                "LDAP_BIND_DN": self.ldap_bind_dn,
                "LDAP_BIND_PASSWORD": self.ldap_bind_password
            }
            template_file = "create_ldap_idp.jinja"
            output_file = "create_ldap_idp.json"
            self._render_template(template_file, output_file, replace_vars)

            cluster_id = self.get_osd_cluster_id()
            cmd = ("ocm post /api/clusters_mgmt/v1/"
                   "clusters/{}/identity_providers "
                   "--body={}".format(cluster_id, output_file))
            log.info("CMD: {}".format(cmd))
            ret = execute_command(cmd)
            if ret is None:
                log.info("Failed to add ldap identity provider")
            self.add_users_to_rhods_group()
        time.sleep(300)
示例#5
0
 def _is_ocmcli_installed(self):
     """Checks if ocm cli is installed"""
     cmd = "ocm version"
     ret = execute_command(cmd)
     if ret is None:
         log.info("ocm cli not installed.")
         return False
     log.info("ocm cli already installed...")
     return True
def get_prometheus_url(project):
    """
    Get prometheus url for the cluster.
    """
    host_jsonpath = "{.spec.host}"
    cmd = "oc get route prometheus -n {} -o jsonpath='{}'".format(
        project, host_jsonpath)
    prometheus_url = execute_command(cmd)
    return "https://" + prometheus_url.strip("\n")
示例#7
0
    def delete_idp(self):
        """Deletes Identity Provider"""

        cmd = ("ocm delete idp -c {} {}".format(self.cluster_name,
                                                self.idp_name))
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to delete identity provider of "
                     "type {}".format(self.idp_name))
示例#8
0
    def upload_result(self):
        """Uploads test results to report portal"""

        cmd = ("rp_preproc -c {} -d {} "
               "--service {} -l {}".format(self.config_file, self.payload_dir,
                                           self.service_url, self.log_path))
        log.info("CMD: {}".format(cmd))
        rp_output = execute_command(cmd)
        rp_output_json = json.loads(rp_output)
        self.write_output_file(json.dumps(rp_output_json))
示例#9
0
 def ocm_describe(self, filter=""):
     """Describes cluster and returns cluster info"""
     cmd = "ocm describe cluster {}".format(self.cluster_name)
     if filter != "":
         cmd += " " + filter
     ret = execute_command(cmd)
     if ret is None:
         log.info("ocm describe for cluster "
                  "{} failed".format(self.cluster_name))
         return None
     return ret
示例#10
0
    def list_idps(self):
        """Lists IDPs for the cluster"""

        cmd = ("ocm list idps --cluster {} --columns name".format(
            self.cluster_name))
        ret = execute_command(cmd)
        if ret is None:
            return []
        if ret != []:
            ret = ret.split('\n')[1:-1]
        return ret
示例#11
0
    def delete_cluster(self):
        """ Delete OSD Cluster"""

        cluster_id = self.get_osd_cluster_id()
        cmd = "ocm delete cluster {}".format(cluster_id)
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to delete osd cluster {}".format(
                self.cluster_name))
            sys.exit(1)
        self.wait_for_osd_cluster_to_get_deleted()
示例#12
0
    def add_users_to_rhods_group(self):
        """Add users to rhods group"""

        self.create_group("rhods-admins")
        # Adds user ldap-admin1..ldap-adminN
        for i in range(1, int(self.num_users_to_create_per_group) + 1):
            self.add_user_to_group(user="******" + str(i),
                                   group="rhods-admins")

        self.create_group("rhods-users")
        # Adds user ldap-user1..ldap-userN
        for i in range(1, int(self.num_users_to_create_per_group) + 1):
            self.add_user_to_group(user="******" + str(i),
                                   group="rhods-users")

# Adds special users
# "(", ")", "|", "<", ">" not working in OSD
# "+" and ";" disabled for now
        for char in [".", "^", "$", "*", "?", "[", "]", "{", "}", "@"]:
            self.add_user_to_group(user="******" + char,
                                   group="rhods-users")

        self.create_group("rhods-noaccess")
        # Adds user ldap-noaccess1..ldap-noaccessN
        for i in range(1, int(self.num_users_to_create_per_group) + 1):
            self.add_user_to_group(user="******" + str(i),
                                   group="rhods-noaccess")

        # Logging users/groups details after adding
        # given user to group

        cmd = "oc get users"
        log.info("CMD: {}".format(cmd))
        users_list = execute_command(cmd)
        log.info("Users present in cluster: {}".format(users_list))

        cmd = "oc get groups"
        log.info("CMD: {}".format(cmd))
        groups_list = execute_command(cmd)
        log.info("Groups present in cluster: {}".format(groups_list))
示例#13
0
    def uninstall_addon(self, addon_name="managed-odh"):
        """Uninstalls addon"""

        addon_state = self.get_addon_state(addon_name)
        if addon_state != "not installed":
            cluster_id = self.get_osd_cluster_id()
            cmd = ("ocm delete /api/clusters_mgmt/v1/clusters/{}/addons/"
                   "{}".format(cluster_id, addon_name))
            log.info("CMD: {}".format(cmd))
            ret = execute_command(cmd)
            if ret is None:
                log.info("Failed to uninstall {} addon on cluster "
                         "{}".format(addon_name, self.cluster_name))
                sys.exit(1)
示例#14
0
    def get_addon_state(self, addon_name="managed-odh"):
        """Gets given addon's state"""

        cmd = ("ocm list addons --cluster {} --columns id,state | grep "
               "{} ".format(self.cluster_name, addon_name))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to get {} addon state for cluster "
                     "{}".format(addon_name, self.cluster_name))
            return None
        match = re.search(addon_name + '\s*(.*)', ret)
        if match is None:
            log.info("regex failed in get_addon_state")
            return None
        return match.group(1).strip()
示例#15
0
    def ocm_login(self):
        """ Login to OCM using ocm cli"""

        cmd = "ocm login --token=\"{}\" ".format(self.token)

        if self.testing_platform == "stage":
            cmd += "--url=staging"

        cmd = "OCM_CONFIG=ocm.json." + self.testing_platform + " " + cmd
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to login to aws openshift platform using token")
            sys.exit(1)
        os.environ["OCM_CONFIG"] = "ocm.json." + self.testing_platform
示例#16
0
    def add_user_to_group(self, user="", group="cluster-admins"):
        """Adds user to given group"""

        if user == "":
            user = self.htpasswd_cluster_admin

        if ((group == "rhods-admins") or (group == "rhods-users")
                or (group == "rhods-noaccess")):
            cmd = "oc adm groups add-users {} {}".format(group, user)
        else:
            cmd = ("ocm create user {} --cluster {} "
                   "--group={}".format(user, self.cluster_name, group))
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to add user {} to group "
                     "{}".format(user, group))
示例#17
0
    def do_encrypt(self, args):
        """
        Encrypt an epub file using the *local* lcpencrypt command line
        """

        print("Let's encrypt {}".format(self.epub_path))

        # Generate a random content id
        content_id = str(uuid.uuid4())
        # Generate a target file path
        output_filename = "{}-{}.crypt.epub".format(self.epub_filename,
                                                    content_id)
        output_file_path = os.path.join(self.config.encrypted_file_path,
                                        output_filename)

        # Execute the encryption using the lcpencrypt utility
        return_code, stdout, stderr = util.execute_command([
            self.config.encrypt_cmd_path, '-input', self.epub_path,
            '-contentid', content_id, '-output', output_file_path
        ])

        if return_code != 0:
            print("Encryption failed, err {}".format(return_code))
            print(stderr)
            return

        # Parse the resulting json message
        result = json.loads(stdout.decode("utf-8"))

        self.encrypted_content_id = result["content-id"]
        self.encrypted_content_encryption_key = result[
            "content-encryption-key"]
        self.encrypted_content_filename = result[
            "protected-content-disposition"]
        self.encrypted_content_location = result["protected-content-location"]
        self.encrypted_content_length = result["protected-content-length"]
        self.encrypted_content_sha256 = result["protected-content-sha256"]

        print("Content id {}".format(self.encrypted_content_id))
        print("Encrypted Content filename {}".format(
            self.encrypted_content_filename))
        print("Encrypted Content location {}".format(
            self.encrypted_content_location))
        print("Encrypted Content length {}".format(
            self.encrypted_content_length))
示例#18
0
    def install_addon(self, addon_name="managed-odh"):
        """Installs addon"""
        replace_vars = {
            "CLUSTER_ID": self.cluster_name,
            "ADDON_NAME": addon_name
        }
        template_file = "install_addon.jinja"
        output_file = "install_operator.json"
        self._render_template(template_file, output_file, replace_vars)

        cluster_id = self.get_osd_cluster_id()
        cmd = ("ocm post /api/clusters_mgmt/v1/clusters/{}/addons "
               "--body={}".format(cluster_id, output_file))
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to install {} addon on cluster "
                     "{}".format(addon_name, self.cluster_name))
            sys.exit(1)
示例#19
0
    def osd_cluster_create(self):
        """Creates OSD cluster"""

        if ((self.channel_group == "candidate")
                and (self.testing_platform == "prod")):
            log.error(
                "Channel group 'candidate' is available only for stage environment."
            )
            sys.exit(1)

        version = ""
        if self.openshift_version != "":
            version_match = re.match(r'(\d+\.\d+)\-latest',
                                     self.openshift_version)
            if version_match is not None:
                version = version_match.group(1)
                chan_grp = ""
                if (self.channel_group == "candidate"):
                    chan_grp = "--channel-group {}".format(self.channel_group)

                version_cmd = "ocm list versions {} | grep -w \"".format(
                    chan_grp) + re.escape(version) + "*\""
                log.info("CMD: {}".format(version_cmd))
                versions = execute_command(version_cmd)
                if versions is not None:
                    version = [ver for ver in versions.split("\n") if ver][-1]
                self.openshift_version = version
            else:
                log.info("Using the osd version given by user as it is...")
            version = "--version {} ".format(self.openshift_version)
        else:
            log.info("Using the latest osd version available in AWS...")

        channel_grp = ""
        if (self.channel_group != ""):
            if ((self.channel_group == "stable")
                    or (self.channel_group == "candidate")):
                if version == "":
                    log.error((
                        "Please enter openshift version as argument."
                        "Channel group option is used along with openshift version."
                    ))
                    sys.exit(1)
                else:
                    channel_grp = "--channel-group {} ".format(
                        self.channel_group)
            else:
                log.error(
                    "Invalid channel group. Values can be 'stable' or 'candidate'."
                )

        cmd = ("ocm create cluster --aws-account-id {} "
               "--aws-access-key-id {} --aws-secret-access-key {} "
               "--ccs --region {} --compute-nodes {} "
               "--compute-machine-type {} {} {}"
               "{}".format(self.aws_account_id, self.aws_access_key_id,
                           self.aws_secret_access_key, self.aws_region,
                           self.num_compute_nodes, self.aws_instance_type,
                           version, channel_grp, self.cluster_name))
        log.info("CMD: {}".format(cmd))
        ret = execute_command(cmd)
        if ret is None:
            log.info("Failed to create osd cluster {}".format(
                self.cluster_name))
            sys.exit(1)