"support user created slice conf files"
[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 8984 2008-04-09 17:48:33Z rmoran $
13 --
14
15 SET client_encoding = 'UNICODE';
16
17 --------------------------------------------------------------------------------
18 -- Aggregates and store procedures
19 --------------------------------------------------------------------------------
20
21 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
22 -- PostgreSQL array.
23 CREATE AGGREGATE array_accum (
24     sfunc = array_append,
25     basetype = anyelement,
26     stype = anyarray,
27     initcond = '{}'
28 );
29
30 --------------------------------------------------------------------------------
31 -- Version
32 --------------------------------------------------------------------------------
33
34 -- Database version
35 CREATE TABLE plc_db_version (
36     version integer NOT NULL,
37     subversion integer NOT NULL DEFAULT 0
38 ) WITH OIDS;
39
40 INSERT INTO plc_db_version (version, subversion) VALUES (4, 9);
41
42 --------------------------------------------------------------------------------
43 -- Accounts
44 --------------------------------------------------------------------------------
45
46 -- Accounts
47 CREATE TABLE persons (
48     -- Mandatory
49     person_id serial PRIMARY KEY, -- Account identifier
50     email text NOT NULL, -- E-mail address
51     first_name text NOT NULL, -- First name
52     last_name text NOT NULL, -- Last name
53     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
54     enabled boolean NOT NULL DEFAULT false, -- Has been disabled
55
56     -- Password
57     password text NOT NULL DEFAULT 'nopass', -- Password (md5crypted)
58     verification_key text, -- Reset password key
59     verification_expires timestamp without time zone,
60
61     -- Optional
62     title text, -- Honorific
63     phone text, -- Telephone number
64     url text, -- Home page
65     bio text, -- Biography
66
67     -- Timestamps
68     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
69     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
70 ) WITH OIDS;
71 CREATE INDEX persons_email_idx ON persons (email);
72
73 --------------------------------------------------------------------------------
74 -- Sites
75 --------------------------------------------------------------------------------
76
77 -- Sites
78 CREATE TABLE sites (
79     -- Mandatory
80     site_id serial PRIMARY KEY, -- Site identifier
81     login_base text NOT NULL, -- Site slice prefix
82     name text NOT NULL, -- Site name
83     abbreviated_name text NOT NULL, -- Site abbreviated name
84     enabled boolean NOT NULL Default true, -- Is this site enabled
85     deleted boolean NOT NULL DEFAULT false, -- Has been deleted
86     is_public boolean NOT NULL DEFAULT true, -- Shows up in public lists
87     max_slices integer NOT NULL DEFAULT 0, -- Maximum number of slices
88     max_slivers integer NOT NULL DEFAULT 1000, -- Maximum number of instantiated slivers
89
90     -- Optional
91     latitude real,
92     longitude real,
93     url text,
94     ext_consortium_id integer,  -- external consortium id
95
96     -- Timestamps
97     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
98     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
99 ) WITH OIDS;
100 CREATE INDEX sites_login_base_idx ON sites (login_base);
101
102 -- Account site membership
103 CREATE TABLE person_site (
104     person_id integer REFERENCES persons NOT NULL, -- Account identifier
105     site_id integer REFERENCES sites NOT NULL, -- Site identifier
106     is_primary boolean NOT NULL DEFAULT false, -- Is the primary site for this account
107     PRIMARY KEY (person_id, site_id)
108 );
109 CREATE INDEX person_site_person_id_idx ON person_site (person_id);
110 CREATE INDEX person_site_site_id_idx ON person_site (site_id);
111
112 -- Ordered by primary site first
113 CREATE VIEW person_site_ordered AS
114 SELECT person_id, site_id
115 FROM person_site
116 ORDER BY is_primary DESC;
117
118 -- Sites that each person is a member of
119 CREATE VIEW person_sites AS
120 SELECT person_id,
121 array_accum(site_id) AS site_ids
122 FROM person_site_ordered
123 GROUP BY person_id;
124
125 -- Accounts at each site
126 CREATE VIEW site_persons AS
127 SELECT site_id,
128 array_accum(person_id) AS person_ids
129 FROM person_site
130 GROUP BY site_id;
131
132 --------------------------------------------------------------------------------
133 -- Mailing Addresses
134 --------------------------------------------------------------------------------
135
136 CREATE TABLE address_types (
137     address_type_id serial PRIMARY KEY, -- Address type identifier
138     name text UNIQUE NOT NULL, -- Address type
139     description text -- Address type description
140 ) WITH OIDS;
141
142 INSERT INTO address_types (name) VALUES ('Personal');
143 INSERT INTO address_types (name) VALUES ('Shipping');
144 -- XXX Used to be Site
145 INSERT INTO address_types (name) VALUES ('Billing');
146
147 -- Mailing addresses
148 CREATE TABLE addresses (
149     address_id serial PRIMARY KEY, -- Address identifier
150     line1 text NOT NULL, -- Address line 1
151     line2 text, -- Address line 2
152     line3 text, -- Address line 3
153     city text NOT NULL, -- City
154     state text NOT NULL, -- State or province
155     postalcode text NOT NULL, -- Postal code
156     country text NOT NULL -- Country
157 ) WITH OIDS;
158
159 -- Each mailing address can be one of several types
160 CREATE TABLE address_address_type (
161     address_id integer REFERENCES addresses NOT NULL, -- Address identifier
162     address_type_id integer REFERENCES address_types NOT NULL, -- Address type
163     PRIMARY KEY (address_id, address_type_id)
164 ) WITH OIDS;
165 CREATE INDEX address_address_type_address_id_idx ON address_address_type (address_id);
166 CREATE INDEX address_address_type_address_type_id_idx ON address_address_type (address_type_id);
167
168 CREATE VIEW address_address_types AS
169 SELECT address_id,
170 array_accum(address_type_id) AS address_type_ids,
171 array_accum(address_types.name) AS address_types
172 FROM address_address_type
173 LEFT JOIN address_types USING (address_type_id)
174 GROUP BY address_id;
175
176 CREATE TABLE site_address (
177     site_id integer REFERENCES sites NOT NULL, -- Site identifier
178     address_id integer REFERENCES addresses NOT NULL, -- Address identifier
179     PRIMARY KEY (site_id, address_id)
180 ) WITH OIDS;
181 CREATE INDEX site_address_site_id_idx ON site_address (site_id);
182 CREATE INDEX site_address_address_id_idx ON site_address (address_id);
183
184 CREATE VIEW site_addresses AS
185 SELECT site_id,
186 array_accum(address_id) AS address_ids
187 FROM site_address
188 GROUP BY site_id;
189
190 --------------------------------------------------------------------------------
191 -- Authentication Keys
192 --------------------------------------------------------------------------------
193
194 -- Valid key types
195 CREATE TABLE key_types (
196     key_type text PRIMARY KEY -- Key type
197 ) WITH OIDS;
198 INSERT INTO key_types (key_type) VALUES ('ssh');
199
200 -- Authentication keys
201 CREATE TABLE keys (
202     key_id serial PRIMARY KEY, -- Key identifier
203     key_type text REFERENCES key_types NOT NULL, -- Key type
204     key text NOT NULL, -- Key material
205     is_blacklisted boolean NOT NULL DEFAULT false -- Has been blacklisted
206 ) WITH OIDS;
207
208 -- Account authentication key(s)
209 CREATE TABLE person_key (
210     key_id integer REFERENCES keys PRIMARY KEY, -- Key identifier
211     person_id integer REFERENCES persons NOT NULL -- Account identifier
212 ) WITH OIDS;
213 CREATE INDEX person_key_person_id_idx ON person_key (person_id);
214
215 CREATE VIEW person_keys AS
216 SELECT person_id,
217 array_accum(key_id) AS key_ids
218 FROM person_key
219 GROUP BY person_id;
220
221 --------------------------------------------------------------------------------
222 -- Account roles
223 --------------------------------------------------------------------------------
224
225 -- Valid account roles
226 CREATE TABLE roles (
227     role_id integer PRIMARY KEY, -- Role identifier
228     name text UNIQUE NOT NULL -- Role symbolic name
229 ) WITH OIDS;
230 INSERT INTO roles (role_id, name) VALUES (10, 'admin');
231 INSERT INTO roles (role_id, name) VALUES (20, 'pi');
232 INSERT INTO roles (role_id, name) VALUES (30, 'user');
233 INSERT INTO roles (role_id, name) VALUES (40, 'tech');
234
235 CREATE TABLE person_role (
236     person_id integer REFERENCES persons NOT NULL, -- Account identifier
237     role_id integer REFERENCES roles NOT NULL, -- Role identifier
238     PRIMARY KEY (person_id, role_id)
239 ) WITH OIDS;
240 CREATE INDEX person_role_person_id_idx ON person_role (person_id);
241
242 -- Account roles
243 CREATE VIEW person_roles AS
244 SELECT person_id,
245 array_accum(role_id) AS role_ids,
246 array_accum(roles.name) AS roles
247 FROM person_role
248 LEFT JOIN roles USING (role_id)
249 GROUP BY person_id;
250
251 --------------------------------------------------------------------------------
252 -- Nodes
253 --------------------------------------------------------------------------------
254
255 -- Valid node boot states
256 CREATE TABLE boot_states (
257     boot_state text PRIMARY KEY
258 ) WITH OIDS;
259 INSERT INTO boot_states (boot_state) VALUES ('boot');
260 INSERT INTO boot_states (boot_state) VALUES ('dbg');
261 INSERT INTO boot_states (boot_state) VALUES ('diag');
262 INSERT INTO boot_states (boot_state) VALUES ('disable');
263 INSERT INTO boot_states (boot_state) VALUES ('inst');
264 INSERT INTO boot_states (boot_state) VALUES ('rins');
265 INSERT INTO boot_states (boot_state) VALUES ('new');
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 NOT NULL, -- At which site 
273
274     boot_state text REFERENCES boot_states NOT NULL DEFAULT 'inst', -- Node boot state
275     deleted boolean NOT NULL DEFAULT false, -- Is deleted
276
277     -- Optional
278     model text, -- Hardware make and model
279     boot_nonce text, -- Random nonce updated by Boot Manager
280     version text, -- Boot CD version string updated by Boot Manager
281     ssh_rsa_key text, -- SSH host key updated by Boot Manager
282     key text, -- Node key generated by API when configuration file is downloaded
283
284     -- Timestamps
285     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
286     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
287     last_contact timestamp without time zone    
288 ) WITH OIDS;
289 CREATE INDEX nodes_hostname_idx ON nodes (hostname);
290 CREATE INDEX nodes_site_id_idx ON nodes (site_id);
291
292 -- Nodes at each site
293 CREATE VIEW site_nodes AS
294 SELECT site_id,
295 array_accum(node_id) AS node_ids
296 FROM nodes
297 WHERE deleted IS false
298 GROUP BY site_id;
299
300 --------------------------------------------------------------------------------
301 -- Node groups
302 --------------------------------------------------------------------------------
303
304 -- Node groups
305 CREATE TABLE nodegroups (
306     nodegroup_id serial PRIMARY KEY, -- Group identifier
307     name text UNIQUE NOT NULL, -- Group name
308     description text -- Group description
309 ) WITH OIDS;
310
311 -- Node group membership
312 CREATE TABLE nodegroup_node (
313     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Group identifier
314     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
315     PRIMARY KEY (nodegroup_id, node_id)
316 ) WITH OIDS;
317 CREATE INDEX nodegroup_node_nodegroup_id_idx ON nodegroup_node (nodegroup_id);
318 CREATE INDEX nodegroup_node_node_id_idx ON nodegroup_node (node_id);
319
320 -- Nodes in each node group
321 CREATE VIEW nodegroup_nodes AS
322 SELECT nodegroup_id,
323 array_accum(node_id) AS node_ids
324 FROM nodegroup_node
325 GROUP BY nodegroup_id;
326
327 -- Node groups that each node is a member of
328 CREATE VIEW node_nodegroups AS
329 SELECT node_id,
330 array_accum(nodegroup_id) AS nodegroup_ids
331 FROM nodegroup_node
332 GROUP BY node_id;
333
334 --------------------------------------------------------------------------------
335 -- Node configuration files
336 --------------------------------------------------------------------------------
337
338 CREATE TABLE conf_files (
339     conf_file_id serial PRIMARY KEY, -- Configuration file identifier
340     enabled bool NOT NULL DEFAULT true, -- Configuration file is active
341     source text NOT NULL, -- Relative path on the boot server where file can be downloaded
342     dest text NOT NULL, -- Absolute path where file should be installed
343     file_permissions text NOT NULL DEFAULT '0644', -- chmod(1) permissions
344     file_owner text NOT NULL DEFAULT 'root', -- chown(1) owner
345     file_group text NOT NULL DEFAULT 'root', -- chgrp(1) owner
346     preinstall_cmd text, -- Shell command to execute prior to installing
347     postinstall_cmd text, -- Shell command to execute after installing
348     error_cmd text, -- Shell command to execute if any error occurs
349     ignore_cmd_errors bool NOT NULL DEFAULT false, -- Install file anyway even if an error occurs
350     always_update bool NOT NULL DEFAULT false -- Always attempt to install file even if unchanged
351     site_id integer NOT NULL REFERENCES sites,  -- Site identifier 
352     creator_id integer NOT NULL REFERENCES persons,  -- Person id of whoever created this file
353     last_modified_by_id integer NOT NULL REFERENCES persons,  -- Person id of whoever last updated this file
354     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,  -- Date this file was created
355     last_modified timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP  -- Date this file was last modified
356 ) WITH OIDS;
357
358 CREATE TABLE conf_file_node (
359     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
360     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
361     PRIMARY KEY (conf_file_id, node_id)
362 );
363 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
364 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
365
366 -- Nodes linked to each configuration file
367 CREATE VIEW conf_file_nodes AS
368 SELECT conf_file_id,
369 array_accum(node_id) AS node_ids
370 FROM conf_file_node
371 GROUP BY conf_file_id;
372
373 -- Configuration files linked to each node
374 CREATE VIEW node_conf_files AS
375 SELECT node_id,
376 array_accum(conf_file_id) AS conf_file_ids
377 FROM conf_file_node
378 GROUP BY node_id;
379
380
381 CREATE TABLE conf_file_slice (
382     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
383     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
384     PRIMARY KEY (conf_file_id, node_id)
385 );
386 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
387 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
388
389 -- Slices linked to each configuration file
390 CREATE VIEW conf_file_nodes AS
391 SELECT conf_file_id,
392 array_accum(node_id) AS node_ids
393 FROM conf_file_node
394 GROUP BY conf_file_id;
395
396 -- Configuration files linked to each slice
397 CREATE VIEW node_conf_files AS
398 SELECT node_id,
399 array_accum(conf_file_id) AS conf_file_ids
400 FROM conf_file_node
401 GROUP BY node_id;
402
403 CREATE TABLE conf_file_nodegroup (
404     conf_file_id integer REFERENCES conf_files NOT NULL, -- Configuration file identifier
405     nodegroup_id integer REFERENCES nodegroups NOT NULL, -- Node group identifier
406     PRIMARY KEY (conf_file_id, nodegroup_id)
407 );
408 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
409 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
410
411 -- Node groups linked to each configuration file
412 CREATE VIEW conf_file_nodegroups AS
413 SELECT conf_file_id,
414 array_accum(nodegroup_id) AS nodegroup_ids
415 FROM conf_file_nodegroup
416 GROUP BY conf_file_id;
417
418 -- Configuration files linked to each node group
419 CREATE VIEW nodegroup_conf_files AS
420 SELECT nodegroup_id,
421 array_accum(conf_file_id) AS conf_file_ids
422 FROM conf_file_nodegroup
423 GROUP BY nodegroup_id;
424
425 --------------------------------------------------------------------------------
426 -- Node network interfaces
427 --------------------------------------------------------------------------------
428
429 -- Valid network addressing schemes
430 CREATE TABLE network_types (
431     type text PRIMARY KEY -- Addressing scheme
432 ) WITH OIDS;
433 INSERT INTO network_types (type) VALUES ('ipv4');
434
435 -- Valid network configuration methods
436 CREATE TABLE network_methods (
437     method text PRIMARY KEY -- Configuration method
438 ) WITH OIDS;
439 INSERT INTO network_methods (method) VALUES ('static');
440 INSERT INTO network_methods (method) VALUES ('dhcp');
441 INSERT INTO network_methods (method) VALUES ('proxy');
442 INSERT INTO network_methods (method) VALUES ('tap');
443 INSERT INTO network_methods (method) VALUES ('ipmi');
444 INSERT INTO network_methods (method) VALUES ('unknown');
445
446 -- Node network interfaces
447 CREATE TABLE nodenetworks (
448     -- Mandatory
449     nodenetwork_id serial PRIMARY KEY, -- Network interface identifier
450     node_id integer REFERENCES nodes NOT NULL, -- Which node
451     is_primary boolean NOT NULL DEFAULT false, -- Is the primary interface for this node
452     type text REFERENCES network_types NOT NULL, -- Addressing scheme
453     method text REFERENCES network_methods NOT NULL, -- Configuration method
454
455     -- Optional, depending on type and method
456     ip text, -- IP address
457     mac text, -- MAC address
458     gateway text, -- Default gateway address
459     network text, -- Network address
460     broadcast text, -- Network broadcast address
461     netmask text, -- Network mask
462     dns1 text, -- Primary DNS server
463     dns2 text, -- Secondary DNS server
464     bwlimit integer, -- Bandwidth limit in bps
465     hostname text -- Hostname of this interface
466 ) WITH OIDS;
467 CREATE INDEX nodenetworks_node_id_idx ON nodenetworks (node_id);
468
469 -- Ordered by primary interface first
470 CREATE VIEW nodenetworks_ordered AS
471 SELECT node_id, nodenetwork_id
472 FROM nodenetworks
473 ORDER BY is_primary DESC;
474
475 -- Network interfaces on each node
476 CREATE VIEW node_nodenetworks AS
477 SELECT node_id,
478 array_accum(nodenetwork_id) AS nodenetwork_ids
479 FROM nodenetworks_ordered
480 GROUP BY node_id;
481
482 --------------------------------------------------------------------------------
483 -- Nodenetwork setting types and nodenetworks settings
484 --------------------------------------------------------------------------------
485
486 CREATE TABLE nodenetwork_setting_types (
487     nodenetwork_setting_type_id serial PRIMARY KEY,     
488                                                 -- Setting Type Identifier
489     name text UNIQUE NOT NULL,                  -- Setting Name    
490     description text,                           -- Optional Description
491     category text NOT NULL,                     -- Category, e.g. Wifi, or whatever
492     min_role_id integer references roles        -- If set, minimal role required
493 ) WITH OIDS;
494
495 CREATE TABLE nodenetwork_setting (
496     nodenetwork_setting_id serial PRIMARY KEY,  -- Nodenetwork Setting Identifier
497     nodenetwork_id integer REFERENCES nodenetworks NOT NULL,
498                                                 -- the nodenetwork this applies to
499     nodenetwork_setting_type_id integer REFERENCES nodenetwork_setting_types NOT NULL,
500                                                 -- the setting type
501     value text
502 ) WITH OIDS;
503
504 CREATE OR REPLACE VIEW nodenetwork_settings AS 
505 SELECT nodenetwork_id,
506 array_accum(nodenetwork_setting_id) AS nodenetwork_setting_ids
507 FROM nodenetwork_setting
508 GROUP BY nodenetwork_id;
509
510 CREATE OR REPLACE VIEW view_nodenetwork_settings AS
511 SELECT
512 nodenetwork_setting.nodenetwork_setting_id,
513 nodenetwork_setting.nodenetwork_id,
514 nodenetwork_setting_types.nodenetwork_setting_type_id,
515 nodenetwork_setting_types.name,
516 nodenetwork_setting_types.description,
517 nodenetwork_setting_types.category,
518 nodenetwork_setting_types.min_role_id,
519 nodenetwork_setting.value
520 FROM nodenetwork_setting
521 INNER JOIN nodenetwork_setting_types USING (nodenetwork_setting_type_id);
522
523 CREATE OR REPLACE VIEW view_nodenetworks AS
524 SELECT
525 nodenetworks.nodenetwork_id,
526 nodenetworks.node_id,
527 nodenetworks.is_primary,
528 nodenetworks.type,
529 nodenetworks.method,
530 nodenetworks.ip,
531 nodenetworks.mac,
532 nodenetworks.gateway,
533 nodenetworks.network,
534 nodenetworks.broadcast,
535 nodenetworks.netmask,
536 nodenetworks.dns1,
537 nodenetworks.dns2,
538 nodenetworks.bwlimit,
539 nodenetworks.hostname,
540 COALESCE((SELECT nodenetwork_setting_ids FROM nodenetwork_settings WHERE nodenetwork_settings.nodenetwork_id = nodenetworks.nodenetwork_id), '{}') AS nodenetwork_setting_ids
541 FROM nodenetworks;
542
543 --------------------------------------------------------------------------------
544 -- Power control units (PCUs)
545 --------------------------------------------------------------------------------
546
547 CREATE TABLE pcus (
548     -- Mandatory
549     pcu_id serial PRIMARY KEY, -- PCU identifier
550     site_id integer REFERENCES sites NOT NULL, -- Site identifier
551     hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
552     ip text NOT NULL, -- IP, not necessarily unique
553
554     -- Optional
555     protocol text, -- Protocol, e.g. ssh or https or telnet
556     username text, -- Username, if applicable
557     "password" text, -- Password, if applicable
558     model text, -- Model, e.g. BayTech or iPal
559     notes text -- Random notes
560 ) WITH OIDS;
561 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
562
563 CREATE VIEW site_pcus AS
564 SELECT site_id,
565 array_accum(pcu_id) AS pcu_ids
566 FROM pcus
567 GROUP BY site_id;
568
569 CREATE TABLE pcu_node (
570     pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
571     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
572     port integer NOT NULL, -- Port number
573     PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
574     UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
575 );
576 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
577 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
578
579 CREATE VIEW node_pcus AS
580 SELECT node_id,
581 array_accum(pcu_id) AS pcu_ids,
582 array_accum(port) AS ports
583 FROM pcu_node
584 GROUP BY node_id;
585
586 CREATE VIEW pcu_nodes AS
587 SELECT pcu_id,
588 array_accum(node_id) AS node_ids,
589 array_accum(port) AS ports
590 FROM pcu_node
591 GROUP BY pcu_id;
592
593 --------------------------------------------------------------------------------
594 -- Slices
595 --------------------------------------------------------------------------------
596
597 CREATE TABLE slice_instantiations (
598     instantiation text PRIMARY KEY
599 ) WITH OIDS;
600 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
601 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
602 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
603 INSERT INTO slice_instantiations (instantiation) VALUES ('nm-controller'); -- NM Controller
604
605 -- Slices
606 CREATE TABLE slices (
607     slice_id serial PRIMARY KEY, -- Slice identifier
608     site_id integer REFERENCES sites NOT NULL, -- Site identifier
609
610     name text NOT NULL, -- Slice name
611     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
612     url text, -- Project URL
613     description text, -- Project description
614
615     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
616
617     creator_person_id integer REFERENCES persons, -- Creator
618     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
619     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
620
621     is_deleted boolean NOT NULL DEFAULT false
622 ) WITH OIDS;
623 CREATE INDEX slices_site_id_idx ON slices (site_id);
624 CREATE INDEX slices_name_idx ON slices (name);
625
626 -- Slivers
627 CREATE TABLE slice_node (
628     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
629     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
630     PRIMARY KEY (slice_id, node_id)
631 ) WITH OIDS;
632 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
633 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
634
635 -- Synonym for slice_node
636 CREATE VIEW slivers AS
637 SELECT * FROM slice_node;
638
639 -- Nodes in each slice
640 CREATE VIEW slice_nodes AS
641 SELECT slice_id,
642 array_accum(node_id) AS node_ids
643 FROM slice_node
644 GROUP BY slice_id;
645
646 -- Slices on each node
647 CREATE VIEW node_slices AS
648 SELECT node_id,
649 array_accum(slice_id) AS slice_ids
650 FROM slice_node
651 GROUP BY node_id;
652
653 -- Slices at each site
654 CREATE VIEW site_slices AS
655 SELECT site_id,
656 array_accum(slice_id) AS slice_ids
657 FROM slices
658 WHERE is_deleted is false
659 GROUP BY site_id;
660
661 -- Slice membership
662 CREATE TABLE slice_person (
663     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
664     person_id integer REFERENCES persons NOT NULL, -- Account identifier
665     PRIMARY KEY (slice_id, person_id)
666 ) WITH OIDS;
667 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
668 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
669
670 -- Members of the slice
671 CREATE VIEW slice_persons AS
672 SELECT slice_id,
673 array_accum(person_id) AS person_ids
674 FROM slice_person
675 GROUP BY slice_id;
676
677 -- Slices of which each person is a member
678 CREATE VIEW person_slices AS
679 SELECT person_id,
680 array_accum(slice_id) AS slice_ids
681 FROM slice_person
682 GROUP BY person_id;
683
684 --------------------------------------------------------------------------------
685 -- Slice whitelist
686 --------------------------------------------------------------------------------
687 -- slice whitelist on nodes
688 CREATE TABLE node_slice_whitelist (
689     node_id integer REFERENCES nodes NOT NULL, -- Node id of whitelist
690     slice_id integer REFERENCES slices NOT NULL, -- Slice id thats allowd on this node
691     PRIMARY KEY (node_id, slice_id)
692 ) WITH OIDS;
693 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
694 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
695
696 -- Slices on each node
697 CREATE VIEW node_slices_whitelist AS
698 SELECT node_id,
699 array_accum(slice_id) AS slice_ids_whitelist
700 FROM node_slice_whitelist
701 GROUP BY node_id;
702
703 --------------------------------------------------------------------------------
704 -- Slice attributes
705 --------------------------------------------------------------------------------
706
707 -- Slice attribute types
708 CREATE TABLE slice_attribute_types (
709     attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
710     name text UNIQUE NOT NULL, -- Attribute name
711     description text, -- Attribute description
712     min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
713 ) WITH OIDS;
714
715 -- Slice/sliver attributes
716 CREATE TABLE slice_attribute (
717     slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
718     slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
719     node_id integer REFERENCES nodes, -- Sliver attribute if set
720     nodegroup_id integer REFERENCES nodegroups, -- Node group attribute if set
721     attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
722     value text
723 ) WITH OIDS;
724 CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
725 CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
726 CREATE INDEX slice_attribute_nodegroup_id_idx ON slice_attribute (nodegroup_id);
727
728 CREATE VIEW slice_attributes AS
729 SELECT slice_id,
730 array_accum(slice_attribute_id) AS slice_attribute_ids
731 FROM slice_attribute
732 GROUP BY slice_id;
733
734 --------------------------------------------------------------------------------
735 -- Initscripts
736 --------------------------------------------------------------------------------
737
738 -- Initscripts
739 CREATE TABLE initscripts (
740     initscript_id serial PRIMARY KEY, -- Initscript identifier
741     name text NOT NULL, -- Initscript name
742     enabled bool NOT NULL DEFAULT true, -- Initscript is active
743     script text NOT NULL, -- Initscript
744     UNIQUE (name)
745 ) WITH OIDS;
746 CREATE INDEX initscripts_name_idx ON initscripts (name);
747
748
749 --------------------------------------------------------------------------------
750 -- Peers
751 --------------------------------------------------------------------------------
752
753 -- Peers
754 CREATE TABLE peers (
755     peer_id serial PRIMARY KEY, -- Peer identifier
756     peername text NOT NULL, -- Peer name
757     peer_url text NOT NULL, -- (HTTPS) URL of the peer PLCAPI interface
758     cacert text, -- (SSL) Public certificate of peer API server
759     key text, -- (GPG) Public key used for authentication
760     deleted boolean NOT NULL DEFAULT false
761 ) WITH OIDS;
762 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
763
764 -- Objects at each peer
765 CREATE TABLE peer_site (
766     site_id integer REFERENCES sites PRIMARY KEY, -- Local site identifier
767     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
768     peer_site_id integer NOT NULL, -- Foreign site identifier at peer
769     UNIQUE (peer_id, peer_site_id) -- The same foreign site should not be cached twice
770 ) WITH OIDS;
771 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
772
773 CREATE VIEW peer_sites AS
774 SELECT peer_id,
775 array_accum(site_id) AS site_ids,
776 array_accum(peer_site_id) AS peer_site_ids
777 FROM peer_site
778 GROUP BY peer_id;
779
780 CREATE TABLE peer_person (
781     person_id integer REFERENCES persons PRIMARY KEY, -- Local user identifier
782     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
783     peer_person_id integer NOT NULL, -- Foreign user identifier at peer
784     UNIQUE (peer_id, peer_person_id) -- The same foreign user should not be cached twice
785 ) WITH OIDS;
786 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
787
788 CREATE VIEW peer_persons AS
789 SELECT peer_id,
790 array_accum(person_id) AS person_ids,
791 array_accum(peer_person_id) AS peer_person_ids
792 FROM peer_person
793 GROUP BY peer_id;
794
795 CREATE TABLE peer_key (
796     key_id integer REFERENCES keys PRIMARY KEY, -- Local key identifier
797     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
798     peer_key_id integer NOT NULL, -- Foreign key identifier at peer
799     UNIQUE (peer_id, peer_key_id) -- The same foreign key should not be cached twice
800 ) WITH OIDS;
801 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
802
803 CREATE VIEW peer_keys AS
804 SELECT peer_id,
805 array_accum(key_id) AS key_ids,
806 array_accum(peer_key_id) AS peer_key_ids
807 FROM peer_key
808 GROUP BY peer_id;
809
810 CREATE TABLE peer_node (
811     node_id integer REFERENCES nodes PRIMARY KEY, -- Local node identifier
812     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
813     peer_node_id integer NOT NULL, -- Foreign node identifier
814     UNIQUE (peer_id, peer_node_id) -- The same foreign node should not be cached twice
815 ) WITH OIDS;
816 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
817
818 CREATE VIEW peer_nodes AS
819 SELECT peer_id,
820 array_accum(node_id) AS node_ids,
821 array_accum(peer_node_id) AS peer_node_ids
822 FROM peer_node
823 GROUP BY peer_id;
824
825 CREATE TABLE peer_slice (
826     slice_id integer REFERENCES slices PRIMARY KEY, -- Local slice identifier
827     peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
828     peer_slice_id integer NOT NULL, -- Slice identifier at peer
829     UNIQUE (peer_id, peer_slice_id) -- The same foreign slice should not be cached twice
830 ) WITH OIDS;
831 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
832
833 CREATE VIEW peer_slices AS
834 SELECT peer_id,
835 array_accum(slice_id) AS slice_ids,
836 array_accum(peer_slice_id) AS peer_slice_ids
837 FROM peer_slice
838 GROUP BY peer_id;
839
840 --------------------------------------------------------------------------------
841 -- Authenticated sessions
842 --------------------------------------------------------------------------------
843
844 -- Authenticated sessions
845 CREATE TABLE sessions (
846     session_id text PRIMARY KEY, -- Session identifier
847     expires timestamp without time zone
848 ) WITH OIDS;
849
850 -- People can have multiple sessions
851 CREATE TABLE person_session (
852     person_id integer REFERENCES persons NOT NULL, -- Account identifier
853     session_id text REFERENCES sessions NOT NULL, -- Session identifier
854     PRIMARY KEY (person_id, session_id),
855     UNIQUE (session_id) -- Sessions are unique
856 ) WITH OIDS;
857 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
858
859 -- Nodes can have only one session
860 CREATE TABLE node_session (
861     node_id integer REFERENCES nodes NOT NULL, -- Node identifier
862     session_id text REFERENCES sessions NOT NULL, -- Session identifier
863     UNIQUE (node_id), -- Nodes can have only one session
864     UNIQUE (session_id) -- Sessions are unique
865 ) WITH OIDS;
866
867
868
869 -------------------------------------------------------------------------------
870 -- PCU Types
871 ------------------------------------------------------------------------------
872 CREATE TABLE pcu_types (
873     pcu_type_id serial PRIMARY KEY,
874     model text NOT NULL ,               -- PCU model name
875     name text                           -- Full PCU model name
876 ) WITH OIDS;
877 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
878
879 CREATE TABLE pcu_protocol_type (
880     pcu_protocol_type_id serial PRIMARY KEY,
881     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
882     port integer NOT NULL,                              -- PCU port
883     protocol text NOT NULL,                             -- Protocol
884     supported boolean NOT NULL DEFAULT True             -- Does PLC support
885 ) WITH OIDS;
886 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
887
888
889 CREATE OR REPLACE VIEW pcu_protocol_types AS
890 SELECT pcu_type_id,
891 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
892 FROM pcu_protocol_type
893 GROUP BY pcu_type_id;
894
895 --------------------------------------------------------------------------------
896 -- Message templates
897 --------------------------------------------------------------------------------
898
899 CREATE TABLE messages (
900     message_id text PRIMARY KEY, -- Message name
901     subject text, -- Message summary
902     template text, -- Message template
903     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
904 ) WITH OIDS;
905
906 --------------------------------------------------------------------------------
907 -- Events
908 --------------------------------------------------------------------------------
909
910 -- Events
911 CREATE TABLE events (
912     event_id serial PRIMARY KEY,  -- Event identifier
913     person_id integer REFERENCES persons, -- Person responsible for event, if any
914     node_id integer REFERENCES nodes, -- Node responsible for event, if any
915     auth_type text, -- Type of auth used. i.e. AuthMethod
916     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
917     call_name text NOT NULL, -- Call responsible for this event
918     call text NOT NULL, -- Call responsible for this event, including parameters
919     message text, -- High level description of this event
920     runtime float DEFAULT 0, -- Event run time
921     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
922 ) WITH OIDS;
923
924 -- Database object(s) that may have been affected by a particular event
925 CREATE TABLE event_object (
926     event_id integer REFERENCES events NOT NULL, -- Event identifier
927     object_id integer NOT NULL, -- Object identifier
928     object_type text NOT NULL Default 'Unknown' -- What type of object is this event affecting
929 ) WITH OIDS;
930 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
931 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
932 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
933
934 CREATE OR REPLACE VIEW event_objects AS
935 SELECT event_id,
936 array_accum(object_id) AS object_ids,
937 array_accum(object_type) AS object_types
938 FROM event_object
939 GROUP BY event_id;
940
941 --------------------------------------------------------------------------------
942 -- Useful views
943 --------------------------------------------------------------------------------
944 CREATE OR REPLACE VIEW view_pcu_types AS
945 SELECT
946 pcu_types.pcu_type_id,
947 pcu_types.model,
948 pcu_types.name,
949 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') AS pcu_protocol_type_ids
950 FROM pcu_types;
951
952 CREATE OR REPLACE VIEW view_events AS
953 SELECT
954 events.event_id,
955 events.person_id,
956 events.node_id,
957 events.auth_type,
958 events.fault_code,
959 events.call_name,
960 events.call,
961 events.message,
962 events.runtime,
963 CAST(date_part('epoch', events.time) AS bigint) AS time,
964 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
965 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
966 FROM events;
967
968 CREATE OR REPLACE VIEW view_event_objects AS 
969 SELECT
970 events.event_id,
971 events.person_id,
972 events.node_id,
973 events.fault_code,
974 events.call_name,
975 events.call,
976 events.message,
977 events.runtime,
978 CAST(date_part('epoch', events.time) AS bigint) AS time,
979 event_object.object_id,
980 event_object.object_type
981 FROM events LEFT JOIN event_object USING (event_id);
982
983 CREATE OR REPLACE VIEW view_persons AS
984 SELECT
985 persons.person_id,
986 persons.email,
987 persons.first_name,
988 persons.last_name,
989 persons.deleted,
990 persons.enabled,
991 persons.password,
992 persons.verification_key,
993 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
994 persons.title,
995 persons.phone,
996 persons.url,
997 persons.bio,
998 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
999 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
1000 peer_person.peer_id,
1001 peer_person.peer_person_id,
1002 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
1003 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
1004 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
1005 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
1006 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
1007 FROM persons
1008 LEFT JOIN peer_person USING (person_id);
1009
1010 CREATE OR REPLACE VIEW view_peers AS
1011 SELECT 
1012 peers.*, 
1013 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
1014 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
1015 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
1016 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
1017 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
1018 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
1019 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
1020 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
1021 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
1022 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
1023 FROM peers;
1024
1025 CREATE OR REPLACE VIEW view_nodes AS
1026 SELECT
1027 nodes.node_id,
1028 nodes.hostname,
1029 nodes.site_id,
1030 nodes.boot_state,
1031 nodes.deleted,
1032 nodes.model,
1033 nodes.boot_nonce,
1034 nodes.version,
1035 nodes.ssh_rsa_key,
1036 nodes.key,
1037 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1038 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1039 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1040 peer_node.peer_id,
1041 peer_node.peer_node_id,
1042 COALESCE((SELECT nodenetwork_ids FROM node_nodenetworks WHERE node_nodenetworks.node_id = nodes.node_id), '{}') AS nodenetwork_ids,
1043 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
1044 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
1045 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') AS slice_ids_whitelist,
1046 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
1047 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
1048 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
1049 node_session.session_id AS session
1050 FROM nodes
1051 LEFT JOIN peer_node USING (node_id)
1052 LEFT JOIN node_session USING (node_id);
1053
1054 CREATE OR REPLACE VIEW view_nodegroups AS
1055 SELECT
1056 nodegroups.*,
1057 COALESCE((SELECT node_ids FROM nodegroup_nodes WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') AS node_ids,
1058 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') AS conf_file_ids
1059 FROM nodegroups;
1060
1061 CREATE OR REPLACE VIEW view_conf_files AS
1062 SELECT
1063 conf_files.conf_file_id,
1064 conf_files.enabled,
1065 conf_files.source,
1066 conf_files.dest,
1067 conf_files.file_permissions,
1068 conf_files.file_owner,
1069 conf_files.file_group,
1070 conf_files.preinstall_cmd,
1071 conf_files.postinstall_cmd,
1072 conf_files.error_cmd,
1073 conf_files.ignore_cmd_errors,
1074 conf_files.always_update,
1075 conf_files.site_id,
1076 conf_files.creator_id,
1077 conf_files.last_modified_by_id,
1078 CAST(date_part('epoch', conf_files.date_created) AS bigint) AS date_created,
1079 CAST(date_part('epoch', conf_files.last_modified) AS bigint) AS last_modified,
1080 COALESCE((SELECT node_ids FROM conf_file_nodes WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') AS node_ids,
1081 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') AS nodegroup_ids
1082 FROM conf_files;
1083
1084 CREATE OR REPLACE VIEW view_pcus AS
1085 SELECT
1086 pcus.*,
1087 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1088 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1089 FROM pcus;
1090
1091 CREATE OR REPLACE VIEW view_sites AS
1092 SELECT
1093 sites.site_id,
1094 sites.login_base,
1095 sites.name,
1096 sites.abbreviated_name,
1097 sites.deleted,
1098 sites.enabled,
1099 sites.is_public,
1100 sites.max_slices,
1101 sites.max_slivers,
1102 sites.latitude,
1103 sites.longitude,
1104 sites.url,
1105 sites.ext_consortium_id,
1106 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1107 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1108 peer_site.peer_id,
1109 peer_site.peer_site_id,
1110 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1111 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1112 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1113 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1114 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
1115 FROM sites
1116 LEFT JOIN peer_site USING (site_id);
1117
1118 CREATE OR REPLACE VIEW view_addresses AS
1119 SELECT
1120 addresses.*,
1121 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1122 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1123 FROM addresses;
1124
1125 CREATE OR REPLACE VIEW view_keys AS
1126 SELECT
1127 keys.*,
1128 person_key.person_id,
1129 peer_key.peer_id,
1130 peer_key.peer_key_id
1131 FROM keys
1132 LEFT JOIN person_key USING (key_id)
1133 LEFT JOIN peer_key USING (key_id);
1134
1135 CREATE OR REPLACE VIEW view_slices AS
1136 SELECT
1137 slices.slice_id,
1138 slices.site_id,
1139 slices.name,
1140 slices.instantiation,
1141 slices.url,
1142 slices.description,
1143 slices.max_nodes,
1144 slices.creator_person_id,
1145 slices.is_deleted,
1146 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1147 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1148 peer_slice.peer_id,
1149 peer_slice.peer_slice_id,
1150 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1151 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1152 COALESCE((SELECT slice_attribute_ids FROM slice_attributes WHERE slice_attributes.slice_id = slices.slice_id), '{}') AS slice_attribute_ids
1153 FROM slices
1154 LEFT JOIN peer_slice USING (slice_id);
1155
1156 CREATE OR REPLACE VIEW view_slice_attributes AS
1157 SELECT
1158 slice_attribute.slice_attribute_id,
1159 slice_attribute.slice_id,
1160 slice_attribute.node_id,
1161 slice_attribute.nodegroup_id,
1162 slice_attribute_types.attribute_type_id,
1163 slice_attribute_types.name,
1164 slice_attribute_types.description,
1165 slice_attribute_types.min_role_id,
1166 slice_attribute.value
1167 FROM slice_attribute
1168 INNER JOIN slice_attribute_types USING (attribute_type_id);
1169
1170 CREATE OR REPLACE VIEW view_sessions AS
1171 SELECT
1172 sessions.session_id,
1173 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1174 person_session.person_id,
1175 node_session.node_id
1176 FROM sessions
1177 LEFT JOIN person_session USING (session_id)
1178 LEFT JOIN node_session USING (session_id);
1179
1180 --------------------------------------------------------------------------------
1181 -- Built-in maintenance account and default site
1182 --------------------------------------------------------------------------------
1183
1184 INSERT INTO persons
1185 (first_name, last_name, email, password, enabled)
1186 VALUES
1187 ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1188
1189 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1190 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1191 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1192 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1193
1194 INSERT INTO sites
1195 (login_base, name, abbreviated_name, max_slices)
1196 VALUES
1197 ('pl', 'PlanetLab Central', 'PLC', 100);