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