forked from Sysdeploy/idlers-agent
refractor
This commit is contained in:
parent
90dcb330a4
commit
4a3222bb84
86
agent.py
86
agent.py
|
@ -234,41 +234,81 @@ class ServerData:
|
||||||
return '127.0.0.1'
|
return '127.0.0.1'
|
||||||
|
|
||||||
def get_os_release_info(self):
|
def get_os_release_info(self):
|
||||||
|
os_release_info = {}
|
||||||
|
|
||||||
|
# reading from /etc/os-release
|
||||||
try:
|
try:
|
||||||
with open('/etc/os-release') as f:
|
with open('/etc/os-release') as f:
|
||||||
lines = f.read().splitlines()
|
lines = f.read().splitlines()
|
||||||
return {line.split('=')[0]: line.split('=')[1].strip('"') for line in lines if '=' in line}
|
os_release_info = {line.split('=')[0]: line.split('=')[1].strip('"') for line in lines if '=' in line}
|
||||||
|
except FileNotFoundError:
|
||||||
|
logging.warning("/etc/os-release not found, trying /etc/VERSION")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("Failed to read /etc/os-release: {}".format(e))
|
logging.error("Failed to read /etc/os-release: {}".format(e))
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
# If /etc/os-release is not found or empty, fallback to /etc/VERSION
|
||||||
|
if not os_release_info:
|
||||||
|
try:
|
||||||
|
with open('/etc/VERSION') as f:
|
||||||
|
lines = f.read().splitlines()
|
||||||
|
for line in lines:
|
||||||
|
if '=' in line:
|
||||||
|
key, value = line.split('=', 1)
|
||||||
|
os_release_info[key.strip()] = value.strip()
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Failed to read /etc/VERSION: {}".format(e))
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return os_release_info
|
||||||
|
|
||||||
|
|
||||||
def get_os_id(self, os_list):
|
def get_os_id(self, os_list):
|
||||||
try:
|
try:
|
||||||
os_info = self.get_os_release_info()
|
os_info = self.get_os_release_info()
|
||||||
if not os_info:
|
if not os_info:
|
||||||
logging.error("No OS release info found.")
|
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")
|
for os_entry in os_list:
|
||||||
current_os = f"{os_info.get('NAME', 'Unknown')} {os_info.get('VERSION', '').strip()}".strip().lower()
|
if os_entry['name'].lower() in ["other", "custom"]:
|
||||||
|
return os_entry['id']
|
||||||
|
return None
|
||||||
|
|
||||||
|
if 'NAME' in os_info and 'VERSION' in os_info:
|
||||||
|
current_os = f"{os_info.get('NAME', 'Unknown')} {os_info.get('VERSION', '').strip()}".strip().lower()
|
||||||
|
else:
|
||||||
|
current_os = f"{os_info.get('os_name', 'Unknown')} {os_info.get('productversion', '').strip()}".strip().lower()
|
||||||
|
|
||||||
for os_entry in os_list:
|
for os_entry in os_list:
|
||||||
if current_os in os_entry['name'].lower():
|
if current_os in os_entry['name'].lower():
|
||||||
return os_entry['id']
|
return os_entry['id']
|
||||||
if 'ubuntu' in current_os:
|
|
||||||
return next(os['id'] for os in os_list if 'ubuntu' in os['name'].lower())
|
# Fallback checks for common OS names if full name doesn't match
|
||||||
elif 'centos' in current_os:
|
for os_entry in os_list:
|
||||||
return next(os['id'] for os in os_list if 'centos' in os['name'].lower())
|
if 'ubuntu' in current_os and 'ubuntu' in os_entry['name'].lower():
|
||||||
elif 'fedora' in current_os:
|
return os_entry['id']
|
||||||
return next(os['id'] for os in os_list if 'fedora' in os['name'].lower())
|
elif 'centos' in current_os and 'centos' in os_entry['name'].lower():
|
||||||
return next(os['id'] for os in os_list if os['name'].lower() == "other" or os['name'].lower() == "custom")
|
return os_entry['id']
|
||||||
|
elif 'fedora' in current_os and 'fedora' in os_entry['name'].lower():
|
||||||
|
return os_entry['id']
|
||||||
|
|
||||||
|
# Default to 'other' or 'custom' if no match found
|
||||||
|
for os_entry in os_list:
|
||||||
|
if os_entry['name'].lower() in ["other", "custom"]:
|
||||||
|
return os_entry['id']
|
||||||
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("Failed to fetch OS ID: {}".format(e))
|
logging.error("Failed to fetch OS ID: {}".format(e))
|
||||||
return 27
|
for os_entry in os_list:
|
||||||
|
if os_entry['name'].lower() in ["other", "custom"]:
|
||||||
|
return os_entry['id']
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
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(os_list),
|
"os_id": self.get_os_id(os_list),
|
||||||
"provider_id": 10,
|
"provider_id": 10,
|
||||||
"location_id": 15,
|
"location_id": 15,
|
||||||
"ssh_port": 22,
|
"ssh_port": 22,
|
||||||
|
@ -431,21 +471,15 @@ class ServerManager:
|
||||||
else:
|
else:
|
||||||
return self.create_note(note_data)
|
return self.create_note(note_data)
|
||||||
|
|
||||||
def get_os_list(self):
|
def get_os_list(self):
|
||||||
try:
|
os_list = self.send_request('GET', '/api/v1/os')
|
||||||
connection = http.client.HTTPSConnection(self.host)
|
if os_list:
|
||||||
connection.request("GET", "/api/v1/os", headers=self.headers)
|
logging.info("OS list fetched successfully")
|
||||||
response = connection.getresponse()
|
return os_list
|
||||||
if response.status == 200:
|
else:
|
||||||
os_list = json.loads(response.read().decode('utf-8'))
|
logging.error("Failed to fetch OS list")
|
||||||
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:
|
|
||||||
logging.error(f"Exception occurred while fetching OS list: {e}")
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
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')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user