def install()

in VMExtension/hpcnodemanager.py [0:0]


def install():
    hutil = parse_context('Install')
    try:
        cleanup_host_entries()
        _uninstall_nodemanager_files()
        if DistroName in ["centos", "redhat", "almalinux", "rocky"]:
            waagent.Run("yum-config-manager --setopt=\\*.skip_if_unavailable=1 --save", chk_err=False)
        _install_cgroup_tool()
        _install_sysstat()
        _install_pstree()
        
        logDir = os.path.join(InstallRoot, "logs")
        if not os.path.isdir(logDir):
            os.makedirs(logDir)
        srcDir = os.path.join(os.getcwd(), "bin")
        waagent.RunGetOutput("chmod +x {0}/*".format(srcDir))
        waagent.RunGetOutput("chmod +x {0}/lib/*".format(srcDir))
        for filename in os.listdir(srcDir):
            srcname = os.path.join(srcDir, filename)
            destname = os.path.join(InstallRoot, filename)
            if os.path.isfile(srcname):
                shutil.copy2(srcname, destname)
            elif os.path.isdir(srcname):
                shutil.copytree(srcname, destname)
        libdir = os.path.join(InstallRoot, 'lib')
        for tmpname in os.listdir(libdir):
            tmppath = os.path.join(libdir, tmpname)
            if tmpname.endswith(".tar.gz") and os.path.isfile(tmppath):
                waagent.Run("tar xzvf {0} -C {1}".format(tmppath, libdir))
                os.remove(tmppath)
        waagent.Run("chmod -R 755 {0}".format(libdir))

        host_name = None
        public_settings = hutil._context._config['runtimeSettings'][0]['handlerSettings'].get('publicSettings')
        if public_settings:
            host_name = public_settings.get('HostName')
        backup_configfile = os.path.join(os.getcwd(), 'nodemanager.json')
        if not host_name:
            # if there is backup nodemanager.json, means it is an update install, if 'HostName' not defined in the extension
            # settings, we shall get from the backup nodemanager.json
            if os.path.isfile(backup_configfile):
                waagent.Log("Backup nodemanager configuration file found")
                host_name = gethostname_from_configfile(backup_configfile)

        curhostname = socket.gethostname().split('.')[0]
        if host_name:
            if host_name.lower() != curhostname.lower():
                waagent.Log("HostName was set: hostname from {0} to {1}".format(curhostname, host_name))
                osutil.set_hostname(host_name)
                osutil.publish_hostname(host_name)
        else:
            host_name = curhostname
        public_settings = hutil._context._config['runtimeSettings'][0]['handlerSettings'].get('publicSettings')
        protect_settings = hutil._context._config['runtimeSettings'][0]['handlerSettings'].get('protectedSettings')
        authentication_key = ""
        if protect_settings is not None:
            authentication_key = protect_settings.get('AuthenticationKey')
            authentication_key = authentication_key if authentication_key is not None else ""
        cluster_connstring = public_settings.get('ClusterConnectionString')
        if not cluster_connstring:
            waagent.Log("ClusterConnectionString is not specified")
            cluster_connstring = protect_settings.get('ClusterName')
            if not cluster_connstring:
                error_msg = "neither ClusterConnectionString nor ClusterName is specified."
                hutil.error(error_msg)
                raise ValueError(error_msg)
        ssl_thumbprint = public_settings.get('SSLThumbprint')
        certsdir = os.path.join(InstallRoot, "certs")
        if not ssl_thumbprint:
            api_prefix = "http://{0}:80/HpcLinux/api/"
            listen_uri = "http://0.0.0.0:40000"
        else:
            api_prefix = "https://{0}:443/HpcLinux/api/"
            listen_uri = "https://0.0.0.0:40002"
            # import the ssl certificate for hpc nodemanager
            if not os.path.isdir(certsdir):
                os.makedirs(certsdir, 0o750)
            else:
                os.chmod(certsdir, 0o750)
            ssl_thumbprint = ssl_thumbprint.upper()
            prvfile = os.path.join("/var/lib/waagent", ssl_thumbprint + ".prv")
            srccrtfile = os.path.join("/var/lib/waagent", ssl_thumbprint + ".crt")
            rsakeyfile = os.path.join(certsdir, "nodemanager_rsa.key")
            dstcrtfile = os.path.join(certsdir, "nodemanager.crt")
            if os.path.isfile(prvfile) and not cmpFileHash(prvfile, rsakeyfile):
                waagent.Run("rm -rf {0}/nodemanager.crt {0}/nodemanager.key {0}/nodemanager.pem {0}/nodemanager_rsa.key".format(certsdir), chk_err=False)
                shutil.copy2(prvfile, rsakeyfile)
                shutil.copy2(srccrtfile, dstcrtfile)
                shutil.copy2(dstcrtfile, os.path.join(certsdir, "nodemanager.pem"))
                waagent.Run("openssl rsa -in {0}/nodemanager_rsa.key -out {0}/nodemanager.key".format(certsdir))
                waagent.Run("chmod 640 {0}/nodemanager.crt {0}/nodemanager.key {0}/nodemanager.pem {0}/nodemanager_rsa.key".format(certsdir))

        node_uri = api_prefix + host_name + "/computenodereported"
        reg_uri = api_prefix + host_name + "/registerrequested"
        hostsfile_uri = api_prefix + "hostsfile"
        metric_ids_uri = api_prefix + host_name + "/getinstanceids"
        namingSvcUris = ['https://{0}:443/HpcNaming/api/fabric/resolve/singleton/'.format(h.split('.')[0].strip()) for h in cluster_connstring.split(',')]
        if os.path.isfile(backup_configfile):
            with open(backup_configfile, 'r') as F:
                configjson = json.load(F)
            configjson["NamingServiceUri"] = namingSvcUris
            configjson["HeartbeatUri"] = node_uri
            configjson["RegisterUri"] = reg_uri
            configjson["HostsFileUri"] = hostsfile_uri
            configjson["MetricInstanceIdsUri"] = metric_ids_uri
            configjson["MetricUri"] = ""
            configjson["ListeningUri"] = listen_uri
        else:
            configjson = {
              "ConfigVersion": "1.0",
              "NamingServiceUri": namingSvcUris,
              "HeartbeatUri": node_uri,
              "RegisterUri": reg_uri,
              "MetricUri": "",
              "MetricInstanceIdsUri": metric_ids_uri,
              "HostsFileUri": hostsfile_uri,
              "HostsFetchInterval": 120,
              "ListeningUri": listen_uri,
              "DefaultServiceName": "SchedulerStatefulService",
              "UdpMetricServiceName": "MonitoringStatefulService"
            }
        if ssl_thumbprint:
            configjson["TrustedCAFile"] = os.path.join(certsdir, "nodemanager.pem")
            configjson["CertificateChainFile"] = os.path.join(certsdir, "nodemanager.crt")
            configjson["PrivateKeyFile"] = os.path.join(certsdir, "nodemanager.key")
        if authentication_key:
            configjson["ClusterAuthenticationKey"] = authentication_key
        configfile = os.path.join(InstallRoot, 'nodemanager.json')
        waagent.SetFileContents(configfile, json.dumps(configjson))
        shutil.copy2(configfile, backup_configfile)
        config_firewall_rules()
        if CGroupV2:
            shutil.copy2(os.path.join(InstallRoot, "hpccgroot.service"), "/etc/systemd/system/hpccgroot.service")
            waagent.Run("systemctl daemon-reload")
            waagent.Run("systemctl enable hpccgroot.service")
            waagent.Run("systemctl restart hpccgroot.service")
        hutil.do_exit(0, 'Install', 'success', '0', 'Install Succeeded.')
    except Exception as e:
        hutil.do_exit(1, 'Install','error','1', '{0}'.format(e))