user in several sites gets 'All My Sites Nodes' instead of 'My Site Nodes'
[plewww.git] / planetlab / nodes / nodes.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 'nifty.php';
22
23 ini_set("memory_limit","48M");
24
25 // -------------------- 
26 // recognized URL arguments
27 $peerscope=$_GET['peerscope'];
28 $pattern=$_GET['pattern'];
29 $site_id=intval($_GET['site_id']);
30 $slice_id=intval($_GET['slice_id']);
31 $person_id=intval($_GET['person_id']);
32
33 // --- decoration
34 $title="Nodes";
35 $tabs=array();
36 $tabs []= tab_nodes_mysite();
37 $tabs []= tab_nodes_local();
38
39 // -------------------- 
40 $node_filter=array();
41
42 //////////////////
43 // performs sanity check and summarize the result in a single column
44 function node_status ($node) {
45
46   // do all this stuff on local nodes only
47   if ( $node['peer_id'] )
48     return "n/a";
49
50   $messages=array();
51   // check that the node has interfaces
52   if (count($node['interface_ids']) == 0) {
53     $messages [] = "No interface";
54   }
55   return plc_vertical_table($messages,'plc-warning');
56 }
57
58 // fetch nodes 
59 $node_columns=array('hostname','node_type','site_id','node_id','boot_state','interface_ids','peer_id', 'arch','slice_ids');
60 // server-side filtering - set pattern in $_GET for filtering on hostname
61 if ($pattern) {
62   $node_filter['hostname']=$pattern;
63   $title .= " matching " . $pattern;
64  } else {
65   $node_filter['hostname']="*";
66  }
67
68 // server-side selection on peerscope
69 $peerscope=new PeerScope($api,$_GET['peerscope']);
70 $node_filter=array_merge($node_filter,$peerscope->filter());
71 $title .= ' - ' . $peerscope->label();
72
73 if ($site_id) {
74   $sites=$api->GetSites(array($site_id));
75   $site=$sites[0];
76   $name=$site['name'];
77   $login_base=$site['login_base'];
78   $title .= t_site($site);
79   $tabs []= tab_site($site);
80   $node_filter['site_id']=array($site_id);
81 }
82
83 if ($slice_id) {
84   $slices=$api->GetSlices(array($slice_id),array('node_ids','name'));
85   $slice=$slices[0];
86   $title .= t_slice($slice);
87   $tabs []= tab_slice($slice);
88   $node_filter['node_id'] = $slice['node_ids'];
89  }
90
91 // person_id is set : this is mostly oriented towards people managing several sites
92 if ($person_id) {
93   // avoid doing a useless call to GetPersons if the person_id is already known though $plc,
94   // as this is mostly done for the 'all my sites nodes' link
95   if ($person_id == plc_my_person_id()) { 
96     $person=plc_my_person();
97     $site_ids = plc_my_site_ids();
98   } else {
99     // fetch the person's site_ids
100     $persons = $api->GetPersons(array('person_id'=>$person_id),array('person_id','email','site_ids'));
101     $person=$persons[0];
102     $site_ids=$person['site_ids'];
103   }
104   $title .= t_person($person);
105   $node_filter['site_id']=$site_ids;
106  }
107
108 // go
109 $nodes=$api->GetNodes($node_filter,$node_columns);
110
111 // build site_ids - interface_ids
112 $site_ids=array();
113 $interface_ids=array();
114 if ($nodes) foreach ($nodes as $node) {
115   $site_ids []= $node['site_id'];
116   $interface_ids = array_merge ($interface_ids,$node['interface_ids']);
117 }
118
119 // fetch related interfaces
120 $interface_columns=array('ip','node_id','interface_id');
121 $interface_filter=array('is_primary'=>TRUE,'interface_id'=>$interface_ids);
122 $interfaces=$api->GetInterfaces($interface_filter,$interface_columns);
123
124 $interface_hash=array();
125 foreach ($interfaces as $interface) $interface_hash[$interface['node_id']]=$interface;
126
127 // fetch related sites
128 $site_columns=array('site_id','login_base');
129 $site_filter=array('site_id'=>$site_ids);
130 $sites=$api->GetSites($site_filter,$site_columns);
131
132 $site_hash=array();
133 foreach ($sites as $site) $site_hash[$site['site_id']]=$site;
134
135 // --------------------
136 drupal_set_title($title);
137
138 plekit_linetabs($tabs);
139
140 if ( ! $nodes ) {
141   drupal_set_message ('No node found');
142   return;
143  }
144   
145 $nifty=new PlekitNifty ('','objects-list','big');
146 $nifty->start();
147 $headers = array (); $offset=0;
148 if (plc_is_admin()) { $headers["I"]="int"; $offset=1; }
149 $headers["P"]="string";
150 $headers["R"]="string";
151 $headers["Site"]="string";
152 $headers["State"]="string";
153 $headers["Hostname"]="string";
154 $headers["Type"]="string";
155 $headers["IP"]="IPAddress";
156 $headers["A"]="string";
157 $headers["S"]='int';
158 $headers["?"]="string";
159
160 # initial sort on hostnames
161 $table=new PlekitTable ("nodes",$headers,4+$offset);
162 $table->start();
163
164 $peers = new Peers ($api);
165 // write rows
166 foreach ($nodes as $node) {
167   $hostname=$node['hostname'];
168   $node_id=$node['node_id'];
169   $site_id=$node['site_id'];
170   $site=$site_hash[$site_id];
171   $login_base = $site['login_base'];
172   $ip=$interface_hash[$node['node_id']]['ip'];
173   $interface_id=$interface_hash[$node['node_id']]['interface_id'];
174   $peer_id=$node['peer_id'];
175   $node_type = $node['node_type'];
176   
177   $table->row_start();
178   if (plc_is_admin()) $table->cell(l_node_t($node_id,$node_id));
179   $peers->cell ($table,$peer_id);
180   $table->cell (topdomain($hostname));
181   $table->cell (l_site_t($site_id,$login_base));
182   $table->cell ($node['boot_state']);
183   $table->cell (l_node_t($node_id,$hostname));
184   $table->cell ($node_type);
185   $table->cell (l_interface_t($interface_id,$ip),array('only-if'=> !$peer_id));
186   $table->cell ($node['arch'],array('only-if'=> !$peer_id));
187   $table->cell (count($node['slice_ids']));
188   $table->cell (node_status($node));
189   $table->row_end();
190   
191 }
192
193 $notes=array();
194 if (plc_is_admin()) $notes []= "I = node_id";
195 $notes []= "R = region";
196 $notes []= "A = arch";
197 $notes []= "S = number of slivers";
198 $notes []= "? = status";
199 $table->end(array('notes'=>$notes));
200 $nifty->end();
201
202 //plekit_linetabs ($tabs,"bottom");
203
204 // Print footer
205 include 'plc_footer.php';
206
207 ?>