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