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