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