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