ckp
[plewww.git] / planetlab / slices / slice.php
1 <?php
2
3 // $Id: index.php 12104 2009-02-19 18:41:19Z thierry $
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
24 // -------------------- 
25 // recognized URL arguments
26 $slice_id=intval($_GET['id']);
27 if ( ! $slice_id ) { plc_error('Malformed URL - id not set'); return; }
28
29 function renew_area ($slice,$site) {
30  
31   // Constants
32   $week= 7 * 24 * 60 * 60; // seconds
33   $max_renewal_length= 8; // weeks from today
34   $max_expiration= mktime() + ($max_renewal_length * $week); // seconds since epoch
35   $max_expiration_date= gmstrftime("%A %b-%d-%y %T %Z", $max_expiration);
36   
37   // the renew area
38   // xxx some extra code needed to enable this area only if the slice description is OK:
39   // description and url must be non void
40   $toggle=new PlekitToggle('renew',"Renew this slice",
41                            array("trigger-bubble"=>"Enter this zone if you wish to renew your slice",
42                                  'start-visible'=>true));
43   $toggle->start();
44
45   // xxx message could take roles into account
46   if ($site['max_slices']<=0) {
47      $message= <<< EOF
48 <p>Slice creation and renewal have been temporarily disabled for your
49 site. This may have occurred because your site's nodes have been down
50 or unreachable for several weeks, and multiple attempts to contact
51 your site's PI(s) and Technical Contact(s) have all failed. If so,
52 contact your site's PI(s) and Technical Contact(s) and ask them to
53 bring up your site's nodes. Please visit your <a
54 href='/db/sites/index.php?id=$site_id'>site details</a> page to find
55 out more about your site's nodes, and how to contact your site's PI(s)
56 and Technical Contact(s).</p>
57 EOF;
58      echo $message;
59  
60   } else {
61     // xxx this is a rough cut and paste from the former UI
62     // Showing a datepicker view could be considered as well with some extra work
63     // Calculate possible extension lengths
64     $renewal_lengths = array();
65     foreach ( array ( 1 => "One more week", 
66                       2 => "Two more weeks", 
67                       4 => "One more month" ) as $weeks => $text ) {
68        if (($slice [ 'expires' ] + ($weeks * $week)) < $max_expiration) {
69          $renewal_lengths [ $weeks ] = "$text (" . 
70            gmstrftime( "%A %b-%d-%y %T %Z", $slice [ 'expires' ] + ( $weeks * $week ) ) 
71            . ")";
72        }
73     }
74
75     if ( empty( $renewal_lengths ) ) {
76       plc_warning("Slice cannot be renewed any further into the future, try again closer to expiration date.");
77      } else {
78       // clean vars
79        $expiration_date = gmstrftime( "%A %b-%d-%y %T %Z", $slice [ 'expires' ] );
80        echo '<p> area under construction </a>';
81        
82        //       // display form
83        //       echo "<form action='/db/slices/renew_slice.php' method='post'>\n";
84        //       echo "<input type=hidden name='id' value='$slice_id'><input type=hidden name='expires' value='". $slice['expires'] ."'>\n";
85      
86 // $message = <<< EOF
87 // <p>You must provide a short description as well as a link to a project website before renewing it.
88 // Do <span class='bold'>not</span> provide bogus information; if a complaint is lodged against your slice 
89 // and PlanetLab Operations is unable to determine what the normal behavior of your slice is, 
90 // your slice may be deleted to resolve the complaint.</p>
91 // EOF;
92 // echo $message;
93 //       
94 //       echo "<p><span class='bold'>NOTE:</span> 
95 // Slices cannot be renewed beyond $max_renewal_length weeks of today ($max_expiration_date).</p>\n";
96 //       
97 //       echo "<table cellpadding=2><tbody>\n";
98 //       
99 //       echo "<tr><th>Name:</th><td colspan=2>". $slice['name'] ."</td></tr>\n";
100 //       
101 //       if( $error['url'] ) 
102 //         $url_style= " style='border: 1px solid red;'";
103 //       echo "<tr><th$url_style>URL: </th><td$url_style><input size=50 name='url' value='". $slice['url'] ."' /></td><td$url_style><font color=red>". $error['url'] ."</font></td></tr>\n";
104 //       
105 //       if( $error['description'] ) 
106 //         $desc_style= " style='border: 1px solid red;'";
107 //       echo "<tr><th$desc_style>Description: </th><td$desc_style><textarea name='description' rows=5 cols=40>". $slice['description'] ."</textarea></td><td$desc_style><font color=red>". $error['description'] ."</font></td></tr>\n";
108 //       
109 //       echo "<tr><th>Expiration Date: </th><td colspan=2>$expiration_date</td></tr>\n";
110 //       
111 //       echo "<tr><th>Renewal Length: </th><td colspan=2><select name='expire_len'>";
112 //       
113 //       // create drop down of lengths to choose
114 //       foreach ($renewal_lengths as $weeks => $text) {
115 //         echo "<option value='$weeks'";
116 //         if( $weeks == $expire_len )
117 //           echo " selected";
118 //         echo ">$text</option>\n";
119        }
120 //       
121 //       echo "</select></td></tr>\n<tr><td colspan=3 align=center><input type=submit value='Renew Slice' name='submitted'></td></tr>\n</tbody></table>\n";
122 //     
123 //     }
124 //     
125   }
126  
127   $toggle->end();
128 }
129
130 ////////////////////
131 // Get all columns as we focus on only one entry
132 $slices= $api->GetSlices( array($slice_id));
133
134 if (empty($slices)) {
135   drupal_set_message ("Slice " . $slice_id . " not found");
136   return;
137  }
138
139 $slice=$slices[0];
140
141 // pull all node info to vars
142 $name= $slice['name'];
143 $expires = date( "d/m/Y", $slice['expires'] );
144 $site_id= $slice['site_id'];
145
146 //$node_ids=$slice['node_ids'];
147 $person_ids=$slice['person_ids'];
148 //$slice_tag_ids= $slice['slice_tag_ids'];
149
150 // get peers
151 $peer_id= $slice['peer_id'];
152 $peers=new Peers ($api);
153
154 // gets site info
155 $sites= $api->GetSites( array( $site_id ) );
156 $site=$sites[0];
157 $site_name= $site['name'];
158 $max_slices = $site['max_slices'];
159
160 // get all persons info
161 if (!empty($person_ids))
162   $persons=$api->GetPersons($person_ids,array('email','enabled'));
163
164 ////////// 
165 drupal_set_title("Details for slice " . $name);
166 $local_peer= ! $peer_id;
167
168 $am_in_slice = in_array(plc_my_person_id(),$person_ids);
169
170 $privileges = (plc_is_admin()  || $am_in_slice);
171
172 $tabs=array();
173 $tabs [] = tab_nodes_slice($slice_id);
174 $tabs [] = tab_site($site_id);
175
176 // are these the right privileges for deletion ?
177 if ($privileges) {
178   $tabs ['Delete']= array('url'=>l_actions(),
179                           'method'=>'post',
180                           'values'=>array('action'=>'delete-slice','slice_id'=>$slice_id),
181                           'bubble'=>"Delete slice $name",
182                           'confirm'=>'Are you sure to delete $name');
183
184   $tabs["Events"]=array_merge(tablook_event(),
185                               array('url'=>l_event("Slice","slice",$slice_id),
186                                     'bubble'=>"Events for slice $name"));
187   $tabs["Comon"]=array_merge(tablook_comon(),
188                              array('url'=>l_comon("slice_id",$slice_id),
189                                    'bubble'=>"Comon page about slice $name"));
190 }
191
192 plekit_linetabs($tabs);
193
194 ////////////////////////////////////////
195 $peers->block_start($peer_id);
196
197 $toggle = new PlekitToggle ('slice',"Details",
198                             array('trigger-bubble'=>'Display and modify details for that slice'));
199 $toggle->start();
200
201 $details=new PlekitDetails($privileges);
202 $details->form_start(l_actions(),array('action'=>'update-slice','slice_id'=>$slice_id));
203
204 $details->start();
205 if (! $local_peer) {
206   $details->th_td("Peer",$peers->peer_link($peer_id));
207   $details->space();
208  }
209
210
211 $details->th_td('Name',$slice['name']);
212 $details->th_td('Description',$slice['description'],'description',
213                 array('input_type'=>'textarea',
214                       'width'=>50,'height'=>5));
215 $details->th_td('URL',$slice['url'],'url',array('width'=>50));
216 $details->th_td('Expires',$expires);
217 $details->th_td('Instantiation',$slice['instantiation']);
218 $details->th_td('Site',l_site_obj($site));
219 $details->end();
220
221 $details->form_end();
222 $toggle->end();
223
224 renew_area ($slice,$site);
225
226 $peers->block_end($peer_id);
227
228 //////////////////// users
229
230 //////////////////// nodes
231
232 //////////////////// tags
233
234 // Print footer
235 include 'plc_footer.php';
236
237 return;
238
239 ?>
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254   // gets all persons from site_id
255     // person info
256   if( !empty( $person_ids ) ) 
257     $persons= $api->GetPersons( $site_info[0]['person_ids'] , array( "person_id", "role_ids", "first_name", "last_name", "email" ) );
258
259   if( $persons ) {
260     // gets site contacts pis stores in dict
261     foreach( $persons as $person )
262       if( in_array( "20", $person['role_ids'] ) ) {
263         $pis[]= array( "email" => $person['email'], "first_name" => $person['first_name'], "last_name" => $person['last_name'], "person_id" => $person['person_id'] );
264         
265       }
266     if ($pis) {
267       sort_persons( $pis );
268     }
269   }
270
271   // slice tag info
272   if( !empty( $slice_tag_ids ) )
273     $slice_attibs= $api->GetSliceTags( $slice_tag_ids, 
274                                        array( "slice_tag_id", "tag_type_id", "value", "description", "min_role_id", "node_id" ) );
275
276   // gets tag type info and combines it to form all tag info array
277   if( $slice_attibs ) {
278     foreach( $slice_attibs as $slice_attib ) {
279       $tag_type= $api->GetTagTypes( array( $slice_attib['tag_type_id'] ), 
280                                     array( "tag_type_id", "tagname", "description" ) );
281       
282       $tags[]= array( "slice_tag_id" => $slice_attib['slice_tag_id'], 
283                       "tag_type_id" => $slice_attib['tag_type_id'], 
284                       "tagname" => $tag_type[0]['tagname'], 
285                       "value" => $slice_attib['value'], 
286                       "description" => $slice_attib['description'], 
287                       "min_role_id" => $slice_attib['min_role_id'], 
288                       "node_id" => $slice_attib['node_id'] );
289     }
290
291   }
292
293   drupal_set_title("Slice details for " . $name);
294   // start form
295
296   if( $peer_id ) {
297     echo "<div class='plc-foreign'>\n";
298   }
299
300   // basic slice menu
301   if( ! $peer_id ) {
302
303     $actions= array( ''=>'Choose Action' );
304     
305     if( in_array( 10, $_roles ) 
306         || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) 
307         || in_array( $slice_id, $_person['slice_ids'] ) ) {
308       $actions['renew']= "Renew $name";
309       $actions['nodes']= "Manage Nodes";
310     }
311     if ( in_array( 10, $_roles )
312          || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) ) {
313       $actions['users']= "Manage Users";
314       $actions['delete']= "Delete $name";
315     }
316     
317     echo "<table><tr><td>\n";
318     if (in_array( 10, $_roles )) {
319       echo plc_event_button("slices","slice",$slice_id);
320       echo "</td><td>";
321     }
322     echo plc_comon_button("slice_id",$slice_id);
323     echo "</td><td>\n";
324
325     echo "<form action='/db/slices/slice_action.php' method='post'>\n";
326     echo "<input type=hidden name=slice_id value=$slice_id>\n";
327
328     echo "<select name='actions' onChange=\"submit();\">\n";
329     foreach( $actions as $key => $val ) {
330       echo "<option value='$key'";
331       
332       if( $key == $_POST['actions'] )
333         echo " selected";
334       
335       echo ">$val\n";
336     }
337     
338     echo "</select><br />\n";
339     echo "</form>\n";
340
341     echo "</td></tr></table>\n";
342   }
343
344   echo "<table cellpadding=3><tbody>\n
345         <tr><th>Slice Name: </th><td> $name </td></tr>\n
346         <tr><th>Description: </th><td> $description </td></tr>\n
347         <tr><th>URL: </th><td> <a href='$url'>$url</a> </td></tr>\n";
348         
349   if( gmmktime() > $slice['expires'] ) { 
350     $class1= ' style="color:red;"'; 
351     $msg1= '(slice is expired)'; 
352   }
353   echo "<tr><th$class1>Expiration: </th><td$class1> $expires &nbsp; $msg1</td></tr>\n";
354   echo "<tr><th>Instantiation: </th><td><select name='instantiation' onChange=\"submit();\"\n";
355   echo "<option value='delegated'"; 
356   if( $instantiation == 'delegated' ) echo " selected"; 
357   echo ">delegated</option>";
358   echo "<option value='plc-instantiated'"; 
359   if( $instantiation == 'plc-instantiated' ) echo " selected"; 
360   echo ">plc-instantiated</option>";
361   echo "<option value='not-instantiated'"; 
362   if( $instantiation == 'not-instantiated' ) echo " selected"; 
363   echo ">not-instantiated</option>";
364   echo "</select>"; 
365
366   echo "</td></tr>\n";
367   echo "<tr><th>Site: </th><td> <a href='/db/sites/index.php?id=$site_id'>". $site_info[0]['name'] ."</a></td></tr>\n";
368   $href="'/db/nodes/index.php?slice_id=" . $slice_id . "'";
369   printf ("<tr><th> <a href=%s># Nodes</a></th><td><a href=%s>Total %d nodes</a></td>\n",$href,$href,count($node_ids));
370   $href="'/db/persons/index.php?slice_id=" . $slice_id . "'";
371   printf ("<tr><th> <a href=%s># Users</a></th><td><a href=%s>Total %d users</a></td>\n",$href,$href,count($person_ids));
372   echo "</tbody></table>\n";
373
374   if ( (!$class1) && in_array( $slice_id, $_person['slice_ids'] ) && (! $peer_id) ) 
375     echo "<p><a href='update_slice.php?id=$slice_id'>Update Information</a>\n";
376
377   echo "<br /><hr />\n";
378
379
380   // slice tags
381   if( $tags ) {
382
383     // builds 2 arrays, one for tags, one for slivers
384     foreach( $tags as $tag ) {
385       if( empty( $tag['node_id'] ) ) {
386         $slice_tag[]= $tag;
387       }
388       else {
389         $sliver_tag[]= $tag;
390         $sliver_nodes[]= $tag['node_id'];
391       }
392     }
393   }
394
395   // Get node info for those slivers
396   $sliver_node_info= $api->GetNodes( $sliver_nodes, array( "node_id", "hostname" ) );
397
398   if( $sliver_node_info ) {
399     foreach( $sliver_node_info as $sliv_node ) {
400       $new_sliver_node_info[$sliv_node['node_id']]= $sliv_node;
401     }
402   }
403
404  if( $peer_id ) {
405    echo "<br /></div>\n";
406   }
407  
408   // slice tags
409   $is_admin=in_array( 10, $_roles );
410   $is_in_slice=in_array( $slice_id, $_person['slice_ids'] );
411   $is_pi=in_array( 20, $_roles );
412   if( $slice_tag ) {
413     echo "<table cellpadding=3><caption class='list_set'>Slice Tags</caption>";
414     echo "<thead><tr>";
415     if( $is_admin )
416       echo "<th></th>";
417     echo "<th>Tag</th><th>Value</th><th>Description</th>";
418     echo "</tr></thead><tbody>\n";
419
420     foreach( $tags as $tag ) {
421       // ignore sliver tags at this stage
422       if( empty( $tag['node_id'] ) ) {
423         echo("<tr>");
424         if( $is_admin ) {
425           printf("<td>");
426           sprintf($label,"\\n [ %s = %s] \\n from %s",$tag['tagname'],$tag['value'],$name);
427           // xxx this is deprecated
428           echo plc_delete_link_button ('tag_action.php?rem_id=' . $tag['slice_tag_id'],
429                                        $label);
430           echo "</td>";
431         }
432         if( $is_admin || ($is_pi && $is_in_slice) ) {
433           printf ("<td><a href='tags.php?type=slice?id=%s'>%s</a></td>",
434                   $tag['slice_tag_id'],$tag['tagname']);
435         } else {
436           printf("<td>%s</td>",$tag['tagname']);
437         }
438         printf("<td align=center>%s</td><td>%s</td>",
439                $tag['value'],$tag['description']);
440         echo "</tr>";
441       }
442     }
443
444     
445     echo "</tbody></table>\n";
446
447   }
448   if( $is_admin || ($is_pi && $is_in_slice) )
449     echo "<a href='tags.php?type=slice&add=$slice_id'>Add a Slice Tag</a>\n";    
450
451
452
453   // sliver tags
454   if( $sliver_tag ) {
455     echo "<table cellpadding=3><caption class='list_set'>Sliver Tags</caption>";
456     echo "<thead><tr>";
457     if( $is_admin )
458       echo "<th></th>";
459     echo "<th>Tag</th><th>Value</th><th>Description</th><th>Node</th>";
460     echo "</tr></thead><tbody>\n";
461
462     foreach( $tags as $tag ) {
463       $nodename=$new_sliver_node_info[$tag['node_id']]['hostname'];
464       // consider only sliver tags at this stage
465       if( !empty( $tag['node_id'] ) ) {
466         echo("<tr>");
467         if( $is_admin ) {
468           echo("<td>");
469           $label=sprintf("\\n [ %s = %s ] \\n from %s \\n on node %s",
470                          $tag['tagname'],$tag['value'],$name,$nodename);
471           echo plc_delete_link_label('/db/nodes/sliver_action.php?rem_id=' . $tag['slice_tag_id'], 
472                                      $label);
473           echo "</td>";
474         }
475         if( $is_admin ) {
476           printf("<td><a href='tags.php?type=slice&id=%s'>%s</a></td>",$tag['slice_tag_id'],$tag['tagname']);
477         } else {
478           printf("<td>%s</td>",$tag['tagname']);
479         }
480         printf("<td align=center>%s</td><td>%s</td><td><a href='/db/nodes/index.php?id=%s'>%s</a></td>",
481                $tag['value'],$tag['description'],$tag['node_id'],$nodename);
482         
483         echo "</tr>";
484       }
485     }
486
487     echo "</tbody></table>\n";
488     
489   }
490   
491   echo "<br /><hr />\n";
492   
493   if( $pis && !$peer_id ) {
494     // site contacts
495     echo "<h5>Contacts</h5>\n";
496                 
497     $pi_rows= count( $pis );
498     $tech_rows= count( $techs );
499     $table_row= 0;
500         
501     echo "<table cellpadding=2><tbody>";
502     if( $pis ) {
503       echo "<tr><td rowspan=$pi_rows><strong>PI's:</strong> &nbsp; </td>\n";
504       
505       foreach( $pis as $pi ) {
506         if( $table_row != 0 )
507           echo "<tr>";
508         printf("<td><a href='/db/persons/index.php?id=%s'>%s %s</td><td><a href='mailto:%s'>%s</a></td></tr>\n",
509                $pi['person_id'],$pi['first_name'],$pi['last_name'],$pi['email'],$pi['email']);
510         $table_row++;
511       }
512       
513     }
514     
515     echo "</table>\n<br /><hr />\n";
516     
517   }
518   
519   
520   echo "<p><a href='index.php'>Back to slice list</a></div>\n";
521  }
522