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