show extra tags in the nodes page as well
[plewww.git] / planetlab / slices / slice.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 'plc_visibletags.php';
20 require_once 'linetabs.php';
21 require_once 'table.php';
22 require_once 'details.php';
23 require_once 'toggle.php';
24 require_once 'form.php';
25
26 // keep css separate for now
27 drupal_set_html_head('
28 <link href="/planetlab/css/my_slice.css" rel="stylesheet" type="text/css" />
29 ');
30
31 // -------------------- admins potentially need to get full list of users
32 ini_set('memory_limit','32M');
33
34 // -------------------- 
35 // recognized URL arguments
36 $slice_id=intval($_GET['id']);
37 if ( ! $slice_id ) { plc_error('Malformed URL - id not set'); return; }
38
39 ////////////////////
40 // Get all columns as we focus on only one entry
41 $slices= $api->GetSlices( array($slice_id));
42
43 if (empty($slices)) {
44   drupal_set_message ("Slice " . $slice_id . " not found");
45   return;
46  }
47
48 $slice=$slices[0];
49
50 // pull all node info to vars
51 $name= $slice['name'];
52 $expires = date( "d/m/Y", $slice['expires'] );
53 $site_id= $slice['site_id'];
54
55 $person_ids=$slice['person_ids'];
56
57 // get peers
58 $peer_id= $slice['peer_id'];
59 $peers=new Peers ($api);
60 $local_peer = ! $peer_id;
61
62 // gets site info
63 $sites= $api->GetSites( array( $site_id ) );
64 $site=$sites[0];
65 $site_name= $site['name'];
66 $max_slices = $site['max_slices'];
67
68 //////////////////////////////////////// building blocks for the renew area
69 // Constants
70 global $DAY;            $DAY = 24*60*60;
71 global $WEEK;           $WEEK = 7 * $DAY; 
72 global $MAX_WEEKS;      $MAX_WEEKS= 8;          // weeks from today
73 global $GRACE_DAYS;     $GRACE_DAYS=10;         // days for renewal promoted on top
74 global $NOW;            $NOW=mktime();
75
76 ////////////////////////////////////////////////////////////
77 // make the renew area on top and open if the expiration time is less than 10 days from now
78 function renew_needed ($slice) {
79   global $DAY, $NOW, $GRACE_DAYS;
80   $current_exp=$slice['expires'];
81
82   $time_left = $current_exp - $NOW;
83   $visible = $time_left/$DAY <= $GRACE_DAYS;
84   return $visible;
85 }
86
87 function renew_area ($slice,$site,$visible) {
88   global $DAY, $WEEK, $MAX_WEEKS, $GRACE_DAYS, $NOW;
89  
90   $current_exp=$slice['expires'];
91   $current_text = gmstrftime("%A %b-%d-%y %T %Z", $current_exp);
92   $max_exp= $NOW + ($MAX_WEEKS * $WEEK); // seconds since epoch
93   $max_text = gmstrftime("%A %b-%d-%y %T %Z", $max_exp);
94
95   // xxx some extra code needed to enable this area only if the slice description is OK:
96   // description and url must be non void
97   $toggle=
98     new PlekitToggle('renew',"Expires $current_text - Renew this slice",
99                      array("bubble"=>
100                            "Enter this zone if you wish to renew your slice",
101                            'visible'=>$visible));
102   $toggle->start();
103
104   // xxx message could take roles into account
105   if ($site['max_slices']<=0) {
106      $message= <<< EOF
107 <p class='my-slice-renewal'>Slice creation and renewal have been temporarily disabled for your
108 <site. This may have occurred because your site's nodes have been down
109 or unreachable for several weeks, and multiple attempts to contact
110 your site's PI(s) and Technical Contact(s) have all failed. If so,
111 contact your site's PI(s) and Technical Contact(s) and ask them to
112 bring up your site's nodes. Please visit your <a
113 href='/db/sites/index.php?id=$site_id'>site details</a> page to find
114 out more about your site's nodes, and how to contact your site's PI(s)
115 and Technical Contact(s).</p>
116 EOF;
117      echo $message;
118  
119   } else {
120     // xxx this is a rough cut and paste from the former UI
121     // showing a datepicker view could be considered as well with some extra work
122     // calculate possible extension lengths
123     $selectors = array();
124     foreach ( array ( 1 => "One more week", 
125                       2 => "Two more weeks", 
126                       3 => "Three more weeks", 
127                       4 => "One more month" ) as $weeks => $text ) {
128       $candidate_exp = $current_exp + $weeks*$WEEK;
129       if ( $candidate_exp < $max_exp) {
130         $selectors []= array('display'=>"$text (" . gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp) . ")",
131                              'value'=>$candidate_exp);
132         $max_renewal_weeks=$weeks;
133         $max_renewal_date= gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp);
134       }
135     }
136
137     if ( empty( $selectors ) ) {
138       print <<< EOF
139 <div class='my-slice-renewal'>
140 Slices annot be renewed more than $MAX_WEEKS weeks from now, i.e. not beyond $max_text. 
141 For this reason, the current slice cannot be renewed any further into the future, try again closer to expiration date.
142 </div>
143 EOF;
144      } else {
145       print <<< EOF
146 <div class='my-slice-renewal'>
147 <p>You must provide a short description as well as a link to a project website before renewing it.
148 Do <span class='bold'>not</span> provide bogus information; if a complaint is lodged against your slice 
149 and PlanetLab Operations is unable to determine what the normal behavior of your slice is, 
150 your slice may be deleted to resolve the complaint.</p>
151 <p><span class='bold'>NOTE:</span> 
152 Slices cannot be renewed beyond another $max_renewal_weeks week(s) ($max_renewal_date).
153 </p>
154 </div>
155 EOF;
156
157       $form = new PlekitForm (l_actions(),
158                               array('action'=>'renew-slice',
159                                     'slice_id'=>$slice['slice_id']));
160       $form->start();
161       print $form->label_html('expires','Duration');
162       print $form->select_html('expires',$selectors,array('label'=>'Pick one'));
163       print $form->submit_html('renew-button','Renew');
164       $form->end();
165     }
166   }
167  
168   $toggle->end();
169 }
170
171 ////////////////////////////////////////////////////////////
172
173 $am_in_slice = in_array(plc_my_person_id(),$person_ids);
174
175 if ($am_in_slice) {
176   drupal_set_title("My slice " . $name);
177  } else {
178   drupal_set_title("Slice " . $name);
179 }
180
181 $privileges = ( $local_peer && (plc_is_admin()  || plc_is_pi() || $am_in_slice));
182 $tags_privileges = $privileges || plc_is_admin();
183
184 $tabs=array();
185 $tabs [] = tab_nodes_slice($slice_id);
186 $tabs [] = tab_site($site_id);
187
188 // are these the right privileges for deletion ?
189 if ($privileges) {
190   $tabs ['Delete']= array('url'=>l_actions(),
191                           'method'=>'post',
192                           'values'=>array('action'=>'delete-slice','slice_id'=>$slice_id),
193                           'bubble'=>"Delete slice $name",
194                           'confirm'=>"Are you sure to delete slice $name");
195
196   $tabs["Events"]=array_merge(tablook_event(),
197                               array('url'=>l_event("Slice","slice",$slice_id),
198                                     'bubble'=>"Events for slice $name"));
199   $tabs["Comon"]=array_merge(tablook_comon(),
200                              array('url'=>l_comon("slice_id",$slice_id),
201                                    'bubble'=>"Comon page about slice $name"));
202 }
203
204 plekit_linetabs($tabs);
205
206 ////////////////////////////////////////
207 $peers->block_start($peer_id);
208
209 //////////////////////////////////////// renewal area 
210 // (1) close to expiration : show on top and open
211
212 if ($local_peer ) {
213   $renew_visible = renew_needed ($slice);
214   if ($renew_visible) renew_area ($slice,$site,true);
215  }
216
217
218 //////////////////// details
219 // default for opening the details section or not ?
220 if ($local_peer) {
221   $default_show_details = true;
222  } else {
223   $default_show_details = ! $renew_visible;
224  }
225   
226 $toggle = 
227   new PlekitToggle ('my-slice-details',"Details",
228                     array('bubble'=>
229                           'Display and modify details for that slice',
230                           'visible'=>get_arg('show_details',$default_show_details)));
231 $toggle->start();
232
233 $details=new PlekitDetails($privileges);
234 $details->form_start(l_actions(),array('action'=>'update-slice',
235                                        'slice_id'=>$slice_id,
236                                        'name'=>$name));
237
238 $details->start();
239 if (! $local_peer) {
240   $details->th_td("Peer",$peers->peer_link($peer_id));
241   $details->space();
242  }
243
244
245 $details->th_td('Name',$slice['name']);
246 $details->th_td('Description',$slice['description'],'description',
247                 array('input_type'=>'textarea',
248                       'width'=>50,'height'=>5));
249 $details->th_td('URL',$slice['url'],'url',array('width'=>50));
250 $details->tr_submit("submit","Update Slice");
251 $details->th_td('Expires',$expires);
252 $details->th_td('Instantiation',$slice['instantiation']);
253 $details->th_td('Site',l_site_obj($site));
254 // xxx show the PIs here
255 //$details->th_td('PIs',...);
256 $details->end();
257
258 $details->form_end();
259 $toggle->end();
260
261 //////////////////// persons
262 $person_columns = array('email','person_id','first_name','last_name','roles');
263 // get persons in slice
264 if (!empty($person_ids))
265   $persons=$api->GetPersons(array('person_id'=>$slice['person_ids']),$person_columns);
266 // just propose to add everyone else
267 // xxx this is maybe too much for admins as it slows stuff down 
268 // as regular persons can see only a fraction of the db anyway
269 $potential_persons=
270   $api->GetPersons(array('~person_id'=>$slice['person_ids'],
271                          'peer_id'=>NULL,
272                          'enabled'=>true),
273                    $person_columns);
274 $count=count($persons);
275
276 $toggle=
277   new PlekitToggle ('my-slice-persons',"$count Users",
278                     array('bubble'=>
279                           'Manage accounts attached to this slice',
280                           'visible'=>get_arg('show_persons',false)));
281 $toggle->start();
282
283 ////////// people currently in
284 // visible:
285 // hide if both current+add are included
286 // so user can chose which section is of interest
287 // show otherwise
288 $toggle_persons = new PlekitToggle ('my-slice-persons-current',
289                                     "$count people currently in $name",
290                                     array('visible'=>get_arg('show_persons_current',!$privileges)));
291 $toggle_persons->start();
292
293 $headers=array();
294 $headers['email']='string';
295 $headers['first']='string';
296 $headers['last']='string';
297 $headers['R']='string';
298 if ($privileges) $headers[plc_delete_icon()]="none";
299 $table=new PlekitTable('persons',$headers,'0',
300                        array('notes_area'=>false));
301 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
302 $form->start();
303 $table->start();
304 if ($persons) foreach ($persons as $person) {
305   $table->row_start();
306   $table->cell(l_person_obj($person));
307   $table->cell($person['first_name']);
308   $table->cell($person['last_name']);
309   $table->cell(plc_vertical_table ($person['roles']));
310   if ($privileges) $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
311   $table->row_end();
312 }
313 // actions area
314 if ($privileges) {
315
316   // remove persons
317   $table->tfoot_start();
318
319   $table->row_start();
320   $table->cell($form->submit_html ("remove-persons-from-slice","Remove selected"),
321                array('hfill'=>true,'align'=>'right'));
322   $table->row_end();
323  }
324 $table->end();
325 $toggle_persons->end();
326
327 ////////// people to add
328 if ($privileges) {
329   $count=count($potential_persons);
330   $toggle_persons = new PlekitToggle ('my-slice-persons-add',
331                                       "$count people may be added to $name",
332                                       array('visible'=>get_arg('show_persons_add',false)));
333   $toggle_persons->start();
334   if ( ! $potential_persons ) {
335     // xxx improve style
336     echo "<p class='not-relevant'>No person to add</p>";
337   } else {
338     $headers=array();
339     $headers['email']='string';
340     $headers['first']='string';
341     $headers['last']='string';
342     $headers['R']='string';
343     $headers['+']="none";
344     $options = array('notes_area'=>false,
345                      'search_width'=>15,
346                      'pagesize'=>8);
347     // show search for admins only as other people won't get that many names to add
348     if ( ! plc_is_admin() ) $options['search_area']=false;
349     
350     $table=new PlekitTable('add_persons',$headers,'0',$options);
351     $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
352     $form->start();
353     $table->start();
354     if ($potential_persons) foreach ($potential_persons as $person) {
355         $table->row_start();
356         $table->cell(l_person_obj($person));
357         $table->cell($person['first_name']);
358         $table->cell($person['last_name']);
359         $table->cell(plc_vertical_table ($person['roles']));
360         $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
361         $table->row_end();
362       }
363     // add users
364     $table->tfoot_start();
365     $table->row_start();
366     $table->cell($form->submit_html ("add-persons-in-slice","Add selected"),
367                  array('hfill'=>true,'align'=>'right'));
368     $table->row_end();
369     $table->end();
370     $form->end();
371   }
372   $toggle_persons->end();
373 }
374 $toggle->end();
375
376 //////////////////////////////////////////////////////////// Nodes
377 // the nodes details to display here
378 // (1) we search for the tag types for which 'category' matches 'node*/ui*'
379 // all these tags will then be tentatively displayed in this area
380 // (2) further information can also be optionally specified in the category:
381 //     (.) we split the category with '/' and search for assignments of the form var=value
382 //     (.) header can be set to supersede the column header (default is tagname)
383 //     (.) rank can be used for ordering the columns (default is tagname)
384 //     (.) type is passed to the javascript table, for sorting (default is 'string')
385
386 // minimal list as a start
387 $node_fixed_columns = array('hostname','node_id','peer_id','slice_ids_whitelist','run_level','boot_state');
388 // create a VisibleTags object : basically the list of tag columns to show
389 $visibletags = new VisibleTags ($api, 'node');
390 $visiblecolumns = $visibletags->column_names();
391 $node_columns=array_merge($node_fixed_columns,$visiblecolumns);
392 $nodes=$api->GetNodes(array('node_id'=>$slice['node_ids']),$node_columns);
393 $potential_nodes=$api->GetNodes(array('~node_id'=>$slice['node_ids']),$node_columns);
394 $count=count($nodes);
395
396 $toggle=new PlekitToggle ('my-slice-nodes',"$count Nodes",
397                           array('bubble'=>
398                                 'Manage nodes attached to this slice',
399                                 'visible'=>get_arg('show_nodes',false)));
400 $toggle->start();
401
402 ////////// nodes currently in
403 $count=count($nodes);
404 $toggle_nodes=new PlekitToggle('my-slice-nodes-current',
405                                "$count nodes currently in $name",
406                                array('visible'=>get_arg('show_nodes_current',!$privileges)));
407 $toggle_nodes->start();
408
409 $headers=array();
410 $notes=array();
411 $headers['peer']='string';
412 $headers['hostname']='string';
413 $short="ST"; $long="Last known status"; $type='string'; 
414         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
415 // the extra tags
416 $headers=array_merge($headers,$visibletags->headers());
417 $notes=array_merge($notes,$visibletags->notes());
418
419 if ($privileges) $headers[plc_delete_icon()]="none";
420
421 $table_options = array('notes'=>$notes,
422                        'search_width'=>15,
423                        'pagesize'=>20);
424 $table=new PlekitTable('nodes',$headers,'1',$table_options);
425
426 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
427 $form->start();
428 $table->start();
429 if ($nodes) foreach ($nodes as $node) {
430   $table->row_start();
431   $peers->cell($table,$node['peer_id']);
432   $table->cell(l_node_obj($node));
433   $run_level=$node['run_level'];
434   if ( empty($run_level)) $run_level=$node['boot_state'];
435   $class=($run_level == 'boot') ? 'node-ok' : 'node-ko';
436   $table->cell($run_level,array('class'=>$class));
437   foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
438
439   if ($privileges) $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
440   $table->row_end();
441 }
442 // actions area
443 if ($privileges) {
444
445   // remove nodes
446   $table->tfoot_start();
447
448   $table->row_start();
449   $table->cell($form->submit_html ("remove-nodes-from-slice","Remove selected"),
450                array('hfill'=>true,'align'=>'right'));
451   $table->row_end();
452  }
453 $table->end();
454 $toggle_nodes->end();
455
456 ////////// nodes to add
457 if ($privileges) {
458   $new_potential_nodes = array();
459   if ($potential_nodes) foreach ($potential_nodes as $node) {
460       $emptywl=empty($node['slice_ids_whitelist']);
461       $inwl = (!emptywl) and in_array($slice['slice_id'],$node['slice_ids_whitelist']);
462       if ($emptywl or $inwl)
463         $new_potential_nodes[]=$node;
464   }
465   $potential_nodes=$new_potential_nodes;
466
467   $count=count($potential_nodes);
468   $toggle_nodes=new PlekitToggle('my-slice-nodes-add',
469                                  "$count more nodes available",
470                                  array('visible'=>get_arg('show_nodes_add',false)));
471   $toggle_nodes->start();
472
473   if ( ! $potential_nodes ) {
474     // xxx improve style
475     echo "<p class='not-relevant'>No node to add</p>";
476   } else {
477     $headers=array();
478     $notes=array();
479     $headers['peer']='string';
480     $headers['hostname']='string';
481     $headers['S']='string';
482     $notes[]='S = last known status';
483     // the extra tags
484     $headers=array_merge($headers,$visibletags->headers());
485     $notes=array_merge($notes,$visibletags->notes());
486     $headers['+']="none";
487     
488     $table=new PlekitTable('add_nodes',$headers,'1', $table_options);
489     $form=new PlekitForm(l_actions(),
490                          array('slice_id'=>$slice['slice_id']));
491     $form->start();
492     $table->start();
493     if ($potential_nodes) foreach ($potential_nodes as $node) {
494         $table->row_start();
495         $peers->cell($table,$node['peer_id']);
496         $table->cell(l_node_obj($node));
497         $run_level=$node['run_level'];
498         if ( empty($run_level)) $run_level=$node['boot_state'];
499         $class=($run_level == 'boot') ? 'node-ok' : 'node-ko';
500         $table->cell($run_level,array('class'=>$class));
501         foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
502         $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
503         $table->row_end();
504       }
505     // add nodes
506     $table->tfoot_start();
507     $table->row_start();
508     $table->cell($form->submit_html ("add-nodes-in-slice","Add selected"),
509                  array('hfill'=>true,'align'=>'right'));
510     $table->row_end();
511     $table->end();
512     $form->end();
513   }
514   $toggle_nodes->end();
515 }
516 $toggle->end();
517
518 //////////////////////////////////////////////////////////// Tags
519 //if ( $local_peer ) {
520   $tags=$api->GetSliceTags (array('slice_id'=>$slice_id));
521   function get_tagname ($tag) { return $tag['tagname'];}
522   $tagnames = array_map ("get_tagname",$tags);
523   
524   $toggle = new PlekitToggle ('slice-tags',count_english_warning($tags,'tag'),
525                               array('bubble'=>'Inspect and set tags on tat slice',
526                                     'visible'=>get_arg('show_tags',false)));
527   $toggle->start();
528   
529   $headers=array(
530     "Name"=>"string",
531     "Value"=>"string",
532     "Node"=>"string",
533     "NodeGroup"=>"string");
534   if ($tags_privileges) $headers[plc_delete_icon()]="none";
535   
536   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
537   $table=new PlekitTable("slice_tags",$headers,'0',$table_options);
538   $form=new PlekitForm(l_actions(),
539                        array('slice_id'=>$slice['slice_id']));
540   $form->start();
541   $table->start();
542   if ($tags) {
543     foreach ($tags as $tag) {
544       $node_name = "ALL";
545       if ($tag['node_id']) {
546         $nodes = $api->GetNodes(array('node_id'=>$tag['node_id']));
547         if($nodes) {
548           $node = $nodes[0];
549           $node_name = $node['hostname'];
550         }
551       }
552       $nodegroup_name="n/a";
553       if ($tag['nodegroup_id']) { 
554         $nodegroup=$api->GetNodeGroups(array('nodegroup_id'=>$tag['nodegroup_id']));
555         if ($nodegroup) {
556           $nodegroup = $nodegroup[0];
557           $nodegroup_name = $nodegroup['groupname'];
558         }
559       }
560       $table->row_start();
561       $table->cell(l_tag_obj($tag));
562       $table->cell($tag['value']);
563       $table->cell($node_name);
564       $table->cell($nodegroup_name);
565       if ($tags_privileges) $table->cell ($form->checkbox_html('slice_tag_ids[]',$tag['slice_tag_id']));
566       $table->row_end();
567     }
568   }
569   if ($tags_privileges) {
570     $table->tfoot_start();
571     $table->row_start();
572     $table->cell($form->submit_html ("delete-slice-tags","Remove selected"),
573                  array('hfill'=>true,'align'=>'right'));
574     $table->row_end();
575     
576     $table->row_start();
577     function tag_selector ($tag) {
578       return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']);
579     }
580     $all_tags= $api->GetTagTypes( array ("category"=>"slice*","-SORT"=>"+tagname"), array("tagname","tag_type_id"));
581     $selector_tag=array_map("tag_selector",$all_tags);
582     
583     function node_selector($node) { 
584       return array("display"=>$node["hostname"],"value"=>$node['node_id']);
585     }
586     $all_nodes = $api->GetNodes( array ("node_id" => $slice['node_ids']), array("hostname","node_id"));
587     $selector_node=array_map("node_selector",$all_nodes);
588     
589     function nodegroup_selector($ng) {
590       return array("display"=>$ng["groupname"],"value"=>$ng['nodegroup_id']);
591     }
592     $all_nodegroups = $api->GetNodeGroups( array("groupname"=>"*"), array("groupname","nodegroup_id"));
593     $selector_nodegroup=array_map("nodegroup_selector",$all_nodegroups);
594     
595     $table->cell($form->select_html("tag_type_id",$selector_tag,array('label'=>"Choose Tag")));
596     $table->cell($form->text_html("value","",array('width'=>8)));
597     $table->cell($form->select_html("node_id",$selector_node,array('label'=>"All Nodes")));
598     $table->cell($form->select_html("nodegroup_id",$selector_nodegroup,array('label'=>"No Nodegroup")));
599     $table->cell($form->submit_html("add-slice-tag","Set Tag"),array('columns'=>2,'align'=>'left'));
600     $table->row_end();
601   }
602     
603   $table->end();
604   $form->end();
605   $toggle->end();
606 //}
607
608
609 //////////////////////// renew slice
610 if ($local_peer ) {
611   if ( ! $renew_visible) renew_area ($slice,$site,false);
612  }
613
614 $peers->block_end($peer_id);
615
616 // Print footer
617 include 'plc_footer.php';
618
619 ?>