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