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