Merge from HEAD. Signed off by tmack.
[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$
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 --------------------------------------------------------------------------------
299 -- Node groups
300 --------------------------------------------------------------------------------
301
302 -- Node groups
303 CREATE TABLE nodegroups (
304     nodegroup_id serial PRIMARY KEY, -- Group identifier
305     name text UNIQUE NOT NULL, -- Group name
306     description text -- Group description
307 ) WITH OIDS;
308
309 -- Node group membership
310 CREATE TABLE nodegroup_node (
311     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Group identifier
312     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
313     PRIMARY KEY (nodegroup_id, node_id)
314 ) WITH OIDS;
315 CREATE INDEX nodegroup_node_nodegroup_id_idx ON nodegroup_node (nodegroup_id);
316 CREATE INDEX nodegroup_node_node_id_idx ON nodegroup_node (node_id);
317
318 -- Nodes in each node group
319 CREATE VIEW nodegroup_nodes AS
320 SELECT nodegroup_id,
321 array_accum(node_id) AS node_ids
322 FROM nodegroup_node
323 GROUP BY nodegroup_id;
324
325 -- Node groups that each node is a member of
326 CREATE VIEW node_nodegroups AS
327 SELECT node_id,
328 array_accum(nodegroup_id) AS nodegroup_ids
329 FROM nodegroup_node
330 GROUP BY node_id;
331
332 --------------------------------------------------------------------------------
333 -- Node configuration files
334 --------------------------------------------------------------------------------
335
336 CREATE TABLE conf_files (
337     conf_file_id serial PRIMARY KEY, -- Configuration file identifier
338     enabled bool NOT NULL DEFAULT true, -- Configuration file is active
339     source text NOT NULL, -- Relative path on the boot server where file can be downloaded
340     dest text NOT NULL, -- Absolute path where file should be installed
341     file_permissions text NOT NULL DEFAULT '0644', -- chmod(1) permissions
342     file_owner text NOT NULL DEFAULT 'root', -- chown(1) owner
343     file_group text NOT NULL DEFAULT 'root', -- chgrp(1) owner
344     preinstall_cmd text, -- Shell command to execute prior to installing
345     postinstall_cmd text, -- Shell command to execute after installing
346     error_cmd text, -- Shell command to execute if any error occurs
347     ignore_cmd_errors bool NOT NULL DEFAULT false, -- Install file anyway even if an error occurs
348     always_update bool NOT NULL DEFAULT false -- Always attempt to install file even if unchanged
349 );
350
351 CREATE TABLE conf_file_node (
352     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
353     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
354     PRIMARY KEY (conf_file_id, node_id)
355 );
356 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
357 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
358
359 -- Nodes linked to each configuration file
360 CREATE VIEW conf_file_nodes AS
361 SELECT conf_file_id,
362 array_accum(node_id) AS node_ids
363 FROM conf_file_node
364 GROUP BY conf_file_id;
365
366 -- Configuration files linked to each node
367 CREATE VIEW node_conf_files AS
368 SELECT node_id,
369 array_accum(conf_file_id) AS conf_file_ids
370 FROM conf_file_node
371 GROUP BY node_id;
372
373 CREATE TABLE conf_file_nodegroup (
374     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
375     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Node group identifier
376     PRIMARY KEY (conf_file_id, nodegroup_id)
377 );
378 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
379 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
380
381 -- Node groups linked to each configuration file
382 CREATE VIEW conf_file_nodegroups AS
383 SELECT conf_file_id,
384 array_accum(nodegroup_id) AS nodegroup_ids
385 FROM conf_file_nodegroup
386 GROUP BY conf_file_id;
387
388 -- Configuration files linked to each node group
389 CREATE VIEW nodegroup_conf_files AS
390 SELECT nodegroup_id,
391 array_accum(conf_file_id) AS conf_file_ids
392 FROM conf_file_nodegroup
393 GROUP BY nodegroup_id;
394
395 --------------------------------------------------------------------------------
396 -- Node network interfaces
397 --------------------------------------------------------------------------------
398
399 -- Valid network addressing schemes
400 CREATE TABLE network_types (
401     type text PRIMARY KEY -- Addressing scheme
402 ) WITH OIDS;
403 INSERT INTO network_types (type) VALUES ('ipv4');
404
405 -- Valid network configuration methods
406 CREATE TABLE network_methods (
407     method text PRIMARY KEY -- Configuration method
408 ) WITH OIDS;
409 INSERT INTO network_methods (method) VALUES ('static');
410 INSERT INTO network_methods (method) VALUES ('dhcp');
411 INSERT INTO network_methods (method) VALUES ('proxy');
412 INSERT INTO network_methods (method) VALUES ('tap');
413 INSERT INTO network_methods (method) VALUES ('ipmi');
414 INSERT INTO network_methods (method) VALUES ('unknown');
415
416 -- Node network interfaces
417 CREATE TABLE nodenetworks (
418     -- Mandatory
419     nodenetwork_id serial PRIMARY KEY, -- Network interface identifier
420     node_id integer REFERENCES nodes NOT NULL, -- Which node
421     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
422     type text REFERENCES network_types NOT NULL, -- Addressing scheme
423     method text REFERENCES network_methods NOT NULL, -- Configuration method
424
425     -- Optional, depending on type and method
426     ip text, -- IP address
427     mac text, -- MAC address
428     gateway text, -- Default gateway address
429     network text, -- Network address
430     broadcast text, -- Network broadcast address
431     netmask text, -- Network mask
432     dns1 text, -- Primary DNS server
433     dns2 text, -- Secondary DNS server
434     bwlimit integer, -- Bandwidth limit in bps
435     hostname text -- Hostname of this interface
436 ) WITH OIDS;
437 CREATE INDEX nodenetworks_node_id_idx ON nodenetworks (node_id);
438
439 -- Ordered by primary interface first
440 CREATE VIEW nodenetworks_ordered AS
441 SELECT node_id, nodenetwork_id
442 FROM nodenetworks
443 ORDER BY is_primary DESC;
444
445 -- Network interfaces on each node
446 CREATE VIEW node_nodenetworks AS
447 SELECT node_id,
448 array_accum(nodenetwork_id) AS nodenetwork_ids
449 FROM nodenetworks_ordered
450 GROUP BY node_id;
451
452 --------------------------------------------------------------------------------
453 -- Power control units (PCUs)
454 --------------------------------------------------------------------------------
455
456 CREATE TABLE pcus (
457     -- Mandatory
458     pcu_id serial PRIMARY KEY, -- PCU identifier
459     site_id integer REFERENCES sites NOT NULL, -- Site identifier
460     hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
461     ip text NOT NULL, -- IP, not necessarily unique
462
463     -- Optional
464     protocol text, -- Protocol, e.g. ssh or https or telnet
465     username text, -- Username, if applicable
466     "password" text, -- Password, if applicable
467     model text, -- Model, e.g. BayTech or iPal
468     notes text -- Random notes
469 ) WITH OIDS;
470 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
471
472 CREATE VIEW site_pcus AS
473 SELECT site_id,
474 array_accum(pcu_id) AS pcu_ids
475 FROM pcus
476 GROUP BY site_id;
477
478 CREATE TABLE pcu_node (
479     pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
480     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
481     port integer NOT NULL, -- Port number
482     PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
483     UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
484 );
485 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
486 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
487
488 CREATE VIEW node_pcus AS
489 SELECT node_id,
490 array_accum(pcu_id) AS pcu_ids,
491 array_accum(port) AS ports
492 FROM pcu_node
493 GROUP BY node_id;
494
495 CREATE VIEW pcu_nodes AS
496 SELECT pcu_id,
497 array_accum(node_id) AS node_ids,
498 array_accum(port) AS ports
499 FROM pcu_node
500 GROUP BY pcu_id;
501
502 --------------------------------------------------------------------------------
503 -- Slices
504 --------------------------------------------------------------------------------
505
506 CREATE TABLE slice_instantiations (
507     instantiation text PRIMARY KEY
508 ) WITH OIDS;
509 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
510 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
511 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
512
513 -- Slices
514 CREATE TABLE slices (
515     slice_id serial PRIMARY KEY, -- Slice identifier
516     site_id integer REFERENCES sites NOT NULL, -- Site identifier
517
518     name text NOT NULL, -- Slice name
519     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
520     url text, -- Project URL
521     description text, -- Project description
522
523     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
524
525     creator_person_id integer REFERENCES persons, -- Creator
526     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
527     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
528
529     is_deleted boolean NOT NULL DEFAULT false
530 ) WITH OIDS;
531 CREATE INDEX slices_site_id_idx ON slices (site_id) WHERE is_deleted IS false;
532 CREATE INDEX slices_name_idx ON slices (name) WHERE is_deleted IS false;
533
534 -- Slivers
535 CREATE TABLE slice_node (
536     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
537     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
538     PRIMARY KEY (slice_id, node_id)
539 ) WITH OIDS;
540 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
541 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
542
543 -- Synonym for slice_node
544 CREATE VIEW slivers AS
545 SELECT * FROM slice_node;
546
547 -- Nodes in each slice
548 CREATE VIEW slice_nodes AS
549 SELECT slice_id,
550 array_accum(node_id) AS node_ids
551 FROM slice_node
552 GROUP BY slice_id;
553
554 -- Slices on each node
555 CREATE VIEW node_slices AS
556 SELECT node_id,
557 array_accum(slice_id) AS slice_ids
558 FROM slice_node
559 GROUP BY node_id;
560
561 -- Slices at each site
562 CREATE VIEW site_slices AS
563 SELECT site_id,
564 array_accum(slice_id) AS slice_ids
565 FROM slices
566 WHERE is_deleted is false
567 GROUP BY site_id;
568
569 -- Slice membership
570 CREATE TABLE slice_person (
571     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
572     person_id integer REFERENCES persons NOT NULL, -- Account identifier
573     PRIMARY KEY (slice_id, person_id)
574 ) WITH OIDS;
575 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
576 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
577
578 -- Members of the slice
579 CREATE VIEW slice_persons AS
580 SELECT slice_id,
581 array_accum(person_id) AS person_ids
582 FROM slice_person
583 GROUP BY slice_id;
584
585 -- Slices of which each person is a member
586 CREATE VIEW person_slices AS
587 SELECT person_id,
588 array_accum(slice_id) AS slice_ids
589 FROM slice_person
590 GROUP BY person_id;
591
592 --------------------------------------------------------------------------------
593 -- Slice attributes
594 --------------------------------------------------------------------------------
595
596 -- Slice attribute types
597 CREATE TABLE slice_attribute_types (
598     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
599     name text UNIQUE NOT NULL, -- Attribute name
600     description text, -- Attribute description
601     min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
602 ) WITH OIDS;
603
604 -- Slice/sliver attributes
605 CREATE TABLE slice_attribute (
606     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
607     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
608     node_id integer REFERENCES nodes, -- Sliver attribute if set
609     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
610     value text
611 ) WITH OIDS;
612 CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
613 CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
614
615 CREATE VIEW slice_attributes AS
616 SELECT slice_id,
617 array_accum(slice_attribute_id) AS slice_attribute_ids
618 FROM slice_attribute
619 GROUP BY slice_id;
620
621 --------------------------------------------------------------------------------
622 -- Initscripts
623 --------------------------------------------------------------------------------
624
625 -- Initscripts
626 CREATE TABLE initscripts (
627     initscript_id serial PRIMARY KEY, -- Initscript identifier
628     name text NOT NULL, -- Initscript name
629     enabled bool NOT NULL DEFAULT true, -- Initscript is active
630     script text NOT NULL, -- Initscript
631     UNIQUE (name)
632 ) WITH OIDS;
633 CREATE INDEX initscripts_name_idx ON initscripts (name);
634
635
636 --------------------------------------------------------------------------------
637 -- Peers
638 --------------------------------------------------------------------------------
639
640 -- Peers
641 CREATE TABLE peers (
642     peer_id serial PRIMARY KEY, -- Peer identifier
643     peername text NOT NULL, -- Peer name
644     peer_url text NOT NULL, -- (HTTPS) URL of the peer PLCAPI interface
645     cacert text, -- (SSL) Public certificate of peer API server
646     key text, -- (GPG) Public key used for authentication
647     deleted boolean NOT NULL DEFAULT false
648 ) WITH OIDS;
649 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
650
651 -- Objects at each peer
652 CREATE TABLE peer_site (
653     site_id integer REFERENCES sites PRIMARY KEY, -- Local site identifier
654     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
655     peer_site_id integer NOT NULL, -- Foreign site identifier at peer
656     UNIQUE (peer_id, peer_site_id) -- The same foreign site should not be cached twice
657 ) WITH OIDS;
658 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
659
660 CREATE VIEW peer_sites AS
661 SELECT peer_id,
662 array_accum(site_id) AS site_ids,
663 array_accum(peer_site_id) AS peer_site_ids
664 FROM peer_site
665 GROUP BY peer_id;
666
667 CREATE TABLE peer_person (
668     person_id integer REFERENCES persons PRIMARY KEY, -- Local user identifier
669     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
670     peer_person_id integer NOT NULL, -- Foreign user identifier at peer
671     UNIQUE (peer_id, peer_person_id) -- The same foreign user should not be cached twice
672 ) WITH OIDS;
673 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
674
675 CREATE VIEW peer_persons AS
676 SELECT peer_id,
677 array_accum(person_id) AS person_ids,
678 array_accum(peer_person_id) AS peer_person_ids
679 FROM peer_person
680 GROUP BY peer_id;
681
682 CREATE TABLE peer_key (
683     key_id integer REFERENCES keys PRIMARY KEY, -- Local key identifier
684     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
685     peer_key_id integer NOT NULL, -- Foreign key identifier at peer
686     UNIQUE (peer_id, peer_key_id) -- The same foreign key should not be cached twice
687 ) WITH OIDS;
688 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
689
690 CREATE VIEW peer_keys AS
691 SELECT peer_id,
692 array_accum(key_id) AS key_ids,
693 array_accum(peer_key_id) AS peer_key_ids
694 FROM peer_key
695 GROUP BY peer_id;
696
697 CREATE TABLE peer_node (
698     node_id integer REFERENCES nodes PRIMARY KEY, -- Local node identifier
699     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
700     peer_node_id integer NOT NULL, -- Foreign node identifier
701     UNIQUE (peer_id, peer_node_id) -- The same foreign node should not be cached twice
702 ) WITH OIDS;
703 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
704
705 CREATE VIEW peer_nodes AS
706 SELECT peer_id,
707 array_accum(node_id) AS node_ids,
708 array_accum(peer_node_id) AS peer_node_ids
709 FROM peer_node
710 GROUP BY peer_id;
711
712 CREATE TABLE peer_slice (
713     slice_id integer REFERENCES slices PRIMARY KEY, -- Local slice identifier
714     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
715     peer_slice_id integer NOT NULL, -- Slice identifier at peer
716     UNIQUE (peer_id, peer_slice_id) -- The same foreign slice should not be cached twice
717 ) WITH OIDS;
718 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
719
720 CREATE VIEW peer_slices AS
721 SELECT peer_id,
722 array_accum(slice_id) AS slice_ids,
723 array_accum(peer_slice_id) AS peer_slice_ids
724 FROM peer_slice
725 GROUP BY peer_id;
726
727 --------------------------------------------------------------------------------
728 -- Authenticated sessions
729 --------------------------------------------------------------------------------
730
731 -- Authenticated sessions
732 CREATE TABLE sessions (
733     session_id text PRIMARY KEY, -- Session identifier
734     expires timestamp without time zone
735 ) WITH OIDS;
736
737 -- People can have multiple sessions
738 CREATE TABLE person_session (
739     person_id integer REFERENCES persons NOT NULL, -- Account identifier
740     session_id text REFERENCES sessions NOT NULL, -- Session identifier
741     PRIMARY KEY (person_id, session_id),
742     UNIQUE (session_id) -- Sessions are unique
743 ) WITH OIDS;
744 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
745
746 -- Nodes can have only one session
747 CREATE TABLE node_session (
748     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
749     session_id text REFERENCES sessions NOT NULL, -- Session identifier
750     UNIQUE (node_id), -- Nodes can have only one session
751     UNIQUE (session_id) -- Sessions are unique
752 ) WITH OIDS;
753
754 --------------------------------------------------------------------------------
755 -- Message templates
756 --------------------------------------------------------------------------------
757
758 CREATE TABLE messages (
759     message_id text PRIMARY KEY, -- Message name
760     subject text, -- Message summary
761     template text, -- Message template
762     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
763 ) WITH OIDS;
764
765 --------------------------------------------------------------------------------
766 -- Events
767 --------------------------------------------------------------------------------
768
769 -- Events
770 CREATE TABLE events (
771     event_id serial PRIMARY KEY,  -- Event identifier
772     person_id integer REFERENCES persons, -- Person responsible for event, if any
773     node_id integer REFERENCES nodes, -- Node responsible for event, if any
774     auth_type text, -- Type of auth used. i.e. AuthMethod
775     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
776     call_name text NOT NULL, -- Call responsible for this event
777     call text NOT NULL, -- Call responsible for this event, including parameters
778     message text, -- High level description of this event
779     runtime float DEFAULT 0, -- Event run time
780     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
781 ) WITH OIDS;
782
783 -- Database object(s) that may have been affected by a particular event
784 CREATE TABLE event_object (
785     event_id integer REFERENCES events NOT NULL, -- Event identifier
786     object_id integer NOT NULL, -- Object identifier
787     object_type text NOT NULL Default 'Unknown' -- What type of object is this event affecting
788 ) WITH OIDS;
789 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
790 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
791 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
792
793 CREATE VIEW event_objects AS
794 SELECT event_id,
795 array_accum(object_id) AS object_ids,
796 array_accum(object_type) AS object_types
797 FROM event_object
798 GROUP BY event_id;
799
800 --------------------------------------------------------------------------------
801 -- Useful views
802 --------------------------------------------------------------------------------
803
804 CREATE OR REPLACE VIEW view_events AS
805 SELECT
806 events.event_id,
807 events.person_id,
808 events.node_id,
809 events.auth_type,
810 events.fault_code,
811 events.call_name,
812 events.call,
813 events.message,
814 events.runtime,
815 CAST(date_part('epoch', events.time) AS bigint) AS time,
816 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
817 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
818 FROM events;
819
820 CREATE OR REPLACE VIEW view_persons AS
821 SELECT
822 persons.person_id,
823 persons.email,
824 persons.first_name,
825 persons.last_name,
826 persons.deleted,
827 persons.enabled,
828 persons.password,
829 persons.verification_key,
830 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
831 persons.title,
832 persons.phone,
833 persons.url,
834 persons.bio,
835 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
836 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
837 peer_person.peer_id,
838 peer_person.peer_person_id,
839 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
840 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
841 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
842 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
843 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
844 FROM persons
845 LEFT JOIN peer_person USING (person_id);
846
847 CREATE OR REPLACE VIEW view_peers AS
848 SELECT 
849 peers.*, 
850 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
851 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
852 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
853 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
854 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
855 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
856 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
857 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
858 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
859 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
860 FROM peers;
861
862 CREATE OR REPLACE VIEW view_nodes AS
863 SELECT
864 nodes.node_id,
865 nodes.hostname,
866 nodes.site_id,
867 nodes.boot_state,
868 nodes.deleted,
869 nodes.model,
870 nodes.boot_nonce,
871 nodes.version,
872 nodes.ssh_rsa_key,
873 nodes.key,
874 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
875 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
876 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
877 peer_node.peer_id,
878 peer_node.peer_node_id,
879 COALESCE((SELECT nodenetwork_ids FROM node_nodenetworks WHERE node_nodenetworks.node_id = nodes.node_id), '{}') AS nodenetwork_ids,
880 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
881 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
882 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
883 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
884 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
885 node_session.session_id AS session
886 FROM nodes
887 LEFT JOIN peer_node USING (node_id)
888 LEFT JOIN node_session USING (node_id);
889
890 CREATE OR REPLACE VIEW view_nodegroups AS
891 SELECT
892 nodegroups.*,
893 COALESCE((SELECT node_ids FROM nodegroup_nodes WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') AS node_ids,
894 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') AS conf_file_ids
895 FROM nodegroups;
896
897 CREATE OR REPLACE VIEW view_conf_files AS
898 SELECT
899 conf_files.*,
900 COALESCE((SELECT node_ids FROM conf_file_nodes WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') AS node_ids,
901 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') AS nodegroup_ids
902 FROM conf_files;
903
904 CREATE OR REPLACE VIEW view_pcus AS
905 SELECT
906 pcus.*,
907 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
908 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
909 FROM pcus;
910
911 CREATE OR REPLACE VIEW view_sites AS
912 SELECT
913 sites.site_id,
914 sites.login_base,
915 sites.name,
916 sites.abbreviated_name,
917 sites.deleted,
918 sites.enabled,
919 sites.is_public,
920 sites.max_slices,
921 sites.max_slivers,
922 sites.latitude,
923 sites.longitude,
924 sites.url,
925 sites.ext_consortium_id,
926 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
927 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
928 peer_site.peer_id,
929 peer_site.peer_site_id,
930 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
931 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
932 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
933 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
934 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
935 FROM sites
936 LEFT JOIN peer_site USING (site_id);
937
938 CREATE OR REPLACE VIEW view_addresses AS
939 SELECT
940 addresses.*,
941 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
942 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
943 FROM addresses;
944
945 CREATE OR REPLACE VIEW view_keys AS
946 SELECT
947 keys.*,
948 person_key.person_id,
949 peer_key.peer_id,
950 peer_key.peer_key_id
951 FROM keys
952 LEFT JOIN person_key USING (key_id)
953 LEFT JOIN peer_key USING (key_id);
954
955 CREATE OR REPLACE VIEW view_slices AS
956 SELECT
957 slices.slice_id,
958 slices.site_id,
959 slices.name,
960 slices.instantiation,
961 slices.url,
962 slices.description,
963 slices.max_nodes,
964 slices.creator_person_id,
965 slices.is_deleted,
966 CAST(date_part('epoch', slices.created) AS bigint) AS created,
967 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
968 peer_slice.peer_id,
969 peer_slice.peer_slice_id,
970 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
971 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
972 COALESCE((SELECT slice_attribute_ids FROM slice_attributes WHERE slice_attributes.slice_id = slices.slice_id), '{}') AS slice_attribute_ids
973 FROM slices
974 LEFT JOIN peer_slice USING (slice_id);
975
976 CREATE OR REPLACE VIEW view_slice_attributes AS
977 SELECT
978 slice_attribute.slice_attribute_id,
979 slice_attribute.slice_id,
980 slice_attribute.node_id,
981 slice_attribute_types.attribute_type_id,
982 slice_attribute_types.name,
983 slice_attribute_types.description,
984 slice_attribute_types.min_role_id,
985 slice_attribute.value
986 FROM slice_attribute
987 INNER JOIN slice_attribute_types USING (attribute_type_id);
988
989 CREATE OR REPLACE VIEW view_sessions AS
990 SELECT
991 sessions.session_id,
992 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
993 person_session.person_id,
994 node_session.node_id
995 FROM sessions
996 LEFT JOIN person_session USING (session_id)
997 LEFT JOIN node_session USING (session_id);
998
999 --------------------------------------------------------------------------------
1000 -- Built-in maintenance account and default site
1001 --------------------------------------------------------------------------------
1002
1003 INSERT INTO persons
1004 (first_name, last_name, email, password, enabled)
1005 VALUES
1006 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1007
1008 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1009 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1010 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1011 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1012
1013 INSERT INTO sites
1014 (login_base, name, abbreviated_name, max_slices)
1015 VALUES
1016 ('pl', 'PlanetLab Central', 'PLC', 100);