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
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Filter import Filter
11 from PLC.Table import Row, Table
12 from PLC.Nodes import Node, Nodes
13 from PLC.NodeGroups import NodeGroup, NodeGroups
17 Representation of a row in the conf_files table. To use,
18 instantiate with a dict of values.
21 table_name = 'conf_files'
22 primary_key = 'conf_file_id'
23 join_tables = ['conf_file_node', 'conf_file_nodegroup']
25 'conf_file_id': Parameter(int, "Configuration file identifier"),
26 'enabled': Parameter(bool, "Configuration file is active"),
27 'source': Parameter(str, "Relative path on the boot server where file can be downloaded", max = 255),
28 'dest': Parameter(str, "Absolute path where file should be installed", max = 255),
29 'file_permissions': Parameter(str, "chmod(1) permissions", max = 20),
30 'file_owner': Parameter(str, "chown(1) owner", max = 50),
31 'file_group': Parameter(str, "chgrp(1) owner", max = 50),
32 'preinstall_cmd': Parameter(str, "Shell command to execute prior to installing", max = 1024, nullok = True),
33 'postinstall_cmd': Parameter(str, "Shell command to execute after installing", max = 1024, nullok = True),
34 'error_cmd': Parameter(str, "Shell command to execute if any error occurs", max = 1024, nullok = True),
35 'ignore_cmd_errors': Parameter(bool, "Install file anyway even if an error occurs"),
36 'always_update': Parameter(bool, "Always attempt to install file even if unchanged"),
37 'node_ids': Parameter(int, "List of nodes linked to this file"),
38 'nodegroup_ids': Parameter(int, "List of node groups linked to this file"),
41 def add_node(self, node, commit = True):
43 Add configuration file to node.
46 assert 'conf_file_id' in self
47 assert isinstance(node, Node)
48 assert 'node_id' in node
50 conf_file_id = self['conf_file_id']
51 node_id = node['node_id']
53 if node_id not in self['node_ids']:
54 self.api.db.do("INSERT INTO conf_file_node (conf_file_id, node_id)" \
55 " VALUES(%(conf_file_id)d, %(node_id)d)",
61 self['node_ids'].append(node_id)
62 node['conf_file_ids'].append(conf_file_id)
64 def remove_node(self, node, commit = True):
66 Remove configuration file from node.
69 assert 'conf_file_id' in self
70 assert isinstance(node, Node)
71 assert 'node_id' in node
73 conf_file_id = self['conf_file_id']
74 node_id = node['node_id']
76 if node_id in self['node_ids']:
77 self.api.db.do("DELETE FROM conf_file_node" \
78 " WHERE conf_file_id = %(conf_file_id)d" \
79 " AND node_id = %(node_id)d",
85 self['node_ids'].remove(node_id)
86 node['conf_file_ids'].remove(conf_file_id)
88 def add_nodegroup(self, nodegroup, commit = True):
90 Add configuration file to node group.
93 assert 'conf_file_id' in self
94 assert isinstance(nodegroup, NodeGroup)
95 assert 'nodegroup_id' in nodegroup
97 conf_file_id = self['conf_file_id']
98 nodegroup_id = nodegroup['nodegroup_id']
100 if nodegroup_id not in self['nodegroup_ids']:
101 self.api.db.do("INSERT INTO conf_file_nodegroup (conf_file_id, nodegroup_id)" \
102 " VALUES(%(conf_file_id)d, %(nodegroup_id)d)",
108 self['nodegroup_ids'].append(nodegroup_id)
109 nodegroup['conf_file_ids'].append(conf_file_id)
111 def remove_nodegroup(self, nodegroup, commit = True):
113 Remove configuration file from node group.
116 assert 'conf_file_id' in self
117 assert isinstance(nodegroup, NodeGroup)
118 assert 'nodegroup_id' in nodegroup
120 conf_file_id = self['conf_file_id']
121 nodegroup_id = nodegroup['nodegroup_id']
123 if nodegroup_id in self['nodegroup_ids']:
124 self.api.db.do("DELETE FROM conf_file_nodegroup" \
125 " WHERE conf_file_id = %(conf_file_id)d" \
126 " AND nodegroup_id = %(nodegroup_id)d",
132 self['nodegroup_ids'].remove(nodegroup_id)
133 nodegroup['conf_file_ids'].remove(conf_file_id)
135 class ConfFiles(Table):
137 Representation of the conf_files table in the database.
140 def __init__(self, api, conf_file_filter = None, columns = None):
141 Table.__init__(self, api, ConfFile, columns)
143 sql = "SELECT %s FROM view_conf_files WHERE True" % \
144 ", ".join(self.columns)
146 if conf_file_filter is not None:
147 if isinstance(conf_file_filter, (list, tuple, set, int, long)):
148 conf_file_filter = Filter(ConfFile.fields, {'conf_file_id': conf_file_filter})
149 elif isinstance(conf_file_filter, dict):
150 conf_file_filter = Filter(ConfFile.fields, conf_file_filter)
151 sql += " AND (%s) %s" % conf_file_filter.sql(api)