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