Added two more columns (deployment tag only for admins and number of
[plewww.git] / planetlab / slices / slice_add.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 // Common functions
11 require_once 'plc_functions.php';
12 require_once 'details.php';
13 require_once 'table.php';
14 require_once 'toggle.php';
15   
16 //plc_debug('POST',$_POST);
17
18 // if not a PI or admin then redirect to slice index
19 $has_privileges = plc_is_admin() || plc_is_pi();
20 if ( ! $has_privileges ) {
21   drupal_set_error("Insufficient privilege to add a slice");
22   header( "index.php" );
23   return 0;
24 }
25
26 // find out which site the slice should be added to
27 // without site_id set in GET, we use the first site that this user is in
28 if (isset($_GET['site_id'])) {
29   $site_id=intval($_GET['site_id']);
30  } else if (isset ($_POST['site_id'])) {
31   $site_id=intval($_POST['site_id']);
32  } else {
33   $site_id=plc_my_site_id();
34  }
35
36 //////////////////// action
37 if ( $_POST['add-slice'] ) {
38   // get post vars
39   $url= $_POST['url'];
40   $instantiation= $_POST['instantiation'];
41   $name= $_POST['name'];
42   $description= $_POST['description'];
43   $person_ids = $_POST['person_ids'];
44
45   $check=true;
46
47   $sites=$api->GetSites(array('site_id'=>$site_id));
48   if ( ! $sites) {
49     drupal_set_error("Cannot find site_id $site_id");
50     $check=false;
51   }
52   $site=$sites[0];
53   $base=$site['login_base'] . '_';
54
55   // validate input
56   if( $name == $base ) {
57     drupal_set_error("You must enter a name for your slice");
58     $check=false;
59   } else if (strpos($name,$base) != 0) {
60     drupal_set_error("Slice name $name should begin with $base");
61     $check=false;
62   } else {
63     // make sure slice name doesnt exist
64     $slices = $api->GetSlices( array( $name ), array( "slice_id" ) );
65     if ( count($slices) != 0) {
66       drupal_set_error("Slice name $name already in use, please choose another");
67       $check=false;
68     }
69   }
70   
71   if ( ($url == "http://") || ( $url=="" ) ) {
72     drupal_set_error("You must enter a URL for your slice's info");
73     $check=false;
74   }
75       
76   if( $description == "" ) {
77     drupal_set_error("Your must enter a description for you slice.");
78     $check=false;
79   }
80   
81   // if no errors then add
82   if ( $check ) {
83     $fields= array( "url" => $url, 
84                     "instantiation" => $instantiation, 
85                     "name" => $name, 
86                     "description" => $description );
87     // add it!
88     $slice_id= $api->AddSlice( $fields );
89
90     if ($slice_id > 0) {
91       drupal_set_message ("Slice $slice_id created");
92       if (isset($_POST['omf-control'])) {
93         if ($api->SetSliceOmfControl($slice_id,'yes') != 'yes') {
94           drupal_set_error("Could not set the 'omf_control' tag on newly created slice...");
95         } else {
96           drupal_set_message("Successfully set the 'omf_control' tag on slice");
97         }
98       }
99
100       if ($person_ids) {
101         // Add people
102         $success=true;
103         $counter=0;
104         foreach ($person_ids as $person_id) {
105           $person_id=intval($person_id);
106           if ($api->AddPersonToSlice($person_id,$slice_id) != 1) {
107             drupal_set_error("Could not add person $person_id in slice :" . $api->error());
108             $success=false;
109           } else {
110             $counter++;
111           }
112         }
113         if ($success) 
114           drupal_set_message ("Added $counter person(s)");
115         else
116           drupal_set_error ("Could not add all selected persons, only $counter were added");
117       }
118       plc_redirect(l_slice($slice_id) );
119     } else {
120       drupal_set_error("Could not create slice $name " . $api->error() );
121       $check=false;
122     }
123   }
124  }
125
126 //////////////////// still here : either it's a blank form or something was wrong
127
128 // Print header
129 require_once 'plc_drupal.php';
130 include 'plc_header.php';
131
132 $sites=$api->GetSites(array($site_id));
133 $site=$sites[0];
134 $sitename=$site['name'];
135 if ( ! $_POST['name']) 
136   $base= $site['login_base'] ."_";
137
138 // propose to add all 'reachable' persons 
139 $site_person_ids=$site['person_ids'];
140 $persons_filter=array("person_id"=>$site_person_ids,
141                       "enabled"=>true);
142 $persons=$api->GetPersons($persons_filter,array('email','enabled','first_name','last_name','person_id'));
143
144 drupal_set_title('Create slice in site "' . $sitename . '"');
145
146 // defaults
147 $url = $_POST['url'];
148 if( !$url ) $url= "http://";
149
150 // check for errors and set error styles
151 if( $error['name'] )
152   $name_error= " class='plc-warning'";
153   
154 if( $error['url'] )
155   $url_error= " class='plc-warning'";
156   
157 if( $error['description'] )
158   $desc_error= " class='plc-warning'";
159
160
161 // is there a need to consider several sites ?
162 $multiple_sites=false;
163 $site_columns=array('name','login_base','site_id');
164 if (plc_is_admin ()) {
165   $multiple_sites=true;
166   $filter=array('-SORT'=>'name');
167  } else if (count (plc_my_site_ids()) > 1) {
168   $multiple_sites=true;
169   $filter=array('-SORT'=>'name','site_id'=>plc_my_site_ids());
170  }
171
172 if ($multiple_sites) {
173   print "<div id='create-slice-in-site'>";
174   $other_sites=$api->GetSites($filter,$site_columns);
175   $selectors=array();
176   foreach ($other_sites as $other_site) {
177     $selector=array('display'=>$other_site['name'],
178                     'value'=>$other_site['site_id']);
179     if ($other_site['site_id']==$site_id) $selector['selected']='selected';
180     $selectors []= $selector;
181   }
182
183   $site_form = new  PleKitForm (l_slice_add(),array(),array('method'=>'get'));
184   $site_form->start();
185   print $site_form->label_html('site_id','Or choose some other site');
186   print $site_form->select_html('site_id',$selectors,array('autosubmit'=>true,
187                                                            'id'=>'create-slice-choose-site'));
188   $site_form->end();
189   print "</div>";
190  }
191                   
192 print <<< EOF
193 <div class='create-slice-instantiations'>
194 <p><span class='bold'>Important:</span> Please provide a short description, as well as a 
195 link to a project website, before creating your slice.</p>
196 <p>
197 PlanetLab's security model requires that anyone who is concerned about a slice's activity be able to immediately learn about that slice. The details that you provide are your public explanation about why the slice behaves as it does. Be sure to describe the <span class='bold'>kind of traffic</span> that your slice generates, and how it handles material that is under <span class='bold'>copyright</span>, if relevant.
198 </p><p>
199 The PlanetLab Operations Centres regularly respond to concerns raised by third parties about site behaviour. Most incidents are resolved rapidly based upon the publicly posted slice details. However, when these details are not sufficiently clear or accurate, and we cannot immediately reach the slice owner, we must delete the slice.
200 </p>
201 <p><span class='bold'>NOTE</span>: All PlanetLab users are <span class='bold'>strongly</span>
202  encouraged to join the PlanetLab 
203 <a href='https://lists.planet-lab.org/mailman/listinfo/users'>Users</a> 
204 mailing list. Most questions about running software on PlanetLab can be answered by 
205 posting to this list. 
206 <br/>Site administrators often use this list to post announcements about service outages. 
207 New software releases and available services are announced here as well.
208 </p>
209 </div>
210 EOF;
211
212 $toggle = new PlekitToggle ('create-slice-details','Slice Details',
213                             array ('visible'=>get_arg('show_slice',true)));
214 $details = new PlekitDetails(TRUE);
215
216 $form_variables = array('site_id'=>plc_my_site_id());
217 $form = $details -> form_start("/db/slices/slice_add.php",$form_variables);
218 print $form->hidden_html("site_id",$site_id);
219
220 $toggle->start();
221 $details->start();
222
223 $running=count($site['slice_ids']);
224 $max=$site['max_slices'];
225 $allocated = " $running running / $max max";
226 if ($running >= $max) $allocated = plc_warning_html($allocated);
227 $details->th_td("Allocated slices",$allocated);
228 $details->th_td("Name",$name ? $name : $base, "name");
229 $details->th_td("URL",$url,"url");
230 $details->th_td("Description",$description,"description",
231                 array('input_type'=>'textarea',
232                       'width'=>50,'height'=>5));
233 $selectors=array(array('display'=>"PLC",'value'=>'plc-instantiated'),
234                  array('display'=>"Delegated",'value'=>'delegated'),
235                  array('display'=>"Controller",'value'=>'nm-controller'),
236                  array('display'=>"None",'value'=>'not-instantiated'));
237
238 $instantiation_select = $form->select_html ("instantiation", $selectors);
239 $details->th_td("Instantiation",$instantiation_select,"instantiation",
240                 array('input_type'=>'select', 'value'=>$instantiation));
241
242 // display the current settings if any (like, we've screwed up the first time)
243 if (isset($_POST['omf-control'])) {
244   $omf_options=array('checked'=>'checked');
245 } else {
246   $omf_options=array();
247 }
248 $details->th_td("OMF friendly",
249                 $form->checkbox_html('omf-control','yes',$omf_options));
250
251 $instantiation_text = <<< EOF
252 <div class='create-slice-instantiations'>
253 <p>There are four possible "instantiation" states for a slice.</p>
254 <ul>
255 <li> <span class='bold'>PLC</span> creates a slice with default settings. </li>
256 <li><span class='bold'>Delegated</span> creates a ticket to use on each node. </li>
257 <li><span class='bold'>Controller</span> creates a slice on all nodes to manipulate Delegated slices. </li>
258 <li><span class='bold'>None</span> allows you to reserve a slice name; you may instantiate the slice later.</li>
259 </ul>
260 <p>PLC instantiated slices can be defined as <span class='bold'>OMF friendly</span>, 
261 in which case slivers come with the OMF <span class='bold'>Resource Controller</span> pre-installed and pre-configured. 
262 Such slivers can then be easily managed through a centralized tool, the OMF Experiment Controller.
263 Using these <a href="http://omf.mytestbed.net">OMF tools</a>, a user can describe, instrument, 
264 and automatically execute their experiments across many slivers.
265 Please refer to <a href="http://mytestbed.net/wiki/omf/OMF_User_Guide">the OMF User Guide</a> 
266 to learn more on how to use this feature.
267 </p>
268 </div>
269 EOF;
270
271 $details->tr($instantiation_text);
272
273 $details->end();
274 $toggle->end();
275
276 if ($persons) {
277   $title = count($persons) . " people can be added in slice";
278   $toggle=new PlekitToggle ('create-slice-persons',$title,
279                           array('visible'=>get_arg('show_persons',true)));
280   $toggle->start();
281   
282   $headers = array();
283   $headers['email']='string';
284   $headers['first']='string';
285   $headers['last']='string';
286   $headers['+']='none';
287   $table = new PlekitTable ('persons_in_slice',$headers,0);
288   $table->start();
289   foreach ($persons as $person) {
290     $table->row_start();
291     $table->cell($person['email']);
292     $table->cell($person['first_name']);
293     $table->cell($person['last_name']);
294     $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
295     $table->row_end();
296   }
297   $table->end();
298   $toggle->end();
299  }
300
301 $add_button = $form->submit_html ("add-slice","Create Slice");
302 print ("<div id='slice_add_button'> $add_button </div>");
303
304 $form->end();
305
306 // Print footer
307 include 'plc_footer.php';
308
309 ?>