def insert_hardware(self, time, data): log("Inserting hardware:", data) query = "INSERT INTO hardware (time, machine_id, component, hw_id, utilisation, temperature, power_consumption) VALUES(%s, %s, %s, %s, %s, %s, %s)" values = (time, settings.VAST_MACHINE_ID, data['component'], data['hw_id'], data['utilisation'], data['temperature'], data['power_consumption']) self.exec(query, values)
def insert_machine(self, time, data): log("Inserting machine:", data) query = "INSERT INTO machine (time, machine_id, account_credit, reliability, rentals_stored, rentals_on_demand, rentals_bid) VALUES(%s, %s, %s, %s, %s, %s, %s)" values = (time, settings.VAST_MACHINE_ID, data['account_credit'], data['reliability'], data['rentals_stored'], data['rentals_on_demand'], data['rentals_bid']) self.exec(query, values)
def get_account(self): data, response = self._get("/users/0/invoices/") if data == None: log("Failed to get account", response.status_code) return return models.Account(**data)
def _get(self, path): path = self._build_url(path) response = self.session.get(path) log("GET request to:", path) if response.status_code != 200: log("Error making GET request to:", path) return None, response return response.json(), response
def cookies(self): if not self.__cookies: try: with open(self.cookie_path, "r") as file: data = file.read() self.cookies = json.loads(data) return self.__cookies except: log("No cookie file found") else: return self.__cookies
def _post(self, path, data, method="POST"): path = self._build_url(path) data = json.dumps(data) log(method, "request to:", path) if method == "POST": response = self.session.post(path, data=data) if method == "PUT": response = self.session.put(path, data=data) if response.status_code != 200: log("Error making " + method + " to:", path, "Response:", response.text) return None, response return response.json(), response
def get_machine(self, id): data, response = self._get("/machines/") if response.status_code != 200: return log("Failed to get machine", response.text) for item in data['machines']: if item['id'] == id: return models.Machine(**item)
def _authenticate(self): log("Authenticating user:"******"pass:"******"Session exists in cache") return else: log("Sesssion expired") data, response = self._post("/users/current/", { "username": self.username, "password": self.password, }, method="PUT") if response.status_code == 200: log("Succesfully logged in") self.user = models.User(**data) self.cookies = response.cookies.get_dict() else: log("Error authenticating user", response.status_code)
import sys import settings import os import time from lib.sys import log if __name__ == '__main__': param = sys.argv[1] log("Started with action:", settings.args.action) if settings.args.action == "account": from crons import account account.run() elif settings.args.action == "system": from crons import system system.run() elif settings.args.action == "event": from crons import event event.run() else: print("None found")
def insert_instance(self, time, data): log("Inserting instance:", data) query = "INSERT INTO instance (time, machine_id, instance_id, earning) VALUES(%s, %s, %s, %s)" values = (time, settings.VAST_MACHINE_ID, data['instance_id'], data['earning']) self.exec(query, values)
def insert_event(self, data): log("Inserting event:", data) query = "INSERT INTO event (time, machine_id, name, val1, val2, val3, val4) VALUES(%s, %s, %s, %s, %s, %s, %s)" values = (data['time'], settings.VAST_MACHINE_ID, data['name'], data['val1'], data['val2'], data['val3'], data['val4']) self.exec(query, values)