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