svn:keywords
[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   $slice_info= $api->GetSlices( array( $slicename ), array( "slice_id" ) );
37
38   header( "location: index.php?id=". $slice_info[0]['slice_id'] );
39   exit();
40
41 }
42
43
44 // if no slice id, display list of slices
45 if( !$_GET['id'] ) {
46   // diplay site select list for admins
47   if( in_array( 10, $_roles ) ) {
48     // auto complete box for finding a slice
49                 
50     drupal_set_html_head('<script type="text/javascript" src="/planetlab/bsn/bsn.Ajax.js"></script>
51     <script type="text/javascript" src="/planetlab/bsn/bsn.DOM.js"></script>
52     <script type="text/javascript" src="/planetlab/bsn/bsn.AutoSuggest.js"></script>');
53
54     echo "<div>\n
55           <form method=post action='index.php'>\n";
56     if( $slicename ) echo "<font color=red>'$slicename' is not a valid slice name.</font>\n";
57     echo "<p><label for='testinput'>Enter Slice Name: </label>\n
58           <input type='text' id='testinput' name='slicename' size=40 value='' />\n
59           <input type=submit value='Select Slice' />\n
60           </div>\n
61           <br />\n";
62      
63     // get site info
64     $site_info= $api->GetSites( NULL, array( "site_id", "name", "peer_id" ) );
65     sort_sites( $site_info );
66
67     // Thierry -- try to select only one entry
68     // xxx still not right if _person in several sites, but that is good enough
69     //if( $site['site_id'] == $_POST['site_id'] || in_array( $site['site_id'], $_person['site_ids'] ) )
70     if ($_POST['site_id'])
71       $selected_site_id = $_POST['site_id'];
72     else if ($_GET['site_id']) 
73       $selected_site_id = $_GET['site_id'];
74     else
75       $selected_site_id = $_person['site_ids'][0];
76
77     echo "Select a site to view slices from: &nbsp;";
78     echo "<select name='site_id' onChange='submit()'>\n";
79
80     foreach( $site_info as $site ) {
81       echo "<option value=". $site['site_id'];
82       if ( $site['site_id'] == $selected_site_id)
83           echo " selected";
84       if ( $site['peer_id'] ) 
85         echo " class='plc-foreign'";
86       echo ">". $site['name'] ."</option>\n";
87       
88     }
89
90     echo "</select>\n";
91   
92   }
93
94   if( $_POST['site_id'] ) {
95     $selection="Site";
96     $site= array( intval( $_POST['site_id'] ) );
97   } elseif( $_GET['site_id'] ) {
98     $selection="Site";
99     $site= array( intval( $_GET['site_id'] ) );
100   } else {
101     $selection="Person";
102     $site= $_person['site_ids'];
103   }
104   
105   // get site's slices
106   $site_info= $api->GetSites( $site, array( "slice_ids","name" ) );
107
108   if ( $selection == "Site" ) 
109     drupal_set_title ("Slices for site " . $site_info[0]['name']);
110   else
111     drupal_set_title ("Slices for " . $_person['email'] . "'s sites");
112
113   // make an array of all slices
114   foreach( $site_info as $site ) {
115     foreach( $site['slice_ids'] as $slice_id ) {
116       $slice_ids[]= $slice_id;
117     }
118     
119   }
120   
121   if (empty ($slice_ids)) {
122     echo "<p><strong>No slice found, or all are expired.</strong>";
123   } else {
124   
125     $slice_info= $api->GetSlices( $slice_ids, array( "slice_id", "name", "site_id", "person_ids", "expires", "peer_id" ) );
126     //print '<pre>'; print_r( $api->trace() ) ; print '</pre>';
127
128     if ( ! $slice_info) {
129       echo "<p><strong>No Slices on site, or all are expired.</strong>\n";
130     } else  {
131       echo "<table class='list_set' border=0 cellpadding=2>\n";
132       echo "<caption class='list_set'>Slice list</caption>\n";
133       echo "<thead><tr class='list_set'><th class='list_set'>Slice Name</th>";
134       echo "<th class='list_set'>Users</th>";
135       echo "<th class='list_set'>Expiration</th></tr>";
136       echo "</thead><tbody>\n";
137       
138       // create a list of person_ids
139       $person_ids = array();
140       foreach( $slice_info as $slice ) {
141           if ( !empty($slice['person_ids']) )
142             $person_ids = array_merge($person_ids, $slice['person_ids']);
143         }
144
145       // create an associative array of persons with person_id as the key
146       $person_list = $api->GetPersons( $person_ids, array("person_id", "email") ); 
147       $persons = array();
148       foreach( $person_list as $person)
149         {
150           $persons[$person['person_id']] = $person;
151         }
152       
153       foreach( $slice_info as $slice ) {
154         $slice_id= $slice['slice_id'];
155         $slice_name= $slice['name'];
156         $slice_expires= date( "M j, Y", $slice['expires'] );
157         $peer_id = $slice['peer_id'];
158       
159         $extraclass="";
160         if ( $peer_id ) 
161           $extraclass="plc-foreign";
162       
163         echo "<tr class='list_set $extraclass'><td><a href='/db/slices/index.php?id=$slice_id'>$slice_name</a></td><td class='list_set'>";
164       
165         if( !empty( $slice['person_ids'] ) ) {
166           foreach( $slice['person_ids'] as $person_id ) {
167             $person = $persons[$person_id];
168             $id= $person['person_id'];
169             $email= $person['email'];
170             echo "<a href='../persons/index.php?id=$id'>$email</a><br />\n";
171           }
172         } else {
173           echo "None";
174         }
175       
176         echo "</td><td class='list_set'>$slice_expires</td></tr>\n";
177     
178       }
179     
180       echo "</tbody></table>\n";
181
182     }
183   }
184   
185
186   echo "</form>\n";
187
188   echo "<script type=\"text/javascript\">
189 var options = {
190         script:\"/planetlab/slices/test.php?\",
191         varname:\"input\",
192         minchars:1
193 };
194 var as = new AutoSuggest('testinput', options);
195 </script>\n";
196
197 }
198 // if nothing else then show slice info
199 else {
200   $slice_id= intval( $_GET['id'] );
201
202   // GetSlices API call
203   $slice_info= $api->GetSlices( array( $slice_id ) );
204
205   if( empty( $slice_info ) ) {
206     header( "location: index.php" );
207     exit();
208   }
209
210   // pull all slice info to vars
211   $instantiation= $slice_info[0]['instantiation'];
212   $name= $slice_info[0]['name'];
213   $url= $slice_info[0]['url'];
214   $expires= date( "M j, Y", $slice_info[0]['expires'] );
215   $site_id= $slice_info[0]['site_id'];
216   $description= $slice_info[0]['description'];
217   $max_nodes= $slice_info[0]['max_nodes'];
218   $node_ids=$slice_info[0]['node_ids'];
219   $person_ids=$slice_info[0]['node_ids'];
220
221   // get peer id
222   $peer_id= $slice_info[0]['peer_id'];
223
224   $person_ids= $slice_info[0]['person_ids'];
225   $node_ids= $slice_info[0]['node_ids'];
226   $slice_tag_ids= $slice_info[0]['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_info[0]['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           echo plc_delete_link_button ('tag_action.php?rem_id=' . $tag['slice_tag_id'],
411                                        $label);
412           echo "</td>";
413         }
414         if( $is_admin || ($is_pi && $is_in_slice) ) {
415           printf ("<td><a href='tags.php?type=slice?id=%s'>%s</a></td>",
416                   $tag['slice_tag_id'],$tag['tagname']);
417         } else {
418           printf("<td>%s</td>",$tag['tagname']);
419         }
420         printf("<td align=center>%s</td><td>%s</td>",
421                $tag['value'],$tag['description']);
422         echo "</tr>";
423       }
424     }
425
426     
427     echo "</tbody></table>\n";
428
429   }
430   if( $is_admin || ($is_pi && $is_in_slice) )
431     echo "<a href='tags.php?type=slice&add=$slice_id'>Add a Slice Tag</a>\n";    
432
433
434
435   // sliver tags
436   if( $sliver_tag ) {
437     echo "<table cellpadding=3><caption class='list_set'>Sliver Tags</caption>";
438     echo "<thead><tr>";
439     if( $is_admin )
440       echo "<th></th>";
441     echo "<th>Tag</th><th>Value</th><th>Description</th><th>Node</th>";
442     echo "</tr></thead><tbody>\n";
443
444     foreach( $tags as $tag ) {
445       $nodename=$new_sliver_node_info[$tag['node_id']]['hostname'];
446       // consider only sliver tags at this stage
447       if( !empty( $tag['node_id'] ) ) {
448         echo("<tr>");
449         if( $is_admin ) {
450           echo("<td>");
451           $label=sprintf("\\n [ %s = %s ] \\n from %s \\n on node %s",
452                          $tag['tagname'],$tag['value'],$name,$nodename);
453           echo plc_delete_link_label('/db/nodes/sliver_action.php?rem_id=' . $tag['slice_tag_id'], 
454                                      $label);
455           echo "</td>";
456         }
457         if( $is_admin ) {
458           printf("<td><a href='tags.php?type=slice&id=%s'>%s</a></td>",$tag['slice_tag_id'],$tag['tagname']);
459         } else {
460           printf("<td>%s</td>",$tag['tagname']);
461         }
462         printf("<td align=center>%s</td><td>%s</td><td><a href='/db/nodes/index.php?id=%s'>%s</a></td>",
463                $tag['value'],$tag['description'],$tag['node_id'],$nodename);
464         
465         echo "</tr>";
466       }
467     }
468
469     echo "</tbody></table>\n";
470     
471   }
472   
473   echo "<br /><hr />\n";
474   
475   if( $pis && !$peer_id ) {
476     // site contacts
477     echo "<h5>Contacts</h5>\n";
478                 
479     $pi_rows= count( $pis );
480     $tech_rows= count( $techs );
481     $table_row= 0;
482         
483     echo "<table cellpadding=2><tbody>";
484     if( $pis ) {
485       echo "<tr><td rowspan=$pi_rows><strong>PI's:</strong> &nbsp; </td>\n";
486       
487       foreach( $pis as $pi ) {
488         if( $table_row != 0 )
489           echo "<tr>";
490         printf("<td><a href='/db/persons/index.php?id=%s'>%s %s</td><td><a href='mailto:%s'>%s</a></td></tr>\n",
491                $pi['person_id'],$pi['first_name'],$pi['last_name'],$pi['email'],$pi['email']);
492         $table_row++;
493       }
494       
495     }
496     
497     echo "</table>\n<br /><hr />\n";
498     
499   }
500   
501   
502   echo "<p><a href='index.php'>Back to slice list</a></div>\n";
503  }
504
505 // Print footer
506 include 'plc_footer.php';
507
508 ?>