spaces not tabs.
[nodeconfig.git] / PlanetLabConf / get_plc_config.php
1 <?php
2 //
3 // Print a subset of the variables from the PLC configuration store in
4 // various formats (Perl, Python, PHP, sh)
5 //
6 // Mark Huang <mlhuang@cs.princeton.edu>
7 // Copyright (C) 2006 The Trustees of Princeton University
8 //
9 // $Id$
10 //
11
12 // Try the new plc_config.php file first
13 include 'plc_config.php';
14 require_once 'plc_api.php';
15 global $adm;
16
17 if (isset($_REQUEST['perl'])) {
18   $shebang = '#!/usr/bin/perl';
19   $format = "our $%s=%s;\n";
20   $end = '';
21 } elseif (isset($_REQUEST['python'])) {
22   $shebang = '#!/usr/bin/python';
23   $format = "%s=%s\n";
24   $end = '';
25 } elseif (isset($_REQUEST['php'])) {
26   $shebang = '<?php';
27   $format = "define('%s', %s);\n";
28   $end = '?>';
29 } else {
30   $shebang = '#!/bin/sh';
31   $format = "%s=%s\n";
32   $end = '';
33 }
34
35 echo $shebang . "\n";
36 $limit_view = True;
37
38 if (isset($_REQUEST['node_id']) ) {
39     $node_id = intval($_REQUEST['node_id']);
40 } else {
41     $node_id = 0;
42 }
43
44
45 if ($node_id) {
46     $nodes = $adm->GetNodes($node_id);
47     if (!empty($nodes)) {
48         $node = $nodes[0];
49         $tags = $adm->GetNodeTags(array('node_id' => $node_id,
50                                         'tagname' => 'infrastructure'));
51         if (!empty($tags)) {
52             $tag = $tags[0];
53             if ( intval($tag['value']) == 1 ) {
54                 $interfaces = $adm->GetInterfaces(array('ip' => $_SERVER['REMOTE_ADDR']));
55                 if (!empty($interfaces)) {
56                     $nodes = $adm->GetNodes(array($interfaces[0]['node_id']));
57                     if (!empty($nodes)) {
58                         $node = $nodes[0];
59                         if ( $node['node_id'] == $node_id )
60                         {
61                             # NOTE: only provide complete view if 
62                             #     node exists
63                             #   node has infrastrucure tag
64                             #   infrastructure tag value == 1
65                             # Check that the requestor is the node.
66                             $limit_view = False;
67                         }
68                       }
69                 }
70             }
71         }
72     }
73 }
74
75 if ( $limit_view ) {
76     $plc_constants = array('PLC_API_HOST', 'PLC_API_PATH', 'PLC_API_PORT',
77                'PLC_WWW_HOST', 'PLC_BOOT_HOST', 'PLC_PLANETFLOW_HOST',
78                'PLC_NAME', 'PLC_SLICE_PREFIX', 'PLC_MONITOR_HOST',
79                'PLC_MAIL_SUPPORT_ADDRESS',
80                'PLC_MAIL_MOM_LIST_ADDRESS',
81                'PLC_MAIL_SLICE_ADDRESS');
82 } else {
83     $plc_constants = array();
84     $const = get_defined_constants(true);
85     foreach ( $const['user'] as $name => $v ){
86         if ( preg_match('/^PLC_/', $name) == 1 ){
87             $plc_constants[] = $name;
88         }        
89     }
90 }
91
92 foreach ($plc_constants as $name) {
93     if (defined($name)) {
94         // Perl, PHP, Python, and sh all support strong single quoting
95         $value = "'" . str_replace("'", "\\'", constant($name)) . "'";
96         printf($format, $name, $value);
97     }
98 }
99 printf($format, 'PLC_API_CA_SSL_CRT', "'/usr/boot/cacert.pem'");
100 printf($format, 'PLC_ROOT_GPG_KEY_PUB', "'/usr/boot/pubring.gpg'");
101
102 echo $end . "\n";
103
104 ?>