24f39c178e9620c2b5026451819e6ea880703406
[plewww.git] / planetlab / nodes / add_node.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 // Common functions
11 require_once 'plc_functions.php';
12   
13 // if not a admin, pi, or tech then redirect to node index
14 // xxx does not take site into account
15 $has_privileges=plc_is_admin() || plc_is_pi() || plc_is_tech();
16 if( ! $has_privileges) {
17   header( "index.php" );
18 }
19
20 // this sets up which box is to be checked the first time the page is loaded
21 $method= $_POST['method'];
22 if( $method == "" ) $method= "static";
23
24 $model= $_POST['model'];
25 if( $model == "" ) $model= "Custom";
26
27
28 $submitted = false;
29
30 // if submitted validate and add
31 if ( $_POST['submitted'] )  {
32   $submitted = true;
33
34   $errors= array();
35
36   $method = trim($_POST['method']);
37   $ip = trim($_POST['ip']);
38   $netmask = trim($_POST['netmask']);
39   $network = trim($_POST['network']);
40   $gateway = trim($_POST['gateway']);
41   $broadcast = trim($_POST['broadcast']);
42   $dns1 = trim($_POST['dns1']);
43   $dns2 = trim($_POST['dns2']);
44   $hostname = trim($_POST['hostname']);
45   $model= trim($_POST['model']);
46
47   // used to generate error strings for static fields only
48   $static_fields= array();
49   $static_fields['netmask']= "Netmask address";
50   $static_fields['network']= "Network address";
51   $static_fields['gateway']= "Gateway address";
52   $static_fields['broadcast']= "Broadcast address";
53   $static_fields['dns1']= "Primary DNS address";
54   
55   if( $method == 'static' ) {
56     foreach( $static_fields as $field => $desc ) {
57       if( trim($_POST[$field]) == "" ) {
58         $errors[] = "$desc is required";
59       } elseif( !is_valid_ip(trim($_POST[$field])) ) {
60         $errors[] = "$desc is not a valid address";
61       }
62     }
63     
64     if( !is_valid_network_addr($network,$netmask) ) {
65       $errors[] = "The network address does not coorespond to the netmask";
66     }
67   }
68   
69   if( $hostname == "" ) {
70     $errors[] = "Hostname is required";
71   }
72   if( $ip == "" ) {
73     $errors[] = "IP is required";
74   }
75   if( count($errors) == 0 ) {
76     $success= 1;
77
78     // add new node and its interface
79     $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
80     $site_id= plc_my_site_id();
81     $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
82
83     if ( $api->error() ) {
84        $errors[] = "Hostname already present or not valid";
85        $success= 0;
86     } else {
87       // now, try to add the network.
88       $optional_vals= array();
89       $optional_vals['is_primary']= true;
90       $optional_vals['ip']= $ip;
91       $optional_vals['type']= 'ipv4';
92       $optional_vals['method']= $method;
93     
94       if( $method == 'static' ) {
95         $optional_vals['gateway']= $gateway;
96         $optional_vals['network']= $network;
97         $optional_vals['broadcast']= $broadcast;
98         $optional_vals['netmask']= $netmask;
99         $optional_vals['dns1']= $dns1;
100         if (!empty($dns2)) {
101           $optional_vals['dns2']= $dns2;
102         }
103       }
104
105       $interface_id= $api->AddInterface( $node_id, $optional_vals);
106       // if AddInterface fails, we have the node created,
107       // but no primary interface is present.
108       // The primary interface can be added later,
109       // but take a look at the possible Methods,
110       // if we specify TUN/TAP Method we will have
111       // an error on download of the configuration file
112     }
113   }
114 }
115
116 // Print header
117 require_once 'plc_drupal.php';
118 include 'plc_header.php';
119
120 ?>
121
122 <script language="javascript">
123 function updateStaticFields() {
124   var is_dhcp= document.fm.method[0].checked;
125
126   document.fm.netmask.disabled= is_dhcp;
127   document.fm.network.disabled= is_dhcp;
128   document.fm.gateway.disabled= is_dhcp;
129   document.fm.broadcast.disabled= is_dhcp;
130   document.fm.dns1.disabled= is_dhcp;
131   document.fm.dns2.disabled= is_dhcp;
132 }
133 </script>
134
135 <?php
136
137 if ( $success ) {
138   $link=l_node2($node_id,"here");
139   drupal_set_title('Node created');
140   print <<< EOF
141
142 <p>The node has been successfully added.
143
144 <p>View node details and download a configuration 
145     file $link.
146 EOF;
147  } else {
148   $sites=$api->GetSites(array(plc_my_site_id()));
149   $sitename=$sites[0]['name'];
150                        
151   drupal_set_title('Add a new node in site "' . $sitename . '"');
152   print <<< EOF
153
154 <p>This page will allow you to add a new machine to your site. This must
155 be done before the machine is turned on, as it will allow you to download 
156 a configuration file when complete for this node.
157
158 <p>Even for DHCP, you must enter the IP address of the node.
159 EOF;
160
161 if( count($errors) > 0 ) {
162   plc_errors ($errors);
163 }
164
165 $self = $_SERVER['PHP_SELF'];
166 if (!empty($_SERVER['QUERY_STRING'])) {
167   $self .= "?" . $_SERVER['QUERY_STRING'];
168 }
169
170 ?>
171
172 <form name="fm" method="post" action="<?php echo $self; ?>">
173 <input type="hidden" name="submitted" value="1">
174
175 <h3>Node Details</h3>
176
177
178 <input type="hidden" name="submitted" value="1">
179
180 <table width="100%" cellspacing="0" cellpadding="4" border="0">
181
182 <tr>
183 <td width=250>Hostname:</td>
184 <td><input type="text" name="hostname"
185 value="<?php print($hostname); ?>" size="40" maxlength="256"></td>
186 </tr>
187
188 <tr>
189 <td>Model:</td>
190 <td><input type="text" name="model"
191 value="<?php print($model); ?>" size="40" maxlength="256"></td>
192 </tr>
193
194 </table>
195
196
197 <h3>Interface Settings</h3>
198
199 <table width="100%" cellspacing="0" cellpadding="4" border="0">
200
201 <tr>
202 <td valign='top' width="250">Addressing Method</td>
203 <td>
204 <input type="radio" name="method" value="dhcp" onChange='updateStaticFields()'
205 <?php if($method == 'dhcp') { echo "checked"; } ?>>DHCP 
206 <input type="radio" name="method" value="static" onChange='updateStaticFields()'
207 <?php if($method == 'static') { echo "checked"; } ?>>Static
208 </td>
209 </tr>
210
211 <tr> 
212 <td valign='top'>IP Address</td>
213 <td><input type="text" name="ip" value="<?php print($ip); ?>"></td>
214 </tr>
215
216 <tr> 
217 <td valign='top'>Netmask</font></td>
218 <td><input type="text" name="netmask" value="<?php print($netmask); ?>"></td>
219 </tr>
220
221 <tr> 
222 <td valign='top'>Network address</td>
223 <td><input type="text" name="network" value="<?php print($network); ?>">
224 </td>
225 </tr>
226
227 <tr> 
228 <td valign='top'>Gateway Address</td>
229 <td><input type="text" name="gateway" value="<?php print($gateway); ?>"></td>
230 </tr>
231
232 <tr> 
233 <td valign='top'>Broadcast address</td>
234 <td><input type="text" name="broadcast" value="<?php print($broadcast); ?>">
235 </td>
236 </tr>
237
238 <tr> 
239 <td valign='top'>Primary DNS</td>
240 <td><input type="text" name="dns1" value="<?php print($dns1); ?>">
241 </td>
242 </tr>
243
244 <tr>
245 <td valign='top'>Secondary DNS (optional)</td>
246 <td><input type="text" name="dns2" value="<?php print($dns2); ?>">
247 </td>
248 </tr>
249
250 <tr>
251 <td></td>
252 <td><input type="submit" name="Submit" value="Add"></td>
253 </tr>
254
255 </table>
256 </form>
257
258 <?php
259
260 }
261
262
263 // Print footer
264 include 'plc_footer.php';
265
266 ?>