def install()

in AzureMonitorAgent/agent.py [0:0]


def install():
    """
    Ensure that this VM distro and version are supported.
    Install the Azure Monitor Linux Agent package, using retries.
    Note: install operation times out from WAAgent at 15 minutes, so do not
    wait longer.
    """

    exit_if_vm_not_supported('Install')
    find_package_manager("Install")
    set_os_arch('Install')
    vm_dist, vm_ver = find_vm_distro('Install')

    # Check if Debian 12 VMs have rsyslog package (required for AMA 1.31+)
    if (vm_dist.startswith('debian')) and vm_ver.startswith('12'):
        check_rsyslog, _ = run_command_and_log("dpkg -s rsyslog")
        if check_rsyslog != 0:
            hutil_log_info("'rsyslog' package missing from Debian 12 machine, installing to allow AMA to run.")
            rsyslog_exit_code, rsyslog_output = run_command_and_log("DEBIAN_FRONTEND=noninteractive apt-get update && \
                                                                    DEBIAN_FRONTEND=noninteractive apt-get install -y rsyslog")
            if rsyslog_exit_code != 0:
                return rsyslog_exit_code, rsyslog_output
    
    # Check if Amazon 2023 VMs have rsyslog package (required for AMA 1.31+)
    if (vm_dist.startswith('amzn')) and vm_ver.startswith('2023'):
        check_rsyslog, _ = run_command_and_log("dnf list installed | grep rsyslog.x86_64")
        if check_rsyslog != 0:
            hutil_log_info("'rsyslog' package missing from Amazon Linux 2023 machine, installing to allow AMA to run.")
            rsyslog_exit_code, rsyslog_output = run_command_and_log("dnf install -y rsyslog")
            if rsyslog_exit_code != 0:
                return rsyslog_exit_code, rsyslog_output
            
    package_directory = os.path.join(os.getcwd(), PackagesDirectory)
    bundle_path = os.path.join(package_directory, BundleFileName)
    os.chmod(bundle_path, 100)
    print(PackageManager, " and ", BundleFileName)
    AMAInstallCommand = "{0} {1} -i {2}".format(PackageManager, PackageManagerOptions, bundle_path)
    hutil_log_info('Running command "{0}"'.format(AMAInstallCommand))

    # Retry, since install can fail due to concurrent package operations
    exit_code, output = run_command_with_retries_output(AMAInstallCommand, retries = 15,
                                         retry_check = retry_if_dpkg_or_rpm_locked,
                                         final_check = final_check_if_dpkg_or_rpm_locked)

    # Retry install for aarch64 rhel8 VMs as initial install fails to create symlink to /etc/systemd/system/azuremonitoragent.service
    # in /etc/systemd/system/multi-user.target.wants/azuremonitoragent.service
    if vm_dist.replace(' ','').lower().startswith('redhat') and vm_ver == '8.6' and platform.machine() == 'aarch64':
        exit_code, output = run_command_with_retries_output(AMAInstallCommand, retries = 15,
                                         retry_check = retry_if_dpkg_or_rpm_locked,
                                         final_check = final_check_if_dpkg_or_rpm_locked)

    if exit_code != 0:
        return exit_code, output

    # System daemon reload is required for systemd to pick up the new service
    exit_code, output = run_command_and_log("systemctl daemon-reload")
    if exit_code != 0:
        return exit_code, output

    # Copy the AMACoreAgent and agentlauncher binaries
    copy_amacoreagent_binaries()

    # Copy KqlExtension binaries
    # Needs to be revisited for aarch64
    copy_kqlextension_binaries()

    # Copy mdsd and fluent-bit with OpenSSL dynamically linked
    if is_feature_enabled('useDynamicSSL'):
        # Check if they have libssl.so.1.1 since AMA is built against this version
        libssl1_1, _ = run_command_and_log('ldconfig -p | grep libssl.so.1.1')
        if libssl1_1 == 0:
            copy_mdsd_fluentbit_binaries()
    
    # Set task limits to max of 65K in suse 12
    # Based on Task 9764411: AMA broken after 1.7 in sles 12 - https://dev.azure.com/msazure/One/_workitems/edit/9764411
    if exit_code == 0:
        vm_dist, _ = find_vm_distro('Install')
        if (vm_dist.startswith('suse') or vm_dist.startswith('sles')):
            try:
                suse_exit_code, suse_output = run_command_and_log("mkdir -p /etc/systemd/system/azuremonitoragent.service.d")
                if suse_exit_code != 0:
                    return suse_exit_code, suse_output

                suse_exit_code, suse_output = run_command_and_log("echo '[Service]' > /etc/systemd/system/azuremonitoragent.service.d/override.conf")
                if suse_exit_code != 0:
                    return suse_exit_code, suse_output

                suse_exit_code, suse_output = run_command_and_log("echo 'TasksMax=65535' >> /etc/systemd/system/azuremonitoragent.service.d/override.conf")
                if suse_exit_code != 0:
                    return suse_exit_code, suse_output

                suse_exit_code, suse_output = run_command_and_log("systemctl daemon-reload")
                if suse_exit_code != 0:
                    return suse_exit_code, suse_output
            except:
                log_and_exit("install", MissingorInvalidParameterErrorCode, "Failed to update /etc/systemd/system/azuremonitoragent.service.d for suse 12,15" )

    return exit_code, output