svn:keywords
[plewww.git] / planetlab / slices / slice_nodes.php
1 <?php
2
3 // $Id$
4 // Thierry on 2007-02-20
5 // There's no reason why we should see this page with a foreign slice, at least
6 // so long as the UI is used in a natural way, given the UI's logic as of now
7 // however it's always possible that someone forges her own url like
8 // http://one-lab.org/db/slices/slice_nodes?id=176
9 // So just to be consistent, we protect ourselves against such a usage
10
11 // Require login
12 require_once 'plc_login.php';
13
14 // Get session and API handles
15 require_once 'plc_session.php';
16 global $plc, $api, $adm;
17
18 // Print header
19 require_once 'plc_drupal.php';
20 drupal_set_title('Slices');
21 include 'plc_header.php';
22
23 // Common functions
24 require_once 'plc_functions.php';
25 require_once 'plc_sorts.php';
26
27 // find person roles
28 $_person= $plc->person;
29 $_roles= $_person['role_ids'];
30
31 //print_r( $_person );
32
33 // if no id ... redirect to slice index
34 if( !$_GET['id'] && !$_POST['id'] ) {
35   header( "location: index.php" );
36   exit();
37  }
38
39
40 // get slice id from GET or POST
41 if( $_GET['id'] )
42   $slice_id= intval( $_GET['id'] );
43 elseif ( $_POST['id'] )
44   $slice_id= intval( $_POST['id'] );
45 else
46   echo "no slice_id<br />\n";
47
48
49 // if add node submitted add the nodes to slice
50 if( $_POST['add'] ) {
51   $add_nodes= $_POST['add_nodes'];
52   
53   foreach( $add_nodes as $nodes) {
54     $new_nodes[]= intval( $nodes );
55   }
56
57   // update it!
58   $api->AddSliceToNodes( $slice_id, $new_nodes );
59
60   $errors= $api->error();
61
62   if( empty( $errors ) )
63     $added= "<font color=blue>Nodes Added.</font><br /><br />";
64   else
65     $added= "<font color=red>Error: '$errors'</font><br /><br />";
66 }
67
68 // if rem node submitted remove the nodes from slice
69 if( $_POST['remove'] ) {
70   $rem_nodes= $_POST['rem_nodes'];
71
72   foreach( $rem_nodes as $nodes) {
73     $new_nodes[]= intval( $nodes );
74   }
75   
76   // Delete them!
77   $api->DeleteSliceFromNodes( $slice_id, $new_nodes );
78
79   $errors= $api->error();
80
81   if( empty( $errors ) )
82     $removed= "<font color=blue>Nodes Removed.</font><br />";
83   else
84     $removed= "<font color=red>Error: '$errors'</font><br /><br />";
85
86 }
87
88
89 // get slice info
90 $slice_info= $adm->GetSlices( array( $slice_id ), array( "name", "node_ids", "peer_id" ) );
91 $slice_readonly = $slice_info[0]['peer_id'];
92 drupal_set_title("Slice " . $slice_info[0]['name'] . " - Nodes");
93
94 // get node info
95 if( !empty( $slice_info[0]['node_ids'] ) )
96   $node_info= $adm->GetNodes( $slice_info[0]['node_ids'], array( "hostname", "node_id", "site_id" , "peer_id") );
97   
98 // get site names and ids
99 $site_info= $adm->GetSites( NULL, array( "site_id", "name", "peer_id" ) );
100 sort_sites( $site_info );
101
102 // if site_id is in post use it, if not use the user's primary
103 if( $_POST['site_id'] )
104   $site_id= $_POST['site_id'];
105 else
106   $site_id= $_person['site_ids'][0];
107
108
109 // get site nodes for $site_id
110 if( $site_id == 'all_site' ) {
111   $full_node_info= $adm->GetNodes( array("node_type","regular"),
112                                    array( "hostname", "node_id" , "peer_id", "boot_state","last_updated") );
113
114   $snode_info= array();
115   foreach( $full_node_info as $full_node ) {
116     if( !in_array( $full_node['node_id'], $slice_info[0]['node_ids'] ) )
117       $snode_info[]= $full_node;
118   }
119 }
120 else {
121   $sid= intval( $site_id );
122   $site_node_info= $adm->GetSites( array( $sid ), array( "node_ids" ) );
123   $site_nodes= $site_node_info[0]['node_ids'];
124         
125   // gets all node_ids from site that arent already associated with the slice
126   foreach( $site_nodes as $snode) {
127     if( !in_array( $snode, $slice_info[0]['node_ids'] ) )
128       $snodes[]= $snode;
129   }
130         
131   // Get node info from new list
132   if( !empty( $snodes ) )
133     $snode_info= $adm->GetNodes( $snodes, array( "hostname", "node_id" , "peer_id", "boot_state","last_updated" ) );
134   
135 }
136
137 // start form   
138 if ( $slice_readonly) 
139   echo "<div class='plc-foreign'>";
140 else
141   echo "<form action='slice_nodes.php?id=$slice_id' method=post>\n";
142
143 // section for adding nodes : for local slices only
144 if ( ! $slice_readonly ) {
145   echo "<hr />";
146   echo "<h5> Select a site to add nodes from.</h5>\n";
147   echo "<table><tr><td>";
148   if ($site_id != 'all_site') {
149     echo plc_comon_button("site_id",$site_id,"_blank");
150     echo "</td><td>";
151   }
152   echo "<select name='site_id' onChange='submit()'>\n";
153   echo "<option value='all_site'";
154   if( $site_id == 'all_site' )
155     echo " selected";
156   echo ">--All Sites--</option>\n";
157
158   foreach( $site_info as $site ) {
159     echo "<option value=". $site['site_id'];
160     if( $site['site_id'] == $site_id )
161       echo " selected";
162     if ($site["peer_id"]) 
163       echo " class='plc-foreign'";
164     echo ">". $site['name'] ."</option>\n";
165     
166   }
167   
168   echo "</select></td></tr></table>\n";
169
170   // show all availible nodes at $site_id
171   //echo "<pre>"; print_r( $snode_info ); echo "</pre>";
172   if( $snode_info ) {
173     echo $added;
174     echo "<table cellpadding=2><tbody >\n<tr>";
175     echo "<th></th> <th> check </th><th>Hostname</th><th> Boot State </th><th> Last Update</th>
176         </tr>";
177     foreach( $snode_info as $snodes ) {
178       $class="";
179       if ($snodes['peer_id']) {
180         $class="class='plc-foreign'";
181       } 
182       echo "<tr " . $class . "><td>";
183       echo plc_comon_button("node_id",$snodes['node_id'],"_blank");
184       echo "</td><td>";
185       echo "<input type=checkbox name='add_nodes[]' value=". $snodes['node_id'] .">";
186       echo "</td><td align='center'>";
187       echo $snodes['hostname'];
188       echo "</td><td align='center'>";
189       echo $snodes['boot_state'];
190       echo "</td><td align='center'>";
191       echo date('Y-m-d',$snodes['last_updated']);
192       echo "</td></tr>\n";
193     }
194   
195     echo "</tbody></table>\n";
196     echo "<p><input type=submit value='Add Nodes' name='add'>\n";
197   } else {
198     echo "<p>No site nodes or all are already added.\n";
199   }
200 }
201
202 echo "<hr />\n";
203
204 // show all nodes currently associated
205 echo $removed;
206 echo "<h5>Nodes currently associated with slice</h5>\n";
207 if( $node_info ) {
208   if ( ! $slice_readonly) {
209     echo "<u>Check boxes of nodes to remove:</u>\n";
210     echo "<table cellpadding=2><tbody><tr>\n";
211     echo "<th></th> <th> check </th><th>Hostname</th><th> Boot State </th><th> Last Update</th>
212         </tr>";
213   } else {
214     echo "<table cellpadding=2><tbody><tr>\n";
215     echo "<th></th> <th> check </th><th>Hostname</th><th> Boot State </th><th> Last Update</th>
216         </tr>";
217   }
218
219   foreach( $node_info as $node ) {
220     $class="";
221       if ($node['peer_id']) {
222         $class="class='plc-foreign'";
223       } 
224     if ( ! $slice_readonly) {
225       echo "<tr " . $class . "><td>";
226       echo plc_comon_button("node_id",$node['node_id'],"_blank");
227       echo "</td><td>";
228       echo "<input type=checkbox name='rem_nodes[]' value=". $node['node_id'] .">";
229       echo "</td><td>" ;
230       echo $node['hostname'];
231       echo "</td><td align='center'>";
232       echo $snodes['boot_state'];
233       echo "</td><td align='center'>";
234       echo date('Y-m-d',$snodes['last_updated']);
235       echo "</td></tr>\n";
236     } else {
237       echo "<tr " . $class . "><td>";
238       echo plc_comon_button("node_id",$node['node_id'],"_blank");
239       echo "</td><td>" ;
240       echo $node['hostname'];
241       echo "</td></tr>";
242     }
243   
244   }
245   
246   echo "</tbody></table>\n";
247   if ( ! $slice_readonly) 
248     echo "<p><input type=submit value='Remove Nodes' name='remove'>\n";
249   
250 } else {
251   echo "<p>No nodes associated with slice.\n";
252 }
253
254 if ($slice_readonly)
255   echo "</div>";
256  else 
257    echo "</form>";
258
259 echo "<p><a href='index.php?id=$slice_id'>Back to Slice</a>\n";
260
261
262 // Print footer
263 include 'plc_footer.php';
264
265 ?>