several fixes
[plewww.git] / planetlab / sites / site.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 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_peers.php';
19 require_once 'linetabs.php';
20 require_once 'table.php';
21 require_once 'details.php';
22 require_once 'form.php';
23 require_once 'toggle.php';
24
25 // -------------------- 
26 // recognized URL arguments
27 $site_id=intval($_GET['id']);
28 if ( ! $site_id ) { plc_error('Malformed URL - id not set'); return; }
29
30 ////////////////////
31 // Get all columns as we focus on only one entry
32 $sites = $api->GetSites( array($site_id));
33
34 if (empty($sites)) {
35   drupal_set_message ("Site " . $site_id . " not found");
36   return;
37  }
38
39 $site=$sites[0];
40 // var names to api return
41 $sitename= htmlentities($site['name']);
42 $abbreviated_name= htmlentities($site['abbreviated_name']);
43 $site_url= $site['url'];
44 $login_base= $site['login_base'];
45 $site_lat= $site['latitude'];
46 $site_long= $site['longitude'];
47 $max_slivers= $site['max_slivers'];
48 $max_slices= $site['max_slices'];
49
50 $enabled = $site['enabled'];
51
52 // get peer 
53 $peer_id= $site['peer_id'];
54 $peers = new Peers ($api);
55 $local_peer = ! $peer_id;
56
57 // extra privileges to admins, and (pi||tech) on this site
58 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
59 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
60 $is_site_admin = ($local_peer && plc_is_admin());
61   
62 $address_ids= $site['address_ids'];
63 $pcu_ids= $site['pcu_ids'];
64 $node_ids= $site['node_ids'];
65 $person_ids= $site['person_ids'];
66 $slice_ids= $site['slice_ids'];
67
68 $api->begin();
69 // gets address info
70 $api->GetAddresses( $address_ids );
71
72 // gets node info
73 $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
74
75 // gets person info
76 $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
77
78 $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
79
80 ////////////////////
81 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
82 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
83 if ($display_pcus) 
84   $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
85
86 // get results
87 if ($display_pcus)
88   list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
89 else
90   list( $addresses, $nodes, $persons, $slices )= $api->commit();
91   
92 $techs = array();
93 $pis = array();
94 $disabled_persons = array();
95 if ($persons) foreach( $persons as $person ) {
96   $role_ids= $person['role_ids'];
97
98   if ( in_array( '20', $role_ids ))     $pis[] = $person;
99   if ( in_array( '40', $role_ids ))     $techs[] = $person;
100   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
101   
102 }
103
104 $has_disabled_persons = count ($disabled_persons) !=0;
105
106 // get number of slivers
107 $slivers_count=0;
108 if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_ids']);
109
110 ////////////////////////////////////////
111 drupal_set_title("Details for site " . $sitename);
112   
113 $tabs=array();
114
115 $tabs []= tab_mysite();
116
117 // available actions
118 if ( $is_site_admin)
119   $tabs['Expire slices'] = array('url'=>l_actions(),
120                                  'method'=>'POST',
121                                  'values'=>array('site_id'=>$site_id,
122                                                  'action'=>'expire-all-slices-in-site'),
123                                  'bubble'=>"Expire all slices and prevent creation of new slices",
124                                  'confirm'=>"Suspend all slices in $login_base");
125 if ( $is_site_admin)
126   $tabs['Delete']=array('url'=>l_actions(),
127                         'method'=>'POST',
128                         'values'=>array('site_id'=>$site_id,
129                                         'action'=>'delete-site'),
130                         'bubble'=>"Delete site $sitename",
131                         'confirm'=>"Are you sure you want to delete site $login_base");
132
133 if ( $is_site_pi ) 
134   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
135                               'method'=>'post',
136                               'bubble'=>'Create new slice in site');
137
138 if (plc_is_admin() || plc_in_site($site_id))
139   $tabs["Events"]=array_merge (tablook_event(),
140                                array('url'=>l_event("Site","site",$site_id),
141                                      'bubble'=>"Events for site $sitename"));
142 if (plc_is_admin() || plc_in_site($site_id))
143   $tabs["Comon"]=array_merge(tablook_comon(),
144                              array('url'=>l_comon("site_id",$site_id),
145                                    'bubble'=>"Comon page for $sitename"));
146
147
148 plekit_linetabs($tabs);
149
150 // show gray background on foreign objects : start a <div> with proper class
151 $peers->block_start ($peer_id);
152
153 if ( $local_peer && ( ! $enabled ) )
154   plc_warning ("This site is not enabled - Please visit " . 
155                href (l_sites_pending(),"this page") . 
156                " to review pending applications.");
157
158 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
159
160
161 $toggle = new PlekitToggle ('site',"Details",
162                             array('visible'=>get_arg('show_details',true),
163                                   'bubble'=>'Display and modify details for that site'));
164 $toggle->start();
165
166 $details = new PlekitDetails($can_update);
167
168 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
169
170 $details->start();
171
172 if ( ! $site['is_public']) 
173   $details->tr(plc_warning_html("This site is not public!"));
174
175 $details->th_td("Full name",$sitename,'name',array('width'=>50));
176 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
177 $details->th_td("URL",$site_url,'url',array('width'=>40));
178 $details->th_td("Latitude",$site_lat,'latitude');
179 $details->th_td("Longitude",$site_long,'longitude');
180
181 // modifiable by admins only
182 if (plc_is_admin()) 
183   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
184 else
185   $details->th_td("Login base",$login_base);
186 if (plc_is_admin())
187   $details->th_td("Max slices",$max_slices,'max_slices');
188 else
189   $details->th_td("Max slices",$max_slices);
190 if (plc_is_admin())
191 {
192   $selectors=array(array('display'=>"False",'value'=>'0'), 
193                                    array('display'=>"True",'value'=>'1'));
194   $selectors[intval($enabled)]['selected'] = 'selected';
195
196   $enable_select = $f->select_html ("enabled", $selectors);
197
198   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
199 } else
200   $details->th_td("Enabled",$enabled);
201
202 $details->tr_submit("submit","Update Site");
203
204 if ( ! $local_peer) {
205   $details->space();
206   $details->th_td("Peer",$peers->peer_link($peer_id));
207  }
208 $details->end();
209 $details->form_end();
210 $toggle->end();
211
212 //////////////////// mode details - for local object
213 if ( $local_peer ) {
214
215   //////////////////// nodes
216   $nb_boot = 0;
217   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
218
219   $nodes_title = "Nodes : ";
220   $nodes_title .= count($nodes) . " total";
221   $nodes_title .= " / " . $nb_boot . " boot";
222   if ($nb_boot < 2 ) 
223     $nodes_title = plc_warning_html ($nodes_title);
224   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
225
226   $toggle=new PlekitToggle ('nodes',$nodes_title,
227                             array('visible'=>get_arg('show_nodes',true)));
228   $toggle->start();
229
230   $headers=array();
231   $sort_column = '0';
232   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
233   $headers['hostname']='string'; 
234   $headers['state']='string';
235
236   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
237                                                                 'notes_area'=>false,
238                                                                 'pagesize_area'=>false));
239   // hash pcus on pcu_id
240   if ($display_pcus) {
241     global $pcu_hash;
242     $pcu_hash= array();
243     if ($pcus) foreach ($pcus as $pcu) $pcu_hash[$pcu['pcu_id']]=$pcu;
244   }
245   // search the pcu, return the string to display and mark the pcu as displayed
246   //  function display_and_mark ($pcu_hash,$pcu_ids,$ports) {
247   function display_and_mark ($pcu_ids,$ports) {
248     global $pcu_hash;
249     if (empty($pcu_ids)) return plc_warning_html('None');
250     $pcu_id=$pcu_ids[0];
251     if (empty($ports)) return plc_error_html('???');
252     $port=$ports[0];
253     $pcu=$pcu_hash[$pcu_id];
254     $display= $pcu['hostname'] . ' : ' . $port;
255     $pcu_hash[$pcu_id]['displayed']=true;
256     return $display;
257   }
258
259   $table->start();
260   foreach ($nodes as $node) {
261     $table->row_start();
262     if ($display_pcus) {
263       //      $table->cell(display_and_mark($pcu_hash,$node['pcu_ids'],$node['ports']));
264       $table->cell(display_and_mark($node['pcu_ids'],$node['ports']));
265     }
266     $table->cell (l_node_obj($node));
267     $table->cell ($node['boot_state']);
268     $table->row_end();
269   }
270   // show undisplayed PCU's if any
271   if ($display_pcus) 
272     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
273         if (!$pcu['displayed']) {
274           $table->row_start();
275           $table->cell($pcu['hostname']); $table->cell(''); $table->cell('');
276           $table->row_end();
277         }
278       }
279     
280   $table->tfoot_start();
281   $table->row_start();
282   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
283   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
284   $table->row_end();
285   $table->end();
286   $toggle->end();
287     
288   //////////////////// Users
289   $persons_title = "Users : ";
290   $persons_title .= count($person_ids) . " total";
291   $persons_title .= " / " . count ($pis) . " PIs";
292   $persons_title .= " / " . count ($techs) . " Techs";
293   if ($has_disabled_persons) 
294     $persons_title .= " / " . count($disabled_persons) . " Disabled";
295   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
296     $persons_title = plc_warning_html ($persons_title);
297   $persons_title .= href(l_persons_site($site_id)," (See as users)");
298
299   $toggle=new PlekitToggle ('persons',$persons_title,
300                             array('visible'=>get_arg('show_persons',false)));
301   $toggle->start();
302
303   $headers = array ();
304   $headers["email"]='string';
305   $headers["S"]='int';
306   $headers["PI"]='string';
307   $headers['User']='string';
308   $headers["Tech"]='string';
309   if ($has_disabled_persons) $headers["Disabled"]='string';
310   $notes=array('S = slices');
311   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
312                                                             'notes'=>$notes,
313                                                             'pagesize_area'=>false));
314   $table->start();
315   if ($persons) foreach ($persons as $person) {
316     $table->row_start();
317     $table->cell(l_person_obj($person));
318     $table->cell(count($person['slice_ids']));
319     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
320     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
321     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
322     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
323     $table->row_end();
324   }
325   $table->end();
326   $toggle->end();
327
328   //////////////////// Slices
329   $slices_title="Slices : ";
330   $slices_title .= $max_slices . " max";
331   $slices_title .= " / " . count($slice_ids) . " running";
332   $slices_title .= " / $slivers_count slivers";
333   if (count($slice_ids) >= $max_slices) 
334     $slices_title = plc_warning_html($slices_title);
335   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
336   
337   $toggle=new PlekitToggle ('slices',$slices_title,
338                             array('visible'=>get_arg('show_slices',false)));
339   $toggle->start();
340
341   $headers = array ();
342   $headers ['name']='string';
343   $headers ['I'] = 'string';
344   $headers ['N']='int';
345   $headers ['U']='int';
346   $notes=array('I = instantiation type',
347                'N = number of nodes',
348                'U = number of users');
349   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
350                                                     'pagesize_area'=>false,
351                                                     'notes'=>$notes));
352
353   $table->start();
354   if ($slices) foreach ($slices as $slice) {
355       $table->row_start();
356       $table->cell(l_slice_obj($slice));
357       $table->cell(instantiation_label($slice));
358       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
359       $table->cell (count($slice['person_ids']));
360       $table->row_end();
361     }
362   if ($is_site_pi) {
363     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
364     $table->tfoot_start();
365     $table->row_start();
366     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
367   }
368     
369   $table->end();
370   $toggle->end();
371
372   //////////////////// Addresses
373   $toggle=new PlekitToggle ('addresses',"Addresses",
374                             array('visible'=>get_arg('show_addresses',false)));
375   $toggle->start();
376   if ( ! $addresses) {
377     print "<p class='addresses'>No known address for this site</p>";
378   } else {
379     $details=new PlekitDetails (false);
380     $details->start();
381     $details->th_td("Addresses","");
382     foreach ($addresses as $address) {
383       $details->th_td(plc_vertical_table($address['address_types']),
384                        plc_vertical_table(array($address['line1'],
385                                                 $address['line2'],
386                                                 $address['line3'],
387                                                 $address['city'],
388                                                 $address['state'],
389                                                 $address['postalcode'],
390                                                 $address['country'])));
391     }
392     $details->end();
393   }
394   $toggle->end();
395
396  }
397
398 ////////////////////////////////////////
399 $peers->block_end($peer_id);
400
401 //plekit_linetabs ($tabs,"bottom");
402
403 // Print footer
404 include 'plc_footer.php';
405
406 ?>