def install()

in nodemanager/scripts/setup.py [0:0]


def install():
	keepcert = False
	managehosts = False
	connectionstring = None
	certfile = None
	certpasswd = None
	authenticationkey = None
	for a in sys.argv[2:]:
		if re.match(r"^[-/](help|usage|\?)", a):
			Usage()
			sys.exit(0)
		if re.match("^[-/](connectionstring|clusname):.+", a):
			connectionstring = get_argvalue(a)
		elif re.match("^[-/]certfile:.+", a):
			certfile = get_argvalue(a)
		elif re.match("^[-/]certpasswd:.+", a):
			certpasswd = get_argvalue(a)
		elif re.match("^[-/]authenticationkey:.+", a):
			authenticationkey = get_argvalue(a)
		elif re.match("^[-/]keepcert", a):
			keepcert = True
		elif re.match("^[-/]managehosts", a):
			managehosts = True
		else:
			print("Invalid argument: %s" % a)
			Usage()
			sys.exit(1)

	if not connectionstring or (not keepcert and not certfile):
		print("One or more parameters are not specified.")
		Usage()
		sys.exit(1)

	if keepcert and (certfile or certpasswd or authenticationkey):
		print("The parameter keepcert cannot be specified with the parameter certfile, certpass or authenticationkey")
		Usage()
		sys.exit(1)

	if keepcert:
		pemfile = os.path.join(InstallRoot, "certs/nodemanager.pem")
		rsakeyfile = os.path.join(InstallRoot, "certs/nodemanager_rsa.key")
		if not os.path.isfile(pemfile) or not os.path.isfile(rsakeyfile):
			Log("nodemanager.pem or nodemanager_rsa.key not found")
			sys.exit(1)
		configfiletemp = os.path.join(InstallRoot, 'nodemanager.json')
		if not os.path.isfile(configfiletemp):
			Log("nodemanager.json not found")
			sys.exit(1)
		with open(configfiletemp, 'r') as F:
			configjsontemp = json.load(F)
			authenticationkey = configjsontemp.get('ClusterAuthenticationKey')
	else:
		if not os.path.isfile(certfile):
			print("certfile not found: %s" % certfile)
			sys.exit(1)
		if not certpasswd:
			certpasswd = getpass.getpass(prompt='Please input the certificate protection password:')

	srcpkgdir = os.path.dirname(__file__)
	srcpkg = os.path.join(srcpkgdir, 'hpcnodeagent.tar.gz')
	if not os.path.isfile(srcpkg):
		Log("hpcnodeagent.tar.gz not found")
		sys.exit(1)

	if is_hpcagent_installed():
		Log("hpc agent was already installed")
		sys.exit(0)

	Log("Start to install HPC Linux node agent")
	try:
		extract_hpcagent_files(srcpkg)
		logdir = os.path.join(InstallRoot, "logs")
		certsdir = os.path.join(InstallRoot, "certs")
		if not os.path.isdir(logdir):
			os.makedirs(logdir)

		host_name = socket.gethostname().split('.')[0]
		api_prefix = "https://{0}:443/HpcLinux/api/"
		node_uri = api_prefix + host_name + "/computenodereported"
		reg_uri = api_prefix + host_name + "/registerrequested"
		metric_inst_uri = api_prefix + host_name + "/getinstanceids"
		configjson = {
		  "NamingServiceUri": ['https://{0}:443/HpcNaming/api/fabric/resolve/singleton/'.format(h.strip()) for h in connectionstring.split(',')],
		  "HeartbeatUri": node_uri,
		  "RegisterUri": reg_uri,
		  "MetricInstanceIdsUri": metric_inst_uri,
		  "MetricUri": "",
		  "TrustedCAFile": os.path.join(certsdir, "nodemanager.pem"),
		  "CertificateChainFile": os.path.join(certsdir, "nodemanager.crt"),
		  "PrivateKeyFile": os.path.join(certsdir, "nodemanager.key"),
		  "ListeningUri": "https://0.0.0.0:40002",
		  "DefaultServiceName": "SchedulerStatefulService",
		  "UdpMetricServiceName": "MonitoringStatefulService",
		  "ClusterAuthenticationKey": authenticationkey if authenticationkey else "",
		}
		if managehosts:
			configjson['HostsFileUri'] = api_prefix + "hostsfile"
		configfile = os.path.join(InstallRoot, 'nodemanager.json')
		SetFileContents(configfile, json.dumps(configjson))
		os.chmod(configfile, 0o640)
		if not keepcert:
			Log("Generating the key pair from {0}".format(certfile))
			generatekeypair(certfile, certpasswd)

		Log("Install depending tools ...")
		install_cgroup_tools()
		install_sysstat()
		install_pstree()

		if Run("command -v setsebool", chk_err=False) == 0:
			Log("Set SELinux boolean value httpd_can_network_connect and allow_httpd_anon_write to true")
			Run("setsebool -P httpd_can_network_connect 1")
			Run("setsebool -P allow_httpd_anon_write 1")

		if Run("firewall-cmd --state", chk_err=False) == 0:
			Log("Configuring firewalld settings")
			Run("firewall-cmd --permanent --zone=public --add-port=40000/tcp")
			Run("firewall-cmd --permanent --zone=public --add-port=40002/tcp")
			Run("firewall-cmd --reload")
			Log("firewalld settings configured")

		Log("Starting the hpc node agent daemon")
		if UseSystemdServiceUnit:
			shutil.move(os.path.join(InstallRoot, "hpcagent.service"), "/etc/systemd/system/hpcagent.service")
			Run("systemctl daemon-reload")
		else:
			shutil.move(os.path.join(InstallRoot, "hpcagent.sh"), "/etc/init.d/hpcagent")
		if SupportSystemd:
			Run("systemctl enable hpcagent")
			errCode, msg = RunGetOutput("systemctl start hpcagent")
		else:
			if DistroName == "ubuntu":
				Run("update-rc.d hpcagent defaults")
			elif DistroName in ["centos", "redhat", "suse", "alma", "almalinux", "rocky", "rockylinux"]:
				Run("chkconfig --add hpcagent")
			else:
				raise Exception("unsupported Linux Distro")
			errCode, msg = RunGetOutput("service hpcagent start")
		if errCode == 0:
			Log("The hpc node agent was installed")
		else:
			Log("The hpc node agent failed to start: " + msg)
			sys.exit(1)
	except Exception as e:
		cleanup_hpc_agent(keepcert)
		Log("Failed to install hpc node agent: {0}, stack trace: {1}".format(e, traceback.format_exc()))
		sys.exit(1)