use ext_consortium_id for pending sites
[plewww.git] / planetlab / sites / site.php
1 <?php
2
3   // $Id$
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_peers.php';
19 require_once 'linetabs.php';
20 require_once 'table.php';
21 require_once 'details.php';
22 require_once 'form.php';
23 require_once 'toggle.php';
24
25 // -------------------- 
26 // recognized URL arguments
27 $site_id=intval($_GET['id']);
28 if ( ! $site_id ) { plc_error('Malformed URL - id not set'); return; }
29
30 ////////////////////
31 // Get all columns as we focus on only one entry
32 $sites = $api->GetSites( array($site_id));
33
34 if (empty($sites)) {
35   drupal_set_message ("Site " . $site_id . " not found");
36   return;
37  }
38
39 $site=$sites[0];
40 // var names to api return
41 $sitename= htmlentities($site['name']);
42 $abbreviated_name= htmlentities($site['abbreviated_name']);
43 $site_url= $site['url'];
44 $login_base= $site['login_base'];
45 $site_lat= $site['latitude'];
46 $site_long= $site['longitude'];
47 $max_slivers= $site['max_slivers'];
48 $max_slices= $site['max_slices'];
49
50 $enabled = $site['enabled'];
51
52 // get peer 
53 $peer_id= $site['peer_id'];
54 $peers = new Peers ($api);
55 $local_peer = ! $peer_id;
56
57 // extra privileges to admins, and (pi||tech) on this site
58 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
59 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
60 $is_site_admin = ($local_peer && plc_is_admin());
61   
62 $address_ids= $site['address_ids'];
63 $pcu_ids= $site['pcu_ids'];
64 $node_ids= $site['node_ids'];
65 $person_ids= $site['person_ids'];
66 $slice_ids= $site['slice_ids'];
67
68 $api->begin();
69 // gets address info
70 $api->GetAddresses( $address_ids );
71
72 // gets node info
73 $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
74
75 // gets person info
76 $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
77
78 $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
79
80 ////////////////////
81 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
82 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
83 if ($display_pcus) 
84   $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
85
86 // get results
87 if ($display_pcus)
88   list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
89 else
90   list( $addresses, $nodes, $persons, $slices )= $api->commit();
91   
92 $techs = array();
93 $pis = array();
94 $disabled_persons = array();
95 if ($persons) foreach( $persons as $person ) {
96   $role_ids= $person['role_ids'];
97
98   if ( in_array( '20', $role_ids ))     $pis[] = $person;
99   if ( in_array( '40', $role_ids ))     $techs[] = $person;
100   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
101   
102 }
103
104 $has_disabled_persons = count ($disabled_persons) !=0;
105
106 // get number of slivers
107 $slivers_count=0;
108 if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_ids']);
109
110 ////////////////////////////////////////
111 drupal_set_title("Details for site " . $sitename);
112   
113 $tabs=array();
114
115 $tabs []= tab_mysite();
116
117 // available actions
118 if ( $is_site_admin)
119   $tabs['Expire slices'] = array('url'=>l_actions(),
120                                  'method'=>'POST',
121                                  'values'=>array('site_id'=>$site_id,
122                                                  'action'=>'expire-all-slices-in-site'),
123                                  'bubble'=>"Expire all slices and prevent creation of new slices",
124                                  'confirm'=>"Suspend all slices in $login_base");
125 if ( $is_site_admin)
126   $tabs['Delete']=array('url'=>l_actions(),
127                         'method'=>'POST',
128                         'values'=>array('site_id'=>$site_id,
129                                         'action'=>'delete-site'),
130                         'bubble'=>"Delete site $sitename",
131                         'confirm'=>"Are you sure you want to delete site $login_base");
132
133 if ( $is_site_pi ) 
134   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
135                               'method'=>'post',
136                               'bubble'=>'Create new slice in site');
137
138 if (plc_is_admin() || plc_in_site($site_id))
139   $tabs["Events"]=array_merge (tablook_event(),
140                                array('url'=>l_event("Site","site",$site_id),
141                                      'bubble'=>"Events for site $sitename"));
142 if (plc_is_admin() || plc_in_site($site_id))
143   $tabs["Comon"]=array_merge(tablook_comon(),
144                              array('url'=>l_comon("site_id",$site_id),
145                                    'bubble'=>"Comon page for $sitename"));
146
147
148 plekit_linetabs($tabs);
149
150 // show gray background on foreign objects : start a <div> with proper class
151 $peers->block_start ($peer_id);
152
153 if ( $local_peer && ( ! $enabled ) ) {
154     if ($site['ext_consortium_id'] == $PENDING_CONSORTIUM_ID) {
155         plc_warning ("This site is not enabled - Please visit " . 
156                      href (l_sites_pending(),"this page") . 
157                      " to review pending applications.");
158     } else {
159         plc_warning ("This site is disabled.");
160     }
161 }
162
163 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
164
165
166 $toggle = new PlekitToggle ('site',"Details",
167                             array('visible'=>get_arg('show_details',true),
168                                   'bubble'=>'Display and modify details for that site'));
169 $toggle->start();
170
171 $details = new PlekitDetails($can_update);
172
173 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
174
175 $details->start();
176
177 if ( ! $site['is_public']) 
178   $details->tr(plc_warning_html("This site is not public!"));
179
180 $details->th_td("Full name",$sitename,'name',array('width'=>50));
181 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
182 $details->th_td("URL",$site_url,'url',array('width'=>40));
183 $details->th_td("Latitude",$site_lat,'latitude');
184 $details->th_td("Longitude",$site_long,'longitude');
185
186 // modifiable by admins only
187 if (plc_is_admin()) 
188   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
189 else
190   $details->th_td("Login base",$login_base);
191 if (plc_is_admin())
192   $details->th_td("Max slices",$max_slices,'max_slices');
193 else
194   $details->th_td("Max slices",$max_slices);
195 if (plc_is_admin())
196 {
197   $selectors=array(array('display'=>"False",'value'=>'0'), 
198                                    array('display'=>"True",'value'=>'1'));
199   $selectors[intval($enabled)]['selected'] = 'selected';
200
201   $enable_select = $f->select_html ("enabled", $selectors);
202
203   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
204 } else
205   $details->th_td("Enabled",$enabled);
206
207 $details->tr_submit("submit","Update Site");
208
209 if ( ! $local_peer) {
210   $details->space();
211   $details->th_td("Peer",$peers->peer_link($peer_id));
212  }
213 $details->end();
214 $details->form_end();
215 $toggle->end();
216
217 //////////////////// mode details - for local object
218 if ( $local_peer ) {
219
220   //////////////////// nodes
221   $nb_boot = 0;
222   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
223
224   $nodes_title = "Nodes : ";
225   $nodes_title .= count($nodes) . " total";
226   $nodes_title .= " / " . $nb_boot . " boot";
227   if ($nb_boot < 2 ) 
228     $nodes_title = plc_warning_html ($nodes_title);
229   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
230
231   $toggle=new PlekitToggle ('nodes',$nodes_title,
232                             array('visible'=>get_arg('show_nodes',true)));
233   $toggle->start();
234
235   $headers=array();
236   $sort_column = '0';
237   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
238   $headers['hostname']='string'; 
239   $headers['state']='string';
240
241   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
242                                                                 'notes_area'=>false,
243                                                                 'pagesize_area'=>false));
244   // hash pcus on pcu_id
245   if ($display_pcus) {
246     global $pcu_hash;
247     $pcu_hash= array();
248     if ($pcus) foreach ($pcus as $pcu) $pcu_hash[$pcu['pcu_id']]=$pcu;
249   }
250   // search the pcu, return the string to display and mark the pcu as displayed
251   //  function display_and_mark ($pcu_hash,$pcu_ids,$ports) {
252   function display_and_mark ($pcu_ids,$ports) {
253     global $pcu_hash;
254     if (empty($pcu_ids)) return plc_warning_html('None');
255     $pcu_id=$pcu_ids[0];
256     if (empty($ports)) return plc_error_html('???');
257     $port=$ports[0];
258     $pcu=$pcu_hash[$pcu_id];
259     $display= $pcu['hostname'] . ' : ' . $port;
260     $pcu_hash[$pcu_id]['displayed']=true;
261     return $display;
262   }
263
264   $table->start();
265   foreach ($nodes as $node) {
266     $table->row_start();
267     if ($display_pcus) {
268       //      $table->cell(display_and_mark($pcu_hash,$node['pcu_ids'],$node['ports']));
269       $table->cell(display_and_mark($node['pcu_ids'],$node['ports']));
270     }
271     $table->cell (l_node_obj($node));
272     $table->cell ($node['boot_state']);
273     $table->row_end();
274   }
275   // show undisplayed PCU's if any
276   if ($display_pcus) 
277     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
278         if (!$pcu['displayed']) {
279           $table->row_start();
280           $table->cell($pcu['hostname']); $table->cell(''); $table->cell('');
281           $table->row_end();
282         }
283       }
284     
285   $table->tfoot_start();
286   $table->row_start();
287   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
288   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
289   $table->row_end();
290   $table->end();
291   $toggle->end();
292     
293   //////////////////// Users
294   $persons_title = "Users : ";
295   $persons_title .= count($person_ids) . " total";
296   $persons_title .= " / " . count ($pis) . " PIs";
297   $persons_title .= " / " . count ($techs) . " Techs";
298   if ($has_disabled_persons) 
299     $persons_title .= " / " . count($disabled_persons) . " Disabled";
300   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
301     $persons_title = plc_warning_html ($persons_title);
302   $persons_title .= href(l_persons_site($site_id)," (See as users)");
303
304   $toggle=new PlekitToggle ('persons',$persons_title,
305                             array('visible'=>get_arg('show_persons',false)));
306   $toggle->start();
307
308   $headers = array ();
309   $headers["email"]='string';
310   $headers["S"]='int';
311   $headers["PI"]='string';
312   $headers['User']='string';
313   $headers["Tech"]='string';
314   if ($has_disabled_persons) $headers["Disabled"]='string';
315   $notes=array('S = slices');
316   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
317                                                             'notes'=>$notes,
318                                                             'pagesize_area'=>false));
319   $table->start();
320   if ($persons) foreach ($persons as $person) {
321     $table->row_start();
322     $table->cell(l_person_obj($person));
323     $table->cell(count($person['slice_ids']));
324     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
325     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
326     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
327     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
328     $table->row_end();
329   }
330   $table->end();
331   $toggle->end();
332
333   //////////////////// Slices
334   $slices_title="Slices : ";
335   $slices_title .= $max_slices . " max";
336   $slices_title .= " / " . count($slice_ids) . " running";
337   $slices_title .= " / $slivers_count slivers";
338   if (count($slice_ids) >= $max_slices) 
339     $slices_title = plc_warning_html($slices_title);
340   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
341   
342   $toggle=new PlekitToggle ('slices',$slices_title,
343                             array('visible'=>get_arg('show_slices',false)));
344   $toggle->start();
345
346   $headers = array ();
347   $headers ['name']='string';
348   $headers ['I'] = 'string';
349   $headers ['N']='int';
350   $headers ['U']='int';
351   $notes=array('I = instantiation type',
352                'N = number of nodes',
353                'U = number of users');
354   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
355                                                     'pagesize_area'=>false,
356                                                     'notes'=>$notes));
357
358   $table->start();
359   if ($slices) foreach ($slices as $slice) {
360       $table->row_start();
361       $table->cell(l_slice_obj($slice));
362       $table->cell(instantiation_label($slice));
363       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
364       $table->cell (count($slice['person_ids']));
365       $table->row_end();
366     }
367   if ($is_site_pi) {
368     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
369     $table->tfoot_start();
370     $table->row_start();
371     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
372   }
373     
374   $table->end();
375   $toggle->end();
376
377   //////////////////// Addresses
378   $toggle=new PlekitToggle ('addresses',"Addresses",
379                             array('visible'=>get_arg('show_addresses',false)));
380   $toggle->start();
381   if ( ! $addresses) {
382     print "<p class='addresses'>No known address for this site</p>";
383   } else {
384     $details=new PlekitDetails (false);
385     $details->start();
386     $details->th_td("Addresses","");
387     foreach ($addresses as $address) {
388       $details->th_td(plc_vertical_table($address['address_types']),
389                        plc_vertical_table(array($address['line1'],
390                                                 $address['line2'],
391                                                 $address['line3'],
392                                                 $address['city'],
393                                                 $address['state'],
394                                                 $address['postalcode'],
395                                                 $address['country'])));
396     }
397     $details->end();
398   }
399   $toggle->end();
400
401  }
402
403 ////////////////////////////////////////
404 $peers->block_end($peer_id);
405
406 //plekit_linetabs ($tabs,"bottom");
407
408 // Print footer
409 include 'plc_footer.php';
410
411 ?>