add node-usb-partition as an option for download.
[plewww.git] / planetlab / nodes / node.php
index a3cf5e8..68ef0fc 100644 (file)
@@ -16,10 +16,12 @@ include 'plc_header.php';
 // Common functions
 require_once 'plc_functions.php';
 require_once 'plc_peers.php';
-require_once 'plc_minitabs.php';
-require_once 'plc_tables.php';
-require_once 'plc_details.php';
-require_once 'plc_forms.php';
+require_once 'linetabs.php';
+require_once 'table.php';
+require_once 'details.php';
+require_once 'form.php';
+require_once 'toggle.php';
+require_once 'plc_objects.php';
 
 // -------------------- 
 // recognized URL arguments
@@ -92,8 +94,9 @@ if( !empty( $conf_file_ids ) )
   $conf_files= $api->GetConfFiles( $conf_file_ids );
 // (*) idem for PCUs
 // gets pcu and port info key to both is $pcu_id
-if( !empty( $pcu_ids ) )
-  $PCUs= $api->GetPCUs( $pcu_ids );
+// turning this off: GetPCUs is not allowed to users, and we don't show PCUs yet anyway
+//if( !empty( $pcu_ids ) )
+//  $PCUs= $api->GetPCUs( $pcu_ids );
 
 //////////////////// display node info
 
@@ -102,213 +105,253 @@ $local_peer= ! $peer_id;
 
   
 // extra privileges to admins, and (pi||tech) on this site
-$privileges = plc_is_admin () || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
+$privileges = (plc_is_admin () && $local_peer) || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
   
 $tabs=array();
 // available actions
+$tabs [] = tab_nodes_site($site_id);
+$tabs [] = tab_site($site_id);
+//$tabs [] = tab_nodes();
+
 if ( $local_peer  && $privileges ) {
     
-  $tabs['Update'] = array ('url'=>"/db/nodes/node_actions.php",
-                          'method'=>'POST',
-                          'values'=>array('action'=>'prompt-update','node_id'=>$node_id),
-                          'bubble'=>"Update details of $hostname");
-  $tabs['Delete'] = array ('url'=>"/db/nodes/node_actions.php",
+  $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
+                              'bubble'=>"Define new network interface on $hostname");
+  $tabs['Delete'] = array ('url'=>l_actions(),
                           'method'=>'POST',
-                          'values'=>array('action'=>'delete','node_id'=>$node_id),
+                          'values'=>array('action'=>'delete-node','node_id'=>$node_id),
                           'bubble'=>"Delete node $hostname",
-                          'confirm'=>'Are you sure to delete ' . $hostname. ' ?');
-  // xxx subject to roles
-  $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
-                              'bubble'=>"Declare new network interface on $hostname");
-  $tabs["Events"]=array_merge(tabs_events(),
+                          'confirm'=>'Are you sure to delete ' . $hostname);
+  $tabs["Events"]=array_merge(tablook_event(),
                              array('url'=>l_event("Node","node",$node_id),
                                    'bubble'=>"Events for node $hostname"));
-  $tabs["Comon"]=array_merge(tabs_comon(),
+  $tabs["Comon"]=array_merge(tablook_comon(),
                             array('url'=>l_comon("node_id",$node_id),
                                   'bubble'=>"Comon page about node $hostname"));
  }
 
-$tabs["All nodes"]=l_nodes();
-
-plc_tabs($tabs);
+plekit_linetabs($tabs);
 
 // show gray background on foreign objects : start a <div> with proper class
 $peers->block_start ($peer_id);
   
-plc_details_start ();
+$toggle = new PlekitToggle ('node',"Details",
+                           array('bubble'=>'Display and modify details for that node',
+                                 'visible'=>get_arg('show_details',true)));
+$toggle->start();
+
+$details=new PlekitDetails($privileges);
+$details->start();
 if ( ! $local_peer) {
-  plc_details_line("Peer",$peers->peer_link($peer_id));
-  plc_details_space_line();
+  $details->th_td("Peer",$peers->peer_link($peer_id));
+  $details->space();
  }
 
