add a non-primary interface : user to select for virtual or physical interface (set...
[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() || plc_is_pi() || plc_is_tech();
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   $hostname = trim($_POST['hostname']);
37   $model= trim($_POST['model']);
38   $ip = trim($_POST['ip']);
39   $method = trim($_POST['method']);
40   $netmask = trim($_POST['netmask']);
41   $network = trim($_POST['network']);
42   $broadcast = trim($_POST['broadcast']);
43   $gateway = trim($_POST['gateway']);
44   $dns1 = trim($_POST['dns1']);
45   $dns2 = trim($_POST['dns2']);
46   $bwlimit = trim($_POST['bwlimit']);
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 match the netmask";
67     }
68   }
69   
70   if( $hostname == "" ) {
71     $errors[] = "Hostname is required";
72   }
73   if( $ip == "" ) {
74     $errors[] = "IP is required";
75   } else if ( ! is_valid_ip ($ip)) {
76     $errors []= "Invalid IP $ip";
77   }
78   
79   if( !empty($errors) ) {
80     drupal_set_error(plc_itemize($errors));
81   } else {
82     // add new node and its interface
83     $node_fields= array( "hostname"=>$hostname, "model"=>$model );
84     $site_id= plc_my_site_id();
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 $sites=$api->GetSites(array(plc_my_site_id()));
141 $site=$sites[0];
142 $sitename=$site['name'];
143                        
144 drupal_set_title('Add a new node in site "' . $sitename . '"');
145
146 // defaults
147 $method = $_POST['method'];
148 if( ! $method ) $method= "static";
149
150 $model = $_POST['model'];
151 if( ! $model ) $model= "Custom";
152
153 print <<< EOF
154 <p class='node_add'>
155 This page lets you declare a new machine in your site. 
156 <br/>
157 This must be done before the machine is turned on, 
158 as it will allow you to download a boot image when complete for this node.
159 <br/>
160   It is now reserved to admins, as regular users are supposed to use the register wizard, that among other things enforces PCU deployment.
161 <br/>
162 An IP address is required even if you use DHCP.
163 </p>
164 EOF;
165
166 $toggle = new PlekitToggle ('add-node',"Add Node",
167                             array('bubble'=>'Add a node - does not enforce PCU - for admins only !',
168                                   'visible'=>get_arg('show_details',true)));
169 $toggle->start();
170
171 $details=new PlekitDetails($has_privileges);
172
173 // xxx hardwire network type for now
174 $form_variables = array('type'=>"ipv4");
175 //$form=$details->form_start(l_actions(),$form_variables);
176 $form=$details->form_start('/db/nodes/node_add.php',$form_variables,
177                            array('onSubmit'=>'return interfaceSubmit()'));
178
179 $details->start();
180
181 $details->th_td("Hostname",$hostname,"hostname");
182 $details->th_td("Model",$model,"model");
183 $method_select = $form->select_html ("method",
184                                      interface_method_selectors($api,$method,true),
185                                      array('id'=>'method',
186                                            'onChange'=>'updateMethodFields()'));
187 $details->th_td("Method",$method_select,"method",
188                 array('input_type'=>'select','value'=>$interface['method']));
189
190 // dont display the 'type' selector as it contains only ipv4
191 //>>> GetNetworkTypes()
192 //[u'ipv4']
193
194 $details->th_td("IP address",$ip,"ip",array('width'=>15,
195                                             'onKeyup'=>'networkHelper()',
196                                             'onChange'=>'networkHelper()'));
197 $details->th_td("Netmask",$netmask,"netmask",array('width'=>15,
198                                                    'onKeyup'=>'networkHelper()',
199                                                    'onChange'=>'networkHelper()'));
200 $details->th_td("Network",$network,"network",array('width'=>15));
201 $details->th_td("Broadcast",$broadcast,"broadcast",array('width'=>15));
202 $details->th_td("Gateway",$gateway,"gateway",array('width'=>15,
203                                                    'onChange'=>'subnetChecker("gateway",false)'));
204 $details->th_td("DNS 1",$dns1,"dns1",array('width'=>15,
205                                            'onChange'=>'subnetChecker("dns1",false)'));
206 $details->th_td("DNS 2",$dns2,"dns2",array('width'=>15,
207                                            'onChange'=>'subnetChecker("dns2",true)'));
208 $details->space();
209 $details->th_td("BW limit (bps)",$bwlimit,"bwlimit",array('width'=>11));
210
211 // the buttons
212 $add_button = $form->submit_html ("add-node","Add New Node");
213 $details->tr($add_button,"right");
214
215 $form->end();
216 $details->end();
217 $toggle->end();
218
219 // Print footer
220 include 'plc_footer.php';
221
222 ?>