ebd300ac39ebc3954f2123c32e38e0c4b71fdde5
[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       // xxx is l_pcu defined & effective ?
38       plc_redirect( l_pcu($pcu_id));
39       exit();
40     } else {
41       $error= $api->error();
42     }
43   }
44   
45   if( !empty( $error ) )
46     echo '<div class="plc-warning">' . $error . '.</div>';
47   
48   echo "<h3>Add a PCU</h3>\n
49 <form action='/db/sites/pcu.php' method=post>\n
50 <p><table border=0 cellpadding=3><tbody>\n
51 <tr><th>Protocol: </th><td><input type=text name='protocol' value=''></td></tr>\n
52 <tr><th>Hostname: </th><td><input type=text name='hostname' size=40 value=''></td></tr>\n
53 <tr><th>IP Address: </th><td><input type=text name='ip' size=40 value=''></td></tr>\n
54 <tr><th>Model: </th><td><input type=text name='model' value=''></td></tr>\n
55 <tr><th>Username: </th><td><input type=text name='username' value=''></td></tr>\n
56 <tr><th>Password: </th><td><input type=text name='password' value=''></td></tr>\n
57 <tr><th>Notes: </th><td><input type=text name='notes' size=40 value=''></td></tr>\n
58 </tbody></table>\n
59 <br /><p><input type=submit name='submitted' value='Add New PCU'>\n";
60 } else {
61   // get PCU info
62   $pcu_id= intval( $_GET['id'] );
63   $pcu_info= $api->GetPCUs( array( intval( $pcu_id ) ) );
64   
65   // if remove is set remove the node from the pcu
66   if( $_GET['remove'] ) {
67     $rem_id= $_GET['remove'];
68     
69     $api->DeleteNodeFromPCU( intval( $rem_id ), $pcu_id );
70     
71     plc_redirect (l_pcu ($pcu_id));
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     plc_redirect (l_pcu($pcu_id));
88                 
89   }
90         
91   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'] ) ) )
92     $pcu_controller= true;
93
94   // get PCU node info
95   $node_info= $api->GetNodes( $pcu_info[0]['node_ids'], array( "hostname", "node_id", "boot_state" ) );
96   echo "<form action='/db/sites/pcu.php?id=$pcu_id' method=post>\n
97 <h3>PCU: ". $pcu_info[0]['hostname'] ."</h3>\n
98 <p><table border=0 cellpadding=3><tbody>\n
99 <tr><th>Protocol: </th><td>";
100
101   if( $pcu_controller )
102     echo "<input type=text name='protocol' value='";
103   
104   echo $pcu_info[0]['protocol'];
105   
106   if( $pcu_controller )
107     echo "'>";
108   
109   echo "</td></tr>\n
110         <tr><th>Hostname: </th><td>";
111   
112   if( $pcu_controller )
113     echo "<input type=text name='hostname' size=40 value='";
114   
115   echo $pcu_info[0]['hostname'];
116   
117   echo "'>";
118   
119   echo "</td></tr>\n
120         <tr><th>IP Address: </th><td>";
121   if( $pcu_controller )
122     echo "<input type=text name='ip' value='";
123   
124   echo $pcu_info[0]['ip'];
125   
126   echo "'>";
127   
128   if( $pcu_controller )
129     echo "</td></tr>\n
130           <tr><th>Model: </th><td>";
131   
132   if( $pcu_controller )
133     echo "<input type=text name='model' value='";
134                 
135   echo $pcu_info[0]['model'];
136   
137   if( $pcu_controller )
138     echo "'>";
139   
140   echo "</td></tr>\n
141         <tr><th>Username: </th><td>";
142         
143   if( $pcu_controller )
144     echo "<input type=text name='username' value='";
145   
146   echo $pcu_info[0]['username'];
147   
148   if( $pcu_controller )
149     echo "'>";
150   
151   echo "</td></tr>\n
152         <tr><th>Password: </th><td>";
153         
154   if( $pcu_controller )
155     echo "<input type=text name='password' value='";
156                 
157   echo $pcu_info[0]['password'];
158   
159   if( $pcu_controller )
160     echo "'>";
161   
162   echo "</td></tr>\n
163         <tr><th>Notes: </th><td>";
164   
165   if( $pcu_controller )
166     echo "<input type=text name='notes' size=40 value='";
167                 
168   echo $pcu_info[0]['notes'];
169   
170   if( $pcu_controller )
171     echo "'>";
172                 
173   echo "</td></tr>\n
174         </tbody></table>\n";
175                 
176   if( $pcu_controller )
177     echo "<br /><p><input type=submit name='submitted' value='Update PCU'>\n";
178         
179   if( !empty( $node_info ) ) {
180     echo "<p><table border=0 cellpadding=3>\n<caption>Nodes</caption>\n<thead><tr><th></th><th>Hostname</td><th>State</th><th></th>";
181     
182     // if user can control PCU add table cells
183     if( $pcu_controller )
184       echo "<th></th>\n";
185     
186     echo "</tr></thead><tbody>\n";
187     
188     // for port numbers
189     $count= 0;
190     
191     foreach( $node_info as $node ) {
192       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>";
193
194       if( $pcu_controller )
195         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";
196
197       echo "</tr>\n";
198                         
199       $count++;
200         
201     }
202     echo "</tbody></table><br />\n";
203     
204   } else {
205     echo "<p>No nodes on PCU.";
206   }
207
208   
209   echo "<br /><p><a href='/db/sites/index.php?id=". $pcu_info[0]['site_id'] ."'>Back to Site</a>\n";
210
211 }
212
213
214
215 // Print footer
216 include 'plc_footer.php';
217
218 ?>