-plc_details_line("Hostname",$hostname);
-plc_details_line("Type",$node_type);
-plc_details_line("Model",$model);
-plc_details_line("Version",$version);
-// no tool to implement this multiple-choice setting yet
-// xxx would need at least to use the proper class, like plc_details_class() or something
-plc_details_space_line ();
-echo "<tr><th>Boot State: </th><td>";
-if ( ! $local_peer) {
-  echo $boot_state;
+$details->form_start(l_actions(),array("action"=>"update-node", "node_id"=>$node_id));
+// xxx can hostname really be changed like this without breaking the rest, bootcd .. ?
+//$details->th_td("Hostname",$hostname,"hostname"); 
+$details->th_td("Hostname",$hostname); 
+$details->th_td("Model",$model,"model");
+$details->tr_submit("submit","Update Node");
+$details->form_end();
+if ($privileges) $details->space();
+
+$details->th_td("Type",$node_type);
+$details->th_td("Version",$version);
+// let's use plc_objects
+$Node = new Node($node);
+$details->th_td("Date created",$Node->dateCreated());
+$details->th_td("Last contact",$Node->lastContact());
+$details->th_td("Last update",$Node->lastUpdated());
+
+// boot area
+$details->space ();
+if ( ! ($local_peer && $privileges)) {
+  // just display it
+  $boot_value=$boot_state;
  } else {
-  echo "<form name='bootstate' action='/db/nodes/node_actions.php' method=post>\n";
-  echo "<input type=hidden name='node_id' value='$node_id'>\n";
-  echo "<input type=hidden name='action' value='boot-state'>\n";
-  echo "<select name='boot_state' onChange=\"submit();\">\n";
-
-  $states= array( 'boot'=>'Boot', 'dbg'=>'Debug', 'inst'=>'Install', 'rins'=>'Reinstall', 'rcnf'=>'Reconfigure', 'new'=>'New' );
-
-  foreach( $states as $key => $val ) {
-    echo "<option value='$key'";
-      
-    if( $key == $boot_state )
-      echo " selected";
-      
-    echo ">$val</option>\n";
-      
+  $boot_value="";
+  $boot_form = new PlekitForm (l_actions(), array("node_id"=>$node_id,
+                                              "action"=>"node-boot-state"));
+  $boot_value .= $boot_form->start_html();
+  $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 'failboot'=>'FailBoot', 
+                  'disabled' => 'Disabled', 'install'=>'Install', 'reinstall'=>'Reinstall');
+  $selectors=array();
+  foreach ($states as $dbname=>$displayname) { 
+    $selector=array("display"=>$displayname, "value"=>$dbname);
+    if ($dbname == $boot_state) $selector['selected']=true;
+    $selectors []= $selector;
   }
-  echo "</select></input></form>";
+  $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
+  $boot_value .= $boot_form->end_html();
  }
-echo "</td></tr>\n";
+$details->th_td ("Boot state",$boot_value);
 
 // same here for the download area
 if ( $local_peer  && $privileges) {
 
-  echo "<tr><th>Download </th><td>";
-  echo "<form name='download' action='/db/nodes/node_actions.php' method='post'>\n";
-  echo "<input type=hidden name='node_id' value='$node_id'></input>\n";
-  echo "<select name='action' onChange='submit();'>\n";
-  echo "<option value='' selected='selected'> Download Mode </option>\n";
-  echo "<option value='' disabled='disabled'> -- All in one images -- </option>"; 
-  echo "<option value='download-node-iso' $new_api_only> Download ISO image for $hostname</option>\n";
-  echo "<option value='download-node-usb' $new_api_only> Download USB image for $hostname</option>\n";
-  echo "<option value='' disabled='disabled'> -- Floppy + generic image -- </option>"; 
-  echo "<option value='download-node-floppy'> Download Floppy file for $hostname</option>\n";
-  echo "<option value='download-generic-iso' $new_api_only> Download generic ISO image (requires floppy) </option>\n";
-  echo "<option value='download-generic-usb' $new_api_only> Download generic USB image (requires floppy) </option>\n";
-  echo "</select></form>";
-  echo "</td></tr>\n";
-
+  $download_value="";
+  $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
+  $download_value .= $download_form->start_html();
+  $selectors = array( 
+                    array("display"=>"-- All in one images --","disabled"=>true),
+                    array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
+                    array("value"=>"download-node-usb","display"=>"Download USB image for $hostname"),
+                    array("value"=>"download-node-usb-partition", "display"=>"Download partitioned, USB image for $hostname"),
+                    array("display"=>"-- Floppy + generic image --","disabled"=>true),
+                    array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
+                    array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
+                    array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
+  $download_value .= $download_form->select_html("action",$selectors,
+                                                array('label'=>"Download mode",'autosubmit'=>true));
+  $download_value .= $download_form->end_html();
+  $details->th_td ("Download",$download_value);
  }
 
 // site info and all site nodes
-plc_details_space_line ();
-plc_details_line("Site",l_site_t($site_id,$site_name));
+$details->space ();
+$details->th_td("Site",l_site_t($site_id,$site_name));
                   
 // build list of node links
 $nodes_area=array();
 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
 }
-plc_details_line_list ("All site nodes",$nodes_area);
-
-plc_details_end ();
-
-plc_form_start(l_actions(), array('node_id'=>$node_id));
+$details->th_tds ("All site nodes",$nodes_area);
+
+$details->end ();
+$toggle->end();
+
+$form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
+$form->start();
+
+//////////////////////////////////////////////////////////// slivers
+{
+  $toggle=new PlekitToggle ('slices',count_english_warning($slices,'sliver'),
+                           array('bubble'=>'Review slices running on that node',
+                                 'visible'=>get_arg('show_slices',false)));
+  $toggle->start();
+  if ( ! $slices  ) {
+    plc_warning ("This node is not associated to any slice");
+  } else {
+    $headers=array();
+    $headers['Peer']="string";
+    $headers['Slice Name']="string";
+    $headers['Sliver']="string";
+    $reasonable_page=10;
+    $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
+    if (count ($slices) <= $reasonable_page) {
+      $table_options['search_area']=false;
+      $table_options['pagesize_area']=false;
+    }
+    $table=new PlekitTable("node_slices",$headers,1,$table_options);
+    $table->start();
+
+    foreach ($slices as $slice) {
+      $table->row_start();
+      $peers->cell ($table,$slice['peer_id']);
+      $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
+      # xxx l_sliver not implemented yet - what should we show exactly ?
+      $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'sliver tags'));
+      $table->row_end();
+    }
+    $table->end();
+  }
+  $toggle->end();
+}
 
 //////////////////////////////////////////////////////////// Tags
