- add conf_files interface
[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 # $Id: ConfFiles.py,v 1.4 2006/10/10 21:54:20 mlhuang Exp $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13
14 class ConfFile(Row):
15     """
16     Representation of a row in the conf_files table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'conf_files'
21     primary_key = 'conf_file_id'
22     join_tables = ['conf_file_node', 'conf_file_nodegroup']
23     fields = {
24         'conf_file_id': Parameter(int, "Configuration file identifier"),
25         'enabled': Parameter(bool, "Configuration file is active"),
26         'source': Parameter(str, "Relative path on the boot server where file can be downloaded", max = 255),
27         'dest': Parameter(str, "Absolute path where file should be installed", max = 255),
28         'file_permissions': Parameter(str, "chmod(1) permissions", max = 20),
29         'file_owner': Parameter(str, "chown(1) owner", max = 50),
30         'file_group': Parameter(str, "chgrp(1) owner", max = 50),
31         'preinstall_cmd': Parameter(str, "Shell command to execute prior to installing", max = 1024),
32         'postinstall_cmd': Parameter(str, "Shell command to execute after installing", max = 1024),
33         'error_cmd': Parameter(str, "Shell command to execute if any error occurs", max = 1024),
34         'ignore_cmd_errors': Parameter(bool, "Install file anyway even if an error occurs"),
35         'always_update': Parameter(bool, "Always attempt to install file even if unchanged"),
36         'node_ids': Parameter(int, "List of nodes linked to this file", ro = True),
37         'nodegroup_ids': Parameter(int, "List of node groups linked to this file", ro = True),
38         }
39
40 class ConfFiles(Table):
41     """
42     Representation of the conf_files table in the database.
43     """
44
45     def __init__(self, api, conf_file_ids = None):
46         sql = "SELECT %s FROM view_conf_files" % \
47               ", ".join(ConfFile.fields)
48         
49         if conf_file_ids:
50             # Separate the list into integers and strings
51             sql += " WHERE conf_file_id IN (%s)" % ", ".join(map(str, api.db.quote(conf_file_ids)))
52
53         rows = api.db.selectall(sql)
54
55         for row in rows:
56             self[row['conf_file_id']] = ConfFile(api, row)
57
58
59         for row in rows:
60             self[row['conf_file_id']] = conf_file = ConfFile(api, row)
61             for aggregate in ['node_ids', 'nodegroup_ids']:
62                 if not conf_file.has_key(aggregate) or conf_file[aggregate] is None:
63                     conf_file[aggregate] = []
64                 else:
65                     conf_file[aggregate] = map(int, conf_file[aggregate].split(','))