forked from Sysdeploy/idlers-agent
Compare commits
2 Commits
fa67103b20
...
f10d0b53b0
Author | SHA1 | Date | |
---|---|---|---|
f10d0b53b0 | |||
3940cbdbde |
59
agent.py
59
agent.py
|
@ -3,6 +3,8 @@ import urllib.request
|
|||
import logging
|
||||
import json
|
||||
import http.client
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
NON_UPDATABLE_KEYS = [
|
||||
'server_type',
|
||||
|
@ -25,10 +27,10 @@ class ServerData:
|
|||
def __init__(self):
|
||||
self.hostname = os.uname().nodename
|
||||
self.public_ip = self.get_public_ip()
|
||||
self.dmidecode_data = parse_dmidecode_output()
|
||||
self.dmidecode_data = self.parse_dmidecode_output()
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
def parse_dmidecode_output():
|
||||
def parse_dmidecode_output(self):
|
||||
'''
|
||||
Example dmidecode output:
|
||||
|
||||
|
@ -177,7 +179,7 @@ class ServerData:
|
|||
processor_model = processor_info[0].get('Version', 'Unknown') if processor_info else 'Unknown'
|
||||
processor_count = len(processor_info)
|
||||
|
||||
note = "Chassis Model: {}, Serial Number: {}\nProcessor Model: {}, Count: {}\nRAM Details:\n{}".format(
|
||||
note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {}".format(
|
||||
chassis_model, chassis_serial, processor_model, processor_count, '\n'.join(['R1', 'R2', 'R3']))
|
||||
|
||||
note_data = {
|
||||
|
@ -244,6 +246,36 @@ class ServerManager:
|
|||
return server['id']
|
||||
return None
|
||||
|
||||
def upsert_server(self, post_data):
|
||||
server_id = self.existing_server_id(post_data)
|
||||
|
||||
if server_id:
|
||||
logging.info('Server already exists with id: {}, Updating...'.format(server_id))
|
||||
response = self.update_server(post_data, server_id)
|
||||
else:
|
||||
logging.info('Server does not exist, Creating...')
|
||||
response = self.create_server(post_data)
|
||||
|
||||
# Extract the server_id from the response
|
||||
server_id = json.loads(response).get('server_id', None)
|
||||
if server_id is None:
|
||||
logging.error('Failed to get server_id from response: {}'.format(response))
|
||||
raise ValueError('Failed to get server_id from response')
|
||||
|
||||
return server_id
|
||||
|
||||
def upsert_note(self, note_data, server_id):
|
||||
note_data['service_id'] = server_id
|
||||
try:
|
||||
note = self.get_note(server_id)
|
||||
except urllib.error.HTTPError:
|
||||
note = None
|
||||
|
||||
if note:
|
||||
return self.update_note(note_data, server_id)
|
||||
else:
|
||||
return self.create_note(note_data)
|
||||
|
||||
def validate_env_vars():
|
||||
api_key = os.getenv('AGENT_API')
|
||||
host = os.getenv('HOST')
|
||||
|
@ -263,26 +295,11 @@ def main():
|
|||
|
||||
server_manager = ServerManager(host, api_key)
|
||||
|
||||
# Check if the server already exists
|
||||
server_id = server_manager.existing_server_id(post_data)
|
||||
|
||||
# If the server exists, update it
|
||||
if server_id:
|
||||
logging.info('Server already exists with id: {}, Updating...'.format(server_id))
|
||||
logging.info(server_manager.update_server(post_data, server_id))
|
||||
else:
|
||||
logging.info('Server does not exist, Creating...')
|
||||
logging.info(server_manager.create_server(post_data))
|
||||
server_id = server_manager.upsert_server(post_data)
|
||||
logging.info('Server id: {}'.format(server_id))
|
||||
|
||||
note_data = server_data.create_note_data(server_id)
|
||||
try:
|
||||
note = server_manager.get_note(server_id)
|
||||
except urllib.error.HTTPError:
|
||||
note = None
|
||||
if note:
|
||||
server_manager.update_note(note_data, server_id)
|
||||
else:
|
||||
server_manager.create_note(note_data)
|
||||
server_manager.upsert_note(note_data, server_id)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user