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