remove old Shell.py implementation (moved to plcsh and PLC/Shell.py)
[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.56 2006/12/20 14:06:40 tmack 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 INSERT INTO roles (role_id, name) VALUES (1000, 'node');
230 INSERT INTO roles (role_id, name) VALUES (2000, 'anonymous');
231 -- xxx not sure this us useful yet
232 --INSERT INTO roles (role_id, name) VALUES (3000, 'peer');
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 NOT NULL, -- 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     message text, -- High level description of this event
761     runtime float DEFAULT 0, -- Event run time
762     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
763 ) WITH OIDS;
764
765 -- Database object(s) that may have been affected by a particular event
766 CREATE TABLE event_object (
767     event_id integer REFERENCES events NOT NULL, -- Event identifier
768     object_id integer NOT NULL -- Object identifier
769 ) WITH OIDS;
770 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
771 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
772
773 CREATE VIEW event_objects AS
774 SELECT event_id,
775 array_accum(object_id) AS object_ids
776 FROM event_object
777 GROUP BY event_id;
778
779 --------------------------------------------------------------------------------
780 -- Useful views
781 --------------------------------------------------------------------------------
782
783 CREATE VIEW view_events AS
784 SELECT
785 events.event_id,
786 events.person_id,
787 events.node_id,
788 events.fault_code,
789 events.call_name,
790 events.call,
791 events.message,
792 events.runtime,
793 CAST(date_part('epoch', events.time) AS bigint) AS time,
794 COALESCE(event_objects.object_ids, '{}') AS object_ids
795 FROM events
796 LEFT JOIN event_objects USING (event_id);
797
798 CREATE VIEW view_persons AS
799 SELECT
800 persons.person_id,
801 persons.email,
802 persons.first_name,
803 persons.last_name,
804 persons.deleted,
805 persons.enabled,
806 persons.password,
807 persons.verification_key,
808 persons.verification_expires,
809 persons.title,
810 persons.phone,
811 persons.url,
812 persons.bio,
813 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
814 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
815 peer_person.peer_id,
816 peer_person.peer_person_id,
817 COALESCE(person_roles.role_ids, '{}') AS role_ids,
818 COALESCE(person_roles.roles, '{}') AS roles,
819 COALESCE(person_sites.site_ids, '{}') AS site_ids,
820 COALESCE(person_keys.key_ids, '{}') AS key_ids,
821 COALESCE(person_slices.slice_ids, '{}') AS slice_ids
822 FROM persons
823 LEFT JOIN peer_person USING (person_id)
824 LEFT JOIN person_roles USING (person_id)
825 LEFT JOIN person_sites USING (person_id)
826 LEFT JOIN person_keys USING (person_id)
827 LEFT JOIN person_slices USING (person_id);
828
829 CREATE VIEW view_peers AS
830 SELECT 
831 peers.*, 
832 COALESCE(peer_sites.site_ids, '{}') AS site_ids,
833 COALESCE(peer_sites.peer_site_ids, '{}') AS peer_site_ids,
834 COALESCE(peer_persons.person_ids, '{}') AS person_ids,
835 COALESCE(peer_persons.peer_person_ids, '{}') AS peer_person_ids,
836 COALESCE(peer_keys.key_ids, '{}') AS key_ids,
837 COALESCE(peer_keys.peer_key_ids, '{}') AS peer_key_ids,
838 COALESCE(peer_nodes.node_ids, '{}') AS node_ids,
839 COALESCE(peer_nodes.peer_node_ids, '{}') AS peer_node_ids,
840 COALESCE(peer_slices.slice_ids, '{}') AS slice_ids,
841 COALESCE(peer_slices.peer_slice_ids, '{}') AS peer_slice_ids
842 FROM peers
843 LEFT JOIN peer_sites USING (peer_id)
844 LEFT JOIN peer_persons USING (peer_id)
845 LEFT JOIN peer_keys USING (peer_id)
846 LEFT JOIN peer_nodes USING (peer_id)
847 LEFT JOIN peer_slices USING (peer_id);
848
849 CREATE VIEW view_nodes AS
850 SELECT
851 nodes.node_id,
852 nodes.hostname,
853 nodes.site_id,
854 nodes.boot_state,
855 nodes.deleted,
856 nodes.model,
857 nodes.boot_nonce,
858 nodes.version,
859 nodes.ssh_rsa_key,
860 nodes.key,
861 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
862 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
863 peer_node.peer_id,
864 peer_node.peer_node_id,
865 COALESCE(node_nodenetworks.nodenetwork_ids, '{}') AS nodenetwork_ids,
866 COALESCE(node_nodegroups.nodegroup_ids, '{}') AS nodegroup_ids,
867 COALESCE(node_slices.slice_ids, '{}') AS slice_ids,
868 COALESCE(node_pcus.pcu_ids, '{}') AS pcu_ids,
869 COALESCE(node_pcus.ports, '{}') AS ports,
870 COALESCE(node_conf_files.conf_file_ids, '{}') AS conf_file_ids,
871 node_session.session_id AS session
872 FROM nodes
873 LEFT JOIN peer_node USING (node_id)
874 LEFT JOIN node_nodenetworks USING (node_id)
875 LEFT JOIN node_nodegroups USING (node_id)
876 LEFT JOIN node_slices USING (node_id)
877 LEFT JOIN node_pcus USING (node_id)
878 LEFT JOIN node_conf_files USING (node_id)
879 LEFT JOIN node_session USING (node_id);
880
881 CREATE VIEW view_nodegroups AS
882 SELECT
883 nodegroups.*,
884 COALESCE(nodegroup_nodes.node_ids, '{}') AS node_ids,
885 COALESCE(nodegroup_conf_files.conf_file_ids, '{}') AS conf_file_ids
886 FROM nodegroups
887 LEFT JOIN nodegroup_nodes USING (nodegroup_id)
888 LEFT JOIN nodegroup_conf_files USING (nodegroup_id);
889
890 CREATE VIEW view_conf_files AS
891 SELECT
892 conf_files.*,
893 COALESCE(conf_file_nodes.node_ids, '{}') AS node_ids,
894 COALESCE(conf_file_nodegroups.nodegroup_ids, '{}') AS nodegroup_ids
895 FROM conf_files
896 LEFT JOIN conf_file_nodes USING (conf_file_id)
897 LEFT JOIN conf_file_nodegroups USING (conf_file_id);
898
899 CREATE VIEW view_pcus AS
900 SELECT
901 pcus.*,
902 COALESCE(pcu_nodes.node_ids, '{}') AS node_ids,
903 COALESCE(pcu_nodes.ports, '{}') AS ports
904 FROM pcus
905 LEFT JOIN pcu_nodes USING (pcu_id);
906
907 CREATE VIEW view_sites AS
908 SELECT
909 sites.site_id,
910 sites.login_base,
911 sites.name,
912 sites.abbreviated_name,
913 sites.deleted,
914 sites.is_public,
915 sites.max_slices,
916 sites.max_slivers,
917 sites.latitude,
918 sites.longitude,
919 sites.url,
920 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
921 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
922 peer_site.peer_id,
923 peer_site.peer_site_id,
924 COALESCE(site_persons.person_ids, '{}') AS person_ids,
925 COALESCE(site_nodes.node_ids, '{}') AS node_ids,
926 COALESCE(site_addresses.address_ids, '{}') AS address_ids,
927 COALESCE(site_slices.slice_ids, '{}') AS slice_ids,
928 COALESCE(site_pcus.pcu_ids, '{}') AS pcu_ids
929 FROM sites
930 LEFT JOIN peer_site USING (site_id)
931 LEFT JOIN site_persons USING (site_id)
932 LEFT JOIN site_nodes USING (site_id)
933 LEFT JOIN site_addresses USING (site_id)
934 LEFT JOIN site_slices USING (site_id)
935 LEFT JOIN site_pcus USING (site_id);
936
937 CREATE VIEW view_addresses AS
938 SELECT
939 addresses.*,
940 COALESCE(address_address_types.address_type_ids, '{}') AS address_type_ids,
941 COALESCE(address_address_types.address_types, '{}') AS address_types
942 FROM addresses
943 LEFT JOIN address_address_types USING (address_id);
944
945 CREATE 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 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(slice_nodes.node_ids, '{}') AS node_ids,
971 COALESCE(slice_persons.person_ids, '{}') AS person_ids,
972 COALESCE(slice_attributes.slice_attribute_ids, '{}') AS slice_attribute_ids
973 FROM slices
974 LEFT JOIN peer_slice USING (slice_id)
975 LEFT JOIN slice_nodes USING (slice_id)
976 LEFT JOIN slice_persons USING (slice_id)
977 LEFT JOIN slice_attributes USING (slice_id);
978
979 CREATE VIEW view_slice_attributes AS
980 SELECT
981 slice_attribute.slice_attribute_id,
982 slice_attribute.slice_id,
983 slice_attribute.node_id,
984 slice_attribute_types.attribute_type_id,
985 slice_attribute_types.name,
986 slice_attribute_types.description,
987 slice_attribute_types.min_role_id,
988 slice_attribute.value
989 FROM slice_attribute
990 INNER JOIN slice_attribute_types USING (attribute_type_id);
991
992 CREATE VIEW view_sessions AS
993 SELECT
994 sessions.session_id,
995 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
996 person_session.person_id,
997 node_session.node_id
998 FROM sessions
999 LEFT JOIN person_session USING (session_id)
1000 LEFT JOIN node_session USING (session_id);
1001
1002 --------------------------------------------------------------------------------
1003 -- Built-in maintenance account and default site
1004 --------------------------------------------------------------------------------
1005
1006 INSERT INTO persons
1007 (first_name, last_name, email, password, enabled)
1008 VALUES
1009 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1010
1011 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1012 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1013 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1014 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1015
1016 INSERT INTO sites
1017 (login_base, name, abbreviated_name, max_slices)
1018 VALUES
1019 ('pl', 'PlanetLab Central', 'PLC', 100);