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