dab93caa33ff3880027436cd7ab3517a3c37e1a0
[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 // Thomas Dreibholz suggested that utf8_decode could be non-needed
42 // and maybe even harmful on modern systems like f23
43 $sitename= htmlentities(utf8_decode($site['name']));
44 // $sitename= htmlentities($site['name']);
45 $abbreviated_name= htmlentities($site['abbreviated_name']);
46 $site_url= $site['url'];
47 $login_base= $site['login_base'];
48 $site_lat= $site['latitude'];
49 $site_long= $site['longitude'];
50 $max_slivers= $site['max_slivers'];
51 $max_slices= $site['max_slices'];
52
53 $enabled = $site['enabled'];
54 $ext_consortium_id = $site ['ext_consortium_id'];
55
56 // get peer 
57 $peer_id= $site['peer_id'];
58 $peers = new Peers ($api);
59 $local_peer = ! $peer_id;
60
61 // extra privileges to admins, and (pi||tech) on this site
62 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
63 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
64 $is_site_admin = ($local_peer && plc_is_admin());
65   
66 $address_ids= $site['address_ids'];
67 $pcu_ids= $site['pcu_ids'];
68 $node_ids= $site['node_ids'];
69 $person_ids= $site['person_ids'];
70 $slice_ids= $site['slice_ids'];
71
72 $api->begin();
73 // gets address info
74 $api->GetAddresses( $address_ids );
75
76 // gets node info
77 $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
78
79 // gets person info
80 $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
81
82 $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
83
84 ////////////////////
85 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
86 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
87 if ($display_pcus) 
88   $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
89
90 // get results
91 if ($display_pcus)
92   list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
93 else
94   list( $addresses, $nodes, $persons, $slices )= $api->commit();
95   
96 $techs = array();
97 $pis = array();
98 $disabled_persons = array();
99 if ($persons) foreach( $persons as $person ) {
100   $role_ids= $person['role_ids'];
101
102   if ( in_array( '20', $role_ids ))     $pis[] = $person;
103   if ( in_array( '40', $role_ids ))     $techs[] = $person;
104   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
105   
106 }
107
108 $has_disabled_persons = count ($disabled_persons) !=0;
109
110 // get number of slivers
111 $slivers_count=0;
112 if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_ids']);
113
114 ////////////////////////////////////////
115 drupal_set_title("Details for site " . $sitename);
116   
117 $tabs=array();
118
119 $tabs []= tab_mysite();
120
121 // available actions
122 if ( $is_site_admin)
123   $tabs['Expire slices'] = array('url'=>l_actions(),
124                                  'method'=>'POST',
125                                  'values'=>array('site_id'=>$site_id,
126                                                  'action'=>'expire-all-slices-in-site'),
127                                  'bubble'=>"Expire all slices and prevent creation of new slices",
128                                  'confirm'=>"Suspend all slices in $login_base");
129 if ( $is_site_admin)
130   $tabs['Delete']=array('url'=>l_actions(),
131                         'method'=>'POST',
132                         'values'=>array('site_id'=>$site_id,
133                                         'action'=>'delete-site'),
134                         'bubble'=>"Delete site $sitename",
135                         'confirm'=>"Are you sure you want to delete site $login_base");
136
137 if ( $is_site_pi ) 
138   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
139                               'method'=>'post',
140                               'bubble'=>'Create new slice in site');
141
142 if (plc_is_admin() || plc_in_site($site_id))
143   $tabs["Events"]=array_merge (tablook_event(),
144                                array('url'=>l_event("Site","site",$site_id),
145                                      'bubble'=>"Events for site $sitename"));
146 if (plc_is_admin() || plc_in_site($site_id))
147   $tabs["Comon"]=array_merge(tablook_comon(),
148                              array('url'=>l_comon("site_id",$site_id),
149                                    'bubble'=>"Comon page for $sitename"));
150
151
152 plekit_linetabs($tabs);
153
154 // show gray background on foreign objects : start a <div> with proper class
155 $peers->block_start ($peer_id);
156
157 // sanity checks
158 if ( $local_peer ) {
159   // pending site
160   global $PENDING_CONSORTIUM_ID;
161   if ( $ext_consortium_id === $PENDING_CONSORTIUM_ID) {
162     if ( ! $enabled ) 
163       plc_warning ("This site is under pending registration - Please visit " . 
164                    href (l_sites_pending(),"this page") . 
165                    " to review pending applications.");
166     else 
167       plc_warning ("This site is pending but is also enabled - something is wrong. You should fix the issue with plcsh");
168   } else {
169     if ( ! $enabled) 
170       plc_warning ("This site is disabled.");
171   }
172 }
173
174 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
175
176
177 $toggle = new PlekitToggle ('site',"Details",
178                             array('visible'=>get_arg('show_details'),
179                                   'bubble'=>'Display and modify details for that site'));
180 $toggle->start();
181
182 $details = new PlekitDetails($can_update);
183
184 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
185
186 $details->start();
187
188 if ( ! $site['is_public']) 
189   $details->tr(plc_warning_html("This site is not public!"));
190
191 $details->th_td("Full name",$sitename,'name',array('width'=>50));
192 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
193 $details->th_td("URL",$site_url,'url',array('width'=>40));
194 $details->th_td("Latitude",$site_lat,'latitude');
195 $details->th_td("Longitude",$site_long,'longitude');
196
197 // modifiable by admins only
198 if (plc_is_admin()) 
199   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
200 else
201   $details->th_td("Login base",$login_base);
202 if (plc_is_admin())
203   $details->th_td("Max slices",$max_slices,'max_slices');
204 else
205   $details->th_td("Max slices",$max_slices);
206 if (plc_is_admin())
207 {
208   $selectors=array(array('display'=>"False",'value'=>'0'), 
209                                    array('display'=>"True",'value'=>'1'));
210   $selectors[intval($enabled)]['selected'] = 'selected';
211
212   $enable_select = $f->select_html ("enabled", $selectors);
213
214   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
215 } else
216   $details->th_td("Enabled",$enabled);
217
218 $details->tr_submit("submit","Update Site");
219
220 if ( ! $local_peer) {
221   $details->space();
222   $details->th_td("Peer",$peers->peer_link($peer_id));
223  }
224 $details->end();
225 $details->form_end();
226 $toggle->end();
227
228 //////////////////// mode details - for local object
229 if ( $local_peer ) {
230
231   //////////////////// nodes
232   $nb_boot = 0;
233   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
234
235   $nodes_title = "Nodes : ";
236   $nodes_title .= count($nodes) . " total";
237   $nodes_title .= " / " . $nb_boot . " boot";
238   if ($nb_boot < 2 ) 
239     $nodes_title = plc_warning_html ($nodes_title);
240   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
241
242   $toggle=new PlekitToggle ('nodes',$nodes_title,
243                             array('visible'=>get_arg('show_nodes')));
244   $toggle->start();
245
246   $headers=array();
247   $sort_column = '0';
248   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
249   $headers['hostname']='string'; 
250   $headers['state']='string';
251
252   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
253                                                                 'notes_area'=>false,
254                                                                 'pagesize_area'=>false));
255   // hash pcus on pcu_id
256   if ($display_pcus) {
257     global $pcu_hash;
258     $pcu_hash= array();
259     if ($pcus) foreach ($pcus as $pcu) $pcu_hash[$pcu['pcu_id']]=$pcu;
260   }
261   // search the pcu, return the string to display and mark the pcu as displayed
262   //  function display_and_mark ($pcu_hash,$pcu_ids,$ports) {
263   function display_and_mark ($pcu_ids,$ports) {
264     global $pcu_hash;
265     if (empty($pcu_ids)) return plc_warning_html('None');
266     $pcu_id=$pcu_ids[0];
267     if (empty($ports)) return plc_error_html('???');
268     $port=$ports[0];
269     $pcu=$pcu_hash[$pcu_id];
270     $display= l_pcu_href($pcu_id, $pcu['hostname'] . ' : ' . $port);
271     $pcu_hash[$pcu_id]['displayed']=true;
272     return $display;
273   }
274
275   $table->start();
276   foreach ($nodes as $node) {
277     $table->row_start();
278     if ($display_pcus) {
279       //      $table->cell(display_and_mark($pcu_hash,$node['pcu_ids'],$node['ports']));
280       $table->cell(display_and_mark($node['pcu_ids'],$node['ports']));
281     }
282     $table->cell (l_node_obj($node));
283     $table->cell ($node['boot_state']);
284     $table->row_end();
285   }
286   // show undisplayed PCU's if any
287   if ($display_pcus) 
288     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
289         if (!$pcu['displayed']) {
290           $table->row_start();
291           $table->cell($pcu['hostname']); $table->cell(''); $table->cell('');
292           $table->row_end();
293         }
294       }
295     
296   $table->tfoot_start();
297   $table->row_start();
298   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
299   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
300   $table->row_end();
301   $table->end();
302   $toggle->end();
303     
304   //////////////////// Users
305   $persons_title = "Users : ";
306   $persons_title .= count($person_ids) . " total";
307   $persons_title .= " / " . count ($pis) . " PIs";
308   $persons_title .= " / " . count ($techs) . " Techs";
309   if ($has_disabled_persons) 
310     $persons_title .= " / " . count($disabled_persons) . " Disabled";
311   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
312     $persons_title = plc_warning_html ($persons_title);
313   $persons_title .= href(l_persons_site($site_id)," (See as users)");
314
315   $toggle=new PlekitToggle ('persons',$persons_title,
316                             array('visible'=>get_arg('show_persons')));
317   $toggle->start();
318
319   $headers = array ();
320   $headers["email"]='string';
321   $headers["S"]='int';
322   $headers["PI"]='string';
323   $headers['User']='string';
324   $headers["Tech"]='string';
325   if ($has_disabled_persons) $headers["Disabled"]='string';
326   $notes=array('S = slices');
327   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
328                                                             'notes'=>$notes,
329                                                             'pagesize_area'=>false));
330   $table->start();
331   if ($persons) foreach ($persons as $person) {
332     $table->row_start();
333     $table->cell(l_person_obj($person));
334     $table->cell(count($person['slice_ids']));
335     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
336     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
337     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
338     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
339     $table->row_end();
340   }
341   $table->end();
342   $toggle->end();
343
344   //////////////////// Slices
345   $slices_title="Slices : ";
346   $slices_title .= $max_slices . " max";
347   $slices_title .= " / " . count($slice_ids) . " running";
348   $slices_title .= " / $slivers_count slivers";
349   if (count($slice_ids) >= $max_slices) 
350     $slices_title = plc_warning_html($slices_title);
351   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
352   
353   $toggle=new PlekitToggle ('slices',$slices_title,
354                             array('visible'=>get_arg('show_slices')));
355   $toggle->start();
356
357   $headers = array ();
358   $headers ['name']='string';
359   $headers ['I'] = 'string';
360   $headers ['N']='int';
361   $headers ['U']='int';
362   $notes=array('I = instantiation type',
363                'N = number of nodes',
364                'U = number of users');
365   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
366                                                     'pagesize_area'=>false,
367                                                     'notes'=>$notes));
368
369   $table->start();
370   if ($slices) foreach ($slices as $slice) {
371       $table->row_start();
372       $table->cell(l_slice_obj($slice));
373       $table->cell(instantiation_label($slice));
374       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
375       $table->cell (count($slice['person_ids']));
376       $table->row_end();
377     }
378   if ($is_site_pi) {
379     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
380     $table->tfoot_start();
381     $table->row_start();
382     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
383   }
384     
385   $table->end();
386   $toggle->end();
387
388   $form=new PlekitForm (l_actions(), array('site_id'=>$site_id));
389   $form->start();
390   //////////////////////////////////////////////////////////// Tags
391   // tags section
392   // already inside a if ( $local_peer )...
393   
394   $tags=$api->GetSiteTags (array('site_id'=>$site_id));
395   function get_tagname ($tag) { return $tag['tagname'];}
396   // xxx looks like tech-only see an error here, 
397   // might be that GetSiteTags is not accessible or something
398   $tagnames = array_map ("get_tagname",$tags);
399   
400   $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
401                               array('bubble'=>'Inspect and set tags on that site',
402                                     'visible'=>get_arg('show_tags')));
403   $toggle->start();
404
405   $headers=array("Name"=>"string",
406                  "Value"=>"string",
407                  );
408   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
409   
410   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
411   $table=new PlekitTable("site_tags",$headers,0,$table_options);
412   $table->start();
413   if ($tags) foreach ($tags as $tag) {
414       $table->row_start();
415       $table->cell(l_tag_obj($tag));
416       $table->cell($tag['value']);
417       // the remove checkbox
418       if (plc_is_admin()) $table->cell ($form->checkbox_html('site_tag_ids[]',$tag['site_tag_id']));
419       $table->row_end();
420     }
421   
422   if ($is_site_pi || $is_site_admin) {
423     $table->tfoot_start();
424
425     // remove tag 
426     $table->row_start();
427     $table->cell($form->submit_html("delete-site-tags","Remove Tags"),
428                  // use the whole columns and right adjust
429                  array('hfill'=>true,'align'=>'right'));
430     $table->row_end();
431
432     // set tag area
433     $table->row_start();
434     // get list of tag names in the site/* category    
435     $all_tags= $api->GetTagTypes( array ("category"=>"site*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
436     // xxx cannot use onchange=submit() - would need to somehow pass action name 
437     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
438     $selector=array_map("tag_selector",$all_tags);
439     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
440     $table->cell($form->text_html("value","",array('width'=>8)));
441     $table->cell($form->submit_html("set-tag-on-site","Set Tag"),array('columns'=>2,'align'=>'left'));
442     $table->row_end();
443   }
444   
445   $table->end();
446   $toggle->end();
447   $form->end();
448
449   //////////////////// Addresses
450   $toggle=new PlekitToggle ('addresses',"Addresses",
451                             array('visible'=>get_arg('show_addresses')));
452   $toggle->start();
453   if ( ! $addresses) {
454     print "<p class='addresses'>No known address for this site</p>";
455   } else {
456     $details=new PlekitDetails (false);
457     $details->start();
458     $details->th_td("Addresses","");
459     foreach ($addresses as $address) {
460       $details->th_td(plc_vertical_table($address['address_types']),
461                        plc_vertical_table(array($address['line1'],
462                                                 $address['line2'],
463                                                 $address['line3'],
464                                                 $address['city'],
465                                                 $address['state'],
466                                                 $address['postalcode'],
467                                                 $address['country'])));
468     }
469     $details->end();
470   }
471   $toggle->end();
472
473  }
474
475 ////////////////////////////////////////
476 $peers->block_end($peer_id);
477
478 //plekit_linetabs ($tabs,"bottom");
479
480 // Print footer
481 include 'plc_footer.php';
482
483 ?>