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