initial import from onelab svn codebase
[plewww.git] / planetlab / nodes / node_groups.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 ) ), array( "name", "nodegroup_id", "node_ids" ) );
72   $node_info= $api->GetNodes( $nodegroup_info[0]['node_ids'], array( "node_id", "hostname" ) );
73
74   //display info 
75   echo "<h2>Node Group ". $nodegroup_info[0]['name'] ."</h2>\n";
76
77   if( empty( $nodegroup_info[0]['node_ids'] ) )
78     echo "<p>No nodes in node group.";
79   else {
80     echo "<table cellpadding=2><thead><tr><th>Hostname</th>";
81
82     // if admin need more cells
83     if( in_array( 10, $_roles ) ) 
84       echo "<th>Remove</th>";
85
86     echo "</tr>\n";
87
88     foreach( $node_info as $node ) {
89       echo "<tr><td><a href='/db/nodes/index.php?id=". $node['node_id'] ."'>". $node['hostname'] ."</a></td>";
90
91       if( in_array( 10, $_roles ) )
92         echo "<td><a href='/db/nodes/node_groups.php?remove=". $node['node_id'] ."&nodegroup_id=". $nodegroup_id ."'>remove</a></td>";
93
94       echo "</tr>";
95
96     }
97
98     echo "</tbody></table>\n";
99
100   }
101
102 }
103 // if no id add else update
104 elseif( $_GET['add'] ) {
105   // add node group and redirect to update nodes for it
106   if( $_POST['add_sub'] ) {
107     $name= $_POST['name'];
108     $description= $_POST['description'];
109
110     $fields= array( 'name'=>$name, 'description'=>$description );
111
112     // add it
113     $api->AddNodeGroup( $fields );
114
115     // get nodegroup_id
116     $group_info= $api->GetNodeGroups( array( $name ), array( "nodegroup_id" ) );
117
118     // redirect
119     header( "location: node_groups.php?id=". $group_info[0]['nodegroup_id'] );
120     exit();
121
122   }
123   
124   // add form
125   echo "<form method=post action='node_groups.php?add=1'>";
126   echo "<h2>Create Node Group</h2>\n";
127   echo "<table><tbody>\n";
128   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";
129   echo "</tbody></table>\n";
130
131   echo "<br /><input type=submit value='Add Node Group' name='add_sub'>\n</form\n";
132
133   echo "<p><a href='index.php'>Back to Node Index</a>\n";
134   
135 }
136 elseif( $_GET['nodegroup_id'] )
137 {
138   // get node group id
139   $node_group_id= $_GET['nodegroup_id'];
140   
141   // if add node submitted, add
142   if( $_POST['add'] )
143   {
144     $add_nodes= $_POST['add_nodes'];
145
146     // add nodes to node group
147     foreach( $add_nodes as $add_node )
148     {
149       $api->AddNodeToNodeGroup( intval( $add_node ), intval( $node_group_id ) );
150
151     }
152     
153   }
154
155   // if remove node submitted, remove
156   if( $_POST['remove'] )
157   {
158     $rem_nodes= $_POST['rem_nodes'];
159
160     // remove nodes from node group
161     foreach( $rem_nodes as $rem_node )
162     {
163       $api->DeleteNodeFromNodeGroup( intval( $rem_node ), intval( $node_group_id ) );
164
165     }
166     
167   }
168
169   // update name and description
170   $name= $_POST['name'];
171   $description= $_POST['description'];
172
173   $fields= array();
174
175   if( $name )
176     $fields['name']= $name;
177
178   if( $description )
179     $fields['description']= $description;
180
181   // api call
182   if( !empty( $fields ) )
183     $api->UpdateNodeGroup( intval( $node_group_id ), $fields );
184
185   // get node_group info
186   $group_info= $api->GetNodeGroups( array( intval( $node_group_id ) ), array( "node_ids", "name", "conf_file_ids", "description" ) );
187
188   $node_ids = $group_info[0]['node_ids'];
189   $name = $group_info[0]['name'];
190   $conf_file_ids = $group_info[0]['conf_file_ids'];
191   $description = $group_info[0]['description'];
192
193   // get node info
194   if( !empty( $node_ids ) )
195     $node_info= $api->GetNodes( $node_ids, array( "hostname", "node_id" ) );
196
197   // get site names and ids
198   $site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
199   sort_sites( $site_info );
200
201   // if site_id is in post use it, if not use the user's primary
202   if( $_POST['site_id'] )
203     $site_id= $_POST['site_id'];
204   else
205     $site_id= $_person['site_ids'][0];
206
207   // get site nodes for $site_id
208   $sid= intval( $site_id );
209   $site_node_info= $api->GetSites( array( $sid ), array( "node_ids" ) );
210   $site_nodes= $site_node_info[0]['node_ids'];
211
212
213   // gets all node_ids from site that arent already associated with the node group
214   foreach( $site_nodes as $snode) {
215     if( !in_array( $snode, $node_ids ) )
216       $snodes[]= $snode;
217
218   }
219
220   // Get node info from new list
221   if( !empty( $snodes ) )
222     $snode_info= $api->GetNodes( $snodes, array( "hostname", "node_id" ) );
223
224
225   // start form
226   echo "<form action='node_groups.php?nodegroup_id=$node_group_id' method=post name='fm'>\n";
227   echo "<h2>Update Node Group id $name</h2>\n";
228
229   echo "Select a site to add nodes from.<br />\n";
230   echo "<select name='site_id' onChange='submit()'>\n";
231
232   foreach( $site_info as $site ) {
233     echo "<option value=". $site['site_id'];
234     if( $site['site_id'] == $site_id )
235       echo " selected";
236     echo ">". $site['name'] ."</option>\n";
237     
238   }
239
240   echo "</select>\n";
241
242   echo "<hr />\n";
243
244   // show all availible nodes at $site_id
245   if( $snode_info ) {
246     echo $added;
247     echo "<table><tbody>\n";
248     
249     foreach( $snode_info as $snodes ) {
250       echo "<tr><td><input type=checkbox name='add_nodes[]' value=". $snodes['node_id'] ."> </td><td> ". $snodes['hostname'] ." </td></tr>\n";
251     
252     }
253     
254     echo "</tbody></table>\n";
255     echo "<p><input type=submit value='Add Nodes' name='add'>\n";
256
257   }
258   else
259     echo "<p>All nodes on site already added.\n";
260
261   echo "<hr />\n";
262
263   // show all nodes currently associated
264   echo $removed;
265   echo "<h5>Nodes already associated with node group</h5>\n";
266   if( $node_info ) {
267     echo "<u>Check boxes of nodes to remove:</u>\n";
268     echo "<table><tbody>\n";
269
270     foreach( $node_info as $node ) {
271       echo "<tr><td><input type=checkbox name='rem_nodes[]' value=". $node['node_id'] ."> </td><td> ". $node['hostname'] ." </td></tr>\n";
272     
273     }
274     
275     echo "</tbody></table>\n";
276     echo "<p><input type=submit value='Remove Nodes' name='remove'>\n";
277     
278   }
279   else
280     echo "<p>No nodes associated with node group.\n";
281
282   echo "<hr />\n";
283
284   echo "<table><tbody>\n";
285   echo "<tr><th>Name: </th><td><input type=text name='name' value='$name'></td></tr>\n";
286   echo "<tr><th>Description: </th><td><input type=text name='description' value='$description' size=50></td></tr>\n";
287
288   echo "</tbody></table>\n";
289   echo "<br /><input type=submit value='Update Node Group' name='update'>\n";
290
291   echo "</form>\n";
292
293   echo "<br /><a href='/db/nodes/node_groups.php'>Back to Node Group Index</a>\n";
294
295
296 }
297
298
299 // Print footer
300 include 'plc_footer.php';
301
302 ?>