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.Nodes import Node, Nodes
11 from PLC.NodeGroups import NodeGroup, NodeGroups
12 from PLC.Storage.AlchemyObject import AlchemyObj
14 class ConfFile(AlchemyObj):
16 Representation of a row in the conf_files table. To use,
17 instantiate with a dict of values.
20 tablename = 'conf_files'
21 join_tables = ['conf_file_node', 'conf_file_nodegroup']
23 'conf_file_id': Parameter(int, "Configuration file identifier", primary_key=True),
24 'enabled': Parameter(bool, "Configuration file is active"),
25 'source': Parameter(str, "Relative path on the boot server where file can be downloaded", max = 255),
26 'dest': Parameter(str, "Absolute path where file should be installed", max = 255),
27 'file_permissions': Parameter(str, "chmod(1) permissions", max = 20),
28 'file_owner': Parameter(str, "chown(1) owner", max = 50),
29 'file_group': Parameter(str, "chgrp(1) owner", max = 50),
30 'preinstall_cmd': Parameter(str, "Shell command to execute prior to installing", max = 1024, nullok = True),
31 'postinstall_cmd': Parameter(str, "Shell command to execute after installing", max = 1024, nullok = True),
32 'error_cmd': Parameter(str, "Shell command to execute if any error occurs", max = 1024, nullok = True),
33 'ignore_cmd_errors': Parameter(bool, "Install file anyway even if an error occurs"),
34 'always_update': Parameter(bool, "Always attempt to install file even if unchanged"),
35 'node_ids': Parameter(int, "List of nodes linked to this file", joined=True),
36 'nodegroup_ids': Parameter(int, "List of node groups linked to this file", joined=True),
39 def add_node(self, node, commit = True):
41 Add configuration file to node.
44 assert 'conf_file_id' in self
45 assert isinstance(node, Node)
46 assert 'node_id' in node
48 conf_file_id = self['conf_file_id']
49 node_id = node['node_id']
51 if node_id not in self['node_ids']:
52 self.api.db.do("INSERT INTO conf_file_node (conf_file_id, node_id)" \
53 " VALUES(%(conf_file_id)d, %(node_id)d)",
59 self['node_ids'].append(node_id)
60 node['conf_file_ids'].append(conf_file_id)
62 def remove_node(self, node, commit = True):
64 Remove configuration file from node.
67 assert 'conf_file_id' in self
68 assert isinstance(node, Node)
69 assert 'node_id' in node
71 conf_file_id = self['conf_file_id']
72 node_id = node['node_id']
74 if node_id in self['node_ids']:
75 self.api.db.do("DELETE FROM conf_file_node" \
76 " WHERE conf_file_id = %(conf_file_id)d" \
77 " AND node_id = %(node_id)d",
83 self['node_ids'].remove(node_id)
84 node['conf_file_ids'].remove(conf_file_id)
86 def add_nodegroup(self, nodegroup, commit = True):
88 Add configuration file to node group.
91 assert 'conf_file_id' in self
92 assert isinstance(nodegroup, NodeGroup)
93 assert 'nodegroup_id' in nodegroup
95 conf_file_id = self['conf_file_id']
96 nodegroup_id = nodegroup['nodegroup_id']
98 if nodegroup_id not in self['nodegroup_ids']:
99 self.api.db.do("INSERT INTO conf_file_nodegroup (conf_file_id, nodegroup_id)" \
100 " VALUES(%(conf_file_id)d, %(nodegroup_id)d)",
106 self['nodegroup_ids'].append(nodegroup_id)
107 nodegroup['conf_file_ids'].append(conf_file_id)
109 def remove_nodegroup(self, nodegroup, commit = True):
111 Remove configuration file from node group.
114 assert 'conf_file_id' in self
115 assert isinstance(nodegroup, NodeGroup)
116 assert 'nodegroup_id' in nodegroup
118 conf_file_id = self['conf_file_id']
119 nodegroup_id = nodegroup['nodegroup_id']
121 if nodegroup_id in self['nodegroup_ids']:
122 self.api.db.do("DELETE FROM conf_file_nodegroup" \
123 " WHERE conf_file_id = %(conf_file_id)d" \
124 " AND nodegroup_id = %(nodegroup_id)d",
130 self['nodegroup_ids'].remove(nodegroup_id)
131 nodegroup['conf_file_ids'].remove(conf_file_id)
133 def sync(self, commit=True, validate=True):
134 AlchemyObj.sync(self, commit, validate)
135 if 'conf_file_id' not in self:
136 AlchemyObj.insert(self, dict(self))
138 AlchemyObj.update(self, {'conf_file_id': self['conf_file_id']}, dict(self))
140 def delete(self, commit=True):
141 assert 'conf_file_id' in self
142 AlchemyObj.delete(self, dict(self))
144 class ConfFiles(list):
146 Representation of the conf_files table in the database.
149 def __init__(self, api, conf_file_filter = None, columns = None):
150 if not conf_file_filter:
151 conf_files = ConfFile().select()
152 elif isinstance(conf_file_filter, (list, tuple, set, int, long)):
153 conf_files = ConfFile().select(filter={'conf_file_id': conf_file_filter})
154 elif isinstance(conf_file_filter, dict):
155 conf_files = ConfFile().select(filter=conf_file_filter)
157 for conf_file in conf_files:
158 self.append(conf_file)