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