merge changes from HEAD
[plcapi.git] / planetlab4.sql
1 --
2 -- PlanetLab Central database schema
3 -- Version 4, 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 --
10 -- Copyright (C) 2006 The Trustees of Princeton University
11 --
12 -- $Id: planetlab4.sql,v 1.79 2007/07/12 17:48:19 tmack Exp $
13 --
14
15 SET client_encoding = 'UNICODE';
16
17 --------------------------------------------------------------------------------
18 -- Aggregates and store procedures
19 --------------------------------------------------------------------------------
20
21 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
22 -- PostgreSQL array.
23 CREATE AGGREGATE array_accum (
24     sfunc = array_append,
25     basetype = anyelement,
26     stype = anyarray,
27     initcond = '{}'
28 );
29
30 --------------------------------------------------------------------------------
31 -- Version
32 --------------------------------------------------------------------------------
33
34 -- Database version
35 CREATE TABLE plc_db_version (
36     version integer NOT NULL,
37     subversion integer NOT NULL DEFAULT 0
38 ) WITH OIDS;
39
40 INSERT INTO plc_db_version (version, subversion) VALUES (4, 2);
41
42 --------------------------------------------------------------------------------
43 -- Accounts
44 --------------------------------------------------------------------------------
45
46 -- Accounts
47 CREATE TABLE persons (
48     -- Mandatory
49     person_id serial PRIMARY KEY, -- Account identifier
50     email text NOT NULL, -- E-mail address
51     first_name text NOT NULL, -- First name
52     last_name text NOT NULL, -- Last name
53     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
54     enabled boolean NOT NULL DEFAULT false, -- Has been disabled
55
56     -- Password
57     password text NOT NULL DEFAULT 'nopass', -- Password (md5crypted)
58     verification_key text, -- Reset password key
59     verification_expires timestamp without time zone,
60
61     -- Optional
62     title text, -- Honorific
63     phone text, -- Telephone number
64     url text, -- Home page
65     bio text, -- Biography
66
67     -- Timestamps
68     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
69     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
70 ) WITH OIDS;
71 CREATE INDEX persons_email_idx ON persons (email) WHERE deleted IS false;
72
73 --------------------------------------------------------------------------------
74 -- Sites
75 --------------------------------------------------------------------------------
76
77 -- Sites
78 CREATE TABLE sites (
79     -- Mandatory
80     site_id serial PRIMARY KEY, -- Site identifier
81     login_base text NOT NULL, -- Site slice prefix
82     name text NOT NULL, -- Site name
83     abbreviated_name text NOT NULL, -- Site abbreviated name
84     enabled boolean NOT NULL Default true, -- Is this site enabled
85     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
86     is_public boolean NOT NULL DEFAULT true, -- Shows up in public lists
87     max_slices integer NOT NULL DEFAULT 0, -- Maximum number of slices
88     max_slivers integer NOT NULL DEFAULT 1000, -- Maximum number of instantiated slivers
89
90     -- Optional
91     latitude real,
92     longitude real,
93     url text,
94     ext_consortium_id integer,  -- external consortium id
95
96     -- Timestamps
97     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
98     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
99 ) WITH OIDS;
100 CREATE INDEX sites_login_base_idx ON sites (login_base) WHERE deleted IS false;
101
102 -- Account site membership
103 CREATE TABLE person_site (
104     person_id integer REFERENCES persons NOT NULL, -- Account identifier
105     site_id integer REFERENCES sites NOT NULL, -- Site identifier
106     is_primary boolean NOT NULL DEFAULT false, -- Is the primary site for this account
107     PRIMARY KEY (person_id, site_id)
108 );
109 CREATE INDEX person_site_person_id_idx ON person_site (person_id);
110 CREATE INDEX person_site_site_id_idx ON person_site (site_id);
111
112 -- Ordered by primary site first
113 CREATE VIEW person_site_ordered AS
114 SELECT person_id, site_id
115 FROM person_site
116 ORDER BY is_primary DESC;
117
118 -- Sites that each person is a member of
119 CREATE VIEW person_sites AS
120 SELECT person_id,
121 array_accum(site_id) AS site_ids
122 FROM person_site_ordered
123 GROUP BY person_id;
124
125 -- Accounts at each site
126 CREATE VIEW site_persons AS
127 SELECT site_id,
128 array_accum(person_id) AS person_ids
129 FROM person_site
130 GROUP BY site_id;
131
132 --------------------------------------------------------------------------------
133 -- Mailing Addresses
134 --------------------------------------------------------------------------------
135
136 CREATE TABLE address_types (
137     address_type_id serial PRIMARY KEY, -- Address type identifier
138     name text UNIQUE NOT NULL, -- Address type
139     description text -- Address type description
140 ) WITH OIDS;
141
142 INSERT INTO address_types (name) VALUES ('Personal');
143 INSERT INTO address_types (name) VALUES ('Shipping');
144 -- XXX Used to be Site
145 INSERT INTO address_types (name) VALUES ('Billing');
146
147 -- Mailing addresses
148 CREATE TABLE addresses (
149     address_id serial PRIMARY KEY, -- Address identifier
150     line1 text NOT NULL, -- Address line 1
151     line2 text, -- Address line 2
152     line3 text, -- Address line 3
153     city text NOT NULL, -- City
154     state text NOT NULL, -- State or province
155     postalcode text NOT NULL, -- Postal code
156     country text NOT NULL -- Country
157 ) WITH OIDS;
158
159 -- Each mailing address can be one of several types
160 CREATE TABLE address_address_type (
161     address_id integer REFERENCES addresses NOT NULL, -- Address identifier
162     address_type_id integer REFERENCES address_types NOT NULL, -- Address type
163     PRIMARY KEY (address_id, address_type_id)
164 ) WITH OIDS;
165 CREATE INDEX address_address_type_address_id_idx ON address_address_type (address_id);
166 CREATE INDEX address_address_type_address_type_id_idx ON address_address_type (address_type_id);
167
168 CREATE VIEW address_address_types AS
169 SELECT address_id,
170 array_accum(address_type_id) AS address_type_ids,
171 array_accum(address_types.name) AS address_types
172 FROM address_address_type
173 LEFT JOIN address_types USING (address_type_id)
174 GROUP BY address_id;
175
176 CREATE TABLE site_address (
177     site_id integer REFERENCES sites NOT NULL, -- Site identifier
178     address_id integer REFERENCES addresses NOT NULL, -- Address identifier
179     PRIMARY KEY (site_id, address_id)
180 ) WITH OIDS;
181 CREATE INDEX site_address_site_id_idx ON site_address (site_id);
182 CREATE INDEX site_address_address_id_idx ON site_address (address_id);
183
184 CREATE VIEW site_addresses AS
185 SELECT site_id,
186 array_accum(address_id) AS address_ids
187 FROM site_address
188 GROUP BY site_id;
189
190 --------------------------------------------------------------------------------
191 -- Authentication Keys
192 --------------------------------------------------------------------------------
193
194 -- Valid key types
195 CREATE TABLE key_types (
196     key_type text PRIMARY KEY -- Key type
197 ) WITH OIDS;
198 INSERT INTO key_types (key_type) VALUES ('ssh');
199
200 -- Authentication keys
201 CREATE TABLE keys (
202     key_id serial PRIMARY KEY, -- Key identifier
203     key_type text REFERENCES key_types NOT NULL, -- Key type
204     key text NOT NULL, -- Key material
205     is_blacklisted boolean NOT NULL DEFAULT false -- Has been blacklisted
206 ) WITH OIDS;
207
208 -- Account authentication key(s)
209 CREATE TABLE person_key (
210     key_id integer REFERENCES keys PRIMARY KEY, -- Key identifier
211     person_id integer REFERENCES persons NOT NULL -- Account identifier
212 ) WITH OIDS;
213 CREATE INDEX person_key_person_id_idx ON person_key (person_id);
214
215 CREATE VIEW person_keys AS
216 SELECT person_id,
217 array_accum(key_id) AS key_ids
218 FROM person_key
219 GROUP BY person_id;
220
221 --------------------------------------------------------------------------------
222 -- Account roles
223 --------------------------------------------------------------------------------
224
225 -- Valid account roles
226 CREATE TABLE roles (
227     role_id integer PRIMARY KEY, -- Role identifier
228     name text UNIQUE NOT NULL -- Role symbolic name
229 ) WITH OIDS;
230 INSERT INTO roles (role_id, name) VALUES (10, 'admin');
231 INSERT INTO roles (role_id, name) VALUES (20, 'pi');
232 INSERT INTO roles (role_id, name) VALUES (30, 'user');
233 INSERT INTO roles (role_id, name) VALUES (40, 'tech');
234
235 CREATE TABLE person_role (
236     person_id integer REFERENCES persons NOT NULL, -- Account identifier
237     role_id integer REFERENCES roles NOT NULL, -- Role identifier
238     PRIMARY KEY (person_id, role_id)
239 ) WITH OIDS;
240 CREATE INDEX person_role_person_id_idx ON person_role (person_id);
241
242 -- Account roles
243 CREATE VIEW person_roles AS
244 SELECT person_id,
245 array_accum(role_id) AS role_ids,
246 array_accum(roles.name) AS roles
247 FROM person_role
248 LEFT JOIN roles USING (role_id)
249 GROUP BY person_id;
250
251 --------------------------------------------------------------------------------
252 -- Nodes
253 --------------------------------------------------------------------------------
254
255 -- Valid node boot states
256 CREATE TABLE boot_states (
257     boot_state text PRIMARY KEY
258 ) WITH OIDS;
259 INSERT INTO boot_states (boot_state) VALUES ('boot');
260 INSERT INTO boot_states (boot_state) VALUES ('dbg');
261 INSERT INTO boot_states (boot_state) VALUES ('inst');
262 INSERT INTO boot_states (boot_state) VALUES ('rins');
263 INSERT INTO boot_states (boot_state) VALUES ('rcnf');
264 INSERT INTO boot_states (boot_state) VALUES ('new');
265
266 -- Nodes
267 CREATE TABLE nodes (
268     -- Mandatory
269     node_id serial PRIMARY KEY, -- Node identifier
270     hostname text NOT NULL, -- Node hostname
271     site_id integer REFERENCES sites NOT NULL, -- At which site 
272
273     boot_state text REFERENCES boot_states NOT NULL DEFAULT 'inst', -- Node boot state
274     deleted boolean NOT NULL DEFAULT false, -- Is deleted
275
276     -- Optional
277     model text, -- Hardware make and model
278     boot_nonce text, -- Random nonce updated by Boot Manager
279     version text, -- Boot CD version string updated by Boot Manager
280     ssh_rsa_key text, -- SSH host key updated by Boot Manager
281     key text, -- Node key generated by API when configuration file is downloaded
282
283     -- Timestamps
284     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
285     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
286     last_contact timestamp without time zone    
287 ) WITH OIDS;
288 CREATE INDEX nodes_hostname_idx ON nodes (hostname) WHERE deleted IS false;
289 CREATE INDEX nodes_site_id_idx ON nodes (site_id) WHERE deleted IS false;
290
291 -- Nodes at each site
292 CREATE VIEW site_nodes AS
293 SELECT site_id,
294 array_accum(node_id) AS node_ids
295 FROM nodes
296 GROUP BY site_id;
297
298 -- slice whitelist on nodes
299 CREATE TABLE node_slice_whitelist (
300     node_id integer REFERENCES nodes NOT NULL, -- Node id of whitelist
301     slice_id integer REFERENCES slices NOT NULL, -- Slice id thats allowd on this node
302     PRIMARY KEY (node_id, slice_id)
303 ) WITH OIDS;
304 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
305 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
306
307 -- Slices on each node
308 CREATE VIEW node_slices_whitelist AS
309 SELECT node_id, 
310 array_accum(slice_id) AS slice_ids_whitelist
311 FROM node_slice_whitelist
312 GROUP BY node_id;
313
314 --------------------------------------------------------------------------------
315 -- Node groups
316 --------------------------------------------------------------------------------
317
318 -- Node groups
319 CREATE TABLE nodegroups (
320     nodegroup_id serial PRIMARY KEY, -- Group identifier
321     name text UNIQUE NOT NULL, -- Group name
322     description text -- Group description
323 ) WITH OIDS;
324
325 -- Node group membership
326 CREATE TABLE nodegroup_node (
327     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Group identifier
328     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
329     PRIMARY KEY (nodegroup_id, node_id)
330 ) WITH OIDS;
331 CREATE INDEX nodegroup_node_nodegroup_id_idx ON nodegroup_node (nodegroup_id);
332 CREATE INDEX nodegroup_node_node_id_idx ON nodegroup_node (node_id);
333
334 -- Nodes in each node group
335 CREATE VIEW nodegroup_nodes AS
336 SELECT nodegroup_id,
337 array_accum(node_id) AS node_ids
338 FROM nodegroup_node
339 GROUP BY nodegroup_id;
340
341 -- Node groups that each node is a member of
342 CREATE VIEW node_nodegroups AS
343 SELECT node_id,
344 array_accum(nodegroup_id) AS nodegroup_ids
345 FROM nodegroup_node
346 GROUP BY node_id;
347
348 --------------------------------------------------------------------------------
349 -- Node configuration files
350 --------------------------------------------------------------------------------
351
352 CREATE TABLE conf_files (
353     conf_file_id serial PRIMARY KEY, -- Configuration file identifier
354     enabled bool NOT NULL DEFAULT true, -- Configuration file is active
355     source text NOT NULL, -- Relative path on the boot server where file can be downloaded
356     dest text NOT NULL, -- Absolute path where file should be installed
357     file_permissions text NOT NULL DEFAULT '0644', -- chmod(1) permissions
358     file_owner text NOT NULL DEFAULT 'root', -- chown(1) owner
359     file_group text NOT NULL DEFAULT 'root', -- chgrp(1) owner
360     preinstall_cmd text, -- Shell command to execute prior to installing
361     postinstall_cmd text, -- Shell command to execute after installing
362     error_cmd text, -- Shell command to execute if any error occurs
363     ignore_cmd_errors bool NOT NULL DEFAULT false, -- Install file anyway even if an error occurs
364     always_update bool NOT NULL DEFAULT false -- Always attempt to install file even if unchanged
365 );
366
367 CREATE TABLE conf_file_node (
368     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
369     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
370     PRIMARY KEY (conf_file_id, node_id)
371 );
372 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
373 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
374
375 -- Nodes linked to each configuration file
376 CREATE VIEW conf_file_nodes AS
377 SELECT conf_file_id,
378 array_accum(node_id) AS node_ids
379 FROM conf_file_node
380 GROUP BY conf_file_id;
381
382 -- Configuration files linked to each node
383 CREATE VIEW node_conf_files AS
384 SELECT node_id,
385 array_accum(conf_file_id) AS conf_file_ids
386 FROM conf_file_node
387 GROUP BY node_id;
388
389 CREATE TABLE conf_file_nodegroup (
390     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
391     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Node group identifier
392     PRIMARY KEY (conf_file_id, nodegroup_id)
393 );
394 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
395 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
396
397 -- Node groups linked to each configuration file
398 CREATE VIEW conf_file_nodegroups AS
399 SELECT conf_file_id,
400 array_accum(nodegroup_id) AS nodegroup_ids
401 FROM conf_file_nodegroup
402 GROUP BY conf_file_id;
403
404 -- Configuration files linked to each node group
405 CREATE VIEW nodegroup_conf_files AS
406 SELECT nodegroup_id,
407 array_accum(conf_file_id) AS conf_file_ids
408 FROM conf_file_nodegroup
409 GROUP BY nodegroup_id;
410
411 --------------------------------------------------------------------------------
412 -- Node network interfaces
413 --------------------------------------------------------------------------------
414
415 -- Valid network addressing schemes
416 CREATE TABLE network_types (
417     type text PRIMARY KEY -- Addressing scheme
418 ) WITH OIDS;
419 INSERT INTO network_types (type) VALUES ('ipv4');
420
421 -- Valid network configuration methods
422 CREATE TABLE network_methods (
423     method text PRIMARY KEY -- Configuration method
424 ) WITH OIDS;
425 INSERT INTO network_methods (method) VALUES ('static');
426 INSERT INTO network_methods (method) VALUES ('dhcp');
427 INSERT INTO network_methods (method) VALUES ('proxy');
428 INSERT INTO network_methods (method) VALUES ('tap');
429 INSERT INTO network_methods (method) VALUES ('ipmi');
430 INSERT INTO network_methods (method) VALUES ('unknown');
431
432 -- Node network interfaces
433 CREATE TABLE nodenetworks (
434     -- Mandatory
435     nodenetwork_id serial PRIMARY KEY, -- Network interface identifier
436     node_id integer REFERENCES nodes NOT NULL, -- Which node
437     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
438     type text REFERENCES network_types NOT NULL, -- Addressing scheme
439     method text REFERENCES network_methods NOT NULL, -- Configuration method
440
441     -- Optional, depending on type and method
442     ip text, -- IP address
443     mac text, -- MAC address
444     gateway text, -- Default gateway address
445     network text, -- Network address
446     broadcast text, -- Network broadcast address
447     netmask text, -- Network mask
448     dns1 text, -- Primary DNS server
449     dns2 text, -- Secondary DNS server
450     bwlimit integer, -- Bandwidth limit in bps
451     hostname text -- Hostname of this interface
452 ) WITH OIDS;
453 CREATE INDEX nodenetworks_node_id_idx ON nodenetworks (node_id);
454
455 -- Ordered by primary interface first
456 CREATE VIEW nodenetworks_ordered AS
457 SELECT node_id, nodenetwork_id
458 FROM nodenetworks
459 ORDER BY is_primary DESC;
460
461 -- Network interfaces on each node
462 CREATE VIEW node_nodenetworks AS
463 SELECT node_id,
464 array_accum(nodenetwork_id) AS nodenetwork_ids
465 FROM nodenetworks_ordered
466 GROUP BY node_id;
467
468 --------------------------------------------------------------------------------
469 -- Power control units (PCUs)
470 --------------------------------------------------------------------------------
471
472 CREATE TABLE pcus (
473     -- Mandatory
474     pcu_id serial PRIMARY KEY, -- PCU identifier
475     site_id integer REFERENCES sites NOT NULL, -- Site identifier
476     hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
477     ip text NOT NULL, -- IP, not necessarily unique
478
479     -- Optional
480     protocol text, -- Protocol, e.g. ssh or https or telnet
481     username text, -- Username, if applicable
482     "password" text, -- Password, if applicable
483     model text, -- Model, e.g. BayTech or iPal
484     notes text -- Random notes
485 ) WITH OIDS;
486 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
487
488 CREATE VIEW site_pcus AS
489 SELECT site_id,
490 array_accum(pcu_id) AS pcu_ids
491 FROM pcus
492 GROUP BY site_id;
493
494 CREATE TABLE pcu_node (
495     pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
496     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
497     port integer NOT NULL, -- Port number
498     PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
499     UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
500 );
501 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
502 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
503
504 CREATE VIEW node_pcus AS
505 SELECT node_id,
506 array_accum(pcu_id) AS pcu_ids,
507 array_accum(port) AS ports
508 FROM pcu_node
509 GROUP BY node_id;
510
511 CREATE VIEW pcu_nodes AS
512 SELECT pcu_id,
513 array_accum(node_id) AS node_ids,
514 array_accum(port) AS ports
515 FROM pcu_node
516 GROUP BY pcu_id;
517
518 --------------------------------------------------------------------------------
519 -- Slices
520 --------------------------------------------------------------------------------
521
522 CREATE TABLE slice_instantiations (
523     instantiation text PRIMARY KEY
524 ) WITH OIDS;
525 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
526 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
527 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
528 INSERT INTO slice_instantiations (instantiation) VALUES ('nm-controller'); -- NM Controller
529
530 -- Slices
531 CREATE TABLE slices (
532     slice_id serial PRIMARY KEY, -- Slice identifier
533     site_id integer REFERENCES sites NOT NULL, -- Site identifier
534
535     name text NOT NULL, -- Slice name
536     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
537     url text, -- Project URL
538     description text, -- Project description
539
540     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
541
542     creator_person_id integer REFERENCES persons, -- Creator
543     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
544     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
545
546     is_deleted boolean NOT NULL DEFAULT false
547 ) WITH OIDS;
548 CREATE INDEX slices_site_id_idx ON slices (site_id) WHERE is_deleted IS false;
549 CREATE INDEX slices_name_idx ON slices (name) WHERE is_deleted IS false;
550
551 -- Slivers
552 CREATE TABLE slice_node (
553     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
554     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
555     PRIMARY KEY (slice_id, node_id)
556 ) WITH OIDS;
557 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
558 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
559
560 -- Synonym for slice_node
561 CREATE VIEW slivers AS
562 SELECT * FROM slice_node;
563
564 -- Nodes in each slice
565 CREATE VIEW slice_nodes AS
566 SELECT slice_id,
567 array_accum(node_id) AS node_ids
568 FROM slice_node
569 GROUP BY slice_id;
570
571 -- Slices on each node
572 CREATE VIEW node_slices AS
573 SELECT node_id,
574 array_accum(slice_id) AS slice_ids
575 FROM slice_node
576 GROUP BY node_id;
577
578 -- Slices at each site
579 CREATE VIEW site_slices AS
580 SELECT site_id,
581 array_accum(slice_id) AS slice_ids
582 FROM slices
583 WHERE is_deleted is false
584 GROUP BY site_id;
585
586 -- Slice membership
587 CREATE TABLE slice_person (
588     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
589     person_id integer REFERENCES persons NOT NULL, -- Account identifier
590     PRIMARY KEY (slice_id, person_id)
591 ) WITH OIDS;
592 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
593 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
594
595 -- Members of the slice
596 CREATE VIEW slice_persons AS
597 SELECT slice_id,
598 array_accum(person_id) AS person_ids
599 FROM slice_person
600 GROUP BY slice_id;
601
602 -- Slices of which each person is a member
603 CREATE VIEW person_slices AS
604 SELECT person_id,
605 array_accum(slice_id) AS slice_ids
606 FROM slice_person
607 GROUP BY person_id;
608
609 --------------------------------------------------------------------------------
610 -- Slice attributes
611 --------------------------------------------------------------------------------
612
613 -- Slice attribute types
614 CREATE TABLE slice_attribute_types (
615     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
616     name text UNIQUE NOT NULL, -- Attribute name
617     description text, -- Attribute description
618     min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
619 ) WITH OIDS;
620
621 -- Slice/sliver attributes
622 CREATE TABLE slice_attribute (
623     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
624     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
625     node_id integer REFERENCES nodes, -- Sliver attribute if set
626     nodegroup_id integer REFERENCES nodegroups, -- Node group attribute if set
627     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
628     value text
629 ) WITH OIDS;
630 CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
631 CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
632 CREATE INDEX slice_attribute_nodegroup_id_idx ON slice_attribute (nodegroup_id);
633
634 CREATE VIEW slice_attributes AS
635 SELECT slice_id,
636 array_accum(slice_attribute_id) AS slice_attribute_ids
637 FROM slice_attribute
638 GROUP BY slice_id;
639
640 --------------------------------------------------------------------------------
641 -- Initscripts
642 --------------------------------------------------------------------------------
643
644 -- Initscripts
645 CREATE TABLE initscripts (
646     initscript_id serial PRIMARY KEY, -- Initscript identifier
647     name text NOT NULL, -- Initscript name
648     enabled bool NOT NULL DEFAULT true, -- Initscript is active
649     script text NOT NULL, -- Initscript
650     UNIQUE (name)
651 ) WITH OIDS;
652 CREATE INDEX initscripts_name_idx ON initscripts (name);
653
654
655 --------------------------------------------------------------------------------
656 -- Peers
657 --------------------------------------------------------------------------------
658
659 -- Peers
660 CREATE TABLE peers (
661     peer_id serial PRIMARY KEY, -- Peer identifier
662     peername text NOT NULL, -- Peer name
663     peer_url text NOT NULL, -- (HTTPS) URL of the peer PLCAPI interface
664     cacert text, -- (SSL) Public certificate of peer API server
665     key text, -- (GPG) Public key used for authentication
666     deleted boolean NOT NULL DEFAULT false
667 ) WITH OIDS;
668 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
669
670 -- Objects at each peer
671 CREATE TABLE peer_site (
672     site_id integer REFERENCES sites PRIMARY KEY, -- Local site identifier
673     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
674     peer_site_id integer NOT NULL, -- Foreign site identifier at peer
675     UNIQUE (peer_id, peer_site_id) -- The same foreign site should not be cached twice
676 ) WITH OIDS;
677 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
678
679 CREATE VIEW peer_sites AS
680 SELECT peer_id,
681 array_accum(site_id) AS site_ids,
682 array_accum(peer_site_id) AS peer_site_ids
683 FROM peer_site
684 GROUP BY peer_id;
685
686 CREATE TABLE peer_person (
687     person_id integer REFERENCES persons PRIMARY KEY, -- Local user identifier
688     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
689     peer_person_id integer NOT NULL, -- Foreign user identifier at peer
690     UNIQUE (peer_id, peer_person_id) -- The same foreign user should not be cached twice
691 ) WITH OIDS;
692 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
693
694 CREATE VIEW peer_persons AS
695 SELECT peer_id,
696 array_accum(person_id) AS person_ids,
697 array_accum(peer_person_id) AS peer_person_ids
698 FROM peer_person
699 GROUP BY peer_id;
700
701 CREATE TABLE peer_key (
702     key_id integer REFERENCES keys PRIMARY KEY, -- Local key identifier
703     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
704     peer_key_id integer NOT NULL, -- Foreign key identifier at peer
705     UNIQUE (peer_id, peer_key_id) -- The same foreign key should not be cached twice
706 ) WITH OIDS;
707 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
708
709 CREATE VIEW peer_keys AS
710 SELECT peer_id,
711 array_accum(key_id) AS key_ids,
712 array_accum(peer_key_id) AS peer_key_ids
713 FROM peer_key
714 GROUP BY peer_id;
715
716 CREATE TABLE peer_node (
717     node_id integer REFERENCES nodes PRIMARY KEY, -- Local node identifier
718     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
719     peer_node_id integer NOT NULL, -- Foreign node identifier
720     UNIQUE (peer_id, peer_node_id) -- The same foreign node should not be cached twice
721 ) WITH OIDS;
722 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
723
724 CREATE VIEW peer_nodes AS
725 SELECT peer_id,
726 array_accum(node_id) AS node_ids,
727 array_accum(peer_node_id) AS peer_node_ids
728 FROM peer_node
729 GROUP BY peer_id;
730
731 CREATE TABLE peer_slice (
732     slice_id integer REFERENCES slices PRIMARY KEY, -- Local slice identifier
733     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
734     peer_slice_id integer NOT NULL, -- Slice identifier at peer
735     UNIQUE (peer_id, peer_slice_id) -- The same foreign slice should not be cached twice
736 ) WITH OIDS;
737 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
738
739 CREATE VIEW peer_slices AS
740 SELECT peer_id,
741 array_accum(slice_id) AS slice_ids,
742 array_accum(peer_slice_id) AS peer_slice_ids
743 FROM peer_slice
744 GROUP BY peer_id;
745
746 --------------------------------------------------------------------------------
747 -- Authenticated sessions
748 --------------------------------------------------------------------------------
749
750 -- Authenticated sessions
751 CREATE TABLE sessions (
752     session_id text PRIMARY KEY, -- Session identifier
753     expires timestamp without time zone
754 ) WITH OIDS;
755
756 -- People can have multiple sessions
757 CREATE TABLE person_session (
758     person_id integer REFERENCES persons NOT NULL, -- Account identifier
759     session_id text REFERENCES sessions NOT NULL, -- Session identifier
760     PRIMARY KEY (person_id, session_id),
761     UNIQUE (session_id) -- Sessions are unique
762 ) WITH OIDS;
763 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
764
765 -- Nodes can have only one session
766 CREATE TABLE node_session (
767     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
768     session_id text REFERENCES sessions NOT NULL, -- Session identifier
769     UNIQUE (node_id), -- Nodes can have only one session
770     UNIQUE (session_id) -- Sessions are unique
771 ) WITH OIDS;
772
773 --------------------------------------------------------------------------------
774 -- Message templates
775 --------------------------------------------------------------------------------
776
777 CREATE TABLE messages (
778     message_id text PRIMARY KEY, -- Message name
779     subject text, -- Message summary
780     template text, -- Message template
781     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
782 ) WITH OIDS;
783
784 --------------------------------------------------------------------------------
785 -- Events
786 --------------------------------------------------------------------------------
787
788 -- Events
789 CREATE TABLE events (
790     event_id serial PRIMARY KEY,  -- Event identifier
791     person_id integer REFERENCES persons, -- Person responsible for event, if any
792     node_id integer REFERENCES nodes, -- Node responsible for event, if any
793     auth_type text, -- Type of auth used. i.e. AuthMethod
794     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
795     call_name text NOT NULL, -- Call responsible for this event
796     call text NOT NULL, -- Call responsible for this event, including parameters
797     message text, -- High level description of this event
798     runtime float DEFAULT 0, -- Event run time
799     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
800 ) WITH OIDS;
801
802 -- Database object(s) that may have been affected by a particular event
803 CREATE TABLE event_object (
804     event_id integer REFERENCES events NOT NULL, -- Event identifier
805     object_id integer NOT NULL, -- Object identifier
806     object_type text NOT NULL Default 'Unknown' -- What type of object is this event affecting
807 ) WITH OIDS;
808 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
809 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
810 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
811
812 CREATE VIEW event_objects AS
813 SELECT event_id,
814 array_accum(object_id) AS object_ids,
815 array_accum(object_type) AS object_types
816 FROM event_object
817 GROUP BY event_id;
818
819 --------------------------------------------------------------------------------
820 -- Useful views
821 --------------------------------------------------------------------------------
822
823 CREATE OR REPLACE VIEW view_events AS
824 SELECT
825 events.event_id,
826 events.person_id,
827 events.node_id,
828 events.auth_type,
829 events.fault_code,
830 events.call_name,
831 events.call,
832 events.message,
833 events.runtime,
834 CAST(date_part('epoch', events.time) AS bigint) AS time,
835 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
836 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
837 FROM events;
838
839 CREATE OR REPLACE VIEW view_persons AS
840 SELECT
841 persons.person_id,
842 persons.email,
843 persons.first_name,
844 persons.last_name,
845 persons.deleted,
846 persons.enabled,
847 persons.password,
848 persons.verification_key,
849 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
850 persons.title,
851 persons.phone,
852 persons.url,
853 persons.bio,
854 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
855 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
856 peer_person.peer_id,
857 peer_person.peer_person_id,
858 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
859 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
860 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
861 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
862 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
863 FROM persons
864 LEFT JOIN peer_person USING (person_id);
865
866 CREATE OR REPLACE VIEW view_peers AS
867 SELECT 
868 peers.*, 
869 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
870 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
871 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
872 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
873 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
874 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
875 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
876 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
877 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
878 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
879 FROM peers;
880
881 CREATE OR REPLACE VIEW view_nodes AS
882 SELECT
883 nodes.node_id,
884 nodes.hostname,
885 nodes.site_id,
886 nodes.boot_state,
887 nodes.deleted,
888 nodes.model,
889 nodes.boot_nonce,
890 nodes.version,
891 nodes.ssh_rsa_key,
892 nodes.key,
893 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
894 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
895 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
896 peer_node.peer_id,
897 peer_node.peer_node_id,
898 COALESCE((SELECT nodenetwork_ids FROM node_nodenetworks WHERE node_nodenetworks.node_id = nodes.node_id), '{}') AS nodenetwork_ids,
899 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
900 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
901 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') AS slice_ids_whitelist,
902 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
903 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
904 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
905 node_session.session_id AS session
906 FROM nodes
907 LEFT JOIN peer_node USING (node_id)
908 LEFT JOIN node_session USING (node_id);
909
910 CREATE OR REPLACE VIEW view_nodegroups AS
911 SELECT
912 nodegroups.*,
913 COALESCE((SELECT node_ids FROM nodegroup_nodes WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') AS node_ids,
914 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') AS conf_file_ids
915 FROM nodegroups;
916
917 CREATE OR REPLACE VIEW view_conf_files AS
918 SELECT
919 conf_files.*,
920 COALESCE((SELECT node_ids FROM conf_file_nodes WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') AS node_ids,
921 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') AS nodegroup_ids
922 FROM conf_files;
923
924 CREATE OR REPLACE VIEW view_pcus AS
925 SELECT
926 pcus.*,
927 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
928 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
929 FROM pcus;
930
931 CREATE OR REPLACE VIEW view_sites AS
932 SELECT
933 sites.site_id,
934 sites.login_base,
935 sites.name,
936 sites.abbreviated_name,
937 sites.deleted,
938 sites.enabled,
939 sites.is_public,
940 sites.max_slices,
941 sites.max_slivers,
942 sites.latitude,
943 sites.longitude,
944 sites.url,
945 sites.ext_consortium_id,
946 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
947 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
948 peer_site.peer_id,
949 peer_site.peer_site_id,
950 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
951 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
952 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
953 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
954 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
955 FROM sites
956 LEFT JOIN peer_site USING (site_id);
957
958 CREATE OR REPLACE VIEW view_addresses AS
959 SELECT
960 addresses.*,
961 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
962 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
963 FROM addresses;
964
965 CREATE OR REPLACE VIEW view_keys AS
966 SELECT
967 keys.*,
968 person_key.person_id,
969 peer_key.peer_id,
970 peer_key.peer_key_id
971 FROM keys
972 LEFT JOIN person_key USING (key_id)
973 LEFT JOIN peer_key USING (key_id);
974
975 CREATE OR REPLACE VIEW view_slices AS
976 SELECT
977 slices.slice_id,
978 slices.site_id,
979 slices.name,
980 slices.instantiation,
981 slices.url,
982 slices.description,
983 slices.max_nodes,
984 slices.creator_person_id,
985 slices.is_deleted,
986 CAST(date_part('epoch', slices.created) AS bigint) AS created,
987 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
988 peer_slice.peer_id,
989 peer_slice.peer_slice_id,
990 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
991 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
992 COALESCE((SELECT slice_attribute_ids FROM slice_attributes WHERE slice_attributes.slice_id = slices.slice_id), '{}') AS slice_attribute_ids
993 FROM slices
994 LEFT JOIN peer_slice USING (slice_id);
995
996 CREATE OR REPLACE VIEW view_slice_attributes AS
997 SELECT
998 slice_attribute.slice_attribute_id,
999 slice_attribute.slice_id,
1000 slice_attribute.node_id,
1001 slice_attribute.nodegroup_id,
1002 slice_attribute_types.attribute_type_id,
1003 slice_attribute_types.name,
1004 slice_attribute_types.description,
1005 slice_attribute_types.min_role_id,
1006 slice_attribute.value
1007 FROM slice_attribute
1008 INNER JOIN slice_attribute_types USING (attribute_type_id);
1009
1010 CREATE OR REPLACE VIEW view_sessions AS
1011 SELECT
1012 sessions.session_id,
1013 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1014 person_session.person_id,
1015 node_session.node_id
1016 FROM sessions
1017 LEFT JOIN person_session USING (session_id)
1018 LEFT JOIN node_session USING (session_id);
1019
1020 --------------------------------------------------------------------------------
1021 -- Built-in maintenance account and default site
1022 --------------------------------------------------------------------------------
1023
1024 INSERT INTO persons
1025 (first_name, last_name, email, password, enabled)
1026 VALUES
1027 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1028
1029 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1030 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1031 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1032 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1033
1034 INSERT INTO sites
1035 (login_base, name, abbreviated_name, max_slices)
1036 VALUES
1037 ('pl', 'PlanetLab Central', 'PLC', 100);