thanks elliot
[plewww.git] / planetlab / nodes / node_add.php
1 <?php
2
3 // $Id$ 
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Common functions
13 require_once 'plc_functions.php';
14 require_once 'details.php';
15 require_once 'form.php';
16   
17 // if not a admin, pi, or tech then redirect to node index
18 // xxx does not take site into account
19 $has_privileges=plc_is_admin() || plc_is_pi() || plc_is_tech();
20 if( ! $has_privileges) {
21   drupal_set_error ("Insufficient provileges to add a node");
22   header( "index.php" );
23   return 0;
24 }
25
26 //plc_debug('POST',$_POST);
27
28 // if submitted validate and add
29 // could go in actions.php but OTOH when things fail it's more convenient 
30 // to show the current values again
31 if ( $_POST['add-node'] )  {
32
33   $errors= array();
34
35   $hostname = trim($_POST['hostname']);
36   $model= trim($_POST['model']);
37   $ip = trim($_POST['ip']);
38   $method = trim($_POST['method']);
39   $netmask = trim($_POST['netmask']);
40   $network = trim($_POST['network']);
41   $broadcast = trim($_POST['broadcast']);
42   $gateway = trim($_POST['gateway']);
43   $dns1 = trim($_POST['dns1']);
44   $dns2 = trim($_POST['dns2']);
45   $bwlimit = trim($_POST['bwlimit']);
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 match the netmask";
66     }
67   }
68   
69   if( $hostname == "" ) {
70     $errors[] = "Hostname is required";
71   }
72   if( $ip == "" ) {
73     $errors[] = "IP is required";
74   } else if ( ! is_valid_ip ($ip)) {
75     $errors []= "Invalid IP $ip";
76   }
77   
78   if( !empty($errors) ) {
79     drupal_set_error(plc_itemize($errors));
80   } else {
81     // add new node and its interface
82     $node_fields= array( "hostname"=>$hostname, "model"=>$model );
83     $site_id= plc_my_site_id();
84     $node_id= $api->AddNode( intval( $site_id ), $node_fields );
85
86     if ( empty($node_id) || ($node_id < 0) ) {
87       drupal_set_error ("AddNode failed - hostname already present, or not valid ?");
88     } else {
89       // now, try to add the network.
90       $interface_fields= array();
91       $interface_fields['is_primary']= true;
92       $interface_fields['ip']= $ip;
93       $interface_fields['type']= $_POST['type'];
94       $interface_fields['method']= $method;
95       if (!empty($bwlimit)) 
96         $interface_fields['bwlimit']=$bwlimit;
97     
98       if ( $method == 'static' ) {
99         $interface_fields['netmask']= $netmask;
100         $interface_fields['network']= $network;
101         $interface_fields['broadcast']= $broadcast;
102         $interface_fields['gateway']= $gateway;
103         $interface_fields['dns1']= $dns1;
104         if (!empty($dns2)) 
105           $interface_fields['dns2']= $dns2;
106       }
107
108       $interface_id= $api->AddInterface( $node_id, $interface_fields);
109       if ($interface_id > 0) {
110         drupal_set_message ("Node successfully created");
111         drupal_set_message ("Download a boot image in the 'Download' drop down below");
112         plc_redirect (l_node($node_id));
113       } else {
114         // if AddInterface fails, we have the node created,
115         // but no primary interface is present.
116         // The primary interface can be added later,
117         // but take a look at the possible Methods,
118         // if we specify TUN/TAP Method we will have
119         // an error on download of the configuration file
120         drupal_set_message ("Node created");
121         drupal_set_error ("But without an interface");
122         drupal_set_error ("Please review manually");
123         plc_redirect (l_node($node_id));
124       }
125     }
126   }
127 }
128
129 // Print header
130 require_once 'plc_drupal.php';
131 include 'plc_header.php';
132
133 // include javacsript helpers
134 require_once 'prototype.php';
135 drupal_set_html_head ('
136 <script type="text/javascript" src="/planetlab/nodes/interface.js"></script>
137 ');
138
139 $sites=$api->GetSites(array(plc_my_site_id()));
140 $site=$sites[0];
141 $sitename=$site['name'];
142                        
143 drupal_set_title('Add a new node in site "' . $sitename . '"');
144
145 // defaults
146 $method = $_POST['method'];
147 if( ! $method ) $method= "static";
148
149 $model = $_POST['model'];
150 if( ! $model ) $model= "Custom";
151
152 print <<< EOF
153 <p class='node_add'>
154 This page lets you declare a new machine in your site. 
155 This must be done before the machine is turned on, as it will allow you to download a boot image when complete for this node.
156 <br/>
157 You must enter an IP address even if you use DHCP.
158 </p>
159 EOF;
160
161 $details=new PlekitDetails($has_privileges);
162
163 // xxx hardwire network type for now
164 $form_variables = array('type'=>"ipv4");
165 //$form=$details->form_start(l_actions(),$form_variables);
166 $form=$details->form_start('/db/nodes/node_add.php',$form_variables);
167
168 $details->start();
169
170 $details->th_td("Hostname",$hostname,"hostname");
171 $details->th_td("Model",$model,"model");
172 $method_select = $form->select_html ("method",
173                                      interface_method_selectors($api,$method,true),
174                                      array('id'=>'method',
175                                            'onChange'=>'updateMethodFields()'));
176 $details->th_td("Method",$method_select,"method",
177                 array('input_type'=>'select','value'=>$interface['method']));
178
179 // dont display the 'type' selector as it contains only ipv4
180 //>>> GetNetworkTypes()
181 //[u'ipv4']
182
183 $details->th_td("IP address",$ip,"ip",array('width'=>15,
184                                             'onKeyup'=>'networkHelper()',
185                                             'onChange'=>'networkHelper()'));
186 $details->th_td("Netmask",$netmask,"netmask",array('width'=>15,
187                                                    'onKeyup'=>'networkHelper()',
188                                                    'onChange'=>'networkHelper()'));
189 $details->th_td("Network",$network,"network",array('width'=>15));
190 $details->th_td("Broadcast",$broadcast,"broadcast",array('width'=>15));
191 $details->th_td("Gateway",$gateway,"gateway",array('width'=>15,
192                                                    'onChange'=>'subnetChecker("gateway",false)'));
193 $details->th_td("DNS 1",$dns1,"dns1",array('width'=>15,
194                                            'onChange'=>'subnetChecker("dns1",false)'));
195 $details->th_td("DNS 2",$dns2,"dns2",array('width'=>15,
196                                            'onChange'=>'subnetChecker("dns2",true)'));
197 $details->space();
198 $details->th_td("BW limit (bps)",$bwlimit,"bwlimit",array('width'=>11));
199
200 // the buttons
201 $add_button = $form->submit_html ("add-node","Add New Node",
202                                   array('onSubmit'=>'interfaceSubmit()'));
203 $details->tr($add_button,"right");
204
205 $details->end();
206 $form->end();
207
208 // Print footer
209 include 'plc_footer.php';
210
211 ?>