class Datacenter(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), unique=True) location = db.Column(db.String(20)) latitude = db.Column(db.String(20)) longitude = db.Column(db.String(20)) def __init__(self, name, location): self.name = name self.location = location def create(self): db.session.add(self) db.session.commit() @staticmethod def get(id): return Datacenter.query.filter_by(id=id).first() def serialize(self): return { 'id': self.id, 'name': self.name, 'location': self.location, 'latitude': self.latitude, 'longitude': self.longitude }
class NodeStatus(db.Model): STATUS_ONLINE = 'online' STATUS_OFFLINE = 'offline' id = db.Column(db.Integer, primary_key=True) node_id = db.Column(db.ForeignKey(Node.id)) last_update = db.Column(db.DateTime, default=datetime.datetime.now()) imposed_status = db.Column(db.String(20)) current_status = db.Column(db.String(20)) def __init__(self, node_id, imposed_status): self.node_id = node_id self.imposed_status = imposed_status def create(self): db.session.add(self) db.session.commit() def serialize(self): return { 'id': self.id, 'node_id': self.node_id, 'last_update': str(self.last_update), 'imposed_status': self.imposed_status, 'current_status': self.current_status }
class NodeIP(db.Model): id = db.Column(db.Integer, primary_key=True) node_id = db.Column(db.ForeignKey(Node.id)) ipv = db.Column(db.Integer) ip = db.Column(db.String(46)) netmask = db.Column(db.Integer) def __init__(self, node_id, ipv, ip, netmask): self.node_id = node_id self.ipv = ipv self.ip = ip self.netmask = netmask def create(self): db.session.add(self) db.session.commit() def serialize(self): return { 'id': self.id, 'node_id': self.node_id, 'ipv': self.ipv, 'ip': self.ip, 'netmask': self.netmask }
class Project(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.ForeignKey(User.id)) name = db.Column(db.String(100)) def __init__(self, user_id, name): self.user_id = user_id self.name = name def create(self): db.session.add(self) db.session.commit() def add_node(self, node_id): node = Node.get(node_id) if not node: raise ValueError("Node with id %d does not exist" % node_id) link = ResourceNodeLink(self.id, node_id) link.create() @staticmethod def get(id): return Project.query.filter_by(id=id).first() def serialize(self): nodes_links = ResourceNodeLink.query.filter_by( project_id=self.id).all() return { 'id': self.id, 'user_id': self.user_id, 'name': self.name, 'nodes_ids': [link.node_id for link in nodes_links] }
class Node(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) type = db.Column(db.String(20)) datacenter_id = db.Column(db.ForeignKey(Datacenter.id)) def __init__(self, name, type, datacenter_id): self.name = name self.type = type datacenter = Datacenter.query.filter_by(id=datacenter_id).first() if not datacenter: raise ValueError("Datacenter with id %d does not exist" % datacenter_id) self.datacenter_id = datacenter_id def create(self): db.session.add(self) db.session.commit() @staticmethod def get(id): return Node.query.filter_by(id=id).first() def add_ip(self, ipv, ip, netmask): node_ip = NodeIP(self.id, ipv, ip, netmask) node_ip.create() def serialize(self): ips = NodeIP.query.filter_by(node_id=self.id).all() status = NodeStatus.query.filter_by(node_id=self.id).order_by( desc(NodeStatus.last_update)).first() return { 'id': self.id, 'name': self.name, 'type': self.type, 'datacenter_id': self.datacenter_id, 'ips': [ip.serialize() for ip in ips], 'status': (lambda status: status.serialize() if status else None)(status) }
class ResourceNodeLink(db.Model): id = db.Column(db.Integer, primary_key=True) project_id = db.Column(db.ForeignKey(Project.id)) node_id = db.Column(db.ForeignKey(Node.id)) def __init__(self, project_id, node_id): project = Project.get(project_id) if not project: raise ValueError("Project with id %d does not exist" % project_id) node = Node.get(node_id) if not node: raise ValueError("Node with id %d does not exist" % node_id) self.project_id = project_id self.node_id = node_id def create(self): db.session.add(self) db.session.commit() def serialize(self): return { 'id': self.id, 'project_id': self.project_id, 'node_id': self.node_id } def serialize_node(self): node = Node.query.filter_by(id=self.node_id).first() return node.serialize() def get_node(self): node = Node.query.filter_by(id=self.node_id).first() return node def serialize_project(self): project = Project.query.filter_by(id=self.project_id).first() return project.serialize() def get_project(self): project = Project.query.filter_by(id=self.project_id).first() return project
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(15), unique=True) def __init__(self, username): self.username = username def create(self): db.session.add(self) db.session.commit() @staticmethod def get(id): return User.query.filter_by(id=id).first() def serialize(self): return { 'id': self.id, 'username': self.username }
class NodeCommand(db.Model): from modules.project.models import ProjectCommandJob STATUS_SENDING = 'sending' STATUS_RECEIVED = 'received' STATUS_PENDING = 'pending' STATUS_COMPLETED = 'completed' STATUS_ERROR = 'error' STATUS_FAILED = 'failed' id = db.Column(db.Integer, primary_key=True) add_date = db.Column(db.DateTime, default=datetime.datetime.now()) node_id = db.Column(db.ForeignKey(Node.id)) project_command_job_id = db.Column(db.ForeignKey(ProjectCommandJob.id)) response = db.Column(db.String(5000)) status = db.Column(db.String(100), default=STATUS_SENDING) def serialize(self): return { 'id': self.id, 'add_date': str(self.add_date), 'node_id': self.node_id, 'project_command_job_id': self.project_command_job_id, 'response': self.response, 'status': self.status }
class ProjectCommandJob(db.Model): id = db.Column(db.Integer, primary_key=True) add_date = db.Column(db.DateTime, default=datetime.datetime.now()) project_id = db.Column(db.ForeignKey(Project.id)) propagated = db.Column(db.Boolean, default=False) propagation_date = db.Column(db.DateTime) cmd = db.Column(db.String(5000)) def __init__(self, project_id, cmd): project = Project.get(project_id) if not project: raise ValueError("Project with id %d does not exist" % project_id) self.project_id = project_id self.cmd = cmd def create(self): db.session.add(self) db.session.commit() def serialize(self): return { 'id': self.id, 'add_date': str(self.add_date), 'project_id': self.project_id, 'propagated': self.propagated, 'propagation_date': str(self.propagation_date), 'cmd': self.cmd } def propagate(self): from modules.node.models import NodeCommand project = Project.query.filter_by(id=self.project_id).first() nodes_links = ResourceNodeLink.query.filter_by( project_id=self.project_id).all() nodes = [link.get_node() for link in nodes_links] for node in nodes: node_command = NodeCommand() node_command.node_id = node.id node_command.project_command_job_id = self.id db.session.add(node_command) self.propagated = True self.propagation_date = datetime.datetime.now() db.session.commit()