ckp
[plewww.git] / planetlab / tags / nodegroups.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10
11 // Common functions
12 require_once 'plc_functions.php';
13 require_once 'plc_sorts.php';
14
15 // find person roles
16 $_person= $plc->person;
17 $_roles= $_person['role_ids'];
18
19
20 // if sent here from another page remove then redirect
21 if( $_GET['remove'] && $_GET['nodegroup_id'] ) {
22   $ng_id= $_GET['nodegroup_id'];
23   $node_id= $_GET['remove'];
24
25   $api->DeleteNodeFromNodeGroup( intval( $node_id ), intval( $ng_id ) );
26
27   header( "location: index.php?id=$node_id" );
28   exit();
29   
30 }
31
32
33 // Print header
34 require_once 'plc_drupal.php'; 
35 drupal_set_title('Node Groups');
36 include 'plc_header.php';
37
38
39 // if no id display list of nodegroups
40 if( !$_GET['id'] && !$_GET['nodegroup_id'] ) {
41   $nodegroup_info= $api->GetNodeGroups( NULL, array( "nodegroup_id", "name", "description" ) );
42
43   echo "<h2>Node Groups</h2>\n
44         <table cellpadding=2><thead><tr><th>Name</th><th>Description</th>";
45
46    // if admin we need to more cells
47    if(  in_array( "10", $_person['role_ids'] ) )
48      echo "<th></th><th></th>";
49    echo "</thead><tbody>";
50
51   foreach( $nodegroup_info as $type ) {
52     echo "<tr><td><a href='/db/nodes/node_groups.php?nodegroup_id=". $type['nodegroup_id'] ."'>". $type['name'] ."</a></td><td>". $type['description'] ."</td>";
53     // if admin display edit/delet links
54     if(  in_array( "10", $_person['role_ids'] ) ) {
55       echo "<td><a href='/db/nodes/node_groups.php?nodegroup_id=". $type['nodegroup_id'] ."'>Edit</a></td>";
56       echo plc_delete_link_button('node_groups.php?del_type=' . $type['nodegroup_id'],
57                                   $type['name']);
58       echo "</td>";
59     }
60     echo "</tr>\n";
61
62   }
63
64   echo "</tbody></table>\n";
65
66 }
67 // if id is set then show nodegroup info
68 elseif( $_GET['id'] ) {
69   $nodegroup_id= $_GET['id'];
70
71   $nodegroup_info= $api->GetNodeGroups( array( intval( $nodegroup_id ) ), 
72                                         array( "name", "nodegroup_id", "node_ids" ) );
73   $node_info= $api->GetNodes( $nodegroup_info[0]['node_ids'], array( "node_id", "hostname" ) );
74
75   //display info 
76   echo "<h2>Node Group ". $nodegroup_info[0]['name'] ."</h2>\n";
77
78   if( empty( $nodegroup_info[0]['node_ids'] ) )
79     echo "<p>No nodes in node group.";
80   else {
81     echo "<table cellpadding=2><thead><tr><th>Hostname</th>";
82
83     // if admin need more cells
84     if( in_array( 10, $_roles ) ) 
85       echo "<th>Remove</th>";
86
87     echo "</tr>\n";
88
89     foreach( $node_info as $node ) {
90       echo "<tr><td><a href='/db/nodes/index.php?id=". $node['node_id'] ."'>". $node['hostname'] ."</a></td>";
91
92       if( in_array( 10, $_roles ) )
93         echo "<td><a href='/db/nodes/node_groups.php?remove=". $node['node_id'] ."&nodegroup_id=". $nodegroup_id ."'>remove</a></td>";
94
95       echo "</tr>";
96
97     }
98
99     echo "</tbody></table>\n";
100
101   }
102
103 }
104 // if no id add else update
105 elseif( $_GET['add'] ) {
106   // add node group and redirect to update nodes for it
107   if( $_POST['add_sub'] ) {
108     $name= $_POST['name'];
109     $description= $_POST['description'];
110
111     $fields= array( 'name'=>$name, 'description'=>$description );
112
113     // add it
114     $api->AddNodeGroup( $fields );
115
116     // get nodegroup_id
117     $group_info= $api->GetNodeGroups( array( $name ), array( "nodegroup_id" ) );
118
119     // redirect
120     header( "location: node_groups.php?id=". $group_info[0]['nodegroup_id'] );
121     exit();
122
123   }
124   
125   // add form
126   echo "<form method=post action='node_groups.php?add=1'>";
127   echo "<h2>Create Node Group</h2>\n";
128   echo "<table><tbody>\n";
129   echo "<tr><th>Name: </th><td><input type=text name='name' value=''></td></tr>\n<tr><th>Description: </th><td><input type=text name='description' size=50></td></tr>\n";
130   echo "</tbody></table>\n";
131
132   echo "<br /><input type=submit value='Add Node Group' name='add_sub'>\n</form\n";
133
134   echo "<p><a href='index.php'>Back to Node Index</a>\n";
135   
136 }
137 elseif( $_GET['nodegroup_id'] )
138 {
139   // get node group id
140   $node_group_id= $_GET['nodegroup_id'];
141   
142   // if add node submitted, add
143   if( $_POST['add'] )
144   {
145     $add_nodes= $_POST['add_nodes'];
146
147     // add nodes to node group
148     foreach( $add_nodes as $add_node )
149     {
150       $api->AddNodeToNodeGroup( intval( $add_node ), intval( $node_group_id ) );
151
152     }
153     
154   }
155
156   // if remove node submitted, remove
157   if( $_POST['remove'] )
158   {
159     $rem_nodes= $_POST['rem_nodes'];
160
161     // remove nodes from node group
162     foreach( $rem_nodes as $rem_node )
163     {
164       $api->DeleteNodeFromNodeGroup( intval( $rem_node ), intval( $node_group_id ) );
165
166     }
167     
168   }
169
170   // update name and description
171   $name= $_POST['name'];
172   $description= $_POST['description'];
173
174   $fields= array();
175
176   if( $name )
177     $fields['name']= $name;
178
179   if( $description )
180     $fields['description']= $description;
181
182   // api call
183   if( !empty( $fields ) )
184     $api->UpdateNodeGroup( intval( $node_group_id ), $fields );
185
186   // get node_group info
187   $group_info= $api->GetNodeGroups( array( intval( $node_group_id ) ),
188                                     array( "node_ids", "name", "conf_file_ids", "description" ) );
189
190   $node_ids = $group_info[0]['node_ids'];
191   $name = $group_info[0]['name'];
192   $conf_file_ids = $group_info[0]['conf_file_ids'];
193   $description = $group_info[0]['description'];
194
195   // get node info
196   if( !empty( $node_ids ) )
197     $node_info= $api->GetNodes( $node_ids, 
198                                 array( "hostname", "node_id" ) );
199
200   // get site names and ids
201   $site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
202   sort_sites( $site_info );
203
204   // if site_id is in post use it, if not use the user's primary
205   if( $_POST['site_id'] )
206     $site_id= $_POST['site_id'];
207   else
208     $site_id= $_person['site_ids'][0];
209
210   // get site nodes for $site_id
211   $sid= intval( $site_id );
212   $site_node_info= $api->GetSites( array( $sid ), array( "node_ids" ) );
213   $site_nodes= $site_node_info[0]['node_ids'];
214
215
216   // gets all node_ids from site that arent already associated with the node group
217   foreach( $site_nodes as $snode) {
218     if( !in_array( $snode, $node_ids ) )
219       $snodes[]= $snode;
220
221   }
222
223   // Get node info from new list
224   if( !empty( $snodes ) )
225     $snode_info= $api->GetNodes( $snodes, array( "hostname", "node_id" ) );
226
227
228   // start form
229   echo "<form action='node_groups.php?nodegroup_id=$node_group_id' method=post name='fm'>\n";
230   echo "<h2>Update Node Group id $name</h2>\n";
231
232   echo "Select a site to add nodes from.<br />\n";
233   echo "<select name='site_id' onChange='submit()'>\n";
234
235   foreach( $site_info as $site ) {
236     echo "<option value=". $site['site_id'];
237     if( $site['site_id'] == $site_id )
238       echo " selected";
239     echo ">". $site['name'] ."</option>\n";
240     
241   }
242
243   echo "</select>\n";
244
245   echo "<hr />\n";
246
247   // show all availible nodes at $site_id
248   if( $snode_info ) {
249     echo $added;
250     echo "<table><tbody>\n";
251     
252     foreach( $snode_info as $snodes ) {
253       echo "<tr><td><input type=checkbox name='add_nodes[]' value=". $snodes['node_id'] ."> </td><td> ". $snodes['hostname'] ." </td></tr>\n";
254     
255     }
256     
257     echo "</tbody></table>\n";
258     echo "<p><input type=submit value='Add Nodes' name='add'>\n";
259
260   }
261   else
262     echo "<p>All nodes on site already added.\n";
263
264   echo "<hr />\n";
265
266   // show all nodes currently associated
267   echo $removed;
268   echo "<h5>Nodes already associated with node group</h5>\n";
269   if( $node_info ) {
270     echo "<u>Check boxes of nodes to remove:</u>\n";
271     echo "<table><tbody>\n";
272
273     foreach( $node_info as $node ) {
274       echo "<tr><td><input type=checkbox name='rem_nodes[]' value=". $node['node_id'] ."> </td><td> ". $node['hostname'] ." </td></tr>\n";
275     
276     }
277     
278     echo "</tbody></table>\n";
279     echo "<p><input type=submit value='Remove Nodes' name='remove'>\n";
280     
281   }
282   else
283     echo "<p>No nodes associated with node group.\n";
284
285   echo "<hr />\n";
286
287   echo "<table><tbody>\n";
288   echo "<tr><th>Name: </th><td><input type=text name='name' value='$name'></td></tr>\n";
289   echo "<tr><th>Description: </th><td><input type=text name='description' value='$description' size=50></td></tr>\n";
290
291   echo "</tbody></table>\n";
292   echo "<br /><input type=submit value='Update Node Group' name='update'>\n";
293
294   echo "</form>\n";
295
296   echo "<br /><a href='/db/nodes/node_groups.php'>Back to Node Group Index</a>\n";
297
298
299 }
300
301
302 // Print footer
303 include 'plc_footer.php';
304
305 ?>