Fix version output when missing.
[plcapi.git] / planetlab5.sql
1 --
2 -- PlanetLab Central database schema
3 -- Version 5, PostgreSQL
4 --
5 -- Aaron Klingaman <alk@cs.princeton.edu>
6 -- Reid Moran <rmoran@cs.princeton.edu>
7 -- Mark Huang <mlhuang@cs.princeton.edu>
8 -- Tony Mack <tmack@cs.princeton.edu>
9 -- Thierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
10 --
11 -- Copyright (C) 2006 The Trustees of Princeton University
12 --
13 -- NOTE: this file was first created for version 4.3, the filename might be confusing
14 --
15 -- $Id$
16 -- $URL$
17 --
18
19 SET client_encoding = 'UNICODE';
20
21 --------------------------------------------------------------------------------
22 -- Version
23 --------------------------------------------------------------------------------
24
25 -- Database version
26 CREATE TABLE plc_db_version (
27     version integer NOT NULL,
28     subversion integer NOT NULL DEFAULT 0
29 ) WITH OIDS;
30
31 -- the migration scripts do not use the major 'version' number
32 -- so 5.0 sets subversion at 100
33 -- in case your database misses the site and persons tags feature, 
34 -- you might wish to first upgrade to 4.3-rc16 before moving to some 5.0
35 -- or run the up script here
36 -- http://svn.planet-lab.org/svn/PLCAPI/branches/4.3/migrations/
37
38 INSERT INTO plc_db_version (version, subversion) VALUES (5, 100);
39
40 --------------------------------------------------------------------------------
41 -- Aggregates and store procedures
42 --------------------------------------------------------------------------------
43
44 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
45 -- PostgreSQL array.
46 CREATE AGGREGATE array_accum (
47     sfunc = array_append,
48     basetype = anyelement,
49     stype = anyarray,
50     initcond = '{}'
51 );
52
53 --------------------------------------------------------------------------------
54 -- Roles
55 --------------------------------------------------------------------------------
56
57 -- Valid account roles
58 CREATE TABLE roles (
59     role_id integer PRIMARY KEY,                        -- Role identifier
60     name text UNIQUE NOT NULL                           -- Role symbolic name
61 ) WITH OIDS;
62 INSERT INTO roles (role_id, name) VALUES (10, 'admin');
63 INSERT INTO roles (role_id, name) VALUES (20, 'pi');
64 INSERT INTO roles (role_id, name) VALUES (30, 'user');
65 INSERT INTO roles (role_id, name) VALUES (40, 'tech');
66
67 --------------------------------------------------------------------------------
68 -- The building block for attaching tags
69 --------------------------------------------------------------------------------
70 CREATE TABLE tag_types (
71
72     tag_type_id serial PRIMARY KEY,                     -- ID
73     tagname text UNIQUE NOT NULL,                       -- Tag Name
74     description text,                                   -- Optional Description
75     min_role_id integer REFERENCES roles DEFAULT 10,    -- set minimal role required
76     category text NOT NULL DEFAULT 'general'            -- Free text for grouping tags together
77 ) WITH OIDS;
78
79 --------------------------------------------------------------------------------
80 -- Accounts
81 --------------------------------------------------------------------------------
82
83 -- Accounts
84 CREATE TABLE persons (
85     -- Mandatory
86     person_id serial PRIMARY KEY,                       -- Account identifier
87     email text NOT NULL,                                -- E-mail address
88     first_name text NOT NULL,                           -- First name
89     last_name text NOT NULL,                            -- Last name
90     deleted boolean NOT NULL DEFAULT false,             -- Has been deleted
91     enabled boolean NOT NULL DEFAULT false,             -- Has been disabled
92
93     password text NOT NULL DEFAULT 'nopass',            -- Password (md5crypted)
94     verification_key text,                              -- Reset password key
95     verification_expires timestamp without time zone,
96
97     -- Optional
98     title text,                                         -- Honorific
99     phone text,                                         -- Telephone number
100     url text,                                           -- Home page
101     bio text,                                           -- Biography
102
103     -- Timestamps
104     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
105     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
106 ) WITH OIDS;
107 CREATE INDEX persons_email_idx ON persons (email);
108
109 --------------------------------------------------------------------------------
110 -- person tags
111 --------------------------------------------------------------------------------
112 CREATE TABLE person_tag (
113     person_tag_id serial PRIMARY KEY,                   -- ID
114     person_id integer REFERENCES persons NOT NULL,      -- person id
115     tag_type_id integer REFERENCES tag_types,           -- tag type id
116     value text                                          -- value attached
117 ) WITH OIDS;
118
119 CREATE OR REPLACE VIEW person_tags AS
120 SELECT person_id,
121 array_accum(person_tag_id) AS person_tag_ids
122 FROM person_tag
123 GROUP BY person_id;
124
125 CREATE OR REPLACE VIEW view_person_tags AS
126 SELECT
127 person_tag.person_tag_id,
128 person_tag.person_id,
129 persons.email,
130 tag_types.tag_type_id,
131 tag_types.tagname,
132 tag_types.description,
133 tag_types.category,
134 tag_types.min_role_id,
135 person_tag.value
136 FROM person_tag 
137 INNER JOIN tag_types USING (tag_type_id)
138 INNER JOIN persons USING (person_id);
139
140 --------------------------------------------------------------------------------
141 -- Sites
142 --------------------------------------------------------------------------------
143
144 -- Sites
145 CREATE TABLE sites (
146     -- Mandatory
147     site_id serial PRIMARY KEY,                         -- Site identifier
148     login_base text NOT NULL,                           -- Site slice prefix
149     name text NOT NULL,                                 -- Site name
150     abbreviated_name text NOT NULL,                     -- Site abbreviated name
151     enabled boolean NOT NULL Default true,              -- Is this site enabled
152     deleted boolean NOT NULL DEFAULT false,             -- Has been deleted
153     is_public boolean NOT NULL DEFAULT true,            -- Shows up in public lists
154     max_slices integer NOT NULL DEFAULT 0,              -- Maximum number of slices
155     max_slivers integer NOT NULL DEFAULT 1000,          -- Maximum number of instantiated slivers
156
157     -- Optional
158     latitude real,
159     longitude real,
160     url text,
161     ext_consortium_id integer,                          -- external consortium id
162
163     -- Timestamps
164     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
165     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
166 ) WITH OIDS;
167 CREATE INDEX sites_login_base_idx ON sites (login_base);
168
169 -- Account site membership
170 CREATE TABLE person_site (
171     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
172     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
173     is_primary boolean NOT NULL DEFAULT false,          -- Is the primary site for this account
174     PRIMARY KEY (person_id, site_id)
175 );
176 CREATE INDEX person_site_person_id_idx ON person_site (person_id);
177 CREATE INDEX person_site_site_id_idx ON person_site (site_id);
178
179 -- Ordered by primary site first
180 CREATE OR REPLACE VIEW person_site_ordered AS
181 SELECT person_id, site_id
182 FROM person_site
183 ORDER BY is_primary DESC;
184
185 -- Sites that each person is a member of
186 CREATE OR REPLACE VIEW person_sites AS
187 SELECT person_id,
188 array_accum(site_id) AS site_ids
189 FROM person_site_ordered
190 GROUP BY person_id;
191
192 -- Accounts at each site
193 CREATE OR REPLACE VIEW site_persons AS
194 SELECT site_id,
195 array_accum(person_id) AS person_ids
196 FROM person_site
197 GROUP BY site_id;
198
199 --------------------------------------------------------------------------------
200 -- site tags
201 --------------------------------------------------------------------------------
202
203 CREATE TABLE site_tag (
204     site_tag_id serial PRIMARY KEY,                     -- ID
205     site_id integer REFERENCES sites NOT NULL,          -- site id
206     tag_type_id integer REFERENCES tag_types,           -- tag type id
207     value text                                          -- value attached
208 ) WITH OIDS;
209
210 CREATE OR REPLACE VIEW site_tags AS
211 SELECT site_id,
212 array_accum(site_tag_id) AS site_tag_ids
213 FROM site_tag
214 GROUP BY site_id;
215
216 CREATE OR REPLACE VIEW view_site_tags AS
217 SELECT
218 site_tag.site_tag_id,
219 site_tag.site_id,
220 sites.login_base,
221 tag_types.tag_type_id,
222 tag_types.tagname,
223 tag_types.description,
224 tag_types.category,
225 tag_types.min_role_id,
226 site_tag.value
227 FROM site_tag 
228 INNER JOIN tag_types USING (tag_type_id)
229 INNER JOIN sites USING (site_id);
230
231 --------------------------------------------------------------------------------
232 -- Mailing Addresses
233 --------------------------------------------------------------------------------
234
235 CREATE TABLE address_types (
236     address_type_id serial PRIMARY KEY,                 -- Address type identifier
237     name text UNIQUE NOT NULL,                          -- Address type
238     description text                                    -- Address type description
239 ) WITH OIDS;
240
241 -- Multi-rows insertion "insert .. values (row1), (row2)" is not supported by pgsql-8.1
242 -- 'Billing' Used to be 'Site'
243 INSERT INTO address_types (name) VALUES ('Personal');
244 INSERT INTO address_types (name) VALUES ('Shipping');
245 INSERT INTO address_types (name) VALUES ('Billing');
246
247 -- Mailing addresses
248 CREATE TABLE addresses (
249     address_id serial PRIMARY KEY,                      -- Address identifier
250     line1 text NOT NULL,                                -- Address line 1
251     line2 text,                                         -- Address line 2
252     line3 text,                                         -- Address line 3
253     city text NOT NULL,                                 -- City
254     state text NOT NULL,                                -- State or province
255     postalcode text NOT NULL,                           -- Postal code
256     country text NOT NULL                               -- Country
257 ) WITH OIDS;
258
259 -- Each mailing address can be one of several types
260 CREATE TABLE address_address_type (
261     address_id integer REFERENCES addresses NOT NULL,           -- Address identifier
262     address_type_id integer REFERENCES address_types NOT NULL,  -- Address type
263     PRIMARY KEY (address_id, address_type_id)
264 ) WITH OIDS;
265 CREATE INDEX address_address_type_address_id_idx ON address_address_type (address_id);
266 CREATE INDEX address_address_type_address_type_id_idx ON address_address_type (address_type_id);
267
268 CREATE OR REPLACE VIEW address_address_types AS
269 SELECT address_id,
270 array_accum(address_type_id) AS address_type_ids,
271 array_accum(address_types.name) AS address_types
272 FROM address_address_type
273 LEFT JOIN address_types USING (address_type_id)
274 GROUP BY address_id;
275
276 CREATE TABLE site_address (
277     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
278     address_id integer REFERENCES addresses NOT NULL,   -- Address identifier
279     PRIMARY KEY (site_id, address_id)
280 ) WITH OIDS;
281 CREATE INDEX site_address_site_id_idx ON site_address (site_id);
282 CREATE INDEX site_address_address_id_idx ON site_address (address_id);
283
284 CREATE OR REPLACE VIEW site_addresses AS
285 SELECT site_id,
286 array_accum(address_id) AS address_ids
287 FROM site_address
288 GROUP BY site_id;
289
290 --------------------------------------------------------------------------------
291 -- Authentication Keys
292 --------------------------------------------------------------------------------
293
294 -- Valid key types
295 CREATE TABLE key_types (
296     key_type text PRIMARY KEY                           -- Key type
297 ) WITH OIDS;
298 INSERT INTO key_types (key_type) VALUES ('ssh');
299
300 -- Authentication keys
301 CREATE TABLE keys (
302     key_id serial PRIMARY KEY,                          -- Key identifier
303     key_type text REFERENCES key_types NOT NULL,        -- Key type
304     key text NOT NULL, -- Key material
305     is_blacklisted boolean NOT NULL DEFAULT false       -- Has been blacklisted
306 ) WITH OIDS;
307
308 -- Account authentication key(s)
309 CREATE TABLE person_key (
310     key_id integer REFERENCES keys PRIMARY KEY,         -- Key identifier
311     person_id integer REFERENCES persons NOT NULL       -- Account identifier
312 ) WITH OIDS;
313 CREATE INDEX person_key_person_id_idx ON person_key (person_id);
314
315 CREATE OR REPLACE VIEW person_keys AS
316 SELECT person_id,
317 array_accum(key_id) AS key_ids
318 FROM person_key
319 GROUP BY person_id;
320
321 --------------------------------------------------------------------------------
322 -- Account roles
323 --------------------------------------------------------------------------------
324
325 CREATE TABLE person_role (
326     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
327     role_id integer REFERENCES roles NOT NULL,          -- Role identifier
328     PRIMARY KEY (person_id, role_id)
329 ) WITH OIDS;
330 CREATE INDEX person_role_person_id_idx ON person_role (person_id);
331
332 -- Account roles
333 CREATE OR REPLACE VIEW person_roles AS
334 SELECT person_id,
335 array_accum(role_id) AS role_ids,
336 array_accum(roles.name) AS roles
337 FROM person_role
338 LEFT JOIN roles USING (role_id)
339 GROUP BY person_id;
340
341 --------------------------------------------------------------------------------
342 -- Nodes
343 --------------------------------------------------------------------------------
344
345 -- Valid node boot states (Nodes.py expect max length to be 20)
346 CREATE TABLE boot_states (
347     boot_state text PRIMARY KEY
348 ) WITH OIDS;
349 INSERT INTO boot_states (boot_state) VALUES ('boot');
350 INSERT INTO boot_states (boot_state) VALUES ('safeboot');
351 INSERT INTO boot_states (boot_state) VALUES ('reinstall');
352 INSERT INTO boot_states (boot_state) VALUES ('disabled');
353
354 CREATE TABLE run_levels  (
355     run_level text PRIMARY KEY
356 ) WITH OIDS;
357 INSERT INTO run_levels  (run_level) VALUES ('boot');
358 INSERT INTO run_levels  (run_level) VALUES ('safeboot');
359 INSERT INTO run_levels  (run_level) VALUES ('failboot');
360 INSERT INTO run_levels  (run_level) VALUES ('reinstall');
361
362 -- Known node types (Nodes.py expect max length to be 20)
363 CREATE TABLE node_types (
364     node_type text PRIMARY KEY
365 ) WITH OIDS;
366 INSERT INTO node_types (node_type) VALUES ('regular');
367 -- old dummynet stuff, to be removed
368 INSERT INTO node_types (node_type) VALUES ('dummynet');
369
370 -- Nodes
371 CREATE TABLE nodes (
372     -- Mandatory
373     node_id serial PRIMARY KEY,                         -- Node identifier
374     node_type text REFERENCES node_types                -- node type
375                DEFAULT 'regular',
376
377     hostname text NOT NULL,                             -- Node hostname
378     site_id integer REFERENCES sites NOT NULL,          -- At which site 
379     boot_state text REFERENCES boot_states NOT NULL     -- Node boot state
380                DEFAULT 'reinstall', 
381     run_level  text REFERENCES run_levels DEFAULT NULL, -- Node Run Level
382     deleted boolean NOT NULL DEFAULT false,             -- Is deleted
383
384     -- Optional
385     model text,                                         -- Hardware make and model
386     boot_nonce text,                                    -- Random nonce updated by Boot Manager
387     version text,                                       -- Boot CD version string updated by Boot Manager
388     ssh_rsa_key text,                                   -- SSH host key updated by Boot Manager
389     key text,                                           -- Node key generated when boot file is downloaded
390         verified boolean NOT NULL DEFAULT false,        -- whether or not the node & pcu are verified
391
392     -- Timestamps
393     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
394     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
395     last_download timestamp without time zone,
396     last_pcu_reboot timestamp without time zone,
397     last_pcu_confirmation timestamp without time zone,
398     last_contact timestamp without time zone    
399 ) WITH OIDS;
400 CREATE INDEX nodes_hostname_idx ON nodes (hostname);
401 CREATE INDEX nodes_site_id_idx ON nodes (site_id);
402
403 -- Nodes at each site
404 CREATE OR REPLACE VIEW site_nodes AS
405 SELECT site_id,
406 array_accum(node_id) AS node_ids
407 FROM nodes
408 WHERE deleted IS false
409 GROUP BY site_id;
410
411 --------------------------------------------------------------------------------
412 -- node tags
413 --------------------------------------------------------------------------------
414
415 CREATE TABLE node_tag (
416     node_tag_id serial PRIMARY KEY,                     -- ID
417     node_id integer REFERENCES nodes NOT NULL,          -- node id
418     tag_type_id integer REFERENCES tag_types,           -- tag type id
419     value text                                          -- value attached
420 ) WITH OIDS;
421
422 --------------------------------------------------------------------------------
423 -- (network) interfaces
424 --------------------------------------------------------------------------------
425
426 -- Valid network addressing schemes
427 CREATE TABLE network_types (
428     type text PRIMARY KEY -- Addressing scheme
429 ) WITH OIDS;
430 INSERT INTO network_types (type) VALUES ('ipv4');
431
432 -- Valid network configuration methods
433 CREATE TABLE network_methods (
434     method text PRIMARY KEY -- Configuration method
435 ) WITH OIDS;
436
437 INSERT INTO network_methods (method) VALUES ('static');
438 INSERT INTO network_methods (method) VALUES ('dhcp');
439 INSERT INTO network_methods (method) VALUES ('proxy');
440 INSERT INTO network_methods (method) VALUES ('tap');
441 INSERT INTO network_methods (method) VALUES ('ipmi');
442 INSERT INTO network_methods (method) VALUES ('unknown');
443
444 -- Network interfaces
445 CREATE TABLE interfaces (
446     -- Mandatory
447     interface_id serial PRIMARY KEY,                    -- Network interface identifier
448     node_id integer REFERENCES nodes NOT NULL,          -- Which node
449     is_primary boolean NOT NULL DEFAULT false,          -- Is the primary interface for this node
450     type text REFERENCES network_types NOT NULL,        -- Addressing scheme
451     method text REFERENCES network_methods NOT NULL,    -- Configuration method
452
453     -- Optional, depending on type and method
454     ip text,                                            -- IP address
455     mac text,                                           -- MAC address
456     gateway text,                                       -- Default gateway address
457     network text,                                       -- Network address
458     broadcast text,                                     -- Network broadcast address
459     netmask text,                                       -- Network mask
460     dns1 text,                                          -- Primary DNS server
461     dns2 text,                                          -- Secondary DNS server
462     bwlimit integer,                                    -- Bandwidth limit in bps
463     hostname text,                                      -- Hostname of this interface
464     last_updated timestamp without time zone -- When the interface was last updated
465 ) WITH OIDS;
466 CREATE INDEX interfaces_node_id_idx ON interfaces (node_id);
467
468 -- Ordered by primary interface first
469 CREATE OR REPLACE VIEW interfaces_ordered AS
470 SELECT node_id, interface_id
471 FROM interfaces
472 ORDER BY is_primary DESC;
473
474 -- Network interfaces on each node
475 CREATE OR REPLACE VIEW node_interfaces AS
476 SELECT node_id,
477 array_accum(interface_id) AS interface_ids
478 FROM interfaces_ordered
479 GROUP BY node_id;
480
481 --------------------------------------------------------------------------------
482 -- Interface tags (formerly known as interface settings)
483 --------------------------------------------------------------------------------
484
485 CREATE TABLE interface_tag (
486     interface_tag_id serial PRIMARY KEY,                -- Interface Setting Identifier
487     interface_id integer REFERENCES interfaces NOT NULL,-- the interface this applies to
488     tag_type_id integer REFERENCES tag_types NOT NULL,  -- the setting type
489     value text                                          -- value attached
490 ) WITH OIDS;
491
492 CREATE OR REPLACE VIEW interface_tags AS 
493 SELECT interface_id,
494 array_accum(interface_tag_id) AS interface_tag_ids
495 FROM interface_tag
496 GROUP BY interface_id;
497
498 CREATE OR REPLACE VIEW view_interface_tags AS
499 SELECT
500 interface_tag.interface_tag_id,
501 interface_tag.interface_id,
502 interfaces.ip,
503 tag_types.tag_type_id,
504 tag_types.tagname,
505 tag_types.description,
506 tag_types.category,
507 tag_types.min_role_id,
508 interface_tag.value
509 FROM interface_tag
510 INNER JOIN tag_types USING (tag_type_id)
511 INNER JOIN interfaces USING (interface_id);
512
513 CREATE OR REPLACE VIEW view_interfaces AS
514 SELECT
515 interfaces.interface_id,
516 interfaces.node_id,
517 interfaces.is_primary,
518 interfaces.type,
519 interfaces.method,
520 interfaces.ip,
521 interfaces.mac,
522 interfaces.gateway,
523 interfaces.network,
524 interfaces.broadcast,
525 interfaces.netmask,
526 interfaces.dns1,
527 interfaces.dns2,
528 interfaces.bwlimit,
529 interfaces.hostname,
530 CAST(date_part('epoch', interfaces.last_updated) AS bigint) AS last_updated,
531 COALESCE((SELECT interface_tag_ids FROM interface_tags WHERE interface_tags.interface_id = interfaces.interface_id), '{}') AS interface_tag_ids
532 FROM interfaces;
533
534 --------------------------------------------------------------------------------
535 -- ilinks : links between interfaces
536 --------------------------------------------------------------------------------
537 CREATE TABLE ilink (
538        ilink_id serial PRIMARY KEY,                             -- id
539        tag_type_id integer REFERENCES tag_types,                -- id of the tag type
540        src_interface_id integer REFERENCES interfaces not NULL, -- id of src interface
541        dst_interface_id integer REFERENCES interfaces NOT NULL, -- id of dst interface
542        value text                                               -- optional value on the link
543 ) WITH OIDS;
544
545 CREATE OR REPLACE VIEW view_ilinks AS
546 SELECT * FROM tag_types 
547 INNER JOIN ilink USING (tag_type_id);
548
549 -- xxx TODO : expose to view_interfaces the set of ilinks a given interface is part of
550 -- this is needed for properly deleting these ilinks when an interface gets deleted
551 -- as this is not done yet, it prevents DeleteInterface, thus DeleteNode, thus DeleteSite
552 -- from working correctly when an iLink is set
553
554 --------------------------------------------------------------------------------
555 -- Node groups
556 --------------------------------------------------------------------------------
557
558 -- Node groups
559 CREATE TABLE nodegroups (
560     nodegroup_id serial PRIMARY KEY,            -- Group identifier
561     groupname text UNIQUE NOT NULL,             -- Group name 
562     tag_type_id integer REFERENCES tag_types,   -- node is in nodegroup if it has this tag defined
563     -- can be null, make management faster & easier
564     value text                                  -- with this value attached
565 ) WITH OIDS;
566
567 -- xxx - first rough implem. similar to former semantics but might be slow
568 CREATE OR REPLACE VIEW nodegroup_node AS
569 SELECT nodegroup_id, node_id 
570 FROM tag_types 
571 JOIN node_tag 
572 USING (tag_type_id) 
573 JOIN nodegroups 
574 USING (tag_type_id,value);
575
576 CREATE OR REPLACE VIEW nodegroup_nodes AS
577 SELECT nodegroup_id,
578 array_accum(node_id) AS node_ids
579 FROM nodegroup_node
580 GROUP BY nodegroup_id;
581
582 -- Node groups that each node is a member of
583 CREATE OR REPLACE VIEW node_nodegroups AS
584 SELECT node_id,
585 array_accum(nodegroup_id) AS nodegroup_ids
586 FROM nodegroup_node
587 GROUP BY node_id;
588
589 --------------------------------------------------------------------------------
590 -- Node configuration files
591 --------------------------------------------------------------------------------
592
593 CREATE TABLE conf_files (
594     conf_file_id serial PRIMARY KEY,                    -- Configuration file identifier
595     enabled bool NOT NULL DEFAULT true,                 -- Configuration file is active
596     source text NOT NULL,                               -- Relative path on the boot server
597                                                         -- where file can be downloaded
598     dest text NOT NULL,                                 -- Absolute path where file should be installed
599     file_permissions text NOT NULL DEFAULT '0644',      -- chmod(1) permissions
600     file_owner text NOT NULL DEFAULT 'root',            -- chown(1) owner
601     file_group text NOT NULL DEFAULT 'root',            -- chgrp(1) owner
602     preinstall_cmd text,                                -- Shell command to execute prior to installing
603     postinstall_cmd text,                               -- Shell command to execute after installing
604     error_cmd text,                                     -- Shell command to execute if any error occurs
605     ignore_cmd_errors bool NOT NULL DEFAULT false,      -- Install file anyway even if an error occurs
606     always_update bool NOT NULL DEFAULT false           -- Always attempt to install file even if unchanged
607 ) WITH OIDS;
608
609 CREATE TABLE conf_file_node (
610     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
611     node_id integer REFERENCES nodes NOT NULL,                  -- Node identifier
612     PRIMARY KEY (conf_file_id, node_id)
613 );
614 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
615 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
616
617 -- Nodes linked to each configuration file
618 CREATE OR REPLACE VIEW conf_file_nodes AS
619 SELECT conf_file_id,
620 array_accum(node_id) AS node_ids
621 FROM conf_file_node
622 GROUP BY conf_file_id;
623
624 -- Configuration files linked to each node
625 CREATE OR REPLACE VIEW node_conf_files AS
626 SELECT node_id,
627 array_accum(conf_file_id) AS conf_file_ids
628 FROM conf_file_node
629 GROUP BY node_id;
630
631 CREATE TABLE conf_file_nodegroup (
632     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
633     nodegroup_id integer REFERENCES nodegroups NOT NULL,        -- Node group identifier
634     PRIMARY KEY (conf_file_id, nodegroup_id)
635 );
636 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
637 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
638
639 -- Node groups linked to each configuration file
640 CREATE OR REPLACE VIEW conf_file_nodegroups AS
641 SELECT conf_file_id,
642 array_accum(nodegroup_id) AS nodegroup_ids
643 FROM conf_file_nodegroup
644 GROUP BY conf_file_id;
645
646 -- Configuration files linked to each node group
647 CREATE OR REPLACE VIEW nodegroup_conf_files AS
648 SELECT nodegroup_id,
649 array_accum(conf_file_id) AS conf_file_ids
650 FROM conf_file_nodegroup
651 GROUP BY nodegroup_id;
652
653 --------------------------------------------------------------------------------
654 -- Power control units (PCUs)
655 --------------------------------------------------------------------------------
656
657 CREATE TABLE pcus (
658     -- Mandatory
659     pcu_id serial PRIMARY KEY,                          -- PCU identifier
660     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
661     hostname text,                                      -- Hostname, not necessarily unique 
662                                                         -- (multiple logical sites could use the same PCU)
663     ip text NOT NULL,                                   -- IP, not necessarily unique
664
665     -- Optional
666     protocol text,                                      -- Protocol, e.g. ssh or https or telnet
667     username text,                                      -- Username, if applicable
668     "password" text,                                    -- Password, if applicable
669     model text,                                         -- Model, e.g. BayTech or iPal
670     last_updated timestamp without time zone,
671     notes text                                          -- Random notes
672 ) WITH OIDS;
673 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
674
675 CREATE OR REPLACE VIEW site_pcus AS
676 SELECT site_id,
677 array_accum(pcu_id) AS pcu_ids
678 FROM pcus
679 GROUP BY site_id;
680
681 CREATE TABLE pcu_node (
682     pcu_id integer REFERENCES pcus NOT NULL,            -- PCU identifier
683     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
684     port integer NOT NULL,                              -- Port number
685     PRIMARY KEY (pcu_id, node_id),                      -- The same node cannot be controlled by different ports
686     UNIQUE (pcu_id, port)                               -- The same port cannot control multiple nodes
687 );
688 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
689 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
690
691 CREATE OR REPLACE VIEW node_pcus AS
692 SELECT node_id,
693 array_accum(pcu_id) AS pcu_ids,
694 array_accum(port) AS ports
695 FROM pcu_node
696 GROUP BY node_id;
697
698 CREATE OR REPLACE VIEW pcu_nodes AS
699 SELECT pcu_id,
700 array_accum(node_id) AS node_ids,
701 array_accum(port) AS ports
702 FROM pcu_node
703 GROUP BY pcu_id;
704
705 --------------------------------------------------------------------------------
706 -- Slices
707 --------------------------------------------------------------------------------
708
709 CREATE TABLE slice_instantiations (
710     instantiation text PRIMARY KEY
711 ) WITH OIDS;
712 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated');   -- Placeholder slice
713 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated');   -- Instantiated by Node Manager
714 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated');          -- Manually instantiated
715 INSERT INTO slice_instantiations (instantiation) VALUES ('nm-controller');      -- NM Controller
716
717 -- Slices
718 CREATE TABLE slices (
719     slice_id serial PRIMARY KEY,                        -- Slice identifier
720     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
721
722     name text NOT NULL,                                 -- Slice name
723     instantiation text REFERENCES slice_instantiations  -- Slice state, e.g. plc-instantiated
724                   NOT NULL DEFAULT 'plc-instantiated',                  
725     url text,                                           -- Project URL
726     description text,                                   -- Project description
727
728     max_nodes integer NOT NULL DEFAULT 100,             -- Maximum number of nodes that can be assigned to this slice
729
730     creator_person_id integer REFERENCES persons,       -- Creator
731     created timestamp without time zone NOT NULL        -- Creation date
732         DEFAULT CURRENT_TIMESTAMP, 
733     expires timestamp without time zone NOT NULL        -- Expiration date
734         DEFAULT CURRENT_TIMESTAMP + '2 weeks', 
735
736     is_deleted boolean NOT NULL DEFAULT false
737 ) WITH OIDS;
738 CREATE INDEX slices_site_id_idx ON slices (site_id);
739 CREATE INDEX slices_name_idx ON slices (name);
740
741 -- Slivers
742 CREATE TABLE slice_node (
743     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
744     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
745     PRIMARY KEY (slice_id, node_id)
746 ) WITH OIDS;
747 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
748 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
749
750 -- Synonym for slice_node
751 CREATE OR REPLACE VIEW slivers AS
752 SELECT * FROM slice_node;
753
754 -- Nodes in each slice
755 CREATE OR REPLACE VIEW slice_nodes AS
756 SELECT slice_id,
757 array_accum(node_id) AS node_ids
758 FROM slice_node
759 GROUP BY slice_id;
760
761 -- Slices on each node
762 CREATE OR REPLACE VIEW node_slices AS
763 SELECT node_id,
764 array_accum(slice_id) AS slice_ids
765 FROM slice_node
766 GROUP BY node_id;
767
768 -- Slices at each site
769 CREATE OR REPLACE VIEW site_slices AS
770 SELECT site_id,
771 array_accum(slice_id) AS slice_ids
772 FROM slices
773 WHERE is_deleted is false
774 GROUP BY site_id;
775
776 -- Slice membership
777 CREATE TABLE slice_person (
778     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
779     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
780     PRIMARY KEY (slice_id, person_id)
781 ) WITH OIDS;
782 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
783 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
784
785 -- Members of the slice
786 CREATE OR REPLACE VIEW slice_persons AS
787 SELECT slice_id,
788 array_accum(person_id) AS person_ids
789 FROM slice_person
790 GROUP BY slice_id;
791
792 -- Slices of which each person is a member
793 CREATE OR REPLACE VIEW person_slices AS
794 SELECT person_id,
795 array_accum(slice_id) AS slice_ids
796 FROM slice_person
797 GROUP BY person_id;
798
799 --------------------------------------------------------------------------------
800 -- Slice whitelist
801 --------------------------------------------------------------------------------
802 -- slice whitelist on nodes
803 CREATE TABLE node_slice_whitelist (
804     node_id integer REFERENCES nodes NOT NULL,          -- Node id of whitelist
805     slice_id integer REFERENCES slices NOT NULL,        -- Slice id thats allowd on this node
806     PRIMARY KEY (node_id, slice_id)
807 ) WITH OIDS;
808 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
809 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
810
811 -- Slices on each node
812 CREATE OR REPLACE VIEW node_slices_whitelist AS
813 SELECT node_id,
814 array_accum(slice_id) AS slice_ids_whitelist
815 FROM node_slice_whitelist
816 GROUP BY node_id;
817
818 --------------------------------------------------------------------------------
819 -- Slice tags (formerly known as slice attributes)
820 --------------------------------------------------------------------------------
821
822 -- Slice/sliver attributes
823 CREATE TABLE slice_tag (
824     slice_tag_id serial PRIMARY KEY,                    -- Slice attribute identifier
825     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
826     node_id integer REFERENCES nodes,                   -- Sliver attribute if set
827     nodegroup_id integer REFERENCES nodegroups,         -- Node group attribute if set
828     tag_type_id integer REFERENCES tag_types NOT NULL,  -- Attribute type identifier
829     value text
830 ) WITH OIDS;
831 CREATE INDEX slice_tag_slice_id_idx ON slice_tag (slice_id);
832 CREATE INDEX slice_tag_node_id_idx ON slice_tag (node_id);
833 CREATE INDEX slice_tag_nodegroup_id_idx ON slice_tag (nodegroup_id);
834
835 --------------------------------------------------------------------------------
836 -- Initscripts
837 --------------------------------------------------------------------------------
838
839 -- Initscripts
840 CREATE TABLE initscripts (
841     initscript_id serial PRIMARY KEY,                   -- Initscript identifier
842     name text NOT NULL,                                 -- Initscript name
843     enabled bool NOT NULL DEFAULT true,                 -- Initscript is active
844     script text NOT NULL,                               -- Initscript body
845     UNIQUE (name)
846 ) WITH OIDS;
847 CREATE INDEX initscripts_name_idx ON initscripts (name);
848
849
850 --------------------------------------------------------------------------------
851 -- Peers
852 --------------------------------------------------------------------------------
853
854 -- Peers
855 CREATE TABLE peers (
856     peer_id serial PRIMARY KEY,                         -- Peer identifier
857     peername text NOT NULL,                             -- Peer name
858     peer_url text NOT NULL,                             -- (HTTPS) URL of the peer PLCAPI interface
859     cacert text,                                        -- (SSL) Public certificate of peer API server
860     key text,                                           -- (GPG) Public key used for authentication
861     shortname text,                                     -- abbreviated name for displaying foreign objects
862     hrn_root text,                                      -- root for this peer domain
863     deleted boolean NOT NULL DEFAULT false
864 ) WITH OIDS;
865 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
866 CREATE INDEX peers_shortname_idx ON peers (shortname) WHERE deleted IS false;
867
868 -- Objects at each peer
869 CREATE TABLE peer_site (
870     site_id integer REFERENCES sites PRIMARY KEY,       -- Local site identifier
871     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
872     peer_site_id integer NOT NULL,                      -- Foreign site identifier at peer
873     UNIQUE (peer_id, peer_site_id)                      -- The same foreign site should not be cached twice
874 ) WITH OIDS;
875 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
876
877 CREATE OR REPLACE VIEW peer_sites AS
878 SELECT peer_id,
879 array_accum(site_id) AS site_ids,
880 array_accum(peer_site_id) AS peer_site_ids
881 FROM peer_site
882 GROUP BY peer_id;
883
884 CREATE TABLE peer_person (
885     person_id integer REFERENCES persons PRIMARY KEY,   -- Local user identifier
886     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
887     peer_person_id integer NOT NULL,                    -- Foreign user identifier at peer
888     UNIQUE (peer_id, peer_person_id)                    -- The same foreign user should not be cached twice
889 ) WITH OIDS;
890 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
891
892 CREATE OR REPLACE VIEW peer_persons AS
893 SELECT peer_id,
894 array_accum(person_id) AS person_ids,
895 array_accum(peer_person_id) AS peer_person_ids
896 FROM peer_person
897 GROUP BY peer_id;
898
899 CREATE TABLE peer_key (
900     key_id integer REFERENCES keys PRIMARY KEY,         -- Local key identifier
901     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
902     peer_key_id integer NOT NULL,                       -- Foreign key identifier at peer
903     UNIQUE (peer_id, peer_key_id)                       -- The same foreign key should not be cached twice
904 ) WITH OIDS;
905 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
906
907 CREATE OR REPLACE VIEW peer_keys AS
908 SELECT peer_id,
909 array_accum(key_id) AS key_ids,
910 array_accum(peer_key_id) AS peer_key_ids
911 FROM peer_key
912 GROUP BY peer_id;
913
914 CREATE TABLE peer_node (
915     node_id integer REFERENCES nodes PRIMARY KEY,       -- Local node identifier
916     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
917     peer_node_id integer NOT NULL,                      -- Foreign node identifier
918     UNIQUE (peer_id, peer_node_id)                      -- The same foreign node should not be cached twice
919 ) WITH OIDS;
920 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
921
922 CREATE OR REPLACE VIEW peer_nodes AS
923 SELECT peer_id,
924 array_accum(node_id) AS node_ids,
925 array_accum(peer_node_id) AS peer_node_ids
926 FROM peer_node
927 GROUP BY peer_id;
928
929 CREATE TABLE peer_slice (
930     slice_id integer REFERENCES slices PRIMARY KEY,     -- Local slice identifier
931     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
932     peer_slice_id integer NOT NULL,                     -- Slice identifier at peer
933     UNIQUE (peer_id, peer_slice_id)                     -- The same foreign slice should not be cached twice
934 ) WITH OIDS;
935 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
936
937 CREATE OR REPLACE VIEW peer_slices AS
938 SELECT peer_id,
939 array_accum(slice_id) AS slice_ids,
940 array_accum(peer_slice_id) AS peer_slice_ids
941 FROM peer_slice
942 GROUP BY peer_id;
943
944 --------------------------------------------------------------------------------
945 -- Authenticated sessions
946 --------------------------------------------------------------------------------
947
948 -- Authenticated sessions
949 CREATE TABLE sessions (
950     session_id text PRIMARY KEY,                        -- Session identifier
951     expires timestamp without time zone
952 ) WITH OIDS;
953
954 -- People can have multiple sessions
955 CREATE TABLE person_session (
956     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
957     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
958     PRIMARY KEY (person_id, session_id),
959     UNIQUE (session_id)                                 -- Sessions are unique
960 ) WITH OIDS;
961 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
962
963 -- Nodes can have only one session
964 CREATE TABLE node_session (
965     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
966     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
967     UNIQUE (node_id),                                   -- Nodes can have only one session
968     UNIQUE (session_id)                                 -- Sessions are unique
969 ) WITH OIDS;
970
971 -------------------------------------------------------------------------------
972 -- PCU Types
973 ------------------------------------------------------------------------------
974 CREATE TABLE pcu_types (
975     pcu_type_id serial PRIMARY KEY,
976     model text NOT NULL ,                               -- PCU model name
977     name text                                           -- Full PCU model name
978 ) WITH OIDS;
979 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
980
981 CREATE TABLE pcu_protocol_type (
982     pcu_protocol_type_id serial PRIMARY KEY,
983     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
984     port integer NOT NULL,                              -- PCU port
985     protocol text NOT NULL,                             -- Protocol
986     supported boolean NOT NULL DEFAULT True             -- Does PLC support
987 ) WITH OIDS;
988 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
989
990
991 CREATE OR REPLACE VIEW pcu_protocol_types AS
992 SELECT pcu_type_id,
993 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
994 FROM pcu_protocol_type
995 GROUP BY pcu_type_id;
996
997 --------------------------------------------------------------------------------
998 -- Message templates
999 --------------------------------------------------------------------------------
1000
1001 CREATE TABLE messages (
1002     message_id text PRIMARY KEY,                        -- Message name
1003     subject text,                                       -- Message summary
1004     template text,                                      -- Message template
1005     enabled bool NOT NULL DEFAULT true                  -- Whether message is enabled
1006 ) WITH OIDS;
1007
1008 --------------------------------------------------------------------------------
1009 -- Events
1010 --------------------------------------------------------------------------------
1011
1012 -- Events
1013 CREATE TABLE events (
1014     event_id serial PRIMARY KEY,                        -- Event identifier
1015     person_id integer REFERENCES persons,               -- Person responsible for event, if any
1016     node_id integer REFERENCES nodes,                   -- Node responsible for event, if any
1017     auth_type text,                                     -- Type of auth used. i.e. AuthMethod
1018     fault_code integer NOT NULL DEFAULT 0,              -- Did this event result in error
1019     call_name text NOT NULL,                            -- Call responsible for this event
1020     call text NOT NULL,                                 -- Call responsible for this event, including parameters
1021     message text,                                       -- High level description of this event
1022     runtime float DEFAULT 0,                            -- Event run time
1023     time timestamp without time zone NOT NULL           -- Event timestamp
1024         DEFAULT CURRENT_TIMESTAMP
1025 ) WITH OIDS;
1026
1027 -- Database object(s) that may have been affected by a particular event
1028 CREATE TABLE event_object (
1029     event_id integer REFERENCES events NOT NULL,        -- Event identifier
1030     object_id integer NOT NULL,                         -- Object identifier
1031     object_type text NOT NULL Default 'Unknown'         -- What type of object is this event affecting
1032 ) WITH OIDS;
1033 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
1034 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
1035 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
1036
1037 CREATE OR REPLACE VIEW event_objects AS
1038 SELECT event_id,
1039 array_accum(object_id) AS object_ids,
1040 array_accum(object_type) AS object_types
1041 FROM event_object
1042 GROUP BY event_id;
1043
1044 --------------------------------------------------------------------------------
1045 -- Useful views
1046 --------------------------------------------------------------------------------
1047 CREATE OR REPLACE VIEW view_pcu_types AS
1048 SELECT
1049 pcu_types.pcu_type_id,
1050 pcu_types.model,
1051 pcu_types.name,
1052 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types
1053                  WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') 
1054 AS pcu_protocol_type_ids
1055 FROM pcu_types;
1056
1057 --------------------------------------------------------------------------------
1058 CREATE OR REPLACE VIEW view_events AS
1059 SELECT
1060 events.event_id,
1061 events.person_id,
1062 events.node_id,
1063 events.auth_type,
1064 events.fault_code,
1065 events.call_name,
1066 events.call,
1067 events.message,
1068 events.runtime,
1069 CAST(date_part('epoch', events.time) AS bigint) AS time,
1070 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
1071 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
1072 FROM events;
1073
1074 CREATE OR REPLACE VIEW view_event_objects AS 
1075 SELECT
1076 events.event_id,
1077 events.person_id,
1078 events.node_id,
1079 events.fault_code,
1080 events.call_name,
1081 events.call,
1082 events.message,
1083 events.runtime,
1084 CAST(date_part('epoch', events.time) AS bigint) AS time,
1085 event_object.object_id,
1086 event_object.object_type
1087 FROM events LEFT JOIN event_object USING (event_id);
1088
1089 --------------------------------------------------------------------------------
1090 CREATE OR REPLACE VIEW view_persons AS
1091 SELECT
1092 persons.person_id,
1093 persons.email,
1094 persons.first_name,
1095 persons.last_name,
1096 persons.deleted,
1097 persons.enabled,
1098 persons.password,
1099 persons.verification_key,
1100 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
1101 persons.title,
1102 persons.phone,
1103 persons.url,
1104 persons.bio,
1105 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
1106 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
1107 peer_person.peer_id,
1108 peer_person.peer_person_id,
1109 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
1110 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
1111 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
1112 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
1113 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids,
1114 COALESCE((SELECT person_tag_ids FROM person_tags WHERE person_tags.person_id = persons.person_id), '{}') AS person_tag_ids
1115 FROM persons
1116 LEFT JOIN peer_person USING (person_id);
1117
1118 --------------------------------------------------------------------------------
1119 CREATE OR REPLACE VIEW view_peers AS
1120 SELECT 
1121 peers.*, 
1122 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
1123 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
1124 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
1125 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
1126 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
1127 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
1128 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
1129 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
1130 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
1131 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
1132 FROM peers;
1133
1134 --------------------------------------------------------------------------------
1135 CREATE OR REPLACE VIEW node_tags AS
1136 SELECT node_id,
1137 array_accum(node_tag_id) AS node_tag_ids
1138 FROM node_tag
1139 GROUP BY node_id;
1140
1141 CREATE OR REPLACE VIEW view_node_tags AS
1142 SELECT
1143 node_tag.node_tag_id,
1144 node_tag.node_id,
1145 nodes.hostname,
1146 tag_types.tag_type_id,
1147 tag_types.tagname,
1148 tag_types.description,
1149 tag_types.category,
1150 tag_types.min_role_id,
1151 node_tag.value
1152 FROM node_tag 
1153 INNER JOIN tag_types USING (tag_type_id)
1154 INNER JOIN nodes USING (node_id);
1155
1156 CREATE OR REPLACE VIEW view_nodes AS
1157 SELECT
1158 nodes.node_id,
1159 nodes.node_type,
1160 nodes.hostname,
1161 nodes.site_id,
1162 nodes.boot_state,
1163 nodes.run_level,
1164 nodes.deleted,
1165 nodes.model,
1166 nodes.boot_nonce,
1167 nodes.version,
1168 nodes.verified,
1169 nodes.ssh_rsa_key,
1170 nodes.key,
1171 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1172 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1173 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1174 CAST(date_part('epoch', nodes.last_boot) AS bigint) AS last_boot,  
1175 CAST(date_part('epoch', nodes.last_download) AS bigint) AS last_download,  
1176 CAST(date_part('epoch', nodes.last_pcu_reboot) AS bigint) AS last_pcu_reboot,  
1177 CAST(date_part('epoch', nodes.last_pcu_confirmation) AS bigint) AS last_pcu_confirmation,  
1178 peer_node.peer_id,
1179 peer_node.peer_node_id,
1180 COALESCE((SELECT interface_ids FROM node_interfaces 
1181                  WHERE node_interfaces.node_id = nodes.node_id), '{}') 
1182 AS interface_ids,
1183 COALESCE((SELECT nodegroup_ids FROM node_nodegroups 
1184                  WHERE node_nodegroups.node_id = nodes.node_id), '{}') 
1185 AS nodegroup_ids,
1186 COALESCE((SELECT slice_ids FROM node_slices 
1187                  WHERE node_slices.node_id = nodes.node_id), '{}') 
1188 AS slice_ids,
1189 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist 
1190                  WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') 
1191 AS slice_ids_whitelist,
1192 COALESCE((SELECT pcu_ids FROM node_pcus 
1193                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1194 AS pcu_ids,
1195 COALESCE((SELECT ports FROM node_pcus
1196                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1197 AS ports,
1198 COALESCE((SELECT conf_file_ids FROM node_conf_files
1199                  WHERE node_conf_files.node_id = nodes.node_id), '{}') 
1200 AS conf_file_ids,
1201 COALESCE((SELECT node_tag_ids FROM node_tags 
1202                  WHERE node_tags.node_id = nodes.node_id), '{}') 
1203 AS node_tag_ids,
1204 node_session.session_id AS session
1205 FROM nodes
1206 LEFT JOIN peer_node USING (node_id)
1207 LEFT JOIN node_session USING (node_id);
1208
1209 --------------------------------------------------------------------------------
1210 CREATE OR REPLACE VIEW view_nodegroups AS
1211 SELECT
1212 nodegroups.*,
1213 tag_types.tagname,
1214 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files 
1215                  WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1216 AS conf_file_ids,
1217 COALESCE((SELECT node_ids FROM nodegroup_nodes 
1218                  WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1219 AS node_ids
1220 FROM nodegroups INNER JOIN tag_types USING (tag_type_id);
1221
1222 --------------------------------------------------------------------------------
1223 CREATE OR REPLACE VIEW view_conf_files AS
1224 SELECT
1225 conf_files.*,
1226 COALESCE((SELECT node_ids FROM conf_file_nodes 
1227                  WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') 
1228 AS node_ids,
1229 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups 
1230                  WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') 
1231 AS nodegroup_ids
1232 FROM conf_files;
1233
1234 --------------------------------------------------------------------------------
1235 DROP VIEW view_pcus;
1236 CREATE OR REPLACE VIEW view_pcus AS
1237 SELECT
1238 pcus.pcu_id,
1239 pcus.site_id,
1240 pcus.hostname,
1241 pcus.ip,
1242 pcus.protocol,
1243 pcus.username,
1244 pcus.password,
1245 pcus.model,
1246 pcus.notes,
1247 CAST(date_part('epoch', pcus.last_updated) AS bigint) AS last_updated,
1248 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1249 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1250 FROM pcus;
1251
1252
1253 --------------------------------------------------------------------------------
1254 CREATE OR REPLACE VIEW view_sites AS
1255 SELECT
1256 sites.site_id,
1257 sites.login_base,
1258 sites.name,
1259 sites.abbreviated_name,
1260 sites.deleted,
1261 sites.enabled,
1262 sites.is_public,
1263 sites.max_slices,
1264 sites.max_slivers,
1265 sites.latitude,
1266 sites.longitude,
1267 sites.url,
1268 sites.ext_consortium_id,
1269 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1270 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1271 peer_site.peer_id,
1272 peer_site.peer_site_id,
1273 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1274 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1275 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1276 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1277 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids,
1278 COALESCE((SELECT site_tag_ids FROM site_tags WHERE site_tags.site_id = sites.site_id), '{}') AS site_tag_ids
1279 FROM sites
1280 LEFT JOIN peer_site USING (site_id);
1281
1282 --------------------------------------------------------------------------------
1283 CREATE OR REPLACE VIEW view_addresses AS
1284 SELECT
1285 addresses.*,
1286 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1287 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1288 FROM addresses;
1289
1290 --------------------------------------------------------------------------------
1291 CREATE OR REPLACE VIEW view_keys AS
1292 SELECT
1293 keys.*,
1294 person_key.person_id,
1295 peer_key.peer_id,
1296 peer_key.peer_key_id
1297 FROM keys
1298 LEFT JOIN person_key USING (key_id)
1299 LEFT JOIN peer_key USING (key_id);
1300
1301 --------------------------------------------------------------------------------
1302 CREATE OR REPLACE VIEW slice_tags AS
1303 SELECT slice_id,
1304 array_accum(slice_tag_id) AS slice_tag_ids
1305 FROM slice_tag
1306 GROUP BY slice_id;
1307
1308 CREATE OR REPLACE VIEW view_slices AS
1309 SELECT
1310 slices.slice_id,
1311 slices.site_id,
1312 slices.name,
1313 slices.instantiation,
1314 slices.url,
1315 slices.description,
1316 slices.max_nodes,
1317 slices.creator_person_id,
1318 slices.is_deleted,
1319 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1320 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1321 peer_slice.peer_id,
1322 peer_slice.peer_slice_id,
1323 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1324 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1325 COALESCE((SELECT slice_tag_ids FROM slice_tags WHERE slice_tags.slice_id = slices.slice_id), '{}') AS slice_tag_ids
1326 FROM slices
1327 LEFT JOIN peer_slice USING (slice_id);
1328
1329 CREATE OR REPLACE VIEW view_slice_tags AS
1330 SELECT
1331 slice_tag.slice_tag_id,
1332 slice_tag.slice_id,
1333 slice_tag.node_id,
1334 slice_tag.nodegroup_id,
1335 tag_types.tag_type_id,
1336 tag_types.tagname,
1337 tag_types.description,
1338 tag_types.category,
1339 tag_types.min_role_id,
1340 slice_tag.value,
1341 slices.name
1342 FROM slice_tag
1343 INNER JOIN tag_types USING (tag_type_id)
1344 INNER JOIN slices USING (slice_id);
1345
1346 --------------------------------------------------------------------------------
1347 CREATE OR REPLACE VIEW view_sessions AS
1348 SELECT
1349 sessions.session_id,
1350 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1351 person_session.person_id,
1352 node_session.node_id
1353 FROM sessions
1354 LEFT JOIN person_session USING (session_id)
1355 LEFT JOIN node_session USING (session_id);
1356
1357 --------------------------------------------------------------------------------
1358 -- Built-in maintenance account and default site
1359 --------------------------------------------------------------------------------
1360
1361 INSERT INTO persons (first_name, last_name, email, password, enabled)
1362 VALUES              ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1363
1364 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1365 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1366 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1367 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1368
1369 INSERT INTO sites (login_base, name, abbreviated_name, max_slices)
1370 VALUES ('pl', 'PlanetLab Central', 'PLC', 100);