fix order
[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_contact timestamp without time zone    
396 ) WITH OIDS;
397 CREATE INDEX nodes_hostname_idx ON nodes (hostname);
398 CREATE INDEX nodes_site_id_idx ON nodes (site_id);
399
400 -- Nodes at each site
401 CREATE OR REPLACE VIEW site_nodes AS
402 SELECT site_id,
403 array_accum(node_id) AS node_ids
404 FROM nodes
405 WHERE deleted IS false
406 GROUP BY site_id;
407
408 --------------------------------------------------------------------------------
409 -- node tags
410 --------------------------------------------------------------------------------
411
412 CREATE TABLE node_tag (
413     node_tag_id serial PRIMARY KEY,                     -- ID
414     node_id integer REFERENCES nodes NOT NULL,          -- node id
415     tag_type_id integer REFERENCES tag_types,           -- tag type id
416     value text                                          -- value attached
417 ) WITH OIDS;
418
419 --------------------------------------------------------------------------------
420 -- (network) interfaces
421 --------------------------------------------------------------------------------
422
423 -- Valid network addressing schemes
424 CREATE TABLE network_types (
425     type text PRIMARY KEY -- Addressing scheme
426 ) WITH OIDS;
427 INSERT INTO network_types (type) VALUES ('ipv4');
428
429 -- Valid network configuration methods
430 CREATE TABLE network_methods (
431     method text PRIMARY KEY -- Configuration method
432 ) WITH OIDS;
433
434 INSERT INTO network_methods (method) VALUES ('static');
435 INSERT INTO network_methods (method) VALUES ('dhcp');
436 INSERT INTO network_methods (method) VALUES ('proxy');
437 INSERT INTO network_methods (method) VALUES ('tap');
438 INSERT INTO network_methods (method) VALUES ('ipmi');
439 INSERT INTO network_methods (method) VALUES ('unknown');
440
441 -- Network interfaces
442 CREATE TABLE interfaces (
443     -- Mandatory
444     interface_id serial PRIMARY KEY,                    -- Network interface identifier
445     node_id integer REFERENCES nodes NOT NULL,          -- Which node
446     is_primary boolean NOT NULL DEFAULT false,          -- Is the primary interface for this node
447     type text REFERENCES network_types NOT NULL,        -- Addressing scheme
448     method text REFERENCES network_methods NOT NULL,    -- Configuration method
449
450     -- Optional, depending on type and method
451     ip text,                                            -- IP address
452     mac text,                                           -- MAC address
453     gateway text,                                       -- Default gateway address
454     network text,                                       -- Network address
455     broadcast text,                                     -- Network broadcast address
456     netmask text,                                       -- Network mask
457     dns1 text,                                          -- Primary DNS server
458     dns2 text,                                          -- Secondary DNS server
459     bwlimit integer,                                    -- Bandwidth limit in bps
460     hostname text                                       -- Hostname of this interface
461 ) WITH OIDS;
462 CREATE INDEX interfaces_node_id_idx ON interfaces (node_id);
463
464 -- Ordered by primary interface first
465 CREATE OR REPLACE VIEW interfaces_ordered AS
466 SELECT node_id, interface_id
467 FROM interfaces
468 ORDER BY is_primary DESC;
469
470 -- Network interfaces on each node
471 CREATE OR REPLACE VIEW node_interfaces AS
472 SELECT node_id,
473 array_accum(interface_id) AS interface_ids
474 FROM interfaces_ordered
475 GROUP BY node_id;
476
477 --------------------------------------------------------------------------------
478 -- Interface tags (formerly known as interface settings)
479 --------------------------------------------------------------------------------
480
481 CREATE TABLE interface_tag (
482     interface_tag_id serial PRIMARY KEY,                -- Interface Setting Identifier
483     interface_id integer REFERENCES interfaces NOT NULL,-- the interface this applies to
484     tag_type_id integer REFERENCES tag_types NOT NULL,  -- the setting type
485     value text                                          -- value attached
486 ) WITH OIDS;
487
488 CREATE OR REPLACE VIEW interface_tags AS 
489 SELECT interface_id,
490 array_accum(interface_tag_id) AS interface_tag_ids
491 FROM interface_tag
492 GROUP BY interface_id;
493
494 CREATE OR REPLACE VIEW view_interface_tags AS
495 SELECT
496 interface_tag.interface_tag_id,
497 interface_tag.interface_id,
498 interfaces.ip,
499 tag_types.tag_type_id,
500 tag_types.tagname,
501 tag_types.description,
502 tag_types.category,
503 tag_types.min_role_id,
504 interface_tag.value
505 FROM interface_tag
506 INNER JOIN tag_types USING (tag_type_id)
507 INNER JOIN interfaces USING (interface_id);
508
509 CREATE OR REPLACE VIEW view_interfaces AS
510 SELECT
511 interfaces.interface_id,
512 interfaces.node_id,
513 interfaces.is_primary,
514 interfaces.type,
515 interfaces.method,
516 interfaces.ip,
517 interfaces.mac,
518 interfaces.gateway,
519 interfaces.network,
520 interfaces.broadcast,
521 interfaces.netmask,
522 interfaces.dns1,
523 interfaces.dns2,
524 interfaces.bwlimit,
525 interfaces.hostname,
526 COALESCE((SELECT interface_tag_ids FROM interface_tags WHERE interface_tags.interface_id = interfaces.interface_id), '{}') AS interface_tag_ids
527 FROM interfaces;
528
529 --------------------------------------------------------------------------------
530 -- ilinks : links between interfaces
531 --------------------------------------------------------------------------------
532 CREATE TABLE ilink (
533        ilink_id serial PRIMARY KEY,                             -- id
534        tag_type_id integer REFERENCES tag_types,                -- id of the tag type
535        src_interface_id integer REFERENCES interfaces not NULL, -- id of src interface
536        dst_interface_id integer REFERENCES interfaces NOT NULL, -- id of dst interface
537        value text                                               -- optional value on the link
538 ) WITH OIDS;
539
540 CREATE OR REPLACE VIEW view_ilinks AS
541 SELECT * FROM tag_types 
542 INNER JOIN ilink USING (tag_type_id);
543
544 -- xxx TODO : expose to view_interfaces the set of ilinks a given interface is part of
545 -- this is needed for properly deleting these ilinks when an interface gets deleted
546 -- as this is not done yet, it prevents DeleteInterface, thus DeleteNode, thus DeleteSite
547 -- from working correctly when an iLink is set
548
549 --------------------------------------------------------------------------------
550 -- Node groups
551 --------------------------------------------------------------------------------
552
553 -- Node groups
554 CREATE TABLE nodegroups (
555     nodegroup_id serial PRIMARY KEY,            -- Group identifier
556     groupname text UNIQUE NOT NULL,             -- Group name 
557     tag_type_id integer REFERENCES tag_types,   -- node is in nodegroup if it has this tag defined
558     -- can be null, make management faster & easier
559     value text                                  -- with this value attached
560 ) WITH OIDS;
561
562 -- xxx - first rough implem. similar to former semantics but might be slow
563 CREATE OR REPLACE VIEW nodegroup_node AS
564 SELECT nodegroup_id, node_id 
565 FROM tag_types 
566 JOIN node_tag 
567 USING (tag_type_id) 
568 JOIN nodegroups 
569 USING (tag_type_id,value);
570
571 CREATE OR REPLACE VIEW nodegroup_nodes AS
572 SELECT nodegroup_id,
573 array_accum(node_id) AS node_ids
574 FROM nodegroup_node
575 GROUP BY nodegroup_id;
576
577 -- Node groups that each node is a member of
578 CREATE OR REPLACE VIEW node_nodegroups AS
579 SELECT node_id,
580 array_accum(nodegroup_id) AS nodegroup_ids
581 FROM nodegroup_node
582 GROUP BY node_id;
583
584 --------------------------------------------------------------------------------
585 -- Node configuration files
586 --------------------------------------------------------------------------------
587
588 CREATE TABLE conf_files (
589     conf_file_id serial PRIMARY KEY,                    -- Configuration file identifier
590     enabled bool NOT NULL DEFAULT true,                 -- Configuration file is active
591     source text NOT NULL,                               -- Relative path on the boot server
592                                                         -- where file can be downloaded
593     dest text NOT NULL,                                 -- Absolute path where file should be installed
594     file_permissions text NOT NULL DEFAULT '0644',      -- chmod(1) permissions
595     file_owner text NOT NULL DEFAULT 'root',            -- chown(1) owner
596     file_group text NOT NULL DEFAULT 'root',            -- chgrp(1) owner
597     preinstall_cmd text,                                -- Shell command to execute prior to installing
598     postinstall_cmd text,                               -- Shell command to execute after installing
599     error_cmd text,                                     -- Shell command to execute if any error occurs
600     ignore_cmd_errors bool NOT NULL DEFAULT false,      -- Install file anyway even if an error occurs
601     always_update bool NOT NULL DEFAULT false           -- Always attempt to install file even if unchanged
602 ) WITH OIDS;
603
604 CREATE TABLE conf_file_node (
605     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
606     node_id integer REFERENCES nodes NOT NULL,                  -- Node identifier
607     PRIMARY KEY (conf_file_id, node_id)
608 );
609 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
610 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
611
612 -- Nodes linked to each configuration file
613 CREATE OR REPLACE VIEW conf_file_nodes AS
614 SELECT conf_file_id,
615 array_accum(node_id) AS node_ids
616 FROM conf_file_node
617 GROUP BY conf_file_id;
618
619 -- Configuration files linked to each node
620 CREATE OR REPLACE VIEW node_conf_files AS
621 SELECT node_id,
622 array_accum(conf_file_id) AS conf_file_ids
623 FROM conf_file_node
624 GROUP BY node_id;
625
626 CREATE TABLE conf_file_nodegroup (
627     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
628     nodegroup_id integer REFERENCES nodegroups NOT NULL,        -- Node group identifier
629     PRIMARY KEY (conf_file_id, nodegroup_id)
630 );
631 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
632 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
633
634 -- Node groups linked to each configuration file
635 CREATE OR REPLACE VIEW conf_file_nodegroups AS
636 SELECT conf_file_id,
637 array_accum(nodegroup_id) AS nodegroup_ids
638 FROM conf_file_nodegroup
639 GROUP BY conf_file_id;
640
641 -- Configuration files linked to each node group
642 CREATE OR REPLACE VIEW nodegroup_conf_files AS
643 SELECT nodegroup_id,
644 array_accum(conf_file_id) AS conf_file_ids
645 FROM conf_file_nodegroup
646 GROUP BY nodegroup_id;
647
648 --------------------------------------------------------------------------------
649 -- Power control units (PCUs)
650 --------------------------------------------------------------------------------
651
652 CREATE TABLE pcus (
653     -- Mandatory
654     pcu_id serial PRIMARY KEY,                          -- PCU identifier
655     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
656     hostname text,                                      -- Hostname, not necessarily unique 
657                                                         -- (multiple logical sites could use the same PCU)
658     ip text NOT NULL,                                   -- IP, not necessarily unique
659
660     -- Optional
661     protocol text,                                      -- Protocol, e.g. ssh or https or telnet
662     username text,                                      -- Username, if applicable
663     "password" text,                                    -- Password, if applicable
664     model text,                                         -- Model, e.g. BayTech or iPal
665     notes text                                          -- Random notes
666 ) WITH OIDS;
667 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
668
669 CREATE OR REPLACE VIEW site_pcus AS
670 SELECT site_id,
671 array_accum(pcu_id) AS pcu_ids
672 FROM pcus
673 GROUP BY site_id;
674
675 CREATE TABLE pcu_node (
676     pcu_id integer REFERENCES pcus NOT NULL,            -- PCU identifier
677     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
678     port integer NOT NULL,                              -- Port number
679     PRIMARY KEY (pcu_id, node_id),                      -- The same node cannot be controlled by different ports
680     UNIQUE (pcu_id, port)                               -- The same port cannot control multiple nodes
681 );
682 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
683 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
684
685 CREATE OR REPLACE VIEW node_pcus AS
686 SELECT node_id,
687 array_accum(pcu_id) AS pcu_ids,
688 array_accum(port) AS ports
689 FROM pcu_node
690 GROUP BY node_id;
691
692 CREATE OR REPLACE VIEW pcu_nodes AS
693 SELECT pcu_id,
694 array_accum(node_id) AS node_ids,
695 array_accum(port) AS ports
696 FROM pcu_node
697 GROUP BY pcu_id;
698
699 --------------------------------------------------------------------------------
700 -- Slices
701 --------------------------------------------------------------------------------
702
703 CREATE TABLE slice_instantiations (
704     instantiation text PRIMARY KEY
705 ) WITH OIDS;
706 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated');   -- Placeholder slice
707 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated');   -- Instantiated by Node Manager
708 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated');          -- Manually instantiated
709 INSERT INTO slice_instantiations (instantiation) VALUES ('nm-controller');      -- NM Controller
710
711 -- Slices
712 CREATE TABLE slices (
713     slice_id serial PRIMARY KEY,                        -- Slice identifier
714     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
715
716     name text NOT NULL,                                 -- Slice name
717     instantiation text REFERENCES slice_instantiations  -- Slice state, e.g. plc-instantiated
718                   NOT NULL DEFAULT 'plc-instantiated',                  
719     url text,                                           -- Project URL
720     description text,                                   -- Project description
721
722     max_nodes integer NOT NULL DEFAULT 100,             -- Maximum number of nodes that can be assigned to this slice
723
724     creator_person_id integer REFERENCES persons,       -- Creator
725     created timestamp without time zone NOT NULL        -- Creation date
726         DEFAULT CURRENT_TIMESTAMP, 
727     expires timestamp without time zone NOT NULL        -- Expiration date
728         DEFAULT CURRENT_TIMESTAMP + '2 weeks', 
729
730     is_deleted boolean NOT NULL DEFAULT false
731 ) WITH OIDS;
732 CREATE INDEX slices_site_id_idx ON slices (site_id);
733 CREATE INDEX slices_name_idx ON slices (name);
734
735 -- Slivers
736 CREATE TABLE slice_node (
737     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
738     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
739     PRIMARY KEY (slice_id, node_id)
740 ) WITH OIDS;
741 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
742 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
743
744 -- Synonym for slice_node
745 CREATE OR REPLACE VIEW slivers AS
746 SELECT * FROM slice_node;
747
748 -- Nodes in each slice
749 CREATE OR REPLACE VIEW slice_nodes AS
750 SELECT slice_id,
751 array_accum(node_id) AS node_ids
752 FROM slice_node
753 GROUP BY slice_id;
754
755 -- Slices on each node
756 CREATE OR REPLACE VIEW node_slices AS
757 SELECT node_id,
758 array_accum(slice_id) AS slice_ids
759 FROM slice_node
760 GROUP BY node_id;
761
762 -- Slices at each site
763 CREATE OR REPLACE VIEW site_slices AS
764 SELECT site_id,
765 array_accum(slice_id) AS slice_ids
766 FROM slices
767 WHERE is_deleted is false
768 GROUP BY site_id;
769
770 -- Slice membership
771 CREATE TABLE slice_person (
772     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
773     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
774     PRIMARY KEY (slice_id, person_id)
775 ) WITH OIDS;
776 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
777 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
778
779 -- Members of the slice
780 CREATE OR REPLACE VIEW slice_persons AS
781 SELECT slice_id,
782 array_accum(person_id) AS person_ids
783 FROM slice_person
784 GROUP BY slice_id;
785
786 -- Slices of which each person is a member
787 CREATE OR REPLACE VIEW person_slices AS
788 SELECT person_id,
789 array_accum(slice_id) AS slice_ids
790 FROM slice_person
791 GROUP BY person_id;
792
793 --------------------------------------------------------------------------------
794 -- Slice whitelist
795 --------------------------------------------------------------------------------
796 -- slice whitelist on nodes
797 CREATE TABLE node_slice_whitelist (
798     node_id integer REFERENCES nodes NOT NULL,          -- Node id of whitelist
799     slice_id integer REFERENCES slices NOT NULL,        -- Slice id thats allowd on this node
800     PRIMARY KEY (node_id, slice_id)
801 ) WITH OIDS;
802 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
803 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
804
805 -- Slices on each node
806 CREATE OR REPLACE VIEW node_slices_whitelist AS
807 SELECT node_id,
808 array_accum(slice_id) AS slice_ids_whitelist
809 FROM node_slice_whitelist
810 GROUP BY node_id;
811
812 --------------------------------------------------------------------------------
813 -- Slice tags (formerly known as slice attributes)
814 --------------------------------------------------------------------------------
815
816 -- Slice/sliver attributes
817 CREATE TABLE slice_tag (
818     slice_tag_id serial PRIMARY KEY,            -- Slice attribute identifier
819     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
820     node_id integer REFERENCES nodes,                   -- Sliver attribute if set
821     nodegroup_id integer REFERENCES nodegroups,         -- Node group attribute if set
822     tag_type_id integer REFERENCES tag_types NOT NULL,  -- Attribute type identifier
823     value text
824 ) WITH OIDS;
825 CREATE INDEX slice_tag_slice_id_idx ON slice_tag (slice_id);
826 CREATE INDEX slice_tag_node_id_idx ON slice_tag (node_id);
827 CREATE INDEX slice_tag_nodegroup_id_idx ON slice_tag (nodegroup_id);
828
829 --------------------------------------------------------------------------------
830 -- Initscripts
831 --------------------------------------------------------------------------------
832
833 -- Initscripts
834 CREATE TABLE initscripts (
835     initscript_id serial PRIMARY KEY,                   -- Initscript identifier
836     name text NOT NULL,                                 -- Initscript name
837     enabled bool NOT NULL DEFAULT true,                 -- Initscript is active
838     script text NOT NULL,                               -- Initscript body
839     UNIQUE (name)
840 ) WITH OIDS;
841 CREATE INDEX initscripts_name_idx ON initscripts (name);
842
843
844 --------------------------------------------------------------------------------
845 -- Peers
846 --------------------------------------------------------------------------------
847
848 -- Peers
849 CREATE TABLE peers (
850     peer_id serial PRIMARY KEY,                         -- Peer identifier
851     peername text NOT NULL,                             -- Peer name
852     peer_url text NOT NULL,                             -- (HTTPS) URL of the peer PLCAPI interface
853     cacert text,                                        -- (SSL) Public certificate of peer API server
854     key text,                                           -- (GPG) Public key used for authentication
855     shortname text,                                     -- abbreviated name for displaying foreign objects
856     hrn_root text,                                              -- root for this peer domain
857     deleted boolean NOT NULL DEFAULT false
858 ) WITH OIDS;
859 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
860 CREATE INDEX peers_shortname_idx ON peers (shortname) WHERE deleted IS false;
861
862 -- Objects at each peer
863 CREATE TABLE peer_site (
864     site_id integer REFERENCES sites PRIMARY KEY,       -- Local site identifier
865     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
866     peer_site_id integer NOT NULL,                      -- Foreign site identifier at peer
867     UNIQUE (peer_id, peer_site_id)                      -- The same foreign site should not be cached twice
868 ) WITH OIDS;
869 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
870
871 CREATE OR REPLACE VIEW peer_sites AS
872 SELECT peer_id,
873 array_accum(site_id) AS site_ids,
874 array_accum(peer_site_id) AS peer_site_ids
875 FROM peer_site
876 GROUP BY peer_id;
877
878 CREATE TABLE peer_person (
879     person_id integer REFERENCES persons PRIMARY KEY,   -- Local user identifier
880     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
881     peer_person_id integer NOT NULL,                    -- Foreign user identifier at peer
882     UNIQUE (peer_id, peer_person_id)                    -- The same foreign user should not be cached twice
883 ) WITH OIDS;
884 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
885
886 CREATE OR REPLACE VIEW peer_persons AS
887 SELECT peer_id,
888 array_accum(person_id) AS person_ids,
889 array_accum(peer_person_id) AS peer_person_ids
890 FROM peer_person
891 GROUP BY peer_id;
892
893 CREATE TABLE peer_key (
894     key_id integer REFERENCES keys PRIMARY KEY,         -- Local key identifier
895     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
896     peer_key_id integer NOT NULL,                       -- Foreign key identifier at peer
897     UNIQUE (peer_id, peer_key_id)                       -- The same foreign key should not be cached twice
898 ) WITH OIDS;
899 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
900
901 CREATE OR REPLACE VIEW peer_keys AS
902 SELECT peer_id,
903 array_accum(key_id) AS key_ids,
904 array_accum(peer_key_id) AS peer_key_ids
905 FROM peer_key
906 GROUP BY peer_id;
907
908 CREATE TABLE peer_node (
909     node_id integer REFERENCES nodes PRIMARY KEY,       -- Local node identifier
910     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
911     peer_node_id integer NOT NULL,                      -- Foreign node identifier
912     UNIQUE (peer_id, peer_node_id)                      -- The same foreign node should not be cached twice
913 ) WITH OIDS;
914 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
915
916 CREATE OR REPLACE VIEW peer_nodes AS
917 SELECT peer_id,
918 array_accum(node_id) AS node_ids,
919 array_accum(peer_node_id) AS peer_node_ids
920 FROM peer_node
921 GROUP BY peer_id;
922
923 CREATE TABLE peer_slice (
924     slice_id integer REFERENCES slices PRIMARY KEY,     -- Local slice identifier
925     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
926     peer_slice_id integer NOT NULL,                     -- Slice identifier at peer
927     UNIQUE (peer_id, peer_slice_id)                     -- The same foreign slice should not be cached twice
928 ) WITH OIDS;
929 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
930
931 CREATE OR REPLACE VIEW peer_slices AS
932 SELECT peer_id,
933 array_accum(slice_id) AS slice_ids,
934 array_accum(peer_slice_id) AS peer_slice_ids
935 FROM peer_slice
936 GROUP BY peer_id;
937
938 --------------------------------------------------------------------------------
939 -- Authenticated sessions
940 --------------------------------------------------------------------------------
941
942 -- Authenticated sessions
943 CREATE TABLE sessions (
944     session_id text PRIMARY KEY,                        -- Session identifier
945     expires timestamp without time zone
946 ) WITH OIDS;
947
948 -- People can have multiple sessions
949 CREATE TABLE person_session (
950     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
951     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
952     PRIMARY KEY (person_id, session_id),
953     UNIQUE (session_id)                                 -- Sessions are unique
954 ) WITH OIDS;
955 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
956
957 -- Nodes can have only one session
958 CREATE TABLE node_session (
959     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
960     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
961     UNIQUE (node_id),                                   -- Nodes can have only one session
962     UNIQUE (session_id)                                 -- Sessions are unique
963 ) WITH OIDS;
964
965 -------------------------------------------------------------------------------
966 -- PCU Types
967 ------------------------------------------------------------------------------
968 CREATE TABLE pcu_types (
969     pcu_type_id serial PRIMARY KEY,
970     model text NOT NULL ,                               -- PCU model name
971     name text                                           -- Full PCU model name
972 ) WITH OIDS;
973 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
974
975 CREATE TABLE pcu_protocol_type (
976     pcu_protocol_type_id serial PRIMARY KEY,
977     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
978     port integer NOT NULL,                              -- PCU port
979     protocol text NOT NULL,                             -- Protocol
980     supported boolean NOT NULL DEFAULT True             -- Does PLC support
981 ) WITH OIDS;
982 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
983
984
985 CREATE OR REPLACE VIEW pcu_protocol_types AS
986 SELECT pcu_type_id,
987 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
988 FROM pcu_protocol_type
989 GROUP BY pcu_type_id;
990
991 --------------------------------------------------------------------------------
992 -- Message templates
993 --------------------------------------------------------------------------------
994
995 CREATE TABLE messages (
996     message_id text PRIMARY KEY,                        -- Message name
997     subject text,                                       -- Message summary
998     template text,                                      -- Message template
999     enabled bool NOT NULL DEFAULT true                  -- Whether message is enabled
1000 ) WITH OIDS;
1001
1002 --------------------------------------------------------------------------------
1003 -- Events
1004 --------------------------------------------------------------------------------
1005
1006 -- Events
1007 CREATE TABLE events (
1008     event_id serial PRIMARY KEY,                        -- Event identifier
1009     person_id integer REFERENCES persons,               -- Person responsible for event, if any
1010     node_id integer REFERENCES nodes,                   -- Node responsible for event, if any
1011     auth_type text,                                     -- Type of auth used. i.e. AuthMethod
1012     fault_code integer NOT NULL DEFAULT 0,              -- Did this event result in error
1013     call_name text NOT NULL,                            -- Call responsible for this event
1014     call text NOT NULL,                                 -- Call responsible for this event, including parameters
1015     message text,                                       -- High level description of this event
1016     runtime float DEFAULT 0,                            -- Event run time
1017     time timestamp without time zone NOT NULL           -- Event timestamp
1018         DEFAULT CURRENT_TIMESTAMP
1019 ) WITH OIDS;
1020
1021 -- Database object(s) that may have been affected by a particular event
1022 CREATE TABLE event_object (
1023     event_id integer REFERENCES events NOT NULL,        -- Event identifier
1024     object_id integer NOT NULL,                         -- Object identifier
1025     object_type text NOT NULL Default 'Unknown'         -- What type of object is this event affecting
1026 ) WITH OIDS;
1027 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
1028 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
1029 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
1030
1031 CREATE OR REPLACE VIEW event_objects AS
1032 SELECT event_id,
1033 array_accum(object_id) AS object_ids,
1034 array_accum(object_type) AS object_types
1035 FROM event_object
1036 GROUP BY event_id;
1037
1038 --------------------------------------------------------------------------------
1039 -- Useful views
1040 --------------------------------------------------------------------------------
1041 CREATE OR REPLACE VIEW view_pcu_types AS
1042 SELECT
1043 pcu_types.pcu_type_id,
1044 pcu_types.model,
1045 pcu_types.name,
1046 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types
1047                  WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') 
1048 AS pcu_protocol_type_ids
1049 FROM pcu_types;
1050
1051 --------------------------------------------------------------------------------
1052 CREATE OR REPLACE VIEW view_events AS
1053 SELECT
1054 events.event_id,
1055 events.person_id,
1056 events.node_id,
1057 events.auth_type,
1058 events.fault_code,
1059 events.call_name,
1060 events.call,
1061 events.message,
1062 events.runtime,
1063 CAST(date_part('epoch', events.time) AS bigint) AS time,
1064 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
1065 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
1066 FROM events;
1067
1068 CREATE OR REPLACE VIEW view_event_objects AS 
1069 SELECT
1070 events.event_id,
1071 events.person_id,
1072 events.node_id,
1073 events.fault_code,
1074 events.call_name,
1075 events.call,
1076 events.message,
1077 events.runtime,
1078 CAST(date_part('epoch', events.time) AS bigint) AS time,
1079 event_object.object_id,
1080 event_object.object_type
1081 FROM events LEFT JOIN event_object USING (event_id);
1082
1083 --------------------------------------------------------------------------------
1084 CREATE OR REPLACE VIEW view_persons AS
1085 SELECT
1086 persons.person_id,
1087 persons.email,
1088 persons.first_name,
1089 persons.last_name,
1090 persons.deleted,
1091 persons.enabled,
1092 persons.password,
1093 persons.verification_key,
1094 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
1095 persons.title,
1096 persons.phone,
1097 persons.url,
1098 persons.bio,
1099 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
1100 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
1101 peer_person.peer_id,
1102 peer_person.peer_person_id,
1103 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
1104 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
1105 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
1106 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
1107 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids,
1108 COALESCE((SELECT person_tag_ids FROM person_tags WHERE person_tags.person_id = persons.person_id), '{}') AS person_tag_ids
1109 FROM persons
1110 LEFT JOIN peer_person USING (person_id);
1111
1112 --------------------------------------------------------------------------------
1113 CREATE OR REPLACE VIEW view_peers AS
1114 SELECT 
1115 peers.*, 
1116 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
1117 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
1118 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
1119 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
1120 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
1121 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
1122 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
1123 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
1124 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
1125 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
1126 FROM peers;
1127
1128 --------------------------------------------------------------------------------
1129 CREATE OR REPLACE VIEW node_tags AS
1130 SELECT node_id,
1131 array_accum(node_tag_id) AS node_tag_ids
1132 FROM node_tag
1133 GROUP BY node_id;
1134
1135 CREATE OR REPLACE VIEW view_node_tags AS
1136 SELECT
1137 node_tag.node_tag_id,
1138 node_tag.node_id,
1139 nodes.hostname,
1140 tag_types.tag_type_id,
1141 tag_types.tagname,
1142 tag_types.description,
1143 tag_types.category,
1144 tag_types.min_role_id,
1145 node_tag.value
1146 FROM node_tag 
1147 INNER JOIN tag_types USING (tag_type_id)
1148 INNER JOIN nodes USING (node_id);
1149
1150 CREATE OR REPLACE VIEW view_nodes AS
1151 SELECT
1152 nodes.node_id,
1153 nodes.node_type,
1154 nodes.hostname,
1155 nodes.site_id,
1156 nodes.boot_state,
1157 nodes.run_level,
1158 nodes.deleted,
1159 nodes.model,
1160 nodes.boot_nonce,
1161 nodes.version,
1162 nodes.verified,
1163 nodes.ssh_rsa_key,
1164 nodes.key,
1165 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1166 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1167 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1168 peer_node.peer_id,
1169 peer_node.peer_node_id,
1170 COALESCE((SELECT interface_ids FROM node_interfaces 
1171                  WHERE node_interfaces.node_id = nodes.node_id), '{}') 
1172 AS interface_ids,
1173 COALESCE((SELECT nodegroup_ids FROM node_nodegroups 
1174                  WHERE node_nodegroups.node_id = nodes.node_id), '{}') 
1175 AS nodegroup_ids,
1176 COALESCE((SELECT slice_ids FROM node_slices 
1177                  WHERE node_slices.node_id = nodes.node_id), '{}') 
1178 AS slice_ids,
1179 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist 
1180                  WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') 
1181 AS slice_ids_whitelist,
1182 COALESCE((SELECT pcu_ids FROM node_pcus 
1183                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1184 AS pcu_ids,
1185 COALESCE((SELECT ports FROM node_pcus
1186                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1187 AS ports,
1188 COALESCE((SELECT conf_file_ids FROM node_conf_files
1189                  WHERE node_conf_files.node_id = nodes.node_id), '{}') 
1190 AS conf_file_ids,
1191 COALESCE((SELECT node_tag_ids FROM node_tags 
1192                  WHERE node_tags.node_id = nodes.node_id), '{}') 
1193 AS node_tag_ids,
1194 node_session.session_id AS session
1195 FROM nodes
1196 LEFT JOIN peer_node USING (node_id)
1197 LEFT JOIN node_session USING (node_id);
1198
1199 --------------------------------------------------------------------------------
1200 CREATE OR REPLACE VIEW view_nodegroups AS
1201 SELECT
1202 nodegroups.*,
1203 tag_types.tagname,
1204 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files 
1205                  WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1206 AS conf_file_ids,
1207 COALESCE((SELECT node_ids FROM nodegroup_nodes 
1208                  WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1209 AS node_ids
1210 FROM nodegroups INNER JOIN tag_types USING (tag_type_id);
1211
1212 --------------------------------------------------------------------------------
1213 CREATE OR REPLACE VIEW view_conf_files AS
1214 SELECT
1215 conf_files.*,
1216 COALESCE((SELECT node_ids FROM conf_file_nodes 
1217                  WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') 
1218 AS node_ids,
1219 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups 
1220                  WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') 
1221 AS nodegroup_ids
1222 FROM conf_files;
1223
1224 --------------------------------------------------------------------------------
1225 CREATE OR REPLACE VIEW view_pcus AS
1226 SELECT
1227 pcus.*,
1228 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1229 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1230 FROM pcus;
1231
1232 --------------------------------------------------------------------------------
1233 CREATE OR REPLACE VIEW view_sites AS
1234 SELECT
1235 sites.site_id,
1236 sites.login_base,
1237 sites.name,
1238 sites.abbreviated_name,
1239 sites.deleted,
1240 sites.enabled,
1241 sites.is_public,
1242 sites.max_slices,
1243 sites.max_slivers,
1244 sites.latitude,
1245 sites.longitude,
1246 sites.url,
1247 sites.ext_consortium_id,
1248 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1249 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1250 peer_site.peer_id,
1251 peer_site.peer_site_id,
1252 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1253 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1254 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1255 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1256 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids,
1257 COALESCE((SELECT site_tag_ids FROM site_tags WHERE site_tags.site_id = sites.site_id), '{}') AS site_tag_ids
1258 FROM sites
1259 LEFT JOIN peer_site USING (site_id);
1260
1261 --------------------------------------------------------------------------------
1262 CREATE OR REPLACE VIEW view_addresses AS
1263 SELECT
1264 addresses.*,
1265 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1266 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1267 FROM addresses;
1268
1269 --------------------------------------------------------------------------------
1270 CREATE OR REPLACE VIEW view_keys AS
1271 SELECT
1272 keys.*,
1273 person_key.person_id,
1274 peer_key.peer_id,
1275 peer_key.peer_key_id
1276 FROM keys
1277 LEFT JOIN person_key USING (key_id)
1278 LEFT JOIN peer_key USING (key_id);
1279
1280 --------------------------------------------------------------------------------
1281 CREATE OR REPLACE VIEW slice_tags AS
1282 SELECT slice_id,
1283 array_accum(slice_tag_id) AS slice_tag_ids
1284 FROM slice_tag
1285 GROUP BY slice_id;
1286
1287 CREATE OR REPLACE VIEW view_slices AS
1288 SELECT
1289 slices.slice_id,
1290 slices.site_id,
1291 slices.name,
1292 slices.instantiation,
1293 slices.url,
1294 slices.description,
1295 slices.max_nodes,
1296 slices.creator_person_id,
1297 slices.is_deleted,
1298 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1299 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1300 peer_slice.peer_id,
1301 peer_slice.peer_slice_id,
1302 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1303 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1304 COALESCE((SELECT slice_tag_ids FROM slice_tags WHERE slice_tags.slice_id = slices.slice_id), '{}') AS slice_tag_ids
1305 FROM slices
1306 LEFT JOIN peer_slice USING (slice_id);
1307
1308 CREATE OR REPLACE VIEW view_slice_tags AS
1309 SELECT
1310 slice_tag.slice_tag_id,
1311 slice_tag.slice_id,
1312 slice_tag.node_id,
1313 slice_tag.nodegroup_id,
1314 tag_types.tag_type_id,
1315 tag_types.tagname,
1316 tag_types.description,
1317 tag_types.category,
1318 tag_types.min_role_id,
1319 slice_tag.value,
1320 slices.name
1321 FROM slice_tag
1322 INNER JOIN tag_types USING (tag_type_id)
1323 INNER JOIN slices USING (slice_id);
1324
1325 --------------------------------------------------------------------------------
1326 CREATE OR REPLACE VIEW view_sessions AS
1327 SELECT
1328 sessions.session_id,
1329 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1330 person_session.person_id,
1331 node_session.node_id
1332 FROM sessions
1333 LEFT JOIN person_session USING (session_id)
1334 LEFT JOIN node_session USING (session_id);
1335
1336 --------------------------------------------------------------------------------
1337 -- Built-in maintenance account and default site
1338 --------------------------------------------------------------------------------
1339
1340 INSERT INTO persons (first_name, last_name, email, password, enabled)
1341 VALUES              ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1342
1343 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1344 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1345 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1346 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1347
1348 INSERT INTO sites (login_base, name, abbreviated_name, max_slices)
1349 VALUES ('pl', 'PlanetLab Central', 'PLC', 100);