+if ($profiling) plc_debug_prof('12: nodes to add',count($potential_nodes));
+
+//////////////////////////////////////// retrieve all slice tags
+$tags=$api->GetSliceTags (array('slice_id'=>$slice_id));
+//////////////////////////////////////////////////////////// tab:initscripts
+// xxx fixme
+// * add a message on how to use this:
+// * explain the 2 mechanisms (initscript_code, initscript)
+// * explain the interface : initscript start|stop|restart slicename
+// xxx fixme
+
+$initscript_info="
+There are two ways to attach an initscript to a slice:<ul>
+
+<li> <span class='bold'> Shared initscripts </span> are global to the
+MyPLC, and managed by the Operations Team. For that reason, regular
+users cannot change these scripts, but can reference one of the
+available names in the drop down below. </li>
+
+<li> You also have the option to provide <span class='bold'> your own
+code </span>, with the following conventions: <ul>
+
+<li> Like regular initscripts, your script must except to receive as a
+first argument <span class='bold'> start </span>, <span class='bold'>
+stop </span> or <span class='bold'> restart </span>. It is important
+to honor this argument, as your slice may be stopped and restarted at
+any time; also this is used whenever the installed code gets changed.
+</li>
+
+<li> As a second argument, you will receive the slicename; in most
+cases this can be safely ignored. </li>
+
+</ul>
+</li>
+ </ul>
+The slice-specific setting has precedence on a shared initscript.
+";
+
+$shared_initscripts=$api->GetInitScripts(array('-SORT'=>'name'),array('name'));
+//$shared_initscripts=$api->GetInitScripts();
+if ($profiling) plc_debug_prof('13: initscripts',count($initscripts));
+// xxx expose this even on foreign slices for now
+if ($local_peer) {
+ $initscript='';
+ $initscript_code='';
+ if ($tags) foreach ($tags as $tag) {
+ if ($tag['tagname']=='initscript') {
+ if ($initscript!='') drupal_set_error("multiple occurrences of 'initscript' tag");
+ $initscript=$tag['value'];
+ }
+ if ($tag['tagname']=='initscript_code') {
+ if ($initscript_code!='') drupal_set_error("multiple occurrences of 'initscript_code' tag");
+ $initscript_code=$tag['value'];
+ // plc_debug_txt('retrieved body',$initscript_code);
+ }
+ }
+ $label="No initscript";
+ $trimmed=trim($initscript_code);
+ if (!empty($trimmed)) $label="Initscript : slice-specific (" . substr($initscript_code,0,20) . " ...)";
+ else if (!empty($initscript)) $label="Initscript: shared " . $initscript;
+
+ $toggle = new PlekitToggle('slice-initscripts',$label,
+ array('bubble'=>'Manage initscript on that slice',
+ 'visible'=>get_arg('show_initscripts'),
+ 'info-text'=>$initscript_info
+ // not messing with persontags to guess whether this should be displayed or not
+ // hopefully some day toggle will know how to handle that using web storage
+ ));
+ $toggle->start();
+
+ $details=new PlekitDetails(TRUE);
+ // we expose the previous values so that actions.php can know if changes are really needed
+ // the code needs to be encoded as it may contain any character
+ // as far as the code, this does not work too well b/c what actions.php receives
+ // seems to have spurrious \r chars, and the comparison between old and new values
+ // is not reliable, which results in changes being made although the code hasn't changed
+ // hve spent too much time on this, good enough for now...
+ $details->form_start(l_actions(),array('action'=>'update-initscripts',
+ 'slice_id'=>$slice_id,
+ 'name'=>$name,
+ 'previous-initscript'=>$initscript,
+ 'previous-initscript-code'=>htmlentities($initscript_code)));
+ $details->start();
+ // comppute a pulldown with available names
+ $selectors=array();
+ $is_found=FALSE;
+ if ($shared_initscripts) foreach ($shared_initscripts as $is) {
+ $is_selector=array('display'=>$is['name'],'value'=>$is['name']);
+ if ($is['name']==$initscript) {
+ $is_selector['selected']=TRUE;
+ $is_found=TRUE;
+ }
+ $selectors[]=$is_selector;
+ }
+ // display a warning when initscript references an unknown script
+ $details->tr_submit('unused','Update initscripts');
+ ////////// by name
+ $details->th_td("shared initscript name",
+ $details->form()->select_html('initscript',$selectors,array('label'=>'none')),
+ 'initscript',
+ array('input_type'=>'select'));
+ if ($initscript && ! $is_found)
+ // xxx better rendering ?
+ $details->th_td('WARNING',plc_warning_html("Current name '" . $initscript . "' is not a known shared initscript name"));
+ ////////// by contents
+ $script_height=8;
+ $script_width=60;
+ if ($initscript_code) {
+ $text=explode("\n",$initscript_code);
+ $script_height=count($text);
+ $script_width=10;
+ foreach ($text as $line) $script_width=max($script_width,strlen($line));
+ }
+ $details->th_td('slice initscript',$initscript_code,'initscript-code',
+ array('input_type'=>'textarea', 'width'=>$script_width,'height'=>$script_height));
+ $details->tr_submit('unused','Update initscripts');
+ $details->form_end();
+ $details->end();
+ $toggle->end();
+}
+
+//////////////////////////////////////////////////////////// tab:tags
+// very wide values get abbreviated
+$tag_value_threshold=24;
+// xxx fixme
+// * this area could use a help message about some special tags:
+// * initscript-related should be taken out
+// * sliverauth-related (ssh_key & hmac) should have a toggle to hide or show
+// xxx fixme
+
+// xxx expose this even on foreign slices for now
+//if ( $local_peer ) {
+ if ($profiling) plc_debug_prof('14: slice tags',count($tags));
+ function get_tagname ($tag) { return $tag['tagname'];}
+ $tagnames = array_map ("get_tagname",$tags);
+
+ $toggle = new PlekitToggle ('slice-tags',count_english_warning($tags,'tag'),
+ array('bubble'=>'Inspect and set tags on that slice',
+ 'visible'=>get_arg('show_tags')));
+ $toggle->start();
+
+ $headers=array(
+ "Name"=>"string",
+ "Value"=>"string",
+ "Node"=>"string",
+ "NodeGroup"=>"string");
+ if ($tags_privileges) $headers[plc_delete_icon()]="none";
+
+ $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
+ $table=new PlekitTable("slice_tags",$headers,'0',$table_options);
+ $form=new PlekitForm(l_actions(),
+ array('slice_id'=>$slice['slice_id']));
+ $form->start();
+ $table->start();
+ if ($tags) {
+ // Get hostnames for nodes in a single pass
+ $_node_ids = array();
+ foreach ($tags as $tag) {
+ if ($tag['node_id']) {
+ array_push($_node_ids, $tag['node_id']);
+ }
+ }
+ $_nodes = $api->GetNodes(array('node_id' => $_node_ids), array('node_id', 'hostname'));
+ $_hostnames = array();
+ foreach ($_nodes as $_node) {
+ $_hostnames[$_node['node_id']] = $_node['hostname'];
+ }
+
+ // Loop through tags again to display
+ foreach ($tags as $tag) {
+ $node_name = "ALL";
+ if ($tag['node_id']) {
+ $node_name = $_hostnames[$tag['node_id']];
+ }
+ $nodegroup_name="n/a";
+ if ($tag['nodegroup_id']) {
+ $nodegroups=$api->GetNodeGroups(array('nodegroup_id'=>$tag['nodegroup_id']));
+ if ($profiling) plc_debug_prof('15: nodegroup for slice tag',$nodegroup);
+ if ($nodegroup) {
+ $nodegroup = $nodegroups[0];
+ $nodegroup_name = $nodegroup['groupname'];
+ }
+ }
+ $table->row_start();
+ $table->cell(l_tag_obj($tag));
+ // very wide values get abbreviated
+ $table->cell(truncate_and_popup($tag['value'],$tag_value_threshold));
+ $table->cell($node_name);
+ $table->cell($nodegroup_name);
+ if ($tags_privileges) $table->cell ($form->checkbox_html('slice_tag_ids[]',$tag['slice_tag_id']));
+ $table->row_end();
+ }
+ }
+ if ($tags_privileges) {
+ $table->tfoot_start();
+ $table->row_start();
+ $table->cell($form->submit_html ("delete-slice-tags","Remove selected"),
+ array('hfill'=>true,'align'=>'right'));
+ $table->row_end();
+
+ $table->row_start();
+ function tag_selector ($tag) {
+ return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']);
+ }
+ $all_tags= $api->GetTagTypes( array ("category"=>"*slice*","-SORT"=>"+tagname"), array("tagname","tag_type_id"));
+ if ($profiling) plc_debug_prof('16: tagtypes',count($all_tags));
+ $selector_tag=array_map("tag_selector",$all_tags);
+
+ function node_selector($node) {
+ return array("display"=>$node["hostname"],"value"=>$node['node_id']);
+ }
+ $selector_node=array_map("node_selector",$slice_nodes);
+
+ function nodegroup_selector($ng) {
+ return array("display"=>$ng["groupname"],"value"=>$ng['nodegroup_id']);
+ }
+ $all_nodegroups = $api->GetNodeGroups( array("groupname"=>"*"), array("groupname","nodegroup_id"));
+ if ($profiling) plc_debug_prof('17: nodegroups',count($all_nodegroups));
+ $selector_nodegroup=array_map("nodegroup_selector",$all_nodegroups);
+
+ $table->cell($form->select_html("tag_type_id",$selector_tag,array('label'=>"Choose Tag")));
+ $table->cell($form->text_html("value","",array('width'=>8)));
+ $table->cell($form->select_html("node_id",$selector_node,array('label'=>"All Nodes")));
+ $table->cell($form->select_html("nodegroup_id",$selector_nodegroup,array('label'=>"No Nodegroup")));
+ $table->cell($form->submit_html("add-slice-tag","Set Tag"),array('columns'=>2,'align'=>'left'));
+ $table->row_end();
+ }
+
+ $table->end();
+ $form->end();
+ $toggle->end();
+//}
+