forked from Sysdeploy/idlers-agent
refractored code for os details
This commit is contained in:
parent
04671ba669
commit
34e2a03db8
87
agent.py
87
agent.py
|
@ -233,17 +233,42 @@ class ServerData:
|
||||||
logging.error("Failed to get public IP: {}".format(e))
|
logging.error("Failed to get public IP: {}".format(e))
|
||||||
return '127.0.0.1'
|
return '127.0.0.1'
|
||||||
|
|
||||||
def get_os(self):
|
def get_os_release_info(self):
|
||||||
server_manager = ServerManager(host, api_key)
|
try:
|
||||||
os_id = server_manager.get_os_id()
|
with open('/etc/os-release') as f:
|
||||||
logging.info("OS ID: {}".format(os_id))
|
lines = f.read().splitlines()
|
||||||
return os_id
|
return {line.split('=')[0]: line.split('=')[1].strip('"') for line in lines if '=' in line}
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Failed to read /etc/os-release: {}".format(e))
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def get_os_id(self, os_list):
|
||||||
|
try:
|
||||||
|
os_info = self.get_os_release_info()
|
||||||
|
if not os_info:
|
||||||
|
logging.error("No OS release info found.")
|
||||||
|
return next(os['id'] for os in os_list if os['name'].lower() == "other" or os['name'].lower() == "custom")
|
||||||
|
current_os = f"{os_info.get('NAME', 'Unknown')} {os_info.get('VERSION', '').strip()}".strip().lower()
|
||||||
|
for os_entry in os_list:
|
||||||
|
if current_os in os_entry['name'].lower():
|
||||||
|
return os_entry['id']
|
||||||
|
if 'ubuntu' in current_os:
|
||||||
|
return next(os['id'] for os in os_list if 'ubuntu' in os['name'].lower())
|
||||||
|
elif 'centos' in current_os:
|
||||||
|
return next(os['id'] for os in os_list if 'centos' in os['name'].lower())
|
||||||
|
elif 'fedora' in current_os:
|
||||||
|
return next(os['id'] for os in os_list if 'fedora' in os['name'].lower())
|
||||||
|
return next(os['id'] for os in os_list if os['name'].lower() == "other" or os['name'].lower() == "custom")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Failed to fetch OS ID: {}".format(e))
|
||||||
|
return 27
|
||||||
|
|
||||||
|
|
||||||
def create_post_data(self):
|
def create_post_data(self):
|
||||||
ram, disk = self.get_ram_and_disk()
|
ram, disk = self.get_ram_and_disk()
|
||||||
post_data = {
|
post_data = {
|
||||||
"server_type": 1,
|
"server_type": 1,
|
||||||
"os_id": self.get_os(host, api_key),
|
"os_id": self.get_os(os_list),
|
||||||
"provider_id": 10,
|
"provider_id": 10,
|
||||||
"location_id": 15,
|
"location_id": 15,
|
||||||
"ssh_port": 22,
|
"ssh_port": 22,
|
||||||
|
@ -406,42 +431,21 @@ class ServerManager:
|
||||||
else:
|
else:
|
||||||
return self.create_note(note_data)
|
return self.create_note(note_data)
|
||||||
|
|
||||||
def get_os_release_info(self):
|
def get_os_list(self):
|
||||||
try:
|
try:
|
||||||
with open('/etc/os-release') as f:
|
connection = http.client.HTTPSConnection(self.host)
|
||||||
lines = f.read().splitlines()
|
connection.request("GET", "/api/v1/os", headers=self.headers)
|
||||||
return {line.split('=')[0]: line.split('=')[1].strip('"') for line in lines if '=' in line}
|
response = connection.getresponse()
|
||||||
|
if response.status == 200:
|
||||||
|
os_list = json.loads(response.read().decode('utf-8'))
|
||||||
|
logging.info("OS list fetched successfully")
|
||||||
|
return os_list
|
||||||
|
else:
|
||||||
|
logging.error(f"Failed to fetch OS list: {response.status} {response.reason}")
|
||||||
|
return []
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("Failed to read /etc/os-release: {}".format(e))
|
logging.error(f"Exception occurred while fetching OS list: {e}")
|
||||||
return {}
|
return []
|
||||||
|
|
||||||
def get_os_id(self):
|
|
||||||
try:
|
|
||||||
os_list = self.send_request('GET', '/api/os')
|
|
||||||
os_info = self.get_os_release_info()
|
|
||||||
|
|
||||||
if not os_info:
|
|
||||||
logging.error("No OS release info found.")
|
|
||||||
return next(os['id'] for os in os_list if os['name'].lower() == "other" or os['name'].lower() == "custom")
|
|
||||||
|
|
||||||
current_os = f"{os_info.get('NAME', 'Unknown')} {os_info.get('VERSION', '').strip()}".strip().lower()
|
|
||||||
|
|
||||||
for os_entry in os_list:
|
|
||||||
if current_os in os_entry['name'].lower():
|
|
||||||
return os_entry['id']
|
|
||||||
|
|
||||||
if 'ubuntu' in current_os:
|
|
||||||
return next(os['id'] for os in os_list if 'ubuntu' in os['name'].lower())
|
|
||||||
elif 'centos' in current_os:
|
|
||||||
return next(os['id'] for os in os_list if 'centos' in os['name'].lower())
|
|
||||||
elif 'fedora' in current_os:
|
|
||||||
return next(os['id'] for os in os_list if 'fedora' in os['name'].lower())
|
|
||||||
|
|
||||||
return next(os['id'] for os in os_list if os['name'].lower() == "other" or os['name'].lower() == "custom")
|
|
||||||
except Exception as e:
|
|
||||||
logging.error("Failed to fetch OS IDs: {}".format(e))
|
|
||||||
return 27 # Default to 'Other' if unable to fetch
|
|
||||||
|
|
||||||
def validate_env_vars():
|
def validate_env_vars():
|
||||||
api_key = os.getenv('AGENT_API')
|
api_key = os.getenv('AGENT_API')
|
||||||
host = os.getenv('HOST')
|
host = os.getenv('HOST')
|
||||||
|
@ -464,6 +468,9 @@ def main():
|
||||||
server_id = server_manager.upsert_server(post_data)
|
server_id = server_manager.upsert_server(post_data)
|
||||||
logging.info('Server id: {}'.format(server_id))
|
logging.info('Server id: {}'.format(server_id))
|
||||||
|
|
||||||
|
os_list = server_manager.get_os_list()
|
||||||
|
post_data = server_data.create_post_data(os_list)
|
||||||
|
|
||||||
note_data = server_data.create_note_data()
|
note_data = server_data.create_note_data()
|
||||||
server_manager.upsert_note(note_data, server_id)
|
server_manager.upsert_note(note_data, server_id)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user