115f177ae96bfb21fe94115f8cfebb07ef555c7b
[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 'toggle.php';
15 require_once 'details.php';
16 require_once 'form.php';
17   
18 // if not a admin, pi, or tech then redirect to node index
19 // xxx does not take site into account
20 $has_privileges=plc_is_admin();
21 $is_pi_or_tech=plc_is_pi() || plc_is_tech();
22 if( ! $has_privileges) {
23   if ( $is_pi_or_tech) {
24     drupal_goto(l_register_node());
25     return 0;
26   }
27   drupal_set_error ("Insufficient privileges to add a node");
28   header( "index.php" );
29   return 0;
30 }
31
32 //plc_debug('POST',$_POST);
33
34 // if submitted validate and add
35 // could go in actions.php but OTOH when things fail it's more convenient 
36 // to show the current values again
37 if ( $_POST['add-node'] )  {
38
39   $errors= array();
40
41   $site_id = trim($_POST['site_id']);
42   $hostname = trim($_POST['hostname']);
43   $model= trim($_POST['model']);
44   $ip = trim($_POST['ip']);
45   $method = trim($_POST['method']);
46   $netmask = trim($_POST['netmask']);
47   $network = trim($_POST['network']);
48   $broadcast = trim($_POST['broadcast']);
49   $gateway = trim($_POST['gateway']);
50   $dns1 = trim($_POST['dns1']);
51   $dns2 = trim($_POST['dns2']);
52   $bwlimit = trim($_POST['bwlimit']);
53
54   // used to generate error strings for static fields only
55   $static_fields= array();
56   $static_fields['netmask']= "Netmask address";
57   $static_fields['network']= "Network address";
58   $static_fields['gateway']= "Gateway address";
59   $static_fields['broadcast']= "Broadcast address";
60   $static_fields['dns1']= "Primary DNS address";
61   
62   if( $method == 'static' ) {
63     foreach( $static_fields as $field => $desc ) {
64       if( trim($_POST[$field]) == "" ) {
65         $errors[] = "$desc is required";
66       } elseif( !is_valid_ip(trim($_POST[$field])) ) {
67         $errors[] = "$desc is not a valid address";
68       }
69     }
70     
71     if( !is_valid_network_addr($network,$netmask) ) {
72       $errors[] = "The network address does not match the netmask";
73     }
74   }
75   
76   if( $hostname == "" ) {
77     $errors[] = "Hostname is required";
78   }
79   if( $ip == "" ) {
80     $errors[] = "IP is required";
81   } else if ( ! is_valid_ip ($ip)) {
82     $errors []= "Invalid IP $ip";
83   }
84   
85   if( !empty($errors) ) {
86     drupal_set_error(plc_itemize($errors));
87   } else {
88     // add new node and its interface
89     $node_fields= array( "hostname"=>$hostname, "model"=>$model );
90     $node_id= $api->AddNode( intval( $site_id ), $node_fields );
91
92     if ( empty($node_id) || ($node_id < 0) ) {
93       drupal_set_error ("AddNode failed - hostname already present, or not valid ?");
94     } else {
95       // now, try to add the network.
96       $interface_fields= array();
97       $interface_fields['is_primary']= true;
98       $interface_fields['ip']= $ip;
99       $interface_fields['type']= $_POST['type'];
100       $interface_fields['method']= $method;
101       if (!empty($bwlimit)) 
102         $interface_fields['bwlimit']=$bwlimit;
103     
104       if ( $method == 'static' ) {
105         $interface_fields['netmask']= $netmask;
106         $interface_fields['network']= $network;
107         $interface_fields['broadcast']= $broadcast;
108         $interface_fields['gateway']= $gateway;
109         $interface_fields['dns1']= $dns1;
110         if (!empty($dns2)) 
111           $interface_fields['dns2']= $dns2;
112       }
113
114       $interface_id= $api->AddInterface( $node_id, $interface_fields);
115       if ($interface_id > 0) {
116         drupal_set_message ("Node successfully created");
117         drupal_set_message ("Download a boot image in the 'Download' drop down below");
118         plc_redirect (l_node($node_id));
119       } else {
120         // if AddInterface fails, we have the node created,
121         // but no primary interface is present.
122         // The primary interface can be added later,
123         // but take a look at the possible Methods,
124         // if we specify TUN/TAP Method we will have
125         // an error on download of the configuration file
126         drupal_set_message ("Node created");
127         drupal_set_error ("But without an interface");
128         drupal_set_error ("Please review manually");
129         plc_redirect (l_node($node_id));
130       }
131     }
132   }
133 }
134
135 // Print header
136 require_once 'plc_drupal.php';
137 include 'plc_header.php';
138
139 // include javacsript helpers
140 require_once 'prototype.php';
141 drupal_set_html_head ('
142 <script type="text/javascript" src="/planetlab/nodes/interface.js"></script>
143 ');
144
145 drupal_set_title('Add a new node to site');
146
147 // defaults
148 $method = $_POST['method'];
149 if( ! $method ) $method= "static";
150
151 $model = $_POST['model'];
152 if( ! $model ) $model= "Custom";
153
154 print <<< EOF
155 <p class='node_add'>
156 This page lets you declare a new machine to a site.
157 <br/>
158 This must be done before the machine is turned on, 
159 as it will allow you to download a boot image when complete for this node.
160 <br/>
161 It is now reserved to admins, as regular users are supposed to use the register wizard, that among other things enforces PCU deployment.
162 <br/>
163 An IP address is required even if you use DHCP.
164 </p>
165 EOF;
166
167 $toggle = new PlekitToggle ('add-node',"Add Node",
168                             array('bubble'=>'Add a node - does not enforce PCU - for admins only !',
169                                   'visible'=>get_arg('show_details',true)));
170 $toggle->start();
171
172 $details=new PlekitDetails($has_privileges);
173
174 // xxx hardwire network type for now
175 $form_variables = array('type'=>"ipv4");
176 //$form=$details->form_start(l_actions(),$form_variables);
177 $form=$details->form_start('/db/nodes/node_add.php',$form_variables,
178                            array('onSubmit'=>'return interfaceSubmit()'));
179
180 $details->start();
181
182 $site_columns=array( "site_id", "name", "login_base" );
183 $local_sites= $api->GetSites( array("peer_id"=>NULL, '-SORT'=>'name'), $site_columns );
184 function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); }
185 $site_selector = array_map ("site_selector",$local_sites);
186 $site_select = $form->select_html("site_id", $site_selector, array('id'=>'site_id'));
187 $details->th_td("Site",
188                 $site_select,
189                 "site_id",
190                 array('input_type'=>'select','value'=>$interface['site_id']));
191
192 $details->th_td("Hostname",$hostname,"hostname");
193 $details->th_td("Model",$model,"model");
194 $method_select = $form->select_html ("method",
195                                      interface_method_selectors($api,$method,true),
196                                      array('id'=>'method',
197                                            'onChange'=>'updateMethodFields()'));
198 $details->th_td("Method",$method_select,"method",
199                 array('input_type'=>'select','value'=>$interface['method']));
200
201 // dont display the 'type' selector as it contains only ipv4
202 //>>> GetNetworkTypes()
203 //[u'ipv4']
204
205 $details->th_td("IP address",$ip,"ip",array('width'=>15,
206                                             'onKeyup'=>'networkHelper()',
207                                             'onChange'=>'networkHelper()'));
208 $details->th_td("Netmask",$netmask,"netmask",array('width'=>15,
209                                                    'onKeyup'=>'networkHelper()',
210                                                    'onChange'=>'networkHelper()'));
211 $details->th_td("Network",$network,"network",array('width'=>15));
212 $details->th_td("Broadcast",$broadcast,"broadcast",array('width'=>15));
213 $details->th_td("Gateway",$gateway,"gateway",array('width'=>15,
214                                                    'onChange'=>'subnetChecker("gateway",false)'));
215 $details->th_td("DNS 1",$dns1,"dns1",array('width'=>15,
216                                            'onChange'=>'subnetChecker("dns1",false)'));
217 $details->th_td("DNS 2",$dns2,"dns2",array('width'=>15,
218                                            'onChange'=>'subnetChecker("dns2",true)'));
219 $details->space();
220 $details->th_td("BW limit (bps)",$bwlimit,"bwlimit",array('width'=>11));
221
222 // the buttons
223 $add_button = $form->submit_html ("add-node","Add New Node");
224 $details->tr($add_button,"right");
225
226 $form->end();
227 $details->end();
228 $toggle->end();
229
230 // Print footer
231 include 'plc_footer.php';
232
233 ?>