- removed anything having to with event_type/event_object
[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.51 2006/11/28 22:00:14 tmack 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     site_id integer REFERENCES sites NOT NULL, -- At which site 
294
295     boot_state text REFERENCES boot_states NOT NULL DEFAULT 'inst', -- Node boot state
296     deleted boolean NOT NULL DEFAULT false, -- Is deleted
297
298     -- Optional
299     model text, -- Hardware make and model
300     boot_nonce text, -- Random nonce updated by Boot Manager
301     version text, -- Boot CD version string updated by Boot Manager
302     -- XXX Should be key_id integer REFERENCES keys
303     ssh_rsa_key text, -- SSH host key updated by Boot Manager
304     key text, -- Node key generated by API when configuration file is downloaded
305
306     -- Timestamps
307     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
308     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
309
310     peer_id integer REFERENCES peers -- From which peer 
311 ) WITH OIDS;
312 CREATE INDEX nodes_hostname_idx ON nodes (hostname) WHERE deleted IS false;
313 CREATE INDEX nodes_site_id_idx ON nodes (site_id) WHERE deleted IS false;
314
315 -- Nodes at each site
316 CREATE VIEW site_nodes AS
317 SELECT site_id,
318 array_accum(node_id) AS node_ids
319 FROM nodes
320 GROUP BY site_id;
321
322 --------------------------------------------------------------------------------
323 -- Node groups
324 --------------------------------------------------------------------------------
325
326 -- Node groups
327 CREATE TABLE nodegroups (
328     nodegroup_id serial PRIMARY KEY, -- Group identifier
329     name text UNIQUE NOT NULL, -- Group name
330     description text -- Group description
331 ) WITH OIDS;
332
333 -- Node group membership
334 CREATE TABLE nodegroup_node (
335     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Group identifier
336     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
337     PRIMARY KEY (nodegroup_id, node_id)
338 ) WITH OIDS;
339 CREATE INDEX nodegroup_node_nodegroup_id_idx ON nodegroup_node (nodegroup_id);
340 CREATE INDEX nodegroup_node_node_id_idx ON nodegroup_node (node_id);
341
342 -- Nodes in each node group
343 CREATE VIEW nodegroup_nodes AS
344 SELECT nodegroup_id,
345 array_accum(node_id) AS node_ids
346 FROM nodegroup_node
347 GROUP BY nodegroup_id;
348
349 -- Node groups that each node is a member of
350 CREATE VIEW node_nodegroups AS
351 SELECT node_id,
352 array_accum(nodegroup_id) AS nodegroup_ids
353 FROM nodegroup_node
354 GROUP BY node_id;
355
356 --------------------------------------------------------------------------------
357 -- Node configuration files
358 --------------------------------------------------------------------------------
359
360 CREATE TABLE conf_files (
361     conf_file_id serial PRIMARY KEY, -- Configuration file identifier
362     enabled bool NOT NULL DEFAULT true, -- Configuration file is active
363     source text NOT NULL, -- Relative path on the boot server where file can be downloaded
364     dest text NOT NULL, -- Absolute path where file should be installed
365     file_permissions text NOT NULL DEFAULT '0644', -- chmod(1) permissions
366     file_owner text NOT NULL DEFAULT 'root', -- chown(1) owner
367     file_group text NOT NULL DEFAULT 'root', -- chgrp(1) owner
368     preinstall_cmd text, -- Shell command to execute prior to installing
369     postinstall_cmd text, -- Shell command to execute after installing
370     error_cmd text, -- Shell command to execute if any error occurs
371     ignore_cmd_errors bool NOT NULL DEFAULT false, -- Install file anyway even if an error occurs
372     always_update bool NOT NULL DEFAULT false -- Always attempt to install file even if unchanged
373 );
374
375 CREATE TABLE conf_file_node (
376     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
377     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
378     PRIMARY KEY (conf_file_id, node_id)
379 );
380 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
381 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
382
383 -- Nodes linked to each configuration file
384 CREATE VIEW conf_file_nodes AS
385 SELECT conf_file_id,
386 array_accum(node_id) AS node_ids
387 FROM conf_file_node
388 GROUP BY conf_file_id;
389
390 -- Configuration files linked to each node
391 CREATE VIEW node_conf_files AS
392 SELECT node_id,
393 array_accum(conf_file_id) AS conf_file_ids
394 FROM conf_file_node
395 GROUP BY node_id;
396
397 CREATE TABLE conf_file_nodegroup (
398     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
399     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Node group identifier
400     PRIMARY KEY (conf_file_id, nodegroup_id)
401 );
402 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
403 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
404
405 -- Node groups linked to each configuration file
406 CREATE VIEW conf_file_nodegroups AS
407 SELECT conf_file_id,
408 array_accum(nodegroup_id) AS nodegroup_ids
409 FROM conf_file_nodegroup
410 GROUP BY conf_file_id;
411
412 -- Configuration files linked to each node group
413 CREATE VIEW nodegroup_conf_files AS
414 SELECT nodegroup_id,
415 array_accum(conf_file_id) AS conf_file_ids
416 FROM conf_file_nodegroup
417 GROUP BY nodegroup_id;
418
419 --------------------------------------------------------------------------------
420 -- Node network interfaces
421 --------------------------------------------------------------------------------
422
423 -- Valid network addressing schemes
424 CREATE TABLE network_types (
425     type text PRIMARY KEY -- Addressing scheme
426 ) WITH OIDS;
427 INSERT INTO network_types (type) VALUES ('ipv4');
428
429 -- Valid network configuration methods
430 CREATE TABLE network_methods (
431     method text PRIMARY KEY -- Configuration method
432 ) WITH OIDS;
433 INSERT INTO network_methods (method) VALUES ('static');
434 INSERT INTO network_methods (method) VALUES ('dhcp');
435 INSERT INTO network_methods (method) VALUES ('proxy');
436 INSERT INTO network_methods (method) VALUES ('tap');
437 INSERT INTO network_methods (method) VALUES ('ipmi');
438 INSERT INTO network_methods (method) VALUES ('unknown');
439
440 -- Node network interfaces
441 CREATE TABLE nodenetworks (
442     -- Mandatory
443     nodenetwork_id serial PRIMARY KEY, -- Network interface identifier
444     node_id integer REFERENCES nodes NOT NULL, -- Which node
445     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
446     type text REFERENCES network_types NOT NULL, -- Addressing scheme
447     method text REFERENCES network_methods NOT NULL, -- Configuration method
448
449     -- Optional, depending on type and method
450     ip text, -- IP address
451     mac text, -- MAC address
452     gateway text, -- Default gateway address
453     network text, -- Network address
454     broadcast text, -- Network broadcast address
455     netmask text, -- Network mask
456     dns1 text, -- Primary DNS server
457     dns2 text, -- Secondary DNS server
458     bwlimit integer, -- Bandwidth limit in bps
459     hostname text -- Hostname of this interface
460 ) WITH OIDS;
461 CREATE INDEX nodenetworks_node_id_idx ON nodenetworks (node_id);
462
463 -- Ordered by primary interface first
464 CREATE VIEW nodenetworks_ordered AS
465 SELECT node_id, nodenetwork_id
466 FROM nodenetworks
467 ORDER BY is_primary DESC;
468
469 -- Network interfaces on each node
470 CREATE VIEW node_nodenetworks AS
471 SELECT node_id,
472 array_accum(nodenetwork_id) AS nodenetwork_ids
473 FROM nodenetworks_ordered
474 GROUP BY node_id;
475
476 --------------------------------------------------------------------------------
477 -- Power control units (PCUs)
478 --------------------------------------------------------------------------------
479
480 CREATE TABLE pcus (
481     -- Mandatory
482     pcu_id serial PRIMARY KEY, -- PCU identifier
483     site_id integer REFERENCES sites NOT NULL, -- Site identifier
484     hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
485     ip text NOT NULL, -- IP, not necessarily unique
486
487     -- Optional
488     protocol text, -- Protocol, e.g. ssh or https or telnet
489     username text, -- Username, if applicable
490     "password" text, -- Password, if applicable
491     model text, -- Model, e.g. BayTech or iPal
492     notes text -- Random notes
493 ) WITH OIDS;
494 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
495
496 CREATE VIEW site_pcus AS
497 SELECT site_id,
498 array_accum(pcu_id) AS pcu_ids
499 FROM pcus
500 GROUP BY site_id;
501
502 CREATE TABLE pcu_node (
503     pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
504     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
505     port integer NOT NULL, -- Port number
506     PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
507     UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
508 );
509 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
510 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
511
512 CREATE VIEW node_pcus AS
513 SELECT node_id,
514 array_accum(pcu_id) AS pcu_ids,
515 array_accum(port) AS ports
516 FROM pcu_node
517 GROUP BY node_id;
518
519 CREATE VIEW pcu_nodes AS
520 SELECT pcu_id,
521 array_accum(node_id) AS node_ids,
522 array_accum(port) AS ports
523 FROM pcu_node
524 GROUP BY pcu_id;
525
526 --------------------------------------------------------------------------------
527 -- Slices
528 --------------------------------------------------------------------------------
529
530 CREATE TABLE slice_instantiations (
531     instantiation text PRIMARY KEY
532 ) WITH OIDS;
533 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
534 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
535 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
536
537 -- Slices
538 CREATE TABLE slices (
539     slice_id serial PRIMARY KEY, -- Slice identifier
540     site_id integer REFERENCES sites NOT NULL, -- Site identifier
541     peer_id integer REFERENCES peers, -- on which peer
542
543     name text NOT NULL, -- Slice name
544     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
545     url text, -- Project URL
546     description text, -- Project description
547
548     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
549
550     creator_person_id integer REFERENCES persons NOT NULL, -- Creator
551     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
552     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
553
554     is_deleted boolean NOT NULL DEFAULT false
555 ) WITH OIDS;
556 CREATE INDEX slices_site_id_idx ON slices (site_id) WHERE is_deleted IS false;
557 CREATE INDEX slices_name_idx ON slices (name) WHERE is_deleted IS false;
558
559 -- Slivers
560 CREATE TABLE slice_node (
561     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
562     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
563     PRIMARY KEY (slice_id, node_id)
564 ) WITH OIDS;
565 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
566 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
567
568 -- Synonym for slice_node
569 CREATE VIEW slivers AS
570 SELECT * FROM slice_node;
571
572 -- Nodes in each slice
573 CREATE VIEW slice_nodes AS
574 SELECT slice_id,
575 array_accum(node_id) AS node_ids
576 FROM slice_node
577 GROUP BY slice_id;
578
579 -- Slices on each node
580 CREATE VIEW node_slices AS
581 SELECT node_id,
582 array_accum(slice_id) AS slice_ids
583 FROM slice_node
584 GROUP BY node_id;
585
586 -- Slices at each site
587 CREATE VIEW site_slices AS
588 SELECT site_id,
589 array_accum(slice_id) AS slice_ids
590 FROM slices
591 WHERE is_deleted is false
592 GROUP BY site_id;
593
594 -- Slice membership
595 CREATE TABLE slice_person (
596     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
597     person_id integer REFERENCES persons NOT NULL, -- Account identifier
598     PRIMARY KEY (slice_id, person_id)
599 ) WITH OIDS;
600 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
601 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
602
603 -- Members of the slice
604 CREATE VIEW slice_persons AS
605 SELECT slice_id,
606 array_accum(person_id) AS person_ids
607 FROM slice_person
608 GROUP BY slice_id;
609
610 -- Slices of which each person is a member
611 CREATE VIEW person_slices AS
612 SELECT person_id,
613 array_accum(slice_id) AS slice_ids
614 FROM slice_person
615 GROUP BY person_id;
616
617 --------------------------------------------------------------------------------
618 -- Slice attributes
619 --------------------------------------------------------------------------------
620
621 -- Slice attribute types
622 CREATE TABLE slice_attribute_types (
623     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
624     name text UNIQUE NOT NULL, -- Attribute name
625     description text, -- Attribute description
626     min_role_id integer REFERENCES roles DEFAULT 10, -- If set, minimum (least powerful) role that can set or change this attribute
627  
628     peer_id integer REFERENCES peers -- From which peer 
629 ) WITH OIDS;
630
631 -- Slice/sliver attributes
632 CREATE TABLE slice_attribute (
633     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
634     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
635     node_id integer REFERENCES nodes, -- Sliver attribute if set
636     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
637     value text,
638
639     peer_id integer REFERENCES peers -- From which peer 
640 ) WITH OIDS;
641 CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
642 CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
643
644 CREATE VIEW slice_attributes AS
645 SELECT slice_id,
646 array_accum(slice_attribute_id) AS slice_attribute_ids
647 FROM slice_attribute
648 GROUP BY slice_id;
649
650 --------------------------------------------------------------------------------
651 -- Authenticated sessions
652 --------------------------------------------------------------------------------
653
654 -- Authenticated sessions
655 CREATE TABLE sessions (
656     session_id text PRIMARY KEY, -- Session identifier
657     expires timestamp without time zone
658 ) WITH OIDS;
659
660 -- People can have multiple sessions
661 CREATE TABLE person_session (
662     person_id integer REFERENCES persons NOT NULL, -- Account identifier
663     session_id text REFERENCES sessions NOT NULL, -- Session identifier
664     PRIMARY KEY (person_id, session_id),
665     UNIQUE (session_id) -- Sessions are unique
666 ) WITH OIDS;
667 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
668
669 -- Nodes can have only one session
670 CREATE TABLE node_session (
671     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
672     session_id text REFERENCES sessions NOT NULL, -- Session identifier
673     UNIQUE (node_id), -- Nodes can have only one session
674     UNIQUE (session_id) -- Sessions are unique
675 ) WITH OIDS;
676
677 --------------------------------------------------------------------------------
678 -- Message templates
679 --------------------------------------------------------------------------------
680
681 CREATE TABLE messages (
682     message_id text PRIMARY KEY, -- Message name
683     subject text, -- Message summary
684     template text, -- Message template
685     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
686 ) WITH OIDS;
687
688 --------------------------------------------------------------------------------
689 -- Events
690 --------------------------------------------------------------------------------
691
692
693 -- Events
694 CREATE TABLE events (
695     event_id serial PRIMARY KEY,  -- Event identifier
696     person_id integer REFERENCES persons, -- Person responsible for event, if any
697     node_id integer REFERENCES nodes, -- Node responsible for event, if any
698     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
699     call_name text Not NULL, -- Call responsible for this event
700     call text NOT NULL, -- Call responsible for this event, including paramters
701     runtime float, -- Event run time
702     time timestamp without time zone  NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
703 ) WITH OIDS;
704
705 -- Event objects
706 CREATE TABLE event_object (
707     event_id integer REFERENCES events NOT NULL, -- Event identifier
708     object_id integer NOT NULL -- Object identifier
709 ) WITH OIDS;
710 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
711 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
712
713 CREATE VIEW event_objects AS
714 SELECT event_id,
715 array_accum(object_id) AS object_ids
716 FROM event_object
717 GROUP BY event_id;
718
719 --------------------------------------------------------------------------------
720 -- Useful views
721 --------------------------------------------------------------------------------
722
723 CREATE VIEW view_events AS
724 SELECT
725 events.event_id,
726 events.person_id,
727 events.node_id,
728 events.fault_code,
729 events.call_name,
730 events.call,
731 events.runtime,
732 CAST(date_part('epoch', events.time) AS bigint) AS time,
733 COALESCE(event_objects.object_ids, '{}') AS object_ids
734 FROM events
735 LEFT JOIN event_objects USING (event_id);
736
737 CREATE VIEW view_persons AS
738 SELECT
739 persons.person_id,
740 persons.email,
741 persons.first_name,
742 persons.last_name,
743 persons.deleted,
744 persons.enabled,
745 persons.password,
746 persons.verification_key,
747 persons.verification_expires,
748 persons.title,
749 persons.phone,
750 persons.url,
751 persons.bio,
752 persons.peer_id,
753 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
754 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
755 COALESCE(person_roles.role_ids, '{}') AS role_ids,
756 COALESCE(person_roles.roles, '{}') AS roles,
757 COALESCE(person_sites.site_ids, '{}') AS site_ids,
758 COALESCE(person_keys.key_ids, '{}') AS key_ids,
759 COALESCE(person_slices.slice_ids, '{}') AS slice_ids
760 FROM persons
761 LEFT JOIN person_roles USING (person_id)
762 LEFT JOIN person_sites USING (person_id)
763 LEFT JOIN person_keys USING (person_id)
764 LEFT JOIN person_slices USING (person_id);
765
766 -- Nodes at each peer
767 CREATE VIEW peer_nodes AS
768 SELECT peer_id,
769 array_accum(node_id) AS node_ids
770 FROM nodes
771 GROUP BY peer_id;
772
773 CREATE VIEW peer_slices AS
774 SELECT peer_id,
775 array_accum(slice_id) AS slice_ids
776 FROM slices
777 GROUP BY peer_id;
778
779 CREATE VIEW view_peers AS
780 SELECT 
781 peers.*, 
782 peer_nodes.node_ids,
783 peer_slices.slice_ids
784 FROM peers
785 LEFT JOIN peer_nodes USING (peer_id)
786 LEFT JOIN peer_slices USING (peer_id);
787
788 CREATE VIEW view_nodes AS
789 SELECT
790 nodes.node_id,
791 nodes.hostname,
792 nodes.site_id,
793 nodes.peer_id,
794 nodes.boot_state,
795 nodes.deleted,
796 nodes.model,
797 nodes.boot_nonce,
798 nodes.version,
799 nodes.ssh_rsa_key,
800 nodes.key,
801 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
802 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
803 COALESCE(node_nodenetworks.nodenetwork_ids, '{}') AS nodenetwork_ids,
804 COALESCE(node_nodegroups.nodegroup_ids, '{}') AS nodegroup_ids,
805 COALESCE(node_slices.slice_ids, '{}') AS slice_ids,
806 COALESCE(node_pcus.pcu_ids, '{}') AS pcu_ids,
807 COALESCE(node_pcus.ports, '{}') AS ports,
808 COALESCE(node_conf_files.conf_file_ids, '{}') AS conf_file_ids,
809 node_session.session_id AS session
810 FROM nodes
811 LEFT JOIN node_nodenetworks USING (node_id)
812 LEFT JOIN node_nodegroups USING (node_id)
813 LEFT JOIN node_slices USING (node_id)
814 LEFT JOIN node_pcus USING (node_id)
815 LEFT JOIN node_conf_files USING (node_id)
816 LEFT JOIN node_session USING (node_id);
817
818 CREATE VIEW view_nodegroups AS
819 SELECT
820 nodegroups.nodegroup_id,
821 nodegroups.name,
822 nodegroups.description,
823 COALESCE(nodegroup_nodes.node_ids, '{}') AS node_ids,
824 COALESCE(nodegroup_conf_files.conf_file_ids, '{}') AS conf_file_ids
825 FROM nodegroups
826 LEFT JOIN nodegroup_nodes USING (nodegroup_id)
827 LEFT JOIN nodegroup_conf_files USING (nodegroup_id);
828
829 CREATE VIEW view_conf_files AS
830 SELECT
831 conf_files.conf_file_id,
832 conf_files.enabled,
833 conf_files.source,
834 conf_files.dest,
835 conf_files.file_permissions,
836 conf_files.file_owner,
837 conf_files.file_group,
838 conf_files.preinstall_cmd,
839 conf_files.postinstall_cmd,
840 conf_files.error_cmd,
841 conf_files.ignore_cmd_errors,
842 conf_files.always_update,
843 COALESCE(conf_file_nodes.node_ids, '{}') AS node_ids,
844 COALESCE(conf_file_nodegroups.nodegroup_ids, '{}') AS nodegroup_ids
845 FROM conf_files
846 LEFT JOIN conf_file_nodes USING (conf_file_id)
847 LEFT JOIN conf_file_nodegroups USING (conf_file_id);
848
849 CREATE VIEW view_pcus AS
850 SELECT
851 pcus.pcu_id,
852 pcus.site_id,
853 pcus.hostname,
854 pcus.ip,
855 pcus.protocol,
856 pcus.username,
857 pcus.password,
858 pcus.model,
859 pcus.notes,
860 COALESCE(pcu_nodes.node_ids, '{}') AS node_ids,
861 COALESCE(pcu_nodes.ports, '{}') AS ports
862 FROM pcus
863 LEFT JOIN pcu_nodes USING (pcu_id);
864
865 CREATE VIEW view_sites AS
866 SELECT
867 sites.site_id,
868 sites.login_base,
869 sites.name,
870 sites.abbreviated_name,
871 sites.deleted,
872 sites.is_public,
873 sites.max_slices,
874 sites.max_slivers,
875 sites.latitude,
876 sites.longitude,
877 sites.url,
878 sites.peer_id,
879 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
880 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
881 COALESCE(site_persons.person_ids, '{}') AS person_ids,
882 COALESCE(site_nodes.node_ids, '{}') AS node_ids,
883 COALESCE(site_addresses.address_ids, '{}') AS address_ids,
884 COALESCE(site_slices.slice_ids, '{}') AS slice_ids,
885 COALESCE(site_pcus.pcu_ids, '{}') AS pcu_ids
886 FROM sites
887 LEFT JOIN site_persons USING (site_id)
888 LEFT JOIN site_nodes USING (site_id)
889 LEFT JOIN site_addresses USING (site_id)
890 LEFT JOIN site_slices USING (site_id)
891 LEFT JOIN site_pcus USING (site_id);
892
893 CREATE VIEW view_addresses AS
894 SELECT
895 addresses.address_id,
896 addresses.line1,
897 addresses.line2,
898 addresses.line3,
899 addresses.city,
900 addresses.state,
901 addresses.postalcode,
902 addresses.country,
903 COALESCE(address_address_types.address_type_ids, '{}') AS address_type_ids,
904 COALESCE(address_address_types.address_types, '{}') AS address_types
905 FROM addresses
906 LEFT JOIN address_address_types USING (address_id);
907
908 CREATE VIEW view_slices AS
909 SELECT
910 slices.slice_id,
911 slices.site_id,
912 slices.peer_id,
913 slices.name,
914 slices.instantiation,
915 slices.url,
916 slices.description,
917 slices.max_nodes,
918 slices.creator_person_id,
919 slices.is_deleted,
920 CAST(date_part('epoch', slices.created) AS bigint) AS created,
921 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
922 COALESCE(slice_nodes.node_ids, '{}') AS node_ids,
923 COALESCE(slice_persons.person_ids, '{}') AS person_ids,
924 COALESCE(slice_attributes.slice_attribute_ids, '{}') AS slice_attribute_ids
925 FROM slices
926 LEFT JOIN slice_nodes USING (slice_id)
927 LEFT JOIN slice_persons USING (slice_id)
928 LEFT JOIN slice_attributes USING (slice_id);
929
930 --
931 CREATE VIEW view_slice_attributes AS
932 SELECT
933 slice_attribute.slice_attribute_id,
934 slice_attribute.slice_id,
935 slice_attribute.node_id,
936 slice_attribute_types.attribute_type_id,
937 slice_attribute_types.name,
938 slice_attribute_types.description,
939 slice_attribute_types.min_role_id,
940 slice_attribute.value,
941 slice_attribute.peer_id
942 FROM slice_attribute
943 INNER JOIN slice_attribute_types USING (attribute_type_id);
944
945 CREATE VIEW view_sessions AS
946 SELECT
947 sessions.session_id,
948 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
949 person_session.person_id,
950 node_session.node_id
951 FROM sessions
952 LEFT JOIN person_session USING (session_id)
953 LEFT JOIN node_session USING (session_id);
954
955 --------------------------------------------------------------------------------
956 -- Built-in maintenance account and default site
957 --------------------------------------------------------------------------------
958
959 INSERT INTO persons
960 (first_name, last_name, email, password, enabled)
961 VALUES
962 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
963
964 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
965 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
966 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
967 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
968
969 INSERT INTO sites
970 (login_base, name, abbreviated_name, max_slices)
971 VALUES
972 ('pl', 'PlanetLab Central', 'PLC', 100);
973
974
975