initial import from onelab svn codebase
[plewww.git] / planetlab / slices / index.php
1 <?php
2
3 // $Id: index.php 970 2007-11-07 17:18:23Z amine $
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/includes/js/bsn.Ajax.js"></script>
51     <script type="text/javascript" src="/planetlab/includes/js/bsn.DOM.js"></script>
52     <script type="text/javascript" src="/planetlab/includes/js/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", "state", "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_state= $slice['state'];
157         $slice_expires= date( "M j, Y", $slice['expires'] );
158         $peer_id = $slice['peer_id'];
159       
160         $extraclass="";
161         if ( $peer_id ) 
162           $extraclass="plc-foreign";
163       
164         echo "<tr class='list_set $extraclass'><td><a href='/db/slices/index.php?id=$slice_id'>$slice_name</a></td><td class='list_set'>";
165       
166         if( !empty( $slice['person_ids'] ) ) {
167           foreach( $slice['person_ids'] as $person_id ) {
168             $person = $persons[$person_id];
169             $id= $person['person_id'];
170             $email= $person['email'];
171             echo "<a href='../persons/index.php?id=$id'>$email</a><br />\n";
172           }
173         } else {
174           echo "None";
175         }
176       
177         echo "</td><td class='list_set'>$slice_expires</td></tr>\n";
178     
179       }
180     
181       echo "</tbody></table>\n";
182
183     }
184   }
185   
186
187   echo "</form>\n";
188
189   echo "<script type=\"text/javascript\">
190 var options = {
191         script:\"/planetlab/slices/test.php?\",
192         varname:\"input\",
193         minchars:1
194 };
195 var as = new AutoSuggest('testinput', options);
196 </script>\n";
197
198 }
199 // if nothing else then show slice info
200 else {
201   $slice_id= intval( $_GET['id'] );
202
203   // GetSlices API call
204   $slice_info= $api->GetSlices( array( $slice_id ) );
205
206   if( empty( $slice_info ) ) {
207     header( "location: index.php" );
208     exit();
209   }
210
211   // pull all slice info to vars
212   $instantiation= $slice_info[0]['instantiation'];
213   $name= $slice_info[0]['name'];
214   $url= $slice_info[0]['url'];
215   $expires= date( "M j, Y", $slice_info[0]['expires'] );
216   $site_id= $slice_info[0]['site_id'];
217   $description= $slice_info[0]['description'];
218   $max_nodes= $slice_info[0]['max_nodes'];
219   $node_ids=$slice_info[0]['node_ids'];
220   $person_ids=$slice_info[0]['node_ids'];
221
222   // get peer id
223   $peer_id= $slice_info[0]['peer_id'];
224
225   $person_ids= $slice_info[0]['person_ids'];
226   $node_ids= $slice_info[0]['node_ids'];
227   $slice_attribute_ids= $slice_info[0]['slice_attribute_ids'];
228
229
230   // node info
231 // looks unused
232 //  if( !empty( $node_ids ) )
233 //    $nodes= $api->GetNodes( $node_ids, array( "node_id", "hostname" ) );
234
235   // site info
236   $site_info= $api->GetSites( array( $site_id ), array( "site_id", "name", "person_ids" ) );
237
238   // gets all persons from site_id
239     // person info
240   if( !empty( $person_ids ) ) 
241     $persons= $api->GetPersons( $site_info[0]['person_ids'] , array( "person_id", "role_ids", "first_name", "last_name", "email" ) );
242
243   if( $persons ) {
244     // gets site contacts pis stores in dict
245     foreach( $persons as $person )
246       if( in_array( "20", $person['role_ids'] ) ) {
247         $pis[]= array( "email" => $person['email'], "first_name" => $person['first_name'], "last_name" => $person['last_name'], "person_id" => $person['person_id'] );
248         
249       }
250     if ($pis) {
251       sort_persons( $pis );
252     }
253   }
254
255   // slice attribute info
256   if( !empty( $slice_attribute_ids ) )
257     $slice_attibs= $api->GetSliceAttributes( $slice_attribute_ids, 
258                                              array( "slice_attribute_id", "attribute_type_id", "value", "description", "min_role_id", "node_id" ) );
259
260   // gets attrib type info and combines it to form all attrib info array
261   if( $slice_attibs ) {
262     foreach( $slice_attibs as $slice_attib ) {
263       $attrib_type= $api->GetSliceAttributeTypes( array( $slice_attib['attribute_type_id'] ), 
264                                                   array( "attribute_type_id", "name", "description" ) );
265       
266       $attributes[]= array( "slice_attribute_id" => $slice_attib['slice_attribute_id'], 
267                             "attribute_type_id" => $slice_attib['attribute_type_id'], 
268                             "name" => $attrib_type[0]['name'], 
269                             "value" => $slice_attib['value'], 
270                             "description" => $slice_attib['description'], 
271                             "min_role_id" => $slice_attib['min_role_id'], 
272                             "node_id" => $slice_attib['node_id'] );
273     }
274
275   }
276
277   drupal_set_title("Slice details for " . $name);
278   // start form
279
280   if( $peer_id ) {
281     echo "<div class='plc-foreign'>\n";
282   }
283
284   // basic slice menu
285   if( ! $peer_id ) {
286
287     $actions= array( ''=>'Choose Action' );
288     
289     if( in_array( 10, $_roles ) 
290         || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) 
291         || in_array( $slice_id, $_person['slice_ids'] ) ) {
292       $actions['renew']= "Renew $name";
293       $actions['nodes']= "Manage Nodes";
294     }
295     if ( in_array( 10, $_roles )
296          || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) ) {
297       $actions['users']= "Manage Users";
298       $actions['delete']= "Delete $name";
299     }
300     
301     echo "<table><tr><td>\n";
302     if (in_array( 10, $_roles )) {
303       echo plc_event_button("slices","slice",$slice_id);
304       echo "</td><td>";
305     }
306     echo plc_comon_button("slice_id",$slice_id);
307     echo "</td><td>\n";
308
309     echo "<form action='/db/slices/slice_action.php' method='post'>\n";
310     echo "<input type=hidden name=slice_id value=$slice_id>\n";
311
312     echo "<select name='actions' onChange=\"submit();\">\n";
313     foreach( $actions as $key => $val ) {
314       echo "<option value='$key'";
315       
316       if( $key == $_POST['actions'] )
317         echo " selected";
318       
319       echo ">$val\n";
320     }
321     
322     echo "</select><br />\n";
323     echo "</form>\n";
324
325     echo "</td></tr></table>\n";
326   }
327
328   echo "<table cellpadding=3><tbody>\n
329         <tr><th>Slice Name: </th><td> $name </td></tr>\n
330         <tr><th>Description: </th><td> $description </td></tr>\n
331         <tr><th>URL: </th><td> <a href='$url'>$url</a> </td></tr>\n";
332         
333   if( gmmktime() > $slice_info[0]['expires'] ) { 
334     $class1= ' style="color:red;"'; 
335     $msg1= '(slice is expired)'; 
336   }
337   echo "<tr><th$class1>Expiration: </th><td$class1> $expires &nbsp; $msg1</td></tr>\n";
338   echo "<tr><th>Instantiation: </th><td><select name='instantiation' onChange=\"submit();\"\n";
339   echo "<option value='delegated'"; 
340   if( $instantiation == 'delegated' ) echo " selected"; 
341   echo ">delegated</option>";
342   echo "<option value='plc-instantiated'"; 
343   if( $instantiation == 'plc-instantiated' ) echo " selected"; 
344   echo ">plc-instantiated</option>";
345   echo "<option value='not-instantiated'"; 
346   if( $instantiation == 'not-instantiated' ) echo " selected"; 
347   echo ">not-instantiated</option>";
348   echo "</select>"; 
349
350   echo "</td></tr>\n";
351   echo "<tr><th>Site: </th><td> <a href='/db/sites/index.php?id=$site_id'>". $site_info[0]['name'] ."</a></td></tr>\n";
352   $href="'/db/nodes/index.php?slice_id=" . $slice_id . "'";
353   printf ("<tr><th> <a href=%s># Nodes</a></th><td><a href=%s>Total %d nodes</a></td>\n",$href,$href,count($node_ids));
354   $href="'/db/persons/index.php?slice_id=" . $slice_id . "'";
355   printf ("<tr><th> <a href=%s># Users</a></th><td><a href=%s>Total %d users</a></td>\n",$href,$href,count($person_ids));
356   echo "</tbody></table>\n";
357
358   if ( (!$class1) && in_array( $slice_id, $_person['slice_ids'] ) && (! $peer_id) ) 
359     echo "<p><a href='update_slice.php?id=$slice_id'>Update Information</a>\n";
360
361   echo "<br /><hr />\n";
362
363
364   // slice attributes
365   if( $attributes ) {
366
367     // builds 2 arrays, one for attribs,one for slivers
368     foreach( $attributes as $attribute ) {
369       if( empty( $attribute['node_id'] ) ) {
370         $slice_attrib[]= $attribute;
371       }
372       else {
373         $sliver_attrib[]= $attribute;
374         $sliver_nodes[]= $attribute['node_id'];
375       }
376     }
377   }
378
379   // Get node info for those slivers
380   $sliver_node_info= $api->GetNodes( $sliver_nodes, array( "node_id", "hostname" ) );
381
382   if( $sliver_node_info ) {
383     foreach( $sliver_node_info as $sliv_node ) {
384       $new_sliver_node_info[$sliv_node['node_id']]= $sliv_node;
385     }
386   }
387
388  if( $peer_id ) {
389    echo "<br /></div>\n";
390   }
391  
392   // slice attributes
393   $is_admin=in_array( 10, $_roles );
394   $is_in_slice=in_array( $slice_id, $_person['slice_ids'] );
395   $is_pi=in_array( 20, $_roles );
396   if( $slice_attrib ) {
397     echo "<table cellpadding=3><caption class='list_set'>Slice Attributes</caption>";
398     echo "<thead><tr>";
399     if( $is_admin )
400       echo "<th></th>";
401     echo "<th>Attribute</th><th>Value</th><th>Description</th>";
402     echo "</tr></thead><tbody>\n";
403
404     foreach( $attributes as $attribute ) {
405       // ignore sliver attributes at this stage
406       if( empty( $attribute['node_id'] ) ) {
407         echo("<tr>");
408         if( $is_admin ) {
409           printf("<td>");
410           sprintf($label,"\\n [ %s = %s] \\n from %s",$attribute['name'],$attribute['value'],$name);
411           echo plc_delete_link_button ('attrib_action.php?rem_id=' . $attribute['slice_attribute_id'],
412                                        $label);
413           echo "</td>";
414         }
415         if( $is_admin || ($is_pi && $is_in_slice) ) {
416           printf ("<td><a href='attributes.php?id=%s'>%s</a></td>",
417                   $attribute['slice_attribute_id'],$attribute['name']);
418         } else {
419           printf("<td>%s</td>",$attribute['name']);
420         }
421         printf("<td align=center>%s</td><td>%s</td>",
422                $attribute['value'],$attribute['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='attributes.php?add=$slice_id'>Add a Slice Attribute\n";    
433
434
435
436   // sliver attributes
437   if( $sliver_attrib ) {
438     echo "<table cellpadding=3><caption class='list_set'>Sliver Attributes</caption>";
439     echo "<thead><tr>";
440     if( $is_admin )
441       echo "<th></th>";
442     echo "<th>Attribute</th><th>Value</th><th>Description</th><th>Node</th>";
443     echo "</tr></thead><tbody>\n";
444
445     foreach( $attributes as $attribute ) {
446       $nodename=$new_sliver_node_info[$attribute['node_id']]['hostname'];
447       // consider only sliver attributes at this stage
448       if( !empty( $attribute['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                          $attribute['name'],$attribute['value'],$name,$nodename);
454           echo plc_delete_link_label('/db/nodes/sliver_action.php?rem_id=' . $attribute['slice_attribute_id'], 
455                                      $label);
456           echo "</td>";
457         }
458         if( $is_admin ) {
459           printf("<td><a href='attributes.php?id=%s'>%s</a></td>",$attribute['slice_attribute_id'],$attribute['name']);
460         } else {
461           printf("<td>%s</td>",$attribute['name']);
462         }
463         printf("<td align=center>%s</td><td>%s</td><td><a href='/db/nodes/index.php?id=%s'>%s</a></td>",
464                $attribute['value'],$attribute['description'],$attribute['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 ?>