ckp
[plewww.git] / planetlab / common / actions.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 //print header
11 require_once 'plc_drupal.php';
12
13 // Common functions
14 require_once 'plc_functions.php';
15
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
18 // interface :
19 // (*) use POST 
20 // (*) set 'action' to one of the following
21 //////////////////////////////////////// persons
22 $known_actions []= "add-person-to-site";
23 //      expects:        person_id & site_id
24 $known_actions []= "remove-person-from-sites";
25 //      expects:        person_id & site_ids
26 $known_actions []= "remove-roles-from-person";
27 //      expects:        person_id & role_ids
28 $known_actions []= "add-role-to-person";
29 //      expects:        role_person_id & id
30 $known_actions []= "enable-person";
31 //      expects:        person_id
32 $known_actions []= "disable-person";
33 //      expects:        person_id
34 $known_actions []= "become-person";
35 //      expects:        person_id
36 $known_actions []= "delete-person";
37 //      expects:        person_id
38 $known_actions []= "delete-keys";
39 //      expects:        key_ids & person_id (for redirecting to the person's page)
40 $known_actions []= "upload-key";
41 //      expects:        person_id & $_FILES['key']
42 $known_actions []= "update-person";
43 //      expects:        person_id & first_name last_name title email phone url bio + [password1 password2]
44
45 //////////////////////////////////////// nodes
46 $known_actions []= "node-boot-state";   
47 //      expects:        node_id boot_state
48 $known_actions []= "delete-node";       
49 //      expects:        node_id
50 $known_actions []= "update-node";       
51 //      expects:        node_id, hostname, model
52
53 //////////////////////////////////////// interfaces
54 $known_actions []= "delete-interfaces"; 
55 //      expects:        interface_ids
56 $known_actions []="add-interface";
57 //      expects:        node_id & interface details
58 $known_actions []="update-interface";
59 //      expects:        interface_id & interface details
60
61 //////////////////////////////////////// sites
62 $known_actions []= "delete-site";       
63 //      expects:        site_id
64 $known_actions []= "expire-all-slices-in-site";
65 //      expects:        slice_ids
66 $known_actions []= "update-site";
67 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
68
69 //////////////////////////////////////// slices
70 $known_actions []= "renew-slice";
71 //      expects:        slice_id & expires
72
73 //////////////////////////////////////// tag types
74 $known_actions []= "update-tag-type";
75 //      expects:        tag_type_id & name & description & category & min_role_id  
76 $known_actions []= "add-tag-type";
77 //      expects:        tag_type_id & tagname & description & category & min_role_id  
78 $known_actions []= "delete-tag-types";
79 //      expects:        tag_type_ids
80
81 //////////////////////////////////////// tags
82 $known_actions []= "set-tag-on-node";
83 //      expects:        node_id tagname value
84 $known_actions []= "set-tag-on-interface";
85 //      expects:        interface_id tagname value
86 $known_actions []= "delete-node-tags";
87 //      expects:        node_id & node_tag_ids
88 $known_actions []= "delete-interface-tags";
89 //      expects:        interface_id & interface_tag_ids
90
91 ////////////////////////////////////////////////////////////
92 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
93                            'broadcast', 'netmask', 'dns1', 'dns2', 
94                            'hostname', 'mac', 'bwlimit' );
95
96 //////////////////////////////
97 // sometimes we don't set 'action', but use the submit button name instead
98 // so if 'action' not set, see if $_POST has one of the actions as a key
99 if ($_POST['action']) 
100   $action=$_POST['action'];
101 else 
102   foreach ($known_actions as $known_action) 
103     if ($_POST[$known_action]) {
104       $action=$known_action;
105       break;
106     }
107
108 //uncomment for debugging incoming data
109 //$action='debug';
110
111 $person_id = $_POST['person_id'];       // usually needed
112
113 if ( ! $action ) {
114   drupal_set_message ("actions.php: action not set or not in known_actions");
115   plc_debug('POST',$_POST);
116   return;
117  }
118
119 switch ($action) {
120
121  case 'add-person-to-site': {
122    $site_id = $_POST['site_id'];
123    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
124    plc_redirect (l_person($person_id));
125  }
126
127  case 'remove-person-from-sites': {
128    $site_ids = $_POST['site_ids'];
129    if ( ! $site_ids) {
130      drupal_set_message("action=$action - No site selected");
131      return;
132    }
133    foreach ( $site_ids as $site_id ) {
134      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
135    }
136    plc_redirect (l_person($person_id));
137  }
138
139  case 'remove-roles-from-person' : {
140    $role_ids=$_POST['role_ids'];
141    if ( ! $role_ids) {
142      drupal_set_message("action=$action - No role selected");
143      return;
144    }
145    foreach( $role_ids as $role_id)  {
146      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
147    }
148    plc_redirect (l_person($person_id));
149  }
150      
151  case 'add-role-to-person' : {
152    $role_id=$_POST['role_id'];
153    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
154    plc_redirect (l_person($person_id));
155  }
156
157  case 'enable-person' : {
158    $fields = array( "enabled"=>true );
159    $api->UpdatePerson( intval( $person_id ), $fields );
160    plc_redirect (l_person($person_id));
161  }
162
163  case 'disable-person' : {
164    $fields = array( "enabled"=>false );
165    $api->UpdatePerson( intval( $person_id ), $fields );
166    plc_redirect (l_person($person_id));
167  }
168
169  case 'become-person' : {
170    $plc->BecomePerson (intval($person_id));
171    plc_redirect (l_persons());
172  }
173
174  case 'delete-person' : {
175   $api->DeletePerson( intval( $person_id ) );
176    plc_redirect (l_persons());
177  }
178
179  case 'delete-keys' : {
180    $key_ids=$_POST['key_ids'];
181    if ( ! $key_ids) {
182      drupal_set_message("action=$action - No key selected");
183      return;
184    }
185    $success=true;
186    $counter=0;
187    foreach( $key_ids as $key_id ) {
188      if ($api->DeleteKey( intval( $key_id )) != 1) 
189        $success=false;
190      else
191        $counter++;
192    }
193    if ($success) 
194      drupal_set_message ("Deleted $counter key(s)");
195    else
196      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
197    plc_redirect(l_person($person_id));
198  }
199
200
201  case 'upload-key' : {
202    if ( ! isset( $_FILES['key'] ) ) {
203      drupal_set_message ("action=$action, no key file set");
204      return;
205    }
206    
207    $key_file= $_FILES['key']['tmp_name'];
208    if ( ! $key_file ) {
209      plc_error("Please select a valid SSH key file to upload");
210      return;
211    } 
212    $fp = fopen( $key_file, "r" );
213    $key = "";
214    if( ! $fp ) {
215      plc_error("Unable to open key file $key_file");
216      return;
217    }
218    // opened the key file, read the one line of contents
219    // The POST operation always creates a file even if the filename
220    // the user specified was garbage.  If there was some problem
221    // with the source file, we'll get a zero length read here.
222    $key = fread($fp, filesize($key_file));
223    fclose($fp);
224    
225    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
226    
227    if ( $key_id == 1) 
228      drupal_set_message ("New key added");
229    else
230      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
231    
232    plc_redirect(l_person($person_id));
233  }
234
235  case 'update-person': {
236    $person_id=$_POST['person_id'];
237    // attempt to update this person
238    $first_name= $_POST['first_name'];
239    $last_name= $_POST['last_name'];
240    $title= $_POST['title'];
241    $email= $_POST['email'];
242    $phone= $_POST['phone'];
243    $url= $_POST['url'];
244    $bio= str_replace("\r", "", $_POST['bio']);
245    $password1= $_POST['password1'];
246    $password2= $_POST['password2'];
247
248    if( $password1 != $password2 ) {
249      drupal_set_error ("The passwords do not match");
250      plc_redirect(l_person($person_id));
251   }
252
253    $fields= array();
254    $fields['first_name']= $first_name;
255    $fields['last_name']= $last_name;
256    $fields['title']= $title;
257    $fields['email']= $email;
258    $fields['phone']= $phone;
259    $fields['url']= $url;
260    $fields['bio']= $bio;
261                 
262    if ( $password1 != "" )
263      $fields['password']= $password1;
264     
265     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
266       drupal_set_message("$first_name $last_name updated");
267     else 
268       drupal_set_error ("Could not update person $person_id" . $api->error());
269
270     plc_redirect(l_person($person_id));
271     break;
272   }
273
274 //////////////////////////////////////////////////////////// nodes
275  case 'node-boot-state': {
276    $node_id=intval($_POST['node_id']);
277    $boot_state=$_POST['boot_state'];
278    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
279    if ($result==1) {
280      drupal_set_message("boot state updated");
281      plc_redirect (l_node($node_id));
282    } else {
283      drupal_set_error("Could not set boot_state '$boot_state'");
284    }
285    break;
286  }
287
288  case 'delete-node': {
289    $node_id=intval($_POST['node_id']);
290    $result=$api->DeleteNode( intval( $node_id ) );
291    if ($api==1) {
292      drupal_set_message("Node $node_id deleted");
293      plc_redirect (l_nodes());
294    } else {
295      drupal_set_error ("Could not delete node $node_id");
296    }
297    break;
298  }
299
300  case 'update-node': {
301    $hostname= $_POST['hostname'];
302    $model= $_POST['model'];
303
304    $fields= array( "hostname"=>$hostname, "model"=>$model );
305    $api->UpdateNode( intval( $node_id ), $fields );
306    $error= $api->error();
307
308    if( empty( $error ) ) {
309      drupal_set_message("Update node $hostname");
310      plc_redirect(l_node($node_id));
311    } else {
312      drupal_set_error($error);
313    }
314    break;
315  }
316
317 //////////////////////////////////////////////////////////// interfaces
318  case 'delete-interfaces' : {
319    $interface_ids=$_POST['interface_ids'];
320    if ( ! $interface_ids) {
321      drupal_set_message("action=$action - No interface selected");
322      return;
323    }
324    $success=true;
325    $counter=0;
326    foreach( $interface_ids as $interface_id ) {
327      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
328        $success=false;
329      else
330        $counter++;
331    }
332    if ($success) 
333      drupal_set_message ("Deleted $counter interface(s)");
334    else
335      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
336    plc_redirect(l_node($_POST['node_id']));
337  }
338
339  case 'add-interface': {
340    $node_id=$_POST['node_id'];
341    foreach ($interface_details as $field) {
342      $interface[$field]= $_POST[$field];
343      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
344        $interface[$field]= intval( $interface[$field] );
345      }
346    }
347    $result=$api->AddInterface( intval( $node_id ), $interface );
348    if ($result >0 ) 
349      drupal_set_message ("Interface $result added into node $node_id");
350    else
351      drupal_set_error ("Could not create interface");
352    plc_redirect (l_node($node_id));
353  }
354    
355  case 'update-interface': {
356    $interface_id=$_POST['interface_id'];
357    foreach ($interface_details as $field) {
358      $interface[$field]= $_POST[$field];
359      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
360        $interface[$field]= intval( $interface[$field] );
361      }
362    }
363    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
364    if ($result == 1 ) 
365      drupal_set_message ("Interface $interface_id updated");
366    else
367      drupal_set_error ("Could not update interface");
368    plc_redirect (l_interface($interface_id));
369  }
370    
371 //////////////////////////////////////////////////////////// sites
372  case 'delete-site': {
373    $site_id = intval($_POST['site_id']);
374    if ($api->DeleteSite($site_id) ==1) 
375      drupal_set_message ("Site $site_id deleted");
376    else
377      drupal_set_error("Failed to delete site $site_id");
378    plc_redirect (l_sites());
379    break;
380  }
381
382  case 'expire-all-slices-in-site': {
383    // xxx todo
384    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
385    return;
386
387    //// old code from sites/expire.php
388    $sites = $api->GetSites( array( intval( $site_id )));
389    $site=$sites[0];
390    // xxx why not 'now?'
391    $expiration= strtotime( $_POST['expires'] );
392    // loop through all slices for site
393    foreach ($site['slice_ids'] as $slice_id) {
394      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
395    }
396    // update site to not allow slice creation or renewal
397    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
398    plc_redirect (l_site($site_id));
399    break;
400  }
401
402  case 'update-site': {
403    $site_id=intval($_POST['site_id']);
404    $name= $_POST['name'];
405    $abbreviated_name= $_POST['abbreviated_name'];
406    $url= $_POST['url'];
407    $latitude= floatval($_POST['latitude']);
408    $longitude= floatval($_POST['longitude']);
409    //$max_slivers= $_POST['max_slivers'];
410    
411    $fields= array( "name" => $name, 
412                    "abbreviated_name" => $abbreviated_name, 
413                    "url" => $url, 
414                    "latitude" => floatval( $latitude ), 
415                    "longitude" => floatval( $longitude ));
416
417    if ($_POST['login_base']) 
418      $fields['login_base'] = $_POST['login_base'];
419    if ($_POST['max_slices']) 
420      $fields['max_slices'] = intval($_POST['max_slices']);
421    
422    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
423    if ($retcod == 1) 
424      drupal_set_message("Site $name updated");
425    else 
426      drupal_set_error ("Could not update site $site_id");
427      
428    plc_redirect(l_site($site_id));
429    break;
430  }
431
432 //////////////////////////////////////////////////////////// slices
433  case 'renew_slice': {
434    $slice_id = intval ($_POST['slice_id']);     
435    $expires = intval ($_POST['expires']);
436    // 8 weeks from now
437    // xxx
438    $now=date();
439    $WEEKS=8;
440    $MAX_FUTURE=$WEEKS*7*24*3600;
441    if ( ($expires-$now) > $MAX_FUTURE) {
442      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS from now");
443      plc_redirect(l_slice($slice_id));
444    }
445    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
446      drupal_set_message("Slice renewed");
447    else
448      drupal_set_error("Could not update slice $slice_id");
449    plc_redirect(l_slice($slice_id));
450    break;
451  }
452
453
454 //////////////////////////////////////////////////////////// tag types
455
456  case 'update-tag-type': {
457   // get post vars 
458    $tag_type_id= intval( $_POST['tag_type_id'] );
459    $tagname = $_POST['tagname'];
460    $min_role_id= intval( $_POST['min_role_id'] );
461    $description= $_POST['description'];  
462    $category= $_POST['category'];  
463   
464    // make tag_type_fields dict
465    $tag_type_fields= array( "min_role_id" => $min_role_id, 
466                             "tagname" => $tagname, 
467                             "description" => $description,
468                             "category" => $category,
469                             );
470
471    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
472      drupal_set_message ("Tag type $tagname updated");
473    else 
474      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
475    plc_redirect(l_tag($tag_type_id));
476  }
477
478  case 'add-tag-type': {
479   // get post vars 
480    $tagname = $_POST['tagname'];
481    $min_role_id= intval( $_POST['min_role_id'] );
482    $description= $_POST['description'];  
483    $category= $_POST['category'];  
484   
485    // make tag_type_fields dict
486    $tag_type_fields= array( "min_role_id" => $min_role_id, 
487                             "tagname" => $tagname, 
488                             "description" => $description,
489                             "category" => $category,
490                             );
491
492   // Add it!
493    $tag_type_id=$api->AddTagType( $tag_type_fields );
494    if ($tag_type_id > 0) 
495      drupal_set_message ("tag type $tag_type_id created");
496    else
497      drupal_set_error ("Could not create tag type $tagname");
498    plc_redirect( l_tags());
499  }
500
501  case 'delete-tag-types': {
502    $tag_type_ids = $_POST['tag_type_ids'];
503    if ( ! $tag_type_ids) {
504      drupal_set_message("action=$action - No tag selected");
505      return;
506    }
507    $success=true;
508    $counter=0;
509    foreach ($tag_type_ids as $tag_type_id) 
510      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
511        $success=false;
512      else
513        $counter++;
514    if ($success) 
515      drupal_set_message ("Deleted $counter tag(s)");
516    else
517      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
518    plc_redirect (l_tags());
519    break;
520  }
521
522 //////////////////////////////////////// tags   
523  case 'set-tag-on-node': 
524  case 'set-tag-on-interface': {
525    
526    $node_mode = false;
527    if ($action == 'set-tag-on-node') $node_mode=true;
528
529    if ($node_mode)
530      $node_id = intval($_POST['node_id']);
531    else 
532      $interface_id=intval($_POST['interface_id']);
533    $tag_type_id = intval($_POST['tag_type_id']);
534    $value = $_POST['value'];
535
536    $tag_types=$api->GetTagTypes(array($tag_type_id));
537    if (count ($tag_types) != 1) {
538      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
539    } else {
540      if ($node_mode) 
541        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
542      else
543        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
544      if ( count ($tags) == 1) {
545        $tag=$tags[0];
546        if ($node_mode) {
547          $tag_id=$tag['node_tag_id'];
548          $result=$api->UpdateNodeTag($tag_id,$value);
549        } else {
550          $tag_id=$tag['interface_tag_id'];
551          $result=$api->UpdateInterfaceTag($tag_id,$value);
552        }
553        if ($result == 1) 
554          drupal_set_message ("Updated tag, new value = $value");
555        else
556          drupal_set_error ("Could not update tag");
557      } else {
558        if ($node_mode)
559          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
560        else
561          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
562        if ($tag_id) 
563          drupal_set_message ("Created tag, new value = $value");
564        else
565          drupal_set_error ("Could not create tag");
566      }
567    }
568    
569    if ($node_mode)
570      plc_redirect (l_node($node_id));
571    else
572      plc_redirect (l_interface($interface_id));
573  }
574
575  case 'delete-node-tags' : 
576  case 'delete-interface-tags' : {
577
578    $node_mode = false;
579    if ($action == 'delete-node-tags') $node_mode=true;
580
581    if ($node_mode)
582      $tag_ids=$_POST['node_tag_ids'];
583    else
584      $tag_ids=$_POST['interface_tag_ids'];
585
586    if ( ! $tag_ids) {
587      drupal_set_message("action=$action - No tag selected");
588      return;
589    }
590    $success=true;
591    $counter=0;
592    foreach( $tag_ids as $tag_id ) {
593      if ($node_mode)
594        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
595      else
596        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
597      if ($retcod != 1) 
598        $success=false;
599      else
600        $counter++;
601    }
602    if ($success) 
603      drupal_set_message ("Deleted $counter tag(s)");
604    else
605      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
606    if ($node_mode)
607      plc_redirect(l_node($_POST['node_id']));
608    else
609      plc_redirect(l_interface($_POST['interface_id']));
610  }
611
612
613 ////////////////////////////////////////
614
615  case 'debug': {
616    plc_debug('GET',$_GET);
617    plc_debug('POST',$_POST);
618    plc_debug('FILES',$_FILES);
619    return;
620  }
621
622  default: {
623    plc_error ("Unknown action $action in actions.php");
624    return;
625  }
626
627  }
628
629 ?>