2 # Functions for interacting with the conf_files table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: ConfFiles.py,v 1.5 2006/10/25 14:29:13 mlhuang Exp $
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13 from PLC.Nodes import Node, Nodes
14 from PLC.NodeGroups import NodeGroup, NodeGroups
18 Representation of a row in the conf_files table. To use,
19 instantiate with a dict of values.
22 table_name = 'conf_files'
23 primary_key = 'conf_file_id'
24 join_tables = ['conf_file_node', 'conf_file_nodegroup']
26 'conf_file_id': Parameter(int, "Configuration file identifier"),
27 'enabled': Parameter(bool, "Configuration file is active"),
28 'source': Parameter(str, "Relative path on the boot server where file can be downloaded", max = 255),
29 'dest': Parameter(str, "Absolute path where file should be installed", max = 255),
30 'file_permissions': Parameter(str, "chmod(1) permissions", max = 20),
31 'file_owner': Parameter(str, "chown(1) owner", max = 50),
32 'file_group': Parameter(str, "chgrp(1) owner", max = 50),
33 'preinstall_cmd': Parameter(str, "Shell command to execute prior to installing", max = 1024, nullok = True),
34 'postinstall_cmd': Parameter(str, "Shell command to execute after installing", max = 1024, nullok = True),
35 'error_cmd': Parameter(str, "Shell command to execute if any error occurs", max = 1024, nullok = True),
36 'ignore_cmd_errors': Parameter(bool, "Install file anyway even if an error occurs"),
37 'always_update': Parameter(bool, "Always attempt to install file even if unchanged"),
38 'node_ids': Parameter(int, "List of nodes linked to this file", ro = True),
39 'nodegroup_ids': Parameter(int, "List of node groups linked to this file", ro = True),
42 def add_node(self, node, commit = True):
44 Add configuration file to node.
47 assert 'conf_file_id' in self
48 assert isinstance(node, Node)
49 assert 'node_id' in node
51 conf_file_id = self['conf_file_id']
52 node_id = node['node_id']
54 if node_id not in self['node_ids']:
55 self.api.db.do("INSERT INTO conf_file_node (conf_file_id, node_id)" \
56 " VALUES(%(conf_file_id)d, %(node_id)d)",
62 self['node_ids'].append(node_id)
63 node['conf_file_ids'].append(conf_file_id)
65 def remove_node(self, node, commit = True):
67 Remove configuration file from node.
70 assert 'conf_file_id' in self
71 assert isinstance(node, Node)
72 assert 'node_id' in node
74 conf_file_id = self['conf_file_id']
75 node_id = node['node_id']
77 if node_id in self['node_ids']:
78 self.api.db.do("DELETE FROM conf_file_node" \
79 " WHERE conf_file_id = %(conf_file_id)d" \
80 " AND node_id = %(node_id)d",
86 self['node_ids'].remove(node_id)
87 node['conf_file_ids'].remove(conf_file_id)
89 def add_nodegroup(self, nodegroup, commit = True):
91 Add configuration file to node group.
94 assert 'conf_file_id' in self
95 assert isinstance(nodegroup, NodeGroup)
96 assert 'nodegroup_id' in nodegroup
98 conf_file_id = self['conf_file_id']
99 nodegroup_id = nodegroup['nodegroup_id']
101 if nodegroup_id not in self['nodegroup_ids']:
102 self.api.db.do("INSERT INTO conf_file_nodegroup (conf_file_id, nodegroup_id)" \
103 " VALUES(%(conf_file_id)d, %(nodegroup_id)d)",
109 self['nodegroup_ids'].append(nodegroup_id)
110 nodegroup['conf_file_ids'].append(conf_file_id)
112 def remove_nodegroup(self, nodegroup, commit = True):
114 Remove configuration file from node group.
117 assert 'conf_file_id' in self
118 assert isinstance(nodegroup, NodeGroup)
119 assert 'nodegroup_id' in nodegroup
121 conf_file_id = self['conf_file_id']
122 nodegroup_id = nodegroup['nodegroup_id']
124 if nodegroup_id in self['nodegroup_ids']:
125 self.api.db.do("DELETE FROM conf_file_nodegroup" \
126 " WHERE conf_file_id = %(conf_file_id)d" \
127 " AND nodegroup_id = %(nodegroup_id)d",
133 self['nodegroup_ids'].remove(nodegroup_id)
134 nodegroup['conf_file_ids'].remove(conf_file_id)
136 class ConfFiles(Table):
138 Representation of the conf_files table in the database.
141 def __init__(self, api, conf_file_ids = None):
142 sql = "SELECT %s FROM view_conf_files" % \
143 ", ".join(ConfFile.fields)
146 # Separate the list into integers and strings
147 sql += " WHERE conf_file_id IN (%s)" % ", ".join(map(str, api.db.quote(conf_file_ids)))
149 rows = api.db.selectall(sql)
152 self[row['conf_file_id']] = ConfFile(api, row)
156 self[row['conf_file_id']] = conf_file = ConfFile(api, row)
157 for aggregate in ['node_ids', 'nodegroup_ids']:
158 if not conf_file.has_key(aggregate) or conf_file[aggregate] is None:
159 conf_file[aggregate] = []
161 conf_file[aggregate] = map(int, conf_file[aggregate].split(','))