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