create() calls get_table()
[plcapi.git] / PLC / ConfFiles.py
1 #
2 # Functions for interacting with the conf_files table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
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
13
14 class ConfFile(AlchemyObj):
15     """
16     Representation of a row in the conf_files table. To use,
17     instantiate with a dict of values.
18     """
19
20     tablename = 'conf_files'
21     join_tables = ['conf_file_node', 'conf_file_nodegroup']
22     fields = {
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),
37         }
38
39     def add_node(self, node, commit = True):
40         """
41         Add configuration file to node.
42         """
43
44         assert 'conf_file_id' in self
45         assert isinstance(node, Node)
46         assert 'node_id' in node
47
48         conf_file_id = self['conf_file_id']
49         node_id = node['node_id']
50
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)",
54                            locals())
55
56             if commit:
57                 self.api.db.commit()
58
59             self['node_ids'].append(node_id)
60             node['conf_file_ids'].append(conf_file_id)
61
62     def remove_node(self, node, commit = True):
63         """
64         Remove configuration file from node.
65         """
66
67         assert 'conf_file_id' in self
68         assert isinstance(node, Node)
69         assert 'node_id' in node
70
71         conf_file_id = self['conf_file_id']
72         node_id = node['node_id']
73
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",
78                            locals())
79
80             if commit:
81                 self.api.db.commit()
82
83             self['node_ids'].remove(node_id)
84             node['conf_file_ids'].remove(conf_file_id)
85
86     def add_nodegroup(self, nodegroup, commit = True):
87         """
88         Add configuration file to node group.
89         """
90
91         assert 'conf_file_id' in self
92         assert isinstance(nodegroup, NodeGroup)
93         assert 'nodegroup_id' in nodegroup
94
95         conf_file_id = self['conf_file_id']
96         nodegroup_id = nodegroup['nodegroup_id']
97
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)",
101                            locals())
102
103             if commit:
104                 self.api.db.commit()
105
106             self['nodegroup_ids'].append(nodegroup_id)
107             nodegroup['conf_file_ids'].append(conf_file_id)
108
109     def remove_nodegroup(self, nodegroup, commit = True):
110         """
111         Remove configuration file from node group.
112         """
113
114         assert 'conf_file_id' in self
115         assert isinstance(nodegroup, NodeGroup)
116         assert 'nodegroup_id' in nodegroup
117
118         conf_file_id = self['conf_file_id']
119         nodegroup_id = nodegroup['nodegroup_id']
120
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",
125                            locals())
126
127             if commit:
128                 self.api.db.commit()
129
130             self['nodegroup_ids'].remove(nodegroup_id)
131             nodegroup['conf_file_ids'].remove(conf_file_id)
132
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))
137         else:
138             AlchemyObj.update(self, {'conf_file_id': self['conf_file_id']}, dict(self))
139
140     def delete(self, commit=True):
141         assert 'conf_file_id' in self
142         AlchemyObj.delete(self, dict(self))
143
144 class ConfFiles(list):
145     """
146     Representation of the conf_files table in the database.
147     """
148
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)
156
157         for conf_file in conf_files:
158             self.append(conf_file)