Compare commits

..

No commits in common. "61909258a0fbbc8fb11cd1e11498fb4a2f4ae85b" and "52ecd2dc531a05c8fe37c53bb83d192ea78a8ecf" have entirely different histories.

View File

@ -3,8 +3,6 @@ import urllib.request
import logging
import json
import http.client
import subprocess
import re
NON_UPDATABLE_KEYS = [
'server_type',
@ -27,10 +25,10 @@ class ServerData:
def __init__(self):
self.hostname = os.uname().nodename
self.public_ip = self.get_public_ip()
self.dmidecode_data = self.parse_dmidecode_output()
self.dmidecode_data = parse_dmidecode_output()
logging.basicConfig(level=logging.INFO)
def parse_dmidecode_output(self):
def parse_dmidecode_output():
'''
Example dmidecode output:
@ -163,40 +161,6 @@ class ServerData:
logging.info("Post data created")
return post_data
def create_note_data(self):
chassis_info = None
for section in self.dmidecode_data:
if section['DMIType'] == 1:
chassis_info = section
break
if chassis_info:
chassis_model = chassis_info.get('Product Name', 'Unknown')
chassis_serial = chassis_info.get('Serial Number', 'Unknown')
else:
chassis_model = chassis_serial = 'Unknown'
processor_info = [section for section in self.dmidecode_data if section['DMIType'] == 4]
processor_model = processor_info[0].get('Version', 'Unknown') if processor_info else 'Unknown'
processor_count = len(processor_info)
ram_info = [section for section in self.dmidecode_data if section['DMIType'] == 17]
ram_details = []
for ram in ram_info:
size = ram.get('Size', 'Unknown')
speed = ram.get('Speed', 'Unknown')
ecc = 'Yes' if ram.get('Total Width') == '72 bits' and ram.get('Data Width') == '64 bits' else 'No'
serial_number = ram.get('Serial Number', 'Unknown')
ram_type = ram.get('Type', 'Unknown')
ram_details.append("Size: {}, Speed: {}, ECC: {}, Serial Number: {}, Type: {}".format(size, speed, ecc, serial_number, ram_type))
note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {}".format(
chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details))
note_data = {
'note': note,
}
return note_data
class ServerManager:
def __init__(self, host, api_key):
@ -255,36 +219,6 @@ 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')
@ -304,11 +238,29 @@ def main():
server_manager = ServerManager(host, api_key)
server_id = server_manager.upsert_server(post_data)
logging.info('Server id: {}'.format(server_id))
# Check if the server already exists
server_id = server_manager.existing_server_id(post_data)
note_data = server_data.create_note_data()
server_manager.upsert_note(note_data, server_id)
# 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))
note_data = {
'service_id': server_id,
'note': 'Bla bla bla'
}
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)
if __name__ == '__main__':
main()