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