X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetlab%2Fcommon%2Factions.php;h=09b186643b5c388f0a3c3b37037806eafb373535;hb=fd9b5dccd591a5bfe48daf1da6bbb52164fa93c9;hp=66d6c0ae5ac63d853079947211f5b173badf474a;hpb=c410670fb3c8027ceb0f111e1a3242335b4fd2f5;p=plewww.git diff --git a/planetlab/common/actions.php b/planetlab/common/actions.php index 66d6c0a..09b1866 100644 --- a/planetlab/common/actions.php +++ b/planetlab/common/actions.php @@ -5,7 +5,8 @@ require_once 'plc_login.php'; // Get session and API handles require_once 'plc_session.php'; -global $plc, $api; +require_once 'plc_api.php'; +global $plc, $api, $adm; //print header require_once 'plc_drupal.php'; @@ -48,9 +49,11 @@ $known_actions []= "node-boot-state"; $known_actions []= "delete-node"; // expects: node_id $known_actions []= "update-node"; -// expects: node_id, hostname, model +// expects: node_id, hostname, model, node_type $known_actions []= "attach-pcu"; // expects: node_id, pcu_id, port (pcu_id <0 means detach) +$known_actions []= "reboot-node-with-pcu"; +// expects: node_id //////////////////////////////////////// interfaces $known_actions []= "delete-interfaces"; @@ -85,6 +88,9 @@ $known_actions []= 'remove-nodes-from-slice'; // expects: slice_id & node_ids $known_actions []= 'add-nodes-in-slice'; // expects: slice_id & node_ids +$known_actions []= 'update-initscripts'; +// expects: slice_id & name & previous-initscript & previous-initscript-code +// & initscript & initscript-code $known_actions []= 'delete-slice-tags'; // expects: slice_tag_id $known_actions []= 'add-slice-tag'; @@ -97,14 +103,26 @@ $known_actions []= "add-tag-type"; // expects: tag_type_id & tagname & description & category & min_role_id $known_actions []= "delete-tag-types"; // expects: tag_type_ids +$known_actions []= "remove-roles-from-tag-type"; +// expects: tag_type_id & role_ids +$known_actions []= "add-role-to-tag-type"; +// expects: tag_type_id_id & id //////////////////////////////////////// tags $known_actions []= "set-tag-on-node"; // expects: node_id tagname value +$known_actions []= "set-tag-on-site"; +// expects: site_id tagname value +$known_actions []= "set-tag-on-person"; +// expects: person_id tagname value $known_actions []= "set-tag-on-interface"; // expects: interface_id tagname value $known_actions []= "delete-node-tags"; // expects: node_id & node_tag_ids +$known_actions []= "delete-site-tags"; +// expects: site_id & site_tag_ids +$known_actions []= "delete-person-tags"; +// expects: person_id & person_tag_ids $known_actions []= "delete-interface-tags"; // expects: interface_id & interface_tag_ids @@ -116,6 +134,12 @@ $known_actions []= "add-nodegroup"; $known_actions []= 'delete-nodegroups'; // expects nodegroup_ids +//////////////////////////////////////// leases +$known_actions []= "manage-leases"; +// expects as 'actions' a list of 'action' of the form +// either [ 'add-leases', [nodenames], slicename, t_from, t_until ] +// or [ 'delete-leases', lease_id ] + //////////////////////////////////////////////////////////// $interface_details= array ('method','type', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1', 'dns2', @@ -333,8 +357,9 @@ switch ($action) { $node_id=intval($_POST['node_id']); $hostname= $_POST['hostname']; $model= $_POST['model']; + $node_type= $_POST['node_type']; - $fields= array( "hostname"=>$hostname, "model"=>$model ); + $fields= array( "hostname"=>$hostname, "model"=>$model, "node_type"=>$node_type ); $api->UpdateNode( $node_id, $fields ); $error= $api->error(); @@ -374,6 +399,24 @@ switch ($action) { plc_redirect(l_node($node_id)); break; } + + case 'reboot-node-with-pcu': { + $node_id=intval($_POST['node_id']); + $hostname= $_POST['hostname']; + $test = $_POST['test']; + settype($test, "boolean"); + + $ret = $api->RebootNodeWithPCU( $node_id, $test ); + $error= $api->error(); + + if( empty( $error ) ) { + drupal_set_message("Reboot node $hostname: $ret"); + plc_redirect(l_node($node_id)); + } else { + drupal_set_error($error); + } + break; + } //////////////////////////////////////////////////////////// interfaces @@ -397,32 +440,46 @@ switch ($action) { drupal_set_error ("Could not delete all selected interfaces, only $counter were removed"); plc_redirect(l_node($_POST['node_id'])); } + case 'new-interface': { plc_redirect(l_interface_add($_POST['node_id'])); } + case 'add-interface': { - $node_id=$_POST['node_id']; + $node_id=intval($_POST['node_id']); foreach ($interface_details as $field) { $interface[$field]= $_POST[$field]; + // these must be integers if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) { - $interface[$field]= intval( $interface[$field] ); + if ( empty ($interface[$field]) ) + unset ($interface[$field]); + else + $interface[$field]= intval( $interface[$field] ); } } - $interface_id =$api->AddInterface( intval( $node_id ), $interface ); - if ($interface_id >0 ) { - $api->begin(); - $api->AddInterfaceTag($interface_id,"alias",strval($interface_id)); - $api->AddInterfaceTag($interface_id,"ifname","eth0"); - list($id1, $id2) = $api->commit(); - if ( $id1 > 0 && $id2 > 0 ) { - drupal_set_message ("Interface $interface_id added into node $node_id"); - } else { - drupal_set_error ("Could not add interface tags to interface $interface_id"); - } - } else { + $interface_id =$api->AddInterface( $node_id , $interface ); + if ($interface_id <= 0 ) { drupal_set_error ("Could not create interface"); + drupal_set_error ($api->error()); + } else { + $ip=$interface['ip']; + drupal_set_message ("Interface $ip added into node $node_id"); + if ($_POST['is-virtual']) { + $ifname=$_POST['ifname']; + if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0) + drupal_set_error ("Could not set tag 'ifname'=$ifname"); + else + drupal_set_message ("Set tag 'ifname'=$ifname"); + $alias=$_POST['alias']; + // deafult to interface_id + if ( ! $alias ) $alias=strval($interface_id); + if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0) + drupal_set_error ("Could not set tag 'alias'=$alias"); + else + drupal_set_message ("Set tag 'alias'=$alias"); + } } - plc_redirect (l_node($node_id)); + plc_redirect (l_node_interfaces($node_id)); } case 'update-interface': { @@ -441,7 +498,7 @@ switch ($action) { if ($result == 1 ) drupal_set_message ("Interface $interface_id updated"); else - drupal_set_error ("Could not update interface"); + drupal_set_error ("Could not update interface: " . $api->error()); plc_redirect (l_interface($interface_id)); } @@ -589,7 +646,7 @@ Our support team will be glad to answer any question that you might have. } } if ($success) { - $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body); + $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body); drupal_set_message ("Deleted $counter person(s)"); } else @@ -629,7 +686,7 @@ Our support team will be glad to answer any question that you might have. } } if ($success) { - $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body); + $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body); drupal_set_message ("Added $counter person(s)"); } else @@ -663,6 +720,49 @@ Our support team will be glad to answer any question that you might have. break; } + case 'update-initscripts': { +// expects: slice_id & name & previous-initscript & previous-initscript-code +// & initscript & initscript-code + $slice_id = intval ($_POST['slice_id']); + $previous_initscript=$_POST['previous-initscript']; + $initscript=$_POST['initscript']; + $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']); + $initscript_code=$_POST['initscript-code']; + + $changes=FALSE; + if (strcmp($initscript,$previous_initscript) != 0) { + $newvalue=$api->SetSliceInitscript($slice_id,$initscript); + $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed"; + if (! $initscript) drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status); + else drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status); + $changes=TRUE; + } + + // somehow some \r chars make it here; just ignore them + $previous_initscript_code=str_replace("\r","",$previous_initscript_code); + // plc_debug_txt('previous initscript_code after cr',$previous_initscript_code); + + $initscript_code=str_replace("\r","",$initscript_code); + // make sure the script ends with a single \n + $initscript_code=trim($initscript_code); + if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n") + $initscript_code.="\n"; + // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code); + + if (strcmp($initscript_code,$previous_initscript_code) != 0) { + $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code); + // plc_debug_txt('newvalue',$newvalue); + $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed"; + if (! $initscript_code) drupal_set_message("Removed initscript code " . $status); + else drupal_set_message("Installed new initscript code " . $status); + $changes=TRUE; + } + if (!$changes) drupal_set_message("No changes required in initscript"); + plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" ); + break; + } + + case 'delete-slice-tags': { $slice_id = intval($_POST['slice_id']); $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']); @@ -677,7 +777,7 @@ Our support team will be glad to answer any question that you might have. } if ($success) drupal_set_message ("Deleted $count slice tag(s)"); - plc_redirect(l_slice($slice_id) . "&show_tags=true" ); + plc_redirect(l_slice($slice_id) . "&show_tags=1" ); break; } @@ -777,17 +877,51 @@ Our support team will be glad to answer any question that you might have. break; } + case 'remove-roles-from-tag-type' : { + $tag_type_id=$_POST['tag_type_id']; + $role_ids=$_POST['role_ids']; + if ( ! $role_ids) { + drupal_set_error("You have not selected role(s) to remove"); + } else { + foreach( $role_ids as $role_id) + if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 ) + drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id"); + } + plc_redirect (l_tag_roles($tag_type_id)); + } + + case 'add-role-to-tag-type' : { + $tag_type_id=$_POST['tag_type_id']; + $role_id=$_POST['role_id']; + if ( ! $role_id) { + drupal_set_error ("You have not selected a role to add"); + } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) { + drupal_set_error("Could not add role $role_id to tag $tag_type_id"); + } + plc_redirect (l_tag_roles($tag_type_id)); + } + //////////////////////////////////////// tags case 'set-tag-on-node': - case 'set-tag-on-interface': { + case 'set-tag-on-site': + case 'set-tag-on-person': + case 'set-tag-on-interface': + { - $node_mode = false; - if ($action == 'set-tag-on-node') $node_mode=true; - - if ($node_mode) - $node_id = intval($_POST['node_id']); + $mode = NULL; + if ($action == 'set-tag-on-node') $mode='node'; + if ($action == 'set-tag-on-site') $mode='site'; + if ($action == 'set-tag-on-person') $mode='person'; + if ($action == 'set-tag-on-interface') $mode='interface'; + + if ($mode=='node') + $id = intval($_POST['node_id']); + else if ($mode=='site') + $id = intval($_POST['site_id']); + else if ($mode=='person') + $id = intval($_POST['person_id']); else - $interface_id=intval($_POST['interface_id']); + $id = intval($_POST['interface_id']); $tag_type_id = intval($_POST['tag_type_id']); $value = $_POST['value']; @@ -795,28 +929,42 @@ Our support team will be glad to answer any question that you might have. if (count ($tag_types) != 1) { drupal_set_error ("Could not locate tag_type_id $tag_type_id
Tag not set."); } else { - if ($node_mode) - $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id)); + if ($mode=='node') + $tags = $api->GetNodeTags (array('node_id'=>$id, 'tag_type_id'=> $tag_type_id)); + else if ($mode=='site') + $tags = $api->GetSiteTags (array('site_id'=>$id, 'tag_type_id'=> $tag_type_id)); + else if ($mode=='person') + $tags = $api->GetPersonTags (array('person_id'=>$id, 'tag_type_id'=> $tag_type_id)); else - $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id)); + $tags = $api->GetInterfaceTags (array('interface_id'=>$id, 'tag_type_id'=> $tag_type_id)); + + // already has a tag set if ( count ($tags) == 1) { $tag=$tags[0]; - if ($node_mode) { - $tag_id=$tag['node_tag_id']; - $result=$api->UpdateNodeTag($tag_id,$value); - } else { - $tag_id=$tag['interface_tag_id']; - $result=$api->UpdateInterfaceTag($tag_id,$value); - } + if ($mode=='node') + $result=$api->UpdateNodeTag($tag['node_tag_id'],$value); + else if ($mode=='site') + $result=$api->UpdateSiteTag($tag['site_tag_id'],$value); + else if ($mode=='person') + $result=$api->UpdatePersonTag($tag['person_tag_id'],$value); + else + $result=$api->UpdateInterfaceTag($tag['interface_tag_id'],$value); + if ($result == 1) drupal_set_message ("Updated tag, new value = $value"); else drupal_set_error ("Could not update tag"); + + // no such tag set yet on that object } else { - if ($node_mode) - $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value); + if ($mode=='node') + $tag_id = $api->AddNodeTag($id,$tag_type_id,$value); + else if ($mode=='site') + $tag_id = $api->AddSiteTag($id,$tag_type_id,$value); + else if ($mode=='person') + $tag_id = $api->AddPersonTag($id,$tag_type_id,$value); else - $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value); + $tag_id = $api->AddInterfaceTag($id,$tag_type_id,$value); if ($tag_id) drupal_set_message ("Created tag, new value = $value"); else @@ -824,22 +972,40 @@ Our support team will be glad to answer any question that you might have. } } - if ($node_mode) - plc_redirect (l_node($node_id)); + if ($mode=='node') + plc_redirect (l_node_tags($id)); + else if ($mode=='site') + plc_redirect (l_site_tags($id)); + else if ($mode=='person') + plc_redirect (l_person_tags($id)); else - plc_redirect (l_interface($interface_id)); + plc_redirect (l_interface_tags($id)); } - case 'delete-node-tags' : - case 'delete-interface-tags' : { + case 'delete-node-tags': + case 'delete-site-tags': + case 'delete-person-tags': + case 'delete-interface-tags': { - $node_mode = false; - if ($action == 'delete-node-tags') $node_mode=true; + $mode = NULL; + if ($action == 'delete-node-tags') $mode='node'; + if ($action == 'delete-site-tags') $mode='site'; + if ($action == 'delete-person-tags') $mode='person'; + if ($action == 'delete-interface-tags') $mode='interface'; - if ($node_mode) + if ($mode=='node') { + $id=$_POST['node_id']; $tag_ids=$_POST['node_tag_ids']; - else + } else if ($mode=='site') { + $id=$_POST['site_id']; + $tag_ids=$_POST['site_tag_ids']; + } else if ($mode=='person') { + $id=$_POST['person_id']; + $tag_ids=$_POST['person_tag_ids']; + } else { + $id=$_POST['interface_id']; $tag_ids=$_POST['interface_tag_ids']; + } if ( ! $tag_ids) { drupal_set_message("action=$action - No tag selected"); @@ -848,8 +1014,12 @@ Our support team will be glad to answer any question that you might have. $success=true; $counter=0; foreach( $tag_ids as $tag_id ) { - if ($node_mode) + if ($mode=='node') $retcod = $api->DeleteNodeTag( intval( $tag_id )); + else if ($mode=='site') + $retcod = $api->DeleteSiteTag( intval( $tag_id )); + else if ($mode=='person') + $retcod = $api->DeletePersonTag( intval( $tag_id )); else $retcod = $api->DeleteInterfaceTag( intval( $tag_id )); if ($retcod != 1) @@ -861,10 +1031,16 @@ Our support team will be glad to answer any question that you might have. drupal_set_message ("Deleted $counter tag(s)"); else drupal_set_error ("Could not delete all selected tags, only $counter were removed"); - if ($node_mode) - plc_redirect(l_node($_POST['node_id'])); + + if ($mode=='node') + plc_redirect (l_node_tags($id)); + else if ($mode=='site') + plc_redirect (l_site_tags($id)); + else if ($mode=='person') + plc_redirect (l_person_tags($id)); else - plc_redirect(l_interface($_POST['interface_id'])); + plc_redirect (l_interface_tags($id)); + } //////////////////////////////////////// nodegroups @@ -930,6 +1106,52 @@ Our support team will be glad to answer any question that you might have. break; } +//////////////////////////////////////// leases + case 'manage-leases': { + $actions=json_decode($_POST['actions']); + $add_requested=0; + $add_done=0; + $del_requested=0; + $del_done=0; + $errors=array(); + foreach ($actions as $action) { + if ($action[0] == 'add-leases') { + $nodenames=$action[1]; + $add_requested += count($nodenames); + $slicename=$action[2]; + $t_from=intval($action[3]); + $t_until=intval($action[4]); + $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until); + // update number of added leases + $ids=$hash['new_ids']; + $add_done += count($ids); + // update global errors array + foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error; + } else if ($action[0]=='delete-leases') { + $lease_id=intval($action[1]); + $del_requested += 1; + if ($api->DeleteLeases(array($lease_id)) == 1) { + $del_done += 1; + } else { + $errors []= "Could not delete lease " . $lease_id; + } + } else { + $errors []= "in actions.php, manage-leases, wrong action ". $action[0]; + } + } + + if (count($errors)==0) { + echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)"); + } else { + foreach ($errors as $error) echo($error. "\n"); + echo("Leases updated only partially (" . + $add_done . "/" . $add_requested . " added and " . + $del_done . "/" . $del_requested . " deleted)"); + } + + break; + } + //////////////////////////////////////// case 'debug': {