-// get tags
+// tags section
 if ( $local_peer ) {
   
   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
   function get_tagname ($tag) { return $tag['tagname'];}
+  // xxx looks like tech-only see an error here, 
+  // might be that GetNodeTags is not accessible or something
   $tagnames = array_map ("get_tagname",$tags);
   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
   
-  plc_section("Tags");
+  $toggle = new PlekitToggle ('tags',count_english_warning($tags,'tag'),
+                             array('bubble'=>'Inspect and set tags on that node',
+                                   'visible'=>get_arg('show_tags',false)));
+  $toggle->start();
+
   $headers=array("Name"=>"string",
                 "Value"=>"string",
-                "Nodegroup"=>"string");
+                "Nodegroup"=>"string",
+                );
+  if (plc_is_admin()) $headers[plc_delete_icon()]="none";
   
   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
-  plc_table_start("node_tags",$headers,0,$table_options);
+  $table=new PlekitTable("node_tags",$headers,0,$table_options);
+  $table->start();
   if ($tags) foreach ($tags as $tag) {
       // does this match a nodegroup ?
       $nodegroup_name="n/a";
       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
       $nodegroup=$nodegroups_hash[$nodegroup_key];
       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
-      plc_table_row_start();
-      plc_table_cell($tag['tagname']);
-      plc_table_cell($tag['value']);
-      plc_table_cell($nodegroup_name);
-      plc_table_row_end();
+      $table->row_start();
+      $table->cell(l_tag_obj($tag));
+      $table->cell($tag['value']);
+      $table->cell($nodegroup_name);
+      // the remove checkbox
+      if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
+      $table->row_end();
     }
   
-  $footers=array();
   if ($privileges) {
-    // remove selected sites
+    $table->tfoot_start();
+
+    // remove tag 
+    $table->row_start();
+    $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
+                // use the whole columns and right adjust
+                array('hfill'=>true,'align'=>'right'));
+    $table->row_end();
+
+    // set tag area
+    $table->row_start();
     // get list of tag names in the node/* category    
     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
-
     // xxx cannot use onchange=submit() - would need to somehow pass action name 
     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
     $selector=array_map("tag_selector",$all_tags);
-    $add_tag_name=plc_form_select_text("tag_type_id",$selector,"Choose");
-    $add_tag_value=plc_form_text_text("value","",8);
-    $add_tag_submit=plc_form_submit_text("set-tag-on-node","Set Tag");
-
-    $add_tag_footer=plc_table_td_text($add_tag_name).plc_table_td_text($add_tag_value).plc_table_td_text($add_tag_submit);
-    $footers[]= $add_tag_footer;
+    $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
+    $table->cell($form->text_html("value","",array('width'=>8)));
+    $table->cell($form->submit_html("set-tag-on-node","Set Tag"),array('columns'=>2,'align'=>'left'));
+    $table->row_end();
   }
   
