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