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