3f5c229a38b1c935e7173b429d0b5aa557402b04
[plewww.git] / planetlab / sites / pcu.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, $adm;
9
10 // Print header
11 require_once 'plc_drupal.php';
12 // set default
13 drupal_set_title('Sites');
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_sorts.php';
19
20 // find person roles
21 $_person= $plc->person;
22 $_roles= $_person['role_ids'];
23
24
25 // if no id: add, else: display(update)
26 if( !$_GET['id'] ) {
27   if( $_POST['submitted'] ) {
28     // get person's site id
29     $site_id= $_person['site_ids'][0];
30     
31     // build dict
32     $fields= array( 'protocol'=>$_POST['protocol'], 'hostname'=>$_POST['hostname'], 'model'=>$_POST['model'], 'username'=>$_POST['username'], 'password'=>$_POST['password'], 'notes'=>$_POST['notes'], 'ip'=>$_POST['ip'] );
33     
34     $pcu_id= $api->AddPCU( $site_id, $fields );
35     
36     if( $pcu_id != 0 ) {
37       header( "location: /db/sites/pcu.php?id=$pcu_id" );
38       exit();
39     } else {
40       $error= $api->error();
41     }
42   }
43   
44   if( !empty( $error ) )
45     echo '<div class="plc-warning">' . $error . '.</div>';
46   
47   echo "<h3>Add a PCU</h3>\n
48 <form action='/db/sites/pcu.php' method=post>\n
49 <p><table border=0 cellpadding=3><tbody>\n
50 <tr><th>Protocol: </th><td><input type=text name='protocol' value=''></td></tr>\n
51 <tr><th>Hostname: </th><td><input type=text name='hostname' size=40 value=''></td></tr>\n
52 <tr><th>IP Address: </th><td><input type=text name='ip' size=40 value=''></td></tr>\n
53 <tr><th>Model: </th><td><input type=text name='model' value=''></td></tr>\n
54 <tr><th>Username: </th><td><input type=text name='username' value=''></td></tr>\n
55 <tr><th>Password: </th><td><input type=text name='password' value=''></td></tr>\n
56 <tr><th>Notes: </th><td><input type=text name='notes' size=40 value=''></td></tr>\n
57 </tbody></table>\n
58 <br /><p><input type=submit name='submitted' value='Add New PCU'>\n";
59 } else {
60   // get PCU info
61   $pcu_id= intval( $_GET['id'] );
62   $pcu_info= $api->GetPCUs( array( intval( $pcu_id ) ) );
63   
64   // if remove is set remove the node from the pcu
65   if( $_GET['remove'] ) {
66     $rem_id= $_GET['remove'];
67     
68     $api->DeleteNodeFromPCU( intval( $rem_id ), $pcu_id );
69     
70     header( "Location: /db/sites/pcu.php?id=$pcu_id" );
71     exit();
72     
73   }
74   
75   //if submitted, update node info
76   if( $_POST['submitted'] ) {
77     $protocol= $_POST['protocol'];
78     $username= $_POST['username'];
79     $hostname= $_POST['hostname'];
80     $ipaddress= $_POST['ip'];
81     $model= $_POST['model'];
82     $password= $_POST['password'];
83     $notes= $_POST['notes'];
84     
85     $api->UpdatePCU( $pcu_id, array( "protocol"=>$protocol, "hostname"=>$hostname, "model"=>$model, "password"=>$password, "notes"=>$notes, "ip"=>$ipaddress ) );
86                 
87     header( "Location: /db/sites/pcu.php?id=$pcu_id" );
88     exit();
89                 
90   }
91         
92   if( in_array( 10, $_roles ) || ( in_array( 20, $_roles ) && in_array( $pcu_info[0]['site_id'], $_person['site_ids'] ) ) || ( in_array( 40, $_roles ) && in_array( $pcu_info[0]['site_id'], $_person['site_ids'] ) ) )
93     $pcu_controller= true;
94
95   // get PCU node info
96   $node_info= $api->GetNodes( $pcu_info[0]['node_ids'], array( "hostname", "node_id", "boot_state" ) );
97   echo "<form action='/db/sites/pcu.php?id=$pcu_id' method=post>\n
98 <h3>PCU: ". $pcu_info[0]['hostname'] ."</h3>\n
99 <p><table border=0 cellpadding=3><tbody>\n
100 <tr><th>Protocol: </th><td>";
101
102   if( $pcu_controller )
103     echo "<input type=text name='protocol' value='";
104   
105   echo $pcu_info[0]['protocol'];
106   
107   if( $pcu_controller )
108     echo "'>";
109   
110   echo "</td></tr>\n
111         <tr><th>Hostname: </th><td>";
112   
113   if( $pcu_controller )
114     echo "<input type=text name='hostname' size=40 value='";
115   
116   echo $pcu_info[0]['hostname'];
117   
118   echo "'>";
119   
120   echo "</td></tr>\n
121         <tr><th>IP Address: </th><td>";
122   if( $pcu_controller )
123     echo "<input type=text name='ip' value='";
124   
125   echo $pcu_info[0]['ip'];
126   
127   echo "'>";
128   
129   if( $pcu_controller )
130     echo "</td></tr>\n
131           <tr><th>Model: </th><td>";
132   
133   if( $pcu_controller )
134     echo "<input type=text name='model' value='";
135                 
136   echo $pcu_info[0]['model'];
137   
138   if( $pcu_controller )
139     echo "'>";
140   
141   echo "</td></tr>\n
142         <tr><th>Username: </th><td>";
143         
144   if( $pcu_controller )
145     echo "<input type=text name='username' value='";
146   
147   echo $pcu_info[0]['username'];
148   
149   if( $pcu_controller )
150     echo "'>";
151   
152   echo "</td></tr>\n
153         <tr><th>Password: </th><td>";
154         
155   if( $pcu_controller )
156     echo "<input type=text name='password' value='";
157                 
158   echo $pcu_info[0]['password'];
159   
160   if( $pcu_controller )
161     echo "'>";
162   
163   echo "</td></tr>\n
164         <tr><th>Notes: </th><td>";
165   
166   if( $pcu_controller )
167     echo "<input type=text name='notes' size=40 value='";
168                 
169   echo $pcu_info[0]['notes'];
170   
171   if( $pcu_controller )
172     echo "'>";
173                 
174   echo "</td></tr>\n
175         </tbody></table>\n";
176                 
177   if( $pcu_controller )
178     echo "<br /><p><input type=submit name='submitted' value='Update PCU'>\n";
179         
180   if( !empty( $node_info ) ) {
181     echo "<p><table border=0 cellpadding=3>\n<caption>Nodes</caption>\n<thead><tr><th></th><th>Hostname</td><th>State</th><th></th>";
182     
183     // if user can control PCU add table cells
184     if( $pcu_controller )
185       echo "<th></th>\n";
186     
187     echo "</tr></thead><tbody>\n";
188     
189     // for port numbers
190     $count= 0;
191     
192     foreach( $node_info as $node ) {
193       echo "<tr><td>". $pcu_info[0]['ports'][$count] ."</td><td><a href='/db/nodes/index.php?id=". $node['node_id'] ."'>". $node['hostname'] ."</a></td><td class='list_set'>". $node['boot_state'] ."</td>";
194
195       if( $pcu_controller )
196         echo "<td><a href='/db/sites/pcu.php?id=$pcu_id&remove=". $node['node_id'] ."' onclick=\"javascript:return confirm('Are you sure you want to remove node ". $node['hostname'] ." from this PCU?')\">remove</a></td>\n";
197
198       echo "</tr>\n";
199                         
200       $count++;
201         
202     }
203     echo "</tbody></table><br />\n";
204     
205   } else {
206     echo "<p>No nodes on PCU.";
207   }
208
209   
210   echo "<br /><p><a href='/db/sites/index.php?id=". $pcu_info[0]['site_id'] ."'>Back to Site</a>\n";
211
212 }
213
214
215
216 // Print footer
217 include 'plc_footer.php';
218
219 ?>