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