getting os details with /api/os #22
80
agent.py
|
@ -241,16 +241,77 @@ 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):
|
||||||
os_id = 27
|
os_release_info = {}
|
||||||
logging.info("OS ID: {}".format(os_id))
|
|
||||||
return os_id
|
# reading from /etc/os-release
|
||||||
|
try:
|
||||||
|
with open('/etc/os-release') as f:
|
||||||
|
lines = f.read().splitlines()
|
||||||
|
os_release_info = {line.split('=')[0]: line.split('=')[1].strip('"') for line in lines if '=' in line}
|
||||||
swapnil marked this conversation as resolved
Outdated
|
|||||||
|
except FileNotFoundError:
|
||||||
|
logging.warning("/etc/os-release not found, trying /etc/VERSION")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Failed to read /etc/os-release: {}".format(e))
|
||||||
|
return {}
|
||||||
|
|
||||||
def create_post_data(self):
|
# 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:
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
Instead of returning a hard-coded nnumber, we'll return "other" or "custom" id Instead of returning a hard-coded nnumber, we'll return "other" or "custom" id
|
|||||||
|
key, value = line.split('=', 1)
|
||||||
|
os_release_info[key.strip()] = value.strip()
|
||||||
|
except Exception as e:
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
Missing parameter for os list Missing parameter for os list
swapnil
commented
Why is almost entry method is wrapped up in try-except? Please list down the cases where you think it'll break inside the Why is almost entry method is wrapped up in try-except? Please list down the cases where you think it'll break inside the `try` and this try-except will be useful.
|
|||||||
|
logging.error("Failed to read /etc/VERSION: {}".format(e))
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return os_release_info
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
I think you mean I think you mean `get_os_id`
|
|||||||
|
|
||||||
|
|
||||||
|
def get_os_id(self, os_list):
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
os id cannot be none, so at least this method should never return a None. Also, this case should never practically happen, even if it happens, you can return 1. os id cannot be none, so at least this method should never return a None. Also, this case should never practically happen, even if it happens, you can return 1.
|
|||||||
|
os_info = self.get_os_release_info()
|
||||||
|
|
||||||
|
if not os_info:
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
It's still not using It's still not using `id` and `versionId`
swapnil
commented
@shailaja We want to use @shailaja We want to use `ID` and `VERSION_ID` instead of `NAME` and `VERSION`, then you don't need to `lower()` that.
swapnil
commented
You also need to modify this line to use ID and VERSION_ID.
You also need to modify this line to use ID and VERSION_ID.
```python
current_os = f"{os_info.get('NAME', 'Unknown')} {os_info.get('VERSION', '').strip()}".strip().lower()
```
swapnil
commented
Also we should refrain from using f-string anywhere in the new code. Also we should refrain from using f-string anywhere in the new code.
|
|||||||
|
logging.error("No OS release info found.")
|
||||||
|
for os_entry in os_list:
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
You missed a You missed a `+` after `" "`
|
|||||||
|
if os_entry['name'].lower() in ["other", "custom"]:
|
||||||
|
return os_entry['id']
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if 'ID' in os_info and 'VERSION_ID' in os_info:
|
||||||
|
current_os = (os_info.get('ID', 'Unknown') + " " + os_info.get('VERSION_ID', '').strip()).strip()
|
||||||
|
else:
|
||||||
|
current_os = (os_info.get('os_name', 'Unknown') +" "+ os_info.get('productversion', '').strip()).strip().lower()
|
||||||
|
|
||||||
|
for os_entry in os_list:
|
||||||
|
if current_os in os_entry['name'].lower():
|
||||||
|
return os_entry['id']
|
||||||
|
|
||||||
|
# Fallback checks for common OS names if full name doesn't match
|
||||||
|
for os_entry in os_list:
|
||||||
|
if 'ubuntu' in current_os and 'ubuntu' in os_entry['name'].lower():
|
||||||
|
return os_entry['id']
|
||||||
|
elif 'centos' in current_os and 'centos' in os_entry['name'].lower():
|
||||||
|
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"]:
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
`get_os_list` method does not exist in the `ServerData` class. You need to take `os_list` as an argument to `create_post_data`
|
|||||||
|
return os_entry['id']
|
||||||
|
return 1
|
||||||
|
|
||||||
swapnil marked this conversation as resolved
Outdated
swapnil
commented
Missing parameter for os list. Missing parameter for os list.
|
|||||||
|
|
||||||
|
|
||||||
|
def create_post_data(self, os_list):
|
||||||
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_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,
|
||||||
|
@ -418,6 +479,11 @@ class ServerManager:
|
||||||
return self.update_note(note_data, server_id)
|
return self.update_note(note_data, server_id)
|
||||||
else:
|
else:
|
||||||
return self.create_note(note_data)
|
return self.create_note(note_data)
|
||||||
|
|
||||||
|
def get_os_list(self):
|
||||||
|
os_list = self.send_request('GET', '/api/os')
|
||||||
|
logging.info("OS list fetched successfully") if os_list else logging.error("Failed to fetch OS list")
|
||||||
|
return os_list or []
|
||||||
|
|
||||||
def validate_env_vars():
|
def validate_env_vars():
|
||||||
api_key = os.getenv('AGENT_API')
|
api_key = os.getenv('AGENT_API')
|
||||||
|
@ -441,6 +507,8 @@ 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()
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
As discussed, we should use a combination of
id
andversionId
. Check the exact key name in the file.