2 # Functions for interacting with the nodegroups table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: NodeGroups.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $
10 from types import StringTypes
12 from PLC.Faults import *
13 from PLC.Parameter import Parameter
14 from PLC.Debug import profile
15 from PLC.Table import Row, Table
16 from PLC.Nodes import Node, Nodes
20 Representation of a row in the nodegroups table. To use, optionally
21 instantiate with a dict of values. Update as you would a
22 dict. Commit to the database with flush().
26 'nodegroup_id': Parameter(int, "Node group identifier"),
27 'name': Parameter(str, "Node group name"),
28 'description': Parameter(str, "Node group description"),
29 'is_custom': Parameter(bool, "Is a custom node group (i.e., is not a site node group)")
32 # These fields are derived from join tables and are not
33 # actually in the nodegroups table.
35 'node_ids': Parameter([int], "List of nodes in this node group"),
38 def __init__(self, api, fields):
39 Row.__init__(self, fields)
42 def validate_name(self, name):
43 conflicts = NodeGroups(self.api, [name])
44 for nodegroup_id in conflicts:
45 if 'nodegroup_id' not in self or self['nodegroup_id'] != nodegroup_id:
46 raise PLCInvalidArgument, "Node group name already in use"
48 def add_node(self, node, commit = True):
50 Add existing node to specified nodegroup.
53 assert 'nodegroup_id' in self
54 assert isinstance(node, Node)
55 assert 'node_id' in node
57 node_id = node['node_id']
58 nodegroup_id = self['nodegroup_id']
59 self.api.db.do("INSERT INTO nodegroup_nodes (nodegroup_id, node_id)" \
60 " VALUES(%(nodegroup_id)d, %(node_id)d)",
66 def flush(self, commit = True):
68 Flush changes back to the database.
73 # Fetch a new nodegroup_id if necessary
74 if 'nodegroup_id' not in self:
75 rows = self.api.db.selectall("SELECT NEXTVAL('nodegroups_nodegroup_id_seq') AS nodegroup_id")
77 raise PLCDBError, "Unable to fetch new nodegroup_id"
78 self['nodegroup_id'] = rows[0]['nodegroup_id']
83 # Filter out fields that cannot be set or updated directly
84 fields = dict(filter(lambda (key, value): key in self.fields,
87 # Parameterize for safety
89 values = [self.api.db.param(key, value) for (key, value) in fields.items()]
92 # Insert new row in nodegroups table
93 sql = "INSERT INTO nodegroups (%s) VALUES (%s)" % \
94 (", ".join(keys), ", ".join(values))
96 # Update existing row in sites table
97 columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
98 sql = "UPDATE nodegroups SET " + \
99 ", ".join(columns) + \
100 " WHERE nodegroup_id = %(nodegroup_id)d"
102 self.api.db.do(sql, fields)
107 def delete(self, commit = True):
109 Delete existing nodegroup from the database.
112 assert 'nodegroup_id' in self
115 tables = ['nodegroup_nodes', 'override_bootscripts',
116 'conf_assoc', 'node_root_access']
118 if self['is_custom']:
119 tables.append('nodegroups')
121 # XXX Cannot delete site node groups yet
125 self.api.db.do("DELETE FROM %s" \
126 " WHERE nodegroup_id = %(nodegroup_id)" % \
132 class NodeGroups(Table):
134 Representation of row(s) from the nodegroups table in the
138 def __init__(self, api, nodegroup_id_or_name_list = None):
141 # N.B.: Node IDs returned may be deleted.
142 sql = "SELECT nodegroups.*, nodegroup_nodes.node_id" \
144 " LEFT JOIN nodegroup_nodes USING (nodegroup_id)"
146 if nodegroup_id_or_name_list:
147 # Separate the list into integers and strings
148 nodegroup_ids = filter(lambda nodegroup_id: isinstance(nodegroup_id, (int, long)),
149 nodegroup_id_or_name_list)
150 names = filter(lambda name: isinstance(name, StringTypes),
151 nodegroup_id_or_name_list)
152 sql += " WHERE (False"
154 sql += " OR nodegroup_id IN (%s)" % ", ".join(map(str, nodegroup_ids))
156 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names)).lower()
159 rows = self.api.db.selectall(sql)
161 if self.has_key(row['nodegroup_id']):
162 nodegroup = self[row['nodegroup_id']]
163 nodegroup.update(row)
165 self[row['nodegroup_id']] = NodeGroup(api, row)