redo http://svn.planet-lab.org/changeset/13771 from scratch
[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 // available actions
116 if ( $is_site_admin)
117   $tabs['Expire slices'] = array('url'=>l_actions(),
118                                  'method'=>'POST',
119                                  'values'=>array('site_id'=>$site_id,
120                                                  'action'=>'expire-all-slices-in-site'),
121                                  'bubble'=>"Expire all slices and prevent creation of new slices",
122                                  'confirm'=>"Suspend all slices in $login_base");
123 if ( $is_site_admin)
124   $tabs['Delete']=array('url'=>l_actions(),
125                         'method'=>'POST',
126                         'values'=>array('site_id'=>$site_id,
127                                         'action'=>'delete-site'),
128                         'bubble'=>"Delete site $sitename",
129                         'confirm'=>"Are you sure you want to delete site $login_base");
130
131 if ( $is_site_pi ) 
132   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
133                               'method'=>'post',
134                               'bubble'=>'Create new slice in site');
135
136 if (plc_is_admin() || plc_in_site($site_id))
137   $tabs["Events"]=array_merge (tablook_event(),
138                                array('url'=>l_event("Site","site",$site_id),
139                                      'bubble'=>"Events for site $sitename"));
140 if (plc_is_admin() || plc_in_site($site_id))
141   $tabs["Comon"]=array_merge(tablook_comon(),
142                              array('url'=>l_comon("site_id",$site_id),
143                                    'bubble'=>"Comon page for $sitename"));
144
145
146 plekit_linetabs($tabs);
147
148 // show gray background on foreign objects : start a <div> with proper class
149 $peers->block_start ($peer_id);
150
151 if ( $local_peer && ( ! $enabled ) )
152   plc_warning ("This site is not enabled - Please visit " . 
153                href (l_sites_pending(),"this page") . 
154                " to review pending applications.");
155
156 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
157
158
159 $toggle = new PlekitToggle ('site',"Details",
160                             array('visible'=>get_arg('show_details',true),
161                                   'bubble'=>'Display and modify details for that site'));
162 $toggle->start();
163
164 $details = new PlekitDetails($can_update);
165
166 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
167
168 $details->start();
169
170 if ( ! $site['is_public']) 
171   $details->tr(plc_warning_html("This site is not public!"));
172
173 $details->th_td("Full name",$sitename,'name',array('width'=>50));
174 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
175 $details->th_td("URL",$site_url,'url',array('width'=>40));
176 $details->th_td("Latitude",$site_lat,'latitude');
177 $details->th_td("Longitude",$site_long,'longitude');
178
179 // modifiable by admins only
180 if (plc_is_admin()) 
181   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
182 else
183   $details->th_td("Login base",$login_base);
184 if (plc_is_admin())
185   $details->th_td("Max slices",$max_slices,'max_slices');
186 else
187   $details->th_td("Max slices",$max_slices);
188 if (plc_is_admin())
189 {
190   $selectors=array(array('display'=>"False",'value'=>'0'), 
191                                    array('display'=>"True",'value'=>'1'));
192   $selectors[intval($enabled)]['selected'] = 'selected';
193
194   $enable_select = $f->select_html ("enabled", $selectors);
195
196   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
197 } else
198   $details->th_td("Enabled",$enabled);
199
200 $details->tr_submit("submit","Update Site");
201
202 if ( ! $local_peer) {
203   $details->space();
204   $details->th_td("Peer",$peers->peer_link($peer_id));
205  }
206 $details->end();
207 $details->form_end();
208 $toggle->end();
209
210 //////////////////// mode details - for local object
211 if ( $local_peer ) {
212
213   //////////////////// nodes
214   $nb_boot = 0;
215   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
216
217   $nodes_title = "Nodes : ";
218   $nodes_title .= count($nodes) . " total";
219   $nodes_title .= " / " . $nb_boot . " boot";
220   if ($nb_boot < 2 ) 
221     $nodes_title = plc_warning_html ($nodes_title);
222   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
223
224   $toggle=new PlekitToggle ('nodes',$nodes_title,
225                             array('visible'=>get_arg('show_nodes',true)));
226   $toggle->start();
227
228   $headers=array();
229   $sort_column = '0';
230   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
231   $headers['hostname']='string'; 
232   $headers['state']='string';
233
234   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
235                                                                 'notes_area'=>false,
236                                                                 'pagesize_area'=>false));
237   // hash pcus on pcu_id
238   if ($display_pcus) {
239     global $pcu_hash;
240     $pcu_hash= array();
241     if ($pcus) foreach ($pcus as $pcu) $pcu_hash[$pcu['pcu_id']]=$pcu;
242   }
243   // search the pcu, return the string to display and mark the pcu as displayed
244   //  function display_and_mark ($pcu_hash,$pcu_ids,$ports) {
245   function display_and_mark ($pcu_ids,$ports) {
246     global $pcu_hash;
247     if (empty($pcu_ids)) return plc_warning_html('None');
248     $pcu_id=$pcu_ids[0];
249     if (empty($ports)) return plc_error_html('???');
250     $port=$ports[0];
251     $pcu=$pcu_hash[$pcu_id];
252     $display= $pcu['hostname'] . ' : ' . $port;
253     $pcu_hash[$pcu_id]['displayed']=true;
254     return $display;
255   }
256
257   $table->start();
258   foreach ($nodes as $node) {
259     $table->row_start();
260     if ($display_pcus) {
261       //      $table->cell(display_and_mark($pcu_hash,$node['pcu_ids'],$node['ports']));
262       $table->cell(display_and_mark($node['pcu_ids'],$node['ports']));
263     }
264     $table->cell (l_node_obj($node));
265     $table->cell ($node['boot_state']);
266     $table->row_end();
267   }
268   // show undisplayed PCU's if any
269   if ($display_pcus) 
270     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
271         if (!$pcu['displayed']) {
272           $table->row_start();
273           $table->cell($pcu['hostname']); $table->cell(''); $table->cell('');
274           $table->row_end();
275         }
276       }
277     
278   $table->tfoot_start();
279   $table->row_start();
280   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
281   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
282   $table->row_end();
283   $table->end();
284   $toggle->end();
285     
286   //////////////////// Users
287   $persons_title = "Users : ";
288   $persons_title .= count($person_ids) . " total";
289   $persons_title .= " / " . count ($pis) . " PIs";
290   $persons_title .= " / " . count ($techs) . " Techs";
291   if ($has_disabled_persons) 
292     $persons_title .= " / " . count($disabled_persons) . " Disabled";
293   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
294     $persons_title = plc_warning_html ($persons_title);
295   $persons_title .= href(l_persons_site($site_id)," (See as users)");
296
297   $toggle=new PlekitToggle ('persons',$persons_title,
298                             array('visible'=>get_arg('show_persons',false)));
299   $toggle->start();
300
301   $headers = array ();
302   $headers["email"]='string';
303   $headers["S"]='int';
304   $headers["PI"]='string';
305   $headers['User']='string';
306   $headers["Tech"]='string';
307   if ($has_disabled_persons) $headers["Disabled"]='string';
308   $notes=array('S = slices');
309   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
310                                                             'notes'=>$notes,
311                                                             'pagesize_area'=>false));
312   $table->start();
313   if ($persons) foreach ($persons as $person) {
314     $table->row_start();
315     $table->cell(l_person_obj($person));
316     $table->cell(count($person['slice_ids']));
317     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
318     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
319     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
320     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
321     $table->row_end();
322   }
323   $table->end();
324   $toggle->end();
325
326   //////////////////// Slices
327   $slices_title="Slices : ";
328   $slices_title .= $max_slices . " max";
329   $slices_title .= " / " . count($slice_ids) . " running";
330   $slices_title .= " / $slivers_count slivers";
331   if (count($slice_ids) >= $max_slices) 
332     $slices_title = plc_warning_html($slices_title);
333   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
334   
335   $toggle=new PlekitToggle ('slices',$slices_title,
336                             array('visible'=>get_arg('show_slices',false)));
337   $toggle->start();
338
339   $headers = array ();
340   $headers ['name']='string';
341   $headers ['I'] = 'string';
342   $headers ['N']='int';
343   $headers ['U']='int';
344   $notes=array('I = instantiation type',
345                'N = number of nodes',
346                'U = number of users');
347   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
348                                                     'pagesize_area'=>false,
349                                                     'notes'=>$notes));
350
351   $table->start();
352   if ($slices) foreach ($slices as $slice) {
353       $table->row_start();
354       $table->cell(l_slice_obj($slice));
355       $table->cell(instantiation_label($slice));
356       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
357       $table->cell (count($slice['person_ids']));
358       $table->row_end();
359     }
360   if ($is_site_pi) {
361     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
362     $table->tfoot_start();
363     $table->row_start();
364     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
365   }
366     
367   $table->end();
368   $toggle->end();
369
370   //////////////////// Addresses
371   $toggle=new PlekitToggle ('addresses',"Addresses",
372                             array('visible'=>get_arg('show_addresses',false)));
373   $toggle->start();
374   if ( ! $addresses) {
375     print "<p class='addresses'>No known address for this site</p>";
376   } else {
377     $details=new PlekitDetails (false);
378     $details->start();
379     $details->th_td("Addresses","");
380     foreach ($addresses as $address) {
381       $details->th_td(plc_vertical_table($address['address_types']),
382                        plc_vertical_table(array($address['line1'],
383                                                 $address['line2'],
384                                                 $address['line3'],
385                                                 $address['city'],
386                                                 $address['state'],
387                                                 $address['postalcode'],
388                                                 $address['country'])));
389     }
390     $details->end();
391   }
392   $toggle->end();
393
394  }
395
396 ////////////////////////////////////////
397 $peers->block_end($peer_id);
398
399 //plekit_linetabs ($tabs,"bottom");
400
401 // Print footer
402 include 'plc_footer.php';
403
404 ?>