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