- fix peer_slice_slice_id_idx
[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.35 2006/11/15 10:59:54 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     deleted boolean NOT NULL DEFAULT false
558 ) WITH OIDS;
559 CREATE INDEX slices_site_id_idx ON slices (site_id) WHERE deleted IS false;
560 CREATE INDEX slices_name_idx ON slices (name) WHERE 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 GROUP BY site_id;
595
596 -- Slices - peer relationship
597 CREATE TABLE peer_slice (
598     peer_id integer REFERENCES peers NOT NULL, -- peer primary key
599     slice_id integer REFERENCES slices NOT NULL, -- node primary key
600     PRIMARY KEY (peer_id, slice_id)
601 ) WITH OIDS;
602 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
603 CREATE INDEX peer_slice_slice_id_idx ON peer_slice (slice_id);
604
605 CREATE VIEW peer_slices AS
606 SELECT peer_id,
607 array_accum(slice_id) AS slice_ids
608 FROM peer_slice
609 GROUP BY peer_id;
610
611 -- Slice membership
612 CREATE TABLE slice_person (
613     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
614     person_id integer REFERENCES persons NOT NULL, -- Account identifier
615     PRIMARY KEY (slice_id, person_id)
616 ) WITH OIDS;
617 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
618 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
619
620 -- Members of the slice
621 CREATE VIEW slice_persons AS
622 SELECT slice_id,
623 array_accum(person_id) AS person_ids
624 FROM slice_person
625 GROUP BY slice_id;
626
627 -- Slices of which each person is a member
628 CREATE VIEW person_slices AS
629 SELECT person_id,
630 array_accum(slice_id) AS slice_ids
631 FROM slice_person
632 GROUP BY person_id;
633
634 --------------------------------------------------------------------------------
635 -- Slice attributes
636 --------------------------------------------------------------------------------
637
638 -- Slice attribute types
639 CREATE TABLE slice_attribute_types (
640     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
641     name text UNIQUE NOT NULL, -- Attribute name
642     description text, -- Attribute description
643     min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
644 ) WITH OIDS;
645
646 -- Slice/sliver attributes
647 CREATE TABLE slice_attribute (
648     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
649     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
650     node_id integer REFERENCES nodes, -- Sliver attribute if set
651     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
652     value text
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     template text, -- Message template
697     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
698 ) WITH OIDS;
699
700 --------------------------------------------------------------------------------
701 -- Events
702 --------------------------------------------------------------------------------
703
704 -- Event types
705 CREATE TABLE event_types (
706     event_type text PRIMARY KEY -- Event type
707 ) WITH OIDS;
708 INSERT INTO event_types (event_type) VALUES ('Add');
709 INSERT INTO event_types (event_type) VALUES ('AddTo');
710 INSERT INTO event_types (event_type) VALUES ('Get');
711 INSERT INTO event_types (event_type) VALUES ('Update');
712 INSERT INTO event_types (event_type) VALUES ('Delete');
713 INSERT INTO event_types (event_type) VALUES ('DeleteFrom');
714 INSERT INTO event_types (event_type) VALUES ('Unknown');
715
716 -- Object types
717 CREATE TABLE object_types (
718     object_type text PRIMARY KEY -- Object type 
719 ) WITH OIDS;
720 INSERT INTO object_types (object_type) VALUES ('AddressType');
721 INSERT INTO object_types (object_type) VALUES ('Address');
722 INSERT INTO object_types (object_type) VALUES ('BootState');
723 INSERT INTO object_types (object_type) VALUES ('ConfFile');
724 INSERT INTO object_types (object_type) VALUES ('KeyType');
725 INSERT INTO object_types (object_type) VALUES ('Key');
726 INSERT INTO object_types (object_type) VALUES ('NetworkMethod');
727 INSERT INTO object_types (object_type) VALUES ('NetworkType');
728 INSERT INTO object_types (object_type) VALUES ('Network');
729 INSERT INTO object_types (object_type) VALUES ('NodeGroup');
730 INSERT INTO object_types (object_type) VALUES ('NodeNetwork');
731 INSERT INTO object_types (object_type) VALUES ('Node');
732 INSERT INTO object_types (object_type) VALUES ('PCU');
733 INSERT INTO object_types (object_type) VALUES ('Person');
734 INSERT INTO object_types (object_type) VALUES ('Role');
735 INSERT INTO object_types (object_type) VALUES ('Session');
736 INSERT INTO object_types (object_type) VALUES ('Site');
737 INSERT INTO object_types (object_type) VALUES ('SliceAttributeType');
738 INSERT INTO object_types (object_type) VALUES ('SliceAttribute');
739 INSERT INTO object_types (object_type) VALUES ('Slice');
740 INSERT INTO object_types (object_type) VALUES ('Unknown');
741
742 -- Events
743 CREATE TABLE events (
744     event_id serial PRIMARY KEY,  -- Event identifier
745     person_id integer REFERENCES persons, -- Person responsible for event, if any
746     node_id integer REFERENCES nodes, -- Node responsible for event, if any
747     event_type text REFERENCES event_types NOT NULL DEFAULT 'Unknown', -- Event type 
748     object_type text REFERENCES object_types NOT NULL DEFAULT 'Unknown', -- Object type associated with event
749     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
750     call text NOT NULL, -- Call responsible for this event
751     runtime float, -- Event run time
752     time timestamp without time zone  NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
753 ) WITH OIDS;
754
755 -- Event objects
756 CREATE TABLE event_object (
757     event_id integer REFERENCES events NOT NULL, -- Event identifier
758     object_id integer NOT NULL -- Object identifier
759 ) WITH OIDS;
760 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
761 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
762
763 CREATE VIEW event_objects AS
764 SELECT event_id,
765 array_accum(object_id) AS object_ids
766 FROM event_object
767 GROUP BY event_id;
768
769 --------------------------------------------------------------------------------
770 -- Useful views
771 --------------------------------------------------------------------------------
772
773 CREATE VIEW view_events AS
774 SELECT
775 events.event_id,
776 events.person_id,
777 events.node_id,
778 events.event_type,
779 events.object_type,
780 events.fault_code,
781 events.call,
782 events.runtime,
783 CAST(date_part('epoch', events.time) AS bigint) AS time,
784 COALESCE(event_objects.object_ids, '{}') AS object_ids
785 FROM events
786 LEFT JOIN event_objects USING (event_id);
787
788 CREATE VIEW view_persons AS
789 SELECT
790 persons.person_id,
791 persons.email,
792 persons.first_name,
793 persons.last_name,
794 persons.deleted,
795 persons.enabled,
796 persons.password,
797 persons.verification_key,
798 persons.verification_expires,
799 persons.title,
800 persons.phone,
801 persons.url,
802 persons.bio,
803 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
804 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
805 COALESCE(person_roles.role_ids, '{}') AS role_ids,
806 COALESCE(person_roles.roles, '{}') AS roles,
807 COALESCE(person_sites.site_ids, '{}') AS site_ids,
808 COALESCE(person_keys.key_ids, '{}') AS key_ids,
809 COALESCE(person_slices.slice_ids, '{}') AS slice_ids
810 FROM persons
811 LEFT JOIN person_roles USING (person_id)
812 LEFT JOIN person_sites USING (person_id)
813 LEFT JOIN person_keys USING (person_id)
814 LEFT JOIN person_slices USING (person_id);
815
816 CREATE VIEW view_nodes AS
817 SELECT
818 nodes.node_id,
819 nodes.hostname,
820 nodes.site_id,
821 nodes.boot_state,
822 nodes.deleted,
823 nodes.model,
824 nodes.boot_nonce,
825 nodes.version,
826 nodes.ssh_rsa_key,
827 nodes.key,
828 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
829 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
830 COALESCE(node_nodenetworks.nodenetwork_ids, '{}') AS nodenetwork_ids,
831 COALESCE(node_nodegroups.nodegroup_ids, '{}') AS nodegroup_ids,
832 COALESCE(node_slices.slice_ids, '{}') AS slice_ids,
833 COALESCE(node_pcus.pcu_ids, '{}') AS pcu_ids,
834 COALESCE(node_pcus.ports, '{}') AS ports,
835 COALESCE(node_conf_files.conf_file_ids, '{}') AS conf_file_ids,
836 node_session.session_id AS session
837 FROM nodes
838 LEFT JOIN peer_node USING (node_id) 
839 LEFT JOIN node_nodenetworks USING (node_id)
840 LEFT JOIN node_nodegroups USING (node_id)
841 LEFT JOIN node_slices USING (node_id)
842 LEFT JOIN node_pcus USING (node_id)
843 LEFT JOIN node_conf_files USING (node_id)
844 LEFT JOIN node_session USING (node_id)
845 WHERE peer_node.peer_id IS NULL;
846
847 CREATE VIEW view_peers AS
848 SELECT 
849 peers.*, 
850 peer_nodes.node_ids,
851 peer_slices.slice_ids
852 FROM peers
853 LEFT JOIN peer_nodes USING (peer_id)
854 LEFT JOIN peer_slices USING (peer_id);
855
856 CREATE VIEW view_foreign_nodes AS
857 SELECT
858 nodes.node_id,
859 nodes.hostname,
860 peer_node.peer_id,
861 nodes.boot_state,
862 nodes.model,
863 nodes.version,
864 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
865 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
866 COALESCE(node_slices.slice_ids, '{}') AS slice_ids,
867 nodes.deleted
868 FROM nodes
869 LEFT JOIN peer_node USING (node_id) 
870 LEFT JOIN node_slices USING (node_id)
871 WHERE peer_node.peer_id IS NOT NULL;
872
873 CREATE VIEW view_nodegroups AS
874 SELECT
875 nodegroups.nodegroup_id,
876 nodegroups.name,
877 nodegroups.description,
878 COALESCE(nodegroup_nodes.node_ids, '{}') AS node_ids,
879 COALESCE(nodegroup_conf_files.conf_file_ids, '{}') AS conf_file_ids
880 FROM nodegroups
881 LEFT JOIN nodegroup_nodes USING (nodegroup_id)
882 LEFT JOIN nodegroup_conf_files USING (nodegroup_id);
883
884 CREATE VIEW view_conf_files AS
885 SELECT
886 conf_files.conf_file_id,
887 conf_files.enabled,
888 conf_files.source,
889 conf_files.dest,
890 conf_files.file_permissions,
891 conf_files.file_owner,
892 conf_files.file_group,
893 conf_files.preinstall_cmd,
894 conf_files.postinstall_cmd,
895 conf_files.error_cmd,
896 conf_files.ignore_cmd_errors,
897 conf_files.always_update,
898 COALESCE(conf_file_nodes.node_ids, '{}') AS node_ids,
899 COALESCE(conf_file_nodegroups.nodegroup_ids, '{}') AS nodegroup_ids
900 FROM conf_files
901 LEFT JOIN conf_file_nodes USING (conf_file_id)
902 LEFT JOIN conf_file_nodegroups USING (conf_file_id);
903
904 CREATE VIEW view_pcus AS
905 SELECT
906 pcus.pcu_id,
907 pcus.site_id,
908 pcus.hostname,
909 pcus.ip,
910 pcus.protocol,
911 pcus.username,
912 pcus.password,
913 pcus.model,
914 pcus.notes,
915 COALESCE(pcu_nodes.node_ids, '{}') AS node_ids,
916 COALESCE(pcu_nodes.ports, '{}') AS ports
917 FROM pcus
918 LEFT JOIN pcu_nodes USING (pcu_id);
919
920 CREATE VIEW view_sites AS
921 SELECT
922 sites.site_id,
923 sites.login_base,
924 sites.name,
925 sites.abbreviated_name,
926 sites.deleted,
927 sites.is_public,
928 sites.max_slices,
929 sites.max_slivers,
930 sites.latitude,
931 sites.longitude,
932 sites.url,
933 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
934 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
935 COALESCE(site_persons.person_ids, '{}') AS person_ids,
936 COALESCE(site_nodes.node_ids, '{}') AS node_ids,
937 COALESCE(site_addresses.address_ids, '{}') AS address_ids,
938 COALESCE(site_slices.slice_ids, '{}') AS slice_ids,
939 COALESCE(site_pcus.pcu_ids, '{}') AS pcu_ids
940 FROM sites
941 LEFT JOIN site_persons USING (site_id)
942 LEFT JOIN site_nodes USING (site_id)
943 LEFT JOIN site_addresses USING (site_id)
944 LEFT JOIN site_slices USING (site_id)
945 LEFT JOIN site_pcus USING (site_id);
946
947 CREATE VIEW view_addresses AS
948 SELECT
949 addresses.address_id,
950 addresses.line1,
951 addresses.line2,
952 addresses.line3,
953 addresses.city,
954 addresses.state,
955 addresses.postalcode,
956 addresses.country,
957 COALESCE(address_address_types.address_type_ids, '{}') AS address_type_ids,
958 COALESCE(address_address_types.address_types, '{}') AS address_types
959 FROM addresses
960 LEFT JOIN address_address_types USING (address_id);
961
962 CREATE VIEW view_slices AS
963 SELECT
964 slices.slice_id,
965 slices.site_id,
966 slices.name,
967 slices.instantiation,
968 slices.url,
969 slices.description,
970 slices.max_nodes,
971 slices.creator_person_id,
972 slices.deleted,
973 CAST(date_part('epoch', slices.created) AS bigint) AS created,
974 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
975 COALESCE(slice_nodes.node_ids, '{}') AS node_ids,
976 COALESCE(slice_persons.person_ids, '{}') AS person_ids,
977 COALESCE(slice_attributes.slice_attribute_ids, '{}') AS slice_attribute_ids
978 FROM slices
979 LEFT JOIN peer_slice USING (slice_id)
980 LEFT JOIN slice_nodes USING (slice_id)
981 LEFT JOIN slice_persons USING (slice_id)
982 LEFT JOIN slice_attributes USING (slice_id)
983 WHERE peer_slice.peer_id IS NULL
984 AND slices.site_id IS NOT NULL
985 AND slices.creator_person_id IS NOT NULL;
986
987 CREATE VIEW view_foreign_slices AS
988 SELECT
989 slices.slice_id,
990 slices.name,
991 peer_slice.peer_id,
992 slices.instantiation,
993 slices.url,
994 slices.description,
995 slices.max_nodes,
996 slices.deleted,
997 CAST(date_part('epoch', slices.created) AS bigint) AS created,
998 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
999 COALESCE(slice_nodes.node_ids, '{}') AS node_ids
1000 FROM slices
1001 LEFT JOIN peer_slice USING (slice_id)
1002 LEFT JOIN slice_nodes USING (slice_id)
1003 WHERE peer_slice.peer_id IS NOT NULL;
1004
1005 --
1006 CREATE VIEW view_slice_attributes AS
1007 SELECT
1008 slice_attribute.slice_attribute_id,
1009 slice_attribute.slice_id,
1010 slice_attribute.node_id,
1011 slice_attribute_types.attribute_type_id,
1012 slice_attribute_types.name,
1013 slice_attribute_types.description,
1014 slice_attribute_types.min_role_id,
1015 slice_attribute.value
1016 FROM slice_attribute
1017 INNER JOIN slice_attribute_types USING (attribute_type_id);
1018
1019 CREATE VIEW view_sessions AS
1020 SELECT
1021 sessions.session_id,
1022 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1023 person_session.person_id,
1024 node_session.node_id
1025 FROM sessions
1026 LEFT JOIN person_session USING (session_id)
1027 LEFT JOIN node_session USING (session_id);
1028
1029 --------------------------------------------------------------------------------
1030 -- Built-in maintenance account and default site
1031 --------------------------------------------------------------------------------
1032
1033 INSERT INTO persons
1034 (first_name, last_name, email, password, enabled)
1035 VALUES
1036 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1037
1038 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1039 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1040 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1041 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1042
1043 INSERT INTO sites
1044 (login_base, name, abbreviated_name, max_slices)
1045 VALUES
1046 ('pl', 'PlanetLab Central', 'PLC', 100);
1047
1048
1049