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