add/remove node actions were missing
[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   plc_redirect( l_slices());
36  }
37
38
39 // get slice id from GET or POST
40 if( $_GET['id'] )
41   $slice_id= intval( $_GET['id'] );
42 elseif ( $_POST['id'] )
43   $slice_id= intval( $_POST['id'] );
44 else
45   echo "no slice_id<br />\n";
46
47
48 // if add node submitted add the nodes to slice
49 if( $_POST['add'] ) {
50   $add_nodes= $_POST['add_nodes'];
51   
52   foreach( $add_nodes as $nodes) {
53     $new_nodes[]= intval( $nodes );
54   }
55
56   // update it!
57   $api->AddSliceToNodes( $slice_id, $new_nodes );
58
59   $errors= $api->error();
60
61   if( empty( $errors ) )
62     $added= "<font color=blue>Nodes Added.</font><br /><br />";
63   else
64     $added= "<font color=red>Error: '$errors'</font><br /><br />";
65 }
66
67 // if rem node submitted remove the nodes from slice
68 if( $_POST['remove'] ) {
69   $rem_nodes= $_POST['rem_nodes'];
70
71   foreach( $rem_nodes as $nodes) {
72     $new_nodes[]= intval( $nodes );
73   }
74   
75   // Delete them!
76   $api->DeleteSliceFromNodes( $slice_id, $new_nodes );
77
78   $errors= $api->error();
79
80   if( empty( $errors ) )
81     $removed= "<font color=blue>Nodes Removed.</font><br />";
82   else
83     $removed= "<font color=red>Error: '$errors'</font><br /><br />";
84
85 }
86
87
88 // get slice info
89 $slice_info= $adm->GetSlices( array( $slice_id ), array( "name", "node_ids", "peer_id" ) );
90 $slice_readonly = $slice_info[0]['peer_id'];
91 drupal_set_title("Slice " . $slice_info[0]['name'] . " - Nodes");
92
93 // get node info
94 if( !empty( $slice_info[0]['node_ids'] ) )
95   $node_info= $adm->GetNodes( $slice_info[0]['node_ids'], array( "hostname", "node_id", "site_id" , "peer_id") );
96   
97 // get site names and ids
98 $site_info= $adm->GetSites( NULL, array( "site_id", "name", "peer_id" ) );
99 sort_sites( $site_info );
100
101 // if site_id is in post use it, if not use the user's primary
102 if( $_POST['site_id'] )
103   $site_id= $_POST['site_id'];
104 else
105   $site_id= $_person['site_ids'][0];
106
107
108 // get site nodes for $site_id
109 if( $site_id == 'all_site' ) {
110   $full_node_info= $adm->GetNodes( array("node_type","regular"),
111                                    array( "hostname", "node_id" , "peer_id", "boot_state","last_updated") );
112
113   $snode_info= array();
114   foreach( $full_node_info as $full_node ) {
115     if( !in_array( $full_node['node_id'], $slice_info[0]['node_ids'] ) )
116       $snode_info[]= $full_node;
117   }
118 }
119 else {
120   $sid= intval( $site_id );
121   $site_node_info= $adm->GetSites( array( $sid ), array( "node_ids" ) );
122   $site_nodes= $site_node_info[0]['node_ids'];
123         
124   // gets all node_ids from site that arent already associated with the slice
125   foreach( $site_nodes as $snode) {
126     if( !in_array( $snode, $slice_info[0]['node_ids'] ) )
127       $snodes[]= $snode;
128   }
129         
130   // Get node info from new list
131   if( !empty( $snodes ) )
132     $snode_info= $adm->GetNodes( $snodes, array( "hostname", "node_id" , "peer_id", "boot_state","last_updated" ) );
133   
134 }
135
136 // start form   
137 if ( $slice_readonly) 
138   echo "<div class='plc-foreign'>";
139 else
140   echo "<form action='slice_nodes.php?id=$slice_id' method=post>\n";
141
142 // section for adding nodes : for local slices only
143 if ( ! $slice_readonly ) {
144   echo "<hr />";
145   echo "<h5> Select a site to add nodes from.</h5>\n";
146   echo "<table><tr><td>";
147   if ($site_id != 'all_site') {
148     echo plc_comon_button("site_id",$site_id,"_blank");
149     echo "</td><td>";
150   }
151   echo "<select name='site_id' onChange='submit()'>\n";
152   echo "<option value='all_site'";
153   if( $site_id == 'all_site' )
154     echo " selected";
155   echo ">--All Sites--</option>\n";
156
157   foreach( $site_info as $site ) {
158     echo "<option value=". $site['site_id'];
159     if( $site['site_id'] == $site_id )
160       echo " selected";
161     if ($site["peer_id"]) 
162       echo " class='plc-foreign'";
163     echo ">". $site['name'] ."</option>\n";
164     
165   }
166   
167   echo "</select></td></tr></table>\n";
168
169   // show all availible nodes at $site_id
170   //echo "<pre>"; print_r( $snode_info ); echo "</pre>";
171   if( $snode_info ) {
172     echo $added;
173     echo "<table cellpadding=2><tbody >\n<tr>";
174     echo "<th></th> <th> check </th><th>Hostname</th><th> Boot State </th><th> Last Update</th>
175         </tr>";
176     foreach( $snode_info as $snodes ) {
177       $class="";
178       if ($snodes['peer_id']) {
179         $class="class='plc-foreign'";
180       } 
181       echo "<tr " . $class . "><td>";
182       echo plc_comon_button("node_id",$snodes['node_id'],"_blank");
183       echo "</td><td>";
184       echo "<input type=checkbox name='add_nodes[]' value=". $snodes['node_id'] .">";
185       echo "</td><td align='center'>";
186       echo $snodes['hostname'];
187       echo "</td><td align='center'>";
188       echo $snodes['boot_state'];
189       echo "</td><td align='center'>";
190       echo date('Y-m-d',$snodes['last_updated']);
191       echo "</td></tr>\n";
192     }
193   
194     echo "</tbody></table>\n";
195     echo "<p><input type=submit value='Add Nodes' name='add'>\n";
196   } else {
197     echo "<p>No site nodes or all are already added.\n";
198   }
199 }
200
201 echo "<hr />\n";
202
203 // show all nodes currently associated
204 echo $removed;
205 echo "<h5>Nodes currently associated with slice</h5>\n";
206 if( $node_info ) {
207   if ( ! $slice_readonly) {
208     echo "<u>Check boxes of nodes to remove:</u>\n";
209     echo "<table cellpadding=2><tbody><tr>\n";
210     echo "<th></th> <th> check </th><th>Hostname</th><th> Boot State </th><th> Last Update</th>
211         </tr>";
212   } else {
213     echo "<table cellpadding=2><tbody><tr>\n";
214     echo "<th></th> <th> check </th><th>Hostname</th><th> Boot State </th><th> Last Update</th>
215         </tr>";
216   }
217
218   foreach( $node_info as $node ) {
219     $class="";
220       if ($node['peer_id']) {
221         $class="class='plc-foreign'";
222       } 
223     if ( ! $slice_readonly) {
224       echo "<tr " . $class . "><td>";
225       echo plc_comon_button("node_id",$node['node_id'],"_blank");
226       echo "</td><td>";
227       echo "<input type=checkbox name='rem_nodes[]' value=". $node['node_id'] .">";
228       echo "</td><td>" ;
229       echo $node['hostname'];
230       echo "</td><td align='center'>";
231       echo $snodes['boot_state'];
232       echo "</td><td align='center'>";
233       echo date('Y-m-d',$snodes['last_updated']);
234       echo "</td></tr>\n";
235     } else {
236       echo "<tr " . $class . "><td>";
237       echo plc_comon_button("node_id",$node['node_id'],"_blank");
238       echo "</td><td>" ;
239       echo $node['hostname'];
240       echo "</td></tr>";
241     }
242   
243   }
244   
245   echo "</tbody></table>\n";
246   if ( ! $slice_readonly) 
247     echo "<p><input type=submit value='Remove Nodes' name='remove'>\n";
248   
249 } else {
250   echo "<p>No nodes associated with slice.\n";
251 }
252
253 if ($slice_readonly)
254   echo "</div>";
255  else 
256    echo "</form>";
257
258 echo "<p><a href='index.php?id=$slice_id'>Back to Slice</a>\n";
259
260
261 // Print footer
262 include 'plc_footer.php';
263
264 ?>