redirects
[plewww.git] / planetlab / slices / index.php
1 <?php
2
3 // $Id$
4 // pattern-matching selection not implemented
5 // due to GetSlices bug, see test.php for details
6 // in addition that would not make much sense
7
8 // Require login
9 require_once 'plc_login.php';
10
11 // Get session and API handles
12 require_once 'plc_session.php';
13 global $plc, $api;
14
15 // Print header
16 require_once 'plc_drupal.php';
17 // set default
18 drupal_set_title('Slices');
19 include 'plc_header.php';
20
21 // Common functions
22 require_once 'plc_functions.php';
23 require_once 'plc_sorts.php';
24
25 // find person roles
26 $_person= $plc->person;
27 $_roles= $_person['role_ids'];
28
29 //print_r( $_person );
30
31
32 // if node_host is set then set id to that node's id.
33 if( $_POST['slicename'] ) {
34   $slicename= $_POST['slicename'];
35
36   $slices= $api->GetSlices( array( $slicename ), array( "slice_id" ) );
37   $slice=$slices[0];
38   $slice_id=$slice['slice_id'];
39
40   plc_redirect(l_slice($slice_id));
41
42 }
43
44
45 // if no slice id, display list of slices
46 if( !$_GET['id'] ) {
47   // diplay site select list for admins
48   if( in_array( 10, $_roles ) ) {
49     // auto complete box for finding a slice
50                 
51     drupal_set_html_head('<script type="text/javascript" src="/planetlab/bsn/bsn.Ajax.js"></script>
52     <script type="text/javascript" src="/planetlab/bsn/bsn.DOM.js"></script>
53     <script type="text/javascript" src="/planetlab/bsn/bsn.AutoSuggest.js"></script>');
54
55     echo "<div>\n
56           <form method=post action='index.php'>\n";
57     if( $slicename ) echo "<font color=red>'$slicename' is not a valid slice name.</font>\n";
58     echo "<p><label for='testinput'>Enter Slice Name: </label>\n
59           <input type='text' id='testinput' name='slicename' size=40 value='' />\n
60           <input type=submit value='Select Slice' />\n
61           </div>\n
62           <br />\n";
63      
64     // get site info
65     $site_info= $api->GetSites( NULL, array( "site_id", "name", "peer_id" ) );
66     sort_sites( $site_info );
67
68     // Thierry -- try to select only one entry
69     // xxx still not right if _person in several sites, but that is good enough
70     //if( $site['site_id'] == $_POST['site_id'] || in_array( $site['site_id'], $_person['site_ids'] ) )
71     if ($_POST['site_id'])
72       $selected_site_id = $_POST['site_id'];
73     else if ($_GET['site_id']) 
74       $selected_site_id = $_GET['site_id'];
75     else
76       $selected_site_id = $_person['site_ids'][0];
77
78     echo "Select a site to view slices from: &nbsp;";
79     echo "<select name='site_id' onChange='submit()'>\n";
80
81     foreach( $site_info as $site ) {
82       echo "<option value=". $site['site_id'];
83       if ( $site['site_id'] == $selected_site_id)
84           echo " selected";
85       if ( $site['peer_id'] ) 
86         echo " class='plc-foreign'";
87       echo ">". $site['name'] ."</option>\n";
88       
89     }
90
91     echo "</select>\n";
92   
93   }
94
95   if( $_POST['site_id'] ) {
96     $selection="Site";
97     $site= array( intval( $_POST['site_id'] ) );
98   } elseif( $_GET['site_id'] ) {
99     $selection="Site";
100     $site= array( intval( $_GET['site_id'] ) );
101   } else {
102     $selection="Person";
103     $site= $_person['site_ids'];
104   }
105   
106   // get site's slices
107   $site_info= $api->GetSites( $site, array( "slice_ids","name" ) );
108
109   if ( $selection == "Site" ) 
110     drupal_set_title ("Slices for site " . $site_info[0]['name']);
111   else
112     drupal_set_title ("Slices for " . $_person['email'] . "'s sites");
113
114   // make an array of all slices
115   foreach( $site_info as $site ) {
116     foreach( $site['slice_ids'] as $slice_id ) {
117       $slice_ids[]= $slice_id;
118     }
119     
120   }
121   
122   if (empty ($slice_ids)) {
123     echo "<p><strong>No slice found, or all are expired.</strong>";
124   } else {
125   
126     $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name", "site_id", "person_ids", "expires", "peer_id" ) );
127     $slice=$slices[0];
128     //print '<pre>'; print_r( $api->trace() ) ; print '</pre>';
129
130     if ( ! $slices) {
131       echo "<p><strong>No Slices on site, or all are expired.</strong>\n";
132     } else  {
133       echo "<table class='list_set' border=0 cellpadding=2>\n";
134       echo "<caption class='list_set'>Slice list</caption>\n";
135       echo "<thead><tr class='list_set'><th class='list_set'>Slice Name</th>";
136       echo "<th class='list_set'>Users</th>";
137       echo "<th class='list_set'>Expiration</th></tr>";
138       echo "</thead><tbody>\n";
139       
140       // create a list of person_ids
141       $person_ids = array();
142       foreach( $slices as $slice ) {
143           if ( !empty($slice['person_ids']) )
144             $person_ids = array_merge($person_ids, $slice['person_ids']);
145         }
146
147       // create an associative array of persons with person_id as the key
148       $person_list = $api->GetPersons( $person_ids, array("person_id", "email") ); 
149       $persons = array();
150       foreach( $person_list as $person)
151         {
152           $persons[$person['person_id']] = $person;
153         }
154       
155       foreach( $slices as $slice ) {
156         $slice_id= $slice['slice_id'];
157         $slice_name= $slice['name'];
158         $slice_expires= date( "M j, Y", $slice['expires'] );
159         $peer_id = $slice['peer_id'];
160       
161         $extraclass="";
162         if ( $peer_id ) 
163           $extraclass="plc-foreign";
164       
165         echo "<tr class='list_set $extraclass'><td><a href='/db/slices/index.php?id=$slice_id'>$slice_name</a></td><td class='list_set'>";
166       
167         if( !empty( $slice['person_ids'] ) ) {
168           foreach( $slice['person_ids'] as $person_id ) {
169             $person = $persons[$person_id];
170             $id= $person['person_id'];
171             $email= $person['email'];
172             echo "<a href='../persons/index.php?id=$id'>$email</a><br />\n";
173           }
174         } else {
175           echo "None";
176         }
177       
178         echo "</td><td class='list_set'>$slice_expires</td></tr>\n";
179     
180       }
181     
182       echo "</tbody></table>\n";
183
184     }
185   }
186   
187
188   echo "</form>\n";
189
190   echo "<script type=\"text/javascript\">
191 var options = {
192         script:\"/planetlab/slices/test.php?\",
193         varname:\"input\",
194         minchars:1
195 };
196 var as = new AutoSuggest('testinput', options);
197 </script>\n";
198
199 }
200 // if nothing else then show slice info
201 else {
202   $slice_id= intval( $_GET['id'] );
203
204   // GetSlices API call
205   $slices= $api->GetSlices( array( $slice_id ) );
206
207   if( empty( $slices ) ) 
208     plc_redirect(l_slices());
209
210   // pull all slice info to vars
211   $instantiation= $slice['instantiation'];
212   $name= $slice['name'];
213   $url= $slice['url'];
214   $expires= date( "M j, Y", $slice['expires'] );
215   $site_id= $slice['site_id'];
216   $description= $slice['description'];
217   $max_nodes= $slice['max_nodes'];
218   $node_ids=$slice['node_ids'];
219   $person_ids=$slice['node_ids'];
220
221   // get peer id
222   $peer_id= $slice['peer_id'];
223
224   $person_ids= $slice['person_ids'];
225   $node_ids= $slice['node_ids'];
226   $slice_tag_ids= $slice['slice_tag_ids'];
227
228
229   // node info
230 // looks unused
231 //  if( !empty( $node_ids ) )
232 //    $nodes= $api->GetNodes( $node_ids, array( "node_id", "hostname" ) );
233
234   // site info
235   $site_info= $api->GetSites( array( $site_id ), array( "site_id", "name", "person_ids" ) );
236
237   // gets all persons from site_id
238     // person info
239   if( !empty( $person_ids ) ) 
240     $persons= $api->GetPersons( $site_info[0]['person_ids'] , array( "person_id", "role_ids", "first_name", "last_name", "email" ) );
241
242   if( $persons ) {
243     // gets site contacts pis stores in dict
244     foreach( $persons as $person )
245       if( in_array( "20", $person['role_ids'] ) ) {
246         $pis[]= array( "email" => $person['email'], "first_name" => $person['first_name'], "last_name" => $person['last_name'], "person_id" => $person['person_id'] );
247         
248       }
249     if ($pis) {
250       sort_persons( $pis );
251     }
252   }
253
254   // slice tag info
255   if( !empty( $slice_tag_ids ) )
256     $slice_attibs= $api->GetSliceTags( $slice_tag_ids, 
257                                        array( "slice_tag_id", "tag_type_id", "value", "description", "min_role_id", "node_id" ) );
258
259   // gets tag type info and combines it to form all tag info array
260   if( $slice_attibs ) {
261     foreach( $slice_attibs as $slice_attib ) {
262       $tag_type= $api->GetTagTypes( array( $slice_attib['tag_type_id'] ), 
263                                                   array( "tag_type_id", "tagname", "description" ) );
264       
265       $tags[]= array( "slice_tag_id" => $slice_attib['slice_tag_id'], 
266                       "tag_type_id" => $slice_attib['tag_type_id'], 
267                       "tagname" => $tag_type[0]['tagname'], 
268                       "value" => $slice_attib['value'], 
269                       "description" => $slice_attib['description'], 
270                       "min_role_id" => $slice_attib['min_role_id'], 
271                       "node_id" => $slice_attib['node_id'] );
272     }
273
274   }
275
276   drupal_set_title("Slice details for " . $name);
277   // start form
278
279   if( $peer_id ) {
280     echo "<div class='plc-foreign'>\n";
281   }
282
283   // basic slice menu
284   if( ! $peer_id ) {
285
286     $actions= array( ''=>'Choose Action' );
287     
288     if( in_array( 10, $_roles ) 
289         || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) 
290         || in_array( $slice_id, $_person['slice_ids'] ) ) {
291       $actions['renew']= "Renew $name";
292       $actions['nodes']= "Manage Nodes";
293     }
294     if ( in_array( 10, $_roles )
295          || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) ) {
296       $actions['users']= "Manage Users";
297       $actions['delete']= "Delete $name";
298     }
299     
300     echo "<table><tr><td>\n";
301     if (in_array( 10, $_roles )) {
302       echo plc_event_button("slices","slice",$slice_id);
303       echo "</td><td>";
304     }
305     echo plc_comon_button("slice_id",$slice_id);
306     echo "</td><td>\n";
307
308     echo "<form action='/db/slices/slice_action.php' method='post'>\n";
309     echo "<input type=hidden name=slice_id value=$slice_id>\n";
310
311     echo "<select name='actions' onChange=\"submit();\">\n";
312     foreach( $actions as $key => $val ) {
313       echo "<option value='$key'";
314       
315       if( $key == $_POST['actions'] )
316         echo " selected";
317       
318       echo ">$val\n";
319     }
320     
321     echo "</select><br />\n";
322     echo "</form>\n";
323
324     echo "</td></tr></table>\n";
325   }
326
327   echo "<table cellpadding=3><tbody>\n
328         <tr><th>Slice Name: </th><td> $name </td></tr>\n
329         <tr><th>Description: </th><td> $description </td></tr>\n
330         <tr><th>URL: </th><td> <a href='$url'>$url</a> </td></tr>\n";
331         
332   if( gmmktime() > $slice['expires'] ) { 
333     $class1= ' style="color:red;"'; 
334     $msg1= '(slice is expired)'; 
335   }
336   echo "<tr><th$class1>Expiration: </th><td$class1> $expires &nbsp; $msg1</td></tr>\n";
337   echo "<tr><th>Instantiation: </th><td><select name='instantiation' onChange=\"submit();\"\n";
338   echo "<option value='delegated'"; 
339   if( $instantiation == 'delegated' ) echo " selected"; 
340   echo ">delegated</option>";
341   echo "<option value='plc-instantiated'"; 
342   if( $instantiation == 'plc-instantiated' ) echo " selected"; 
343   echo ">plc-instantiated</option>";
344   echo "<option value='not-instantiated'"; 
345   if( $instantiation == 'not-instantiated' ) echo " selected"; 
346   echo ">not-instantiated</option>";
347   echo "</select>"; 
348
349   echo "</td></tr>\n";
350   echo "<tr><th>Site: </th><td> <a href='/db/sites/index.php?id=$site_id'>". $site_info[0]['name'] ."</a></td></tr>\n";
351   $href="'/db/nodes/index.php?slice_id=" . $slice_id . "'";
352   printf ("<tr><th> <a href=%s># Nodes</a></th><td><a href=%s>Total %d nodes</a></td>\n",$href,$href,count($node_ids));
353   $href="'/db/persons/index.php?slice_id=" . $slice_id . "'";
354   printf ("<tr><th> <a href=%s># Users</a></th><td><a href=%s>Total %d users</a></td>\n",$href,$href,count($person_ids));
355   echo "</tbody></table>\n";
356
357   if ( (!$class1) && in_array( $slice_id, $_person['slice_ids'] ) && (! $peer_id) ) 
358     echo "<p><a href='update_slice.php?id=$slice_id'>Update Information</a>\n";
359
360   echo "<br /><hr />\n";
361
362
363   // slice tags
364   if( $tags ) {
365
366     // builds 2 arrays, one for tags, one for slivers
367     foreach( $tags as $tag ) {
368       if( empty( $tag['node_id'] ) ) {
369         $slice_tag[]= $tag;
370       }
371       else {
372         $sliver_tag[]= $tag;
373         $sliver_nodes[]= $tag['node_id'];
374       }
375     }
376   }
377
378   // Get node info for those slivers
379   $sliver_node_info= $api->GetNodes( $sliver_nodes, array( "node_id", "hostname" ) );
380
381   if( $sliver_node_info ) {
382     foreach( $sliver_node_info as $sliv_node ) {
383       $new_sliver_node_info[$sliv_node['node_id']]= $sliv_node;
384     }
385   }
386
387  if( $peer_id ) {
388    echo "<br /></div>\n";
389   }
390  
391   // slice tags
392   $is_admin=in_array( 10, $_roles );
393   $is_in_slice=in_array( $slice_id, $_person['slice_ids'] );
394   $is_pi=in_array( 20, $_roles );
395   if( $slice_tag ) {
396     echo "<table cellpadding=3><caption class='list_set'>Slice Tags</caption>";
397     echo "<thead><tr>";
398     if( $is_admin )
399       echo "<th></th>";
400     echo "<th>Tag</th><th>Value</th><th>Description</th>";
401     echo "</tr></thead><tbody>\n";
402
403     foreach( $tags as $tag ) {
404       // ignore sliver tags at this stage
405       if( empty( $tag['node_id'] ) ) {
406         echo("<tr>");
407         if( $is_admin ) {
408           printf("<td>");
409           sprintf($label,"\\n [ %s = %s] \\n from %s",$tag['tagname'],$tag['value'],$name);
410           // xxx this is deprecated
411           echo plc_delete_link_button ('tag_action.php?rem_id=' . $tag['slice_tag_id'],
412                                        $label);
413           echo "</td>";
414         }
415         if( $is_admin || ($is_pi && $is_in_slice) ) {
416           printf ("<td><a href='tags.php?type=slice?id=%s'>%s</a></td>",
417                   $tag['slice_tag_id'],$tag['tagname']);
418         } else {
419           printf("<td>%s</td>",$tag['tagname']);
420         }
421         printf("<td align=center>%s</td><td>%s</td>",
422                $tag['value'],$tag['description']);
423         echo "</tr>";
424       }
425     }
426
427     
428     echo "</tbody></table>\n";
429
430   }
431   if( $is_admin || ($is_pi && $is_in_slice) )
432     echo "<a href='tags.php?type=slice&add=$slice_id'>Add a Slice Tag</a>\n";    
433
434
435
436   // sliver tags
437   if( $sliver_tag ) {
438     echo "<table cellpadding=3><caption class='list_set'>Sliver Tags</caption>";
439     echo "<thead><tr>";
440     if( $is_admin )
441       echo "<th></th>";
442     echo "<th>Tag</th><th>Value</th><th>Description</th><th>Node</th>";
443     echo "</tr></thead><tbody>\n";
444
445     foreach( $tags as $tag ) {
446       $nodename=$new_sliver_node_info[$tag['node_id']]['hostname'];
447       // consider only sliver tags at this stage
448       if( !empty( $tag['node_id'] ) ) {
449         echo("<tr>");
450         if( $is_admin ) {
451           echo("<td>");
452           $label=sprintf("\\n [ %s = %s ] \\n from %s \\n on node %s",
453                          $tag['tagname'],$tag['value'],$name,$nodename);
454           echo plc_delete_link_label('/db/nodes/sliver_action.php?rem_id=' . $tag['slice_tag_id'], 
455                                      $label);
456           echo "</td>";
457         }
458         if( $is_admin ) {
459           printf("<td><a href='tags.php?type=slice&id=%s'>%s</a></td>",$tag['slice_tag_id'],$tag['tagname']);
460         } else {
461           printf("<td>%s</td>",$tag['tagname']);
462         }
463         printf("<td align=center>%s</td><td>%s</td><td><a href='/db/nodes/index.php?id=%s'>%s</a></td>",
464                $tag['value'],$tag['description'],$tag['node_id'],$nodename);
465         
466         echo "</tr>";
467       }
468     }
469
470     echo "</tbody></table>\n";
471     
472   }
473   
474   echo "<br /><hr />\n";
475   
476   if( $pis && !$peer_id ) {
477     // site contacts
478     echo "<h5>Contacts</h5>\n";
479                 
480     $pi_rows= count( $pis );
481     $tech_rows= count( $techs );
482     $table_row= 0;
483         
484     echo "<table cellpadding=2><tbody>";
485     if( $pis ) {
486       echo "<tr><td rowspan=$pi_rows><strong>PI's:</strong> &nbsp; </td>\n";
487       
488       foreach( $pis as $pi ) {
489         if( $table_row != 0 )
490           echo "<tr>";
491         printf("<td><a href='/db/persons/index.php?id=%s'>%s %s</td><td><a href='mailto:%s'>%s</a></td></tr>\n",
492                $pi['person_id'],$pi['first_name'],$pi['last_name'],$pi['email'],$pi['email']);
493         $table_row++;
494       }
495       
496     }
497     
498     echo "</table>\n<br /><hr />\n";
499     
500   }
501   
502   
503   echo "<p><a href='index.php'>Back to slice list</a></div>\n";
504  }
505
506 // Print footer
507 include 'plc_footer.php';
508
509 ?>