Setting tag plcapi-5.4-2
[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.Filter import Filter
11 from PLC.Table import Row, Table
12 from PLC.Nodes import Node, Nodes
13 from PLC.NodeGroups import NodeGroup, NodeGroups
14
15 class ConfFile(Row):
16     """
17     Representation of a row in the conf_files table. To use,
18     instantiate with a dict of values.
19     """
20
21     table_name = 'conf_files'
22     primary_key = 'conf_file_id'
23     join_tables = ['conf_file_node', 'conf_file_nodegroup']
24     fields = {
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"),
39         }
40
41     def add_node(self, node, commit = True):
42         """
43         Add configuration file to node.
44         """
45
46         assert 'conf_file_id' in self
47         assert isinstance(node, Node)
48         assert 'node_id' in node
49
50         conf_file_id = self['conf_file_id']
51         node_id = node['node_id']
52
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)",
56                            locals())
57
58             if commit:
59                 self.api.db.commit()
60
61             self['node_ids'].append(node_id)
62             node['conf_file_ids'].append(conf_file_id)
63
64     def remove_node(self, node, commit = True):
65         """
66         Remove configuration file from node.
67         """
68
69         assert 'conf_file_id' in self
70         assert isinstance(node, Node)
71         assert 'node_id' in node
72
73         conf_file_id = self['conf_file_id']
74         node_id = node['node_id']
75
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",
80                            locals())
81
82             if commit:
83                 self.api.db.commit()
84
85             self['node_ids'].remove(node_id)
86             node['conf_file_ids'].remove(conf_file_id)
87
88     def add_nodegroup(self, nodegroup, commit = True):
89         """
90         Add configuration file to node group.
91         """
92
93         assert 'conf_file_id' in self
94         assert isinstance(nodegroup, NodeGroup)
95         assert 'nodegroup_id' in nodegroup
96
97         conf_file_id = self['conf_file_id']
98         nodegroup_id = nodegroup['nodegroup_id']
99
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)",
103                            locals())
104
105             if commit:
106                 self.api.db.commit()
107
108             self['nodegroup_ids'].append(nodegroup_id)
109             nodegroup['conf_file_ids'].append(conf_file_id)
110
111     def remove_nodegroup(self, nodegroup, commit = True):
112         """
113         Remove configuration file from node group.
114         """
115
116         assert 'conf_file_id' in self
117         assert isinstance(nodegroup, NodeGroup)
118         assert 'nodegroup_id' in nodegroup
119
120         conf_file_id = self['conf_file_id']
121         nodegroup_id = nodegroup['nodegroup_id']
122
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",
127                            locals())
128
129             if commit:
130                 self.api.db.commit()
131
132             self['nodegroup_ids'].remove(nodegroup_id)
133             nodegroup['conf_file_ids'].remove(conf_file_id)
134
135 class ConfFiles(Table):
136     """
137     Representation of the conf_files table in the database.
138     """
139
140     def __init__(self, api, conf_file_filter = None, columns = None):
141         Table.__init__(self, api, ConfFile, columns)
142
143         sql = "SELECT %s FROM view_conf_files WHERE True" % \
144               ", ".join(self.columns)
145
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)
152
153         self.selectall(sql)