def install_agent(self, path_to_agent_file, file_name, to_directory, ip, port, secure_port, username, password): #check if port 2144 is used, if so fail - probably means that Hyperic agent is already running on this machine s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(('localhost', int(2144))) s.shutdown(2) msg = "Port 2144 is used, this probably means that Hyperic agent is already running on this machine" sys.stderr.write(msg) formatter = ProviderContext.get_formatter() formatter.format(Information(msg, 'Informational')) return except: logger.info("Port 2144 is available"); #make sure that all the path variables ends with '/' if not path_to_agent_file.endswith('/'): path_to_agent_file += '/' if not to_directory.endswith('/'): to_directory += '/' #determin the type of extractor according to the file name if file_name.endswith('.zip'): opener, mode = zipfile.ZipFile, 'r' if file_name.endswith('.tar.gz') or file_name.endswith('.tgz'): opener, mode = tarfile.open, 'r:gz' elif file_name.endswith('.tar.bz2') or file_name.endswith('.tbz'): opener, mode = tarfile.open, 'r:bz2' else: msg = "Could not extract the agent's file as no appropriate extractor is found" sys.stderr.write(msg) formatter = ProviderContext.get_formatter() formatter.format(Information(msg, 'Informational')) return #check if this agent is already installed on this machine if os.path.exists(to_directory + file_name[0:file_name.find("-noJRE")]): msg = file_name[0:file_name.find("-noJRE")] + " is already installed on this machine" sys.stderr.write(msg) formatter = ProviderContext.get_formatter() formatter.format(Information(msg, 'Informational')) return #create the directory and extract the agent if not os.path.exists(to_directory): os.makedirs(to_directory) cwd = os.getcwd() os.chdir(to_directory) try: file = opener(path_to_agent_file + file_name, mode) try: file.extractall() finally: file.close() finally: os.chdir(cwd) #change the agent's properties file and start the agent agent_name = file_name[0:file_name.find("-noJRE")] properties_file = to_directory + agent_name + "/conf/agent.properties" start_agent = to_directory + agent_name + "/bin/hq-agent.sh start" for line in fileinput.input([properties_file],inplace=True): line=line.replace("#agent.setup.camIP=localhost","agent.setup.camIP=" + ip) line=line.replace("#agent.setup.camPort=7080","agent.setup.camPort=" + port) line=line.replace("#agent.setup.camSSLPort=7443","agent.setup.camSSLPort=" + secure_port) line=line.replace("#agent.setup.camSecure=yes" ,"agent.setup.camSecure=yes") line=line.replace("#agent.setup.camLogin=hqadmin" , "agent.setup.camLogin="******"#agent.setup.camPword=hqadmin" , "agent.setup.camPword=" + password) line=line.replace("#agent.setup.agentIP=*default*" , "agent.setup.agentIP=*default*") line=line.replace("#agent.setup.agentPort=*default*" , "agent.setup.agentPort=*default*") line=line.replace("#agent.setup.resetupTokens=no" , "agent.setup.resetupTokens=no") line=line.replace("#agent.setup.acceptUnverifiedCertificate=no" , "agent.setup.acceptUnverifiedCertificate=yes") line=line.replace("accept.unverified.certificates=false" , "accept.unverified.certificates=true") line=line.replace("#agent.setup.unidirectional=no" , "agent.setup.unidirectional=no") sys.stdout.write(line) os.system(start_agent) formatter = ProviderContext.get_formatter() formatter.format(Information("Started the agent", 'Informational'))
def restart_agent(self, agent_directory): if not agent_directory.endswith('/'): agent_directory += '/' os.system(agent_directory + 'bin/hq-agent.sh restart') formatter = ProviderContext.get_formatter() formatter.format(Information("", 'Informational'))