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