-  plc_table_end("node_tags",array('footers'=>$footers));
- }
-
-//////////////////////////////////////////////////////////// slices
-// display slices
-
-plc_section ("Slices");
-if ( ! $slices  ) {
-  plc_warning ("This node is not associated to any slice");
- } else {
-  $headers=array();
-  $headers['Peer']="string";
-  $headers['Name']="string";
-  $headers['Slivers']="string";
-  $reasonable_page=10;
-  $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
-  if (count ($slices) <= $reasonable_page) {
-    $table_options['search_area']=false;
-    $table_options['pagesize_area']=false;
-  }
-  plc_table_start ("node_slices",$headers,1,$table_options);
-
-  foreach ($slices as $slice) {
-    plc_table_row_start();
-    plc_table_cell ($peers->shortname($peer_id));
-    plc_table_cell (l_slice_t ($slice['slice_id'],$slice['name']));
-    plc_table_cell (l_sliver_t ($node_id,$slice['slice_id'],'view'));
-    plc_table_row_end();
-  }
-  plc_table_end("node_slices");
- }
+  $table->end();
+  $toggle->end();
+}
 
 //////////////////////////////////////////////////////////// interfaces
 if ( $local_peer ) {
-
-  plc_section ("Interfaces");
+  $toggle=new PlekitToggle ('interfaces',count_english_warning($interfaces,'interface'),
+                           array('bubble'=>'Inspect and tune interfaces on that node',
+                                 'visible'=>get_arg('show_interfaces',false)));
+  $toggle->start();
   // display interfaces
   if( ! $interfaces ) {
     echo '<p>';
-    plc_warning_text("This node has no interface");
+    plc_warning_html("This node has no interface");
     echo "Please add an interface to make this a usable PLC node.</p>\n";
   } else {
     $headers=array();
-    if ( $privileges ) {
-      // a single symbol, marking 'p' for primary and a delete button for non-primary
-      $headers[' ']='string';
-    }
-        
+
     $headers["IP"]="IPAddress";
     $headers["Method"]="string";
     $headers["Type"]="string";
     $headers["MAC"]="string";
     $headers["bw limit"]="FileSize";
+    // a single symbol, marking 'p' for primary and a delete button for non-primary
+    if ( $privileges ) $headers[plc_delete_icon()]='string';
 
     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
-    plc_table_start("node_interfaces",$headers,2,$table_options);
+    $table=new PlekitTable("node_interfaces",$headers,2,$table_options);
+    $table->start();
        
     foreach ( $interfaces as $interface ) {
       $interface_id= $interface['interface_id'];
@@ -326,36 +369,42 @@ if ( $local_peer ) {
       $interface_type= $interface['type'];
       $interface_method= $interface['method'];
 
-      plc_table_row_start();
+      $table->row_start();
+      $table->cell(l_interface_t($interface_id,$interface_ip));
+      $table->cell($interface_method);
+      $table->cell($interface_type);
+      $table->cell($interface_mac);
+      $table->cell($interface_bwlimit);
       if ( $privileges ) {
-       if (!$interface_primary) {
-         // xxx 
-         plc_table_cell (plc_delete_link_button ('interfaces.php?id=' . $interface_id . '&delete=1&submitted=1', 
-                                                 '\\nInterface ' . $interface_ip));
+       if ($interface_primary) {
+         $table->cell(plc_bubble("p","Cannot delete a primary interface"));
        } else {
-         plc_table_cell('p');
+         $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
        }
       }
-      plc_table_cell(l_interface_t($interface_id,$interface_ip));
-      plc_table_cell($interface_method);
-      plc_table_cell($interface_type);
-      plc_table_cell($interface_mac);
-      plc_table_cell($interface_bwlimit);
-      plc_table_row_end();
+      $table->row_end();
     }
     if ($privileges) {
-      $button=plc_form_simple_button(l_interface_add($node_id),"Add interface","GET");
-      $footers=array(plc_table_td_text($button,6,"right"));
+      $table->tfoot_start();
+      $table->row_start();
+      $add_button=new PlekitFormButton (l_interface_add($node_id),"add","Add Interface","GET");
+      // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
+      $table->cell($add_button->html(),array('columns'=> 3,'align'=>'left'));
+      $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), 
+                  array('columns'=>$table->columns()-3,'align'=>'right'));
+      $table->row_end();
     }
-    plc_table_end("node_interfaces",array("footers"=>$footers));
+    $table->end();
   }
+  $toggle->end();
  }
 
-plc_form_end();
+$form->end();
 
 ////////////////////////////////////////////////////////////
 $peers->block_end($peer_id);
 
+//plekit_linetabs ($tabs,"bottom");
 
 // Print footer
 include 'plc_footer.php';