Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.7 kB
1
Indexable
Never
Existing Code:
class EC2ClientLogCollection(__BaseLogCollection):
    def __init__(self, region, **kwargs):
        super().__init__(**kwargs)
        self.__ssm_client = get_ssm_client(region=region, rds_domain=None)

    def setup_ec2_pas_logs(self, instance_id):
        cmds = [f"sudo mkdir -p {EC2_CLIENT_LOGS_DIR_PATH}"]
        self.__ssm_client.run_cmd(instance_id=instance_id, cmds=cmds)

    def transfer_ec2_pas_logs_to_s3(self, instance_id, workflow_path):
        credentials = self.get_pas_credentials(PAS_ADMIN_OPS_ROLE)
        cmds = self.get_rds_s3_file_transfer_template(
            credentials["AccessKeyId"], credentials["SecretAccessKey"]
        )

        cmds.append(f"tar -zhcvf /tmp/{EC2_CLIENT_LOGS_TAR_NAME} {EC2_CLIENT_LOGS_DIR_PATH}")

        cmds.append(
            f"aws s3 cp /tmp/{EC2_CLIENT_LOGS_TAR_NAME} "
            + f"{get_pas_logs_s3_dir_for_ec2_client(workflow_path, instance_id)} "
        )
        self.__ssm_client.run_cmd(instance_id=instance_id, cmds=cmds)

My code:
class RDSHostLogCollection(__BaseLogCollection):
    def __init__(self, domain, region, **kwargs):
        super().__init__(**kwargs)
        self.__ssm_client = get_ssm_client(region=region, rds_domain=domain)

    def collect_rpm_version_configs(self, instance_id, workflow_path):
        """
        Get RPM version info
        """
        credentials = self.get_pas_credentials(PAS_ADMIN_OPS_ROLE)
        cmds = self.get_rds_s3_file_transfer_template(
            credentials["AccessKeyId"], credentials["SecretAccessKey"]
        )
        cmds.append("export logfile=/rdsdbdata/tmp/pas_rpm_version/pas_rpm_version.log")
        cmds.append("sudo mkdir -p /rdsdbdata/tmp/pas_rpm_version/ || true")
        cmds.append("sudo chmod -R 777 /rdsdbdata/tmp/pas_rpm_version/")
        cmds.append(
            "sudo env -i /usr/bin/yum list installed | "
            "grep -i 'rds\|aws\|gdb\|glib\|kernel\|csd\|ams\|apg\|mysql\|psql\|postgres\|oscar' | "  # noqa: W605
            "sudo sed 's/^/pas_rpm_version: /' > /rdsdbdata/tmp/pas_rpm_version/pas_rpm_version.log"
        )
        cmds.append("echo END >> /rdsdbdata/tmp/pas_rpm_version/pas_rpm_version.log")

        cmds.append(
            "aws s3 cp ${logfile} "
            + f"{get_pas_logs_s3_dir_for_instance(workflow_path, instance_id[0].strip())} "
            + ">> $logfile 2>&1"
        )

        self.__ssm_client.run_cmd(instance_id[1], cmds)

I want to remove hardcoding in my code and make it look like Existing code where they have not done hardcoding for example  they have this cmds.append(f"tar -zhcvf /tmp/{EC2_CLIENT_LOGS_TAR_NAME} {EC2_CLIENT_LOGS_DIR_PATH}") instead of hardcoding
Leave a Comment