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