add links for nodes with PCUs to PCU update in site.php
[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
19 // find person roles
20 $_person= $plc->person;
21 $_roles= $_person['role_ids'];
22
23
24 // if no id: add, else: display(update)
25 if( !$_GET['id'] ) {
26   if( $_POST['submitted'] ) {
27     // get person's site id
28     $site_id= $_person['site_ids'][0];
29     
30     // build dict
31     $fields= array( 'protocol'=>$_POST['protocol'], 'hostname'=>$_POST['hostname'], 'model'=>$_POST['model'], 'username'=>$_POST['username'], 'password'=>$_POST['password'], 'notes'=>$_POST['notes'], 'ip'=>$_POST['ip'] );
32     
33     $pcu_id= $api->AddPCU( $site_id, $fields );
34     
35     if( $pcu_id != 0 ) {
36       // xxx is l_pcu defined & effective ?
37       plc_redirect( l_pcu($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     plc_redirect (l_pcu ($pcu_id));
71     
72   }
73   
74   //if submitted, update node info
75   if( $_POST['submitted'] ) {
76     $protocol= $_POST['protocol'];
77     $username= $_POST['username'];
78     $hostname= $_POST['hostname'];
79     $ipaddress= $_POST['ip'];
80     $model= $_POST['model'];
81     $password= $_POST['password'];
82     $notes= $_POST['notes'];
83     
84     $api->UpdatePCU( $pcu_id, array( "protocol"=>$protocol, "hostname"=>$hostname, "model"=>$model, "password"=>$password, "notes"=>$notes, "ip"=>$ipaddress ) );
85                 
86     plc_redirect (l_pcu($pcu_id));
87                 
88   }
89         
90   if( in_array( 10, $_roles) ) {
91       $is_admin= true;
92   } else {
93       $is_admin = false;
94   }
95
96   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'] ) ) )
97     $pcu_controller= true;
98
99   // get PCU node info
100   $node_info= $api->GetNodes( $pcu_info[0]['node_ids'], array( "hostname", "node_id", "boot_state" ) );
101   echo "<form action='/db/sites/pcu.php?id=$pcu_id' method=post>\n
102 <h3>PCU: ". $pcu_info[0]['hostname'] ."</h3>\n
103 <p><table border=0 cellpadding=3><tbody>\n
104 <tr><th>Protocol: </th><td>";
105
106   if( $pcu_controller )
107     echo "<input type=text name='protocol' value='";
108   
109   echo $pcu_info[0]['protocol'];
110   
111   if( $pcu_controller )
112     echo "'>";
113   
114   echo "</td></tr>\n
115         <tr><th>Hostname: </th><td>";
116   
117   if( $pcu_controller )
118     echo "<input type=text name='hostname' size=40 value='";
119   
120   echo $pcu_info[0]['hostname'];
121   
122   echo "'>";
123   
124   echo "</td></tr>\n
125         <tr><th>IP Address: </th><td>";
126   if( $pcu_controller )
127     echo "<input type=text name='ip' value='";
128   
129   echo $pcu_info[0]['ip'];
130   
131   echo "'>";
132   
133   if( $pcu_controller )
134     echo "</td></tr>\n
135           <tr><th>Model: </th><td>";
136   
137   // NOTE: in general, this value should not be edited, so only allow admins.
138   if( $pcu_controller && $is_admin )
139     echo "<input type=text name='model' value='";
140                 
141   echo $pcu_info[0]['model'];
142   
143   if( $pcu_controller && $is_admin )
144     echo "'>";
145   
146   echo "</td></tr>\n
147         <tr><th>Username: </th><td>";
148         
149   if( $pcu_controller )
150     echo "<input type=text name='username' value='";
151   
152   echo $pcu_info[0]['username'];
153   
154   if( $pcu_controller )
155     echo "'>";
156   
157   echo "</td></tr>\n
158         <tr><th>Password: </th><td>";
159         
160   if( $pcu_controller )
161     echo "<input type=text name='password' value='";
162                 
163   echo $pcu_info[0]['password'];
164   
165   if( $pcu_controller )
166     echo "'>";
167   
168   echo "</td></tr>\n
169         <tr><th>Notes: </th><td>";
170   
171   if( $pcu_controller )
172     echo "<input type=text name='notes' size=40 value='";
173                 
174   echo $pcu_info[0]['notes'];
175   
176   if( $pcu_controller )
177     echo "'>";
178                 
179   echo "</td></tr>\n
180         </tbody></table>\n";
181                 
182   if( $pcu_controller )
183     echo "<br /><p><input type=submit name='submitted' value='Update PCU'>\n";
184         
185   if( !empty( $node_info ) ) {
186     echo "<p><table border=0 cellpadding=3>\n<caption>Nodes</caption>\n<thead><tr><th></th><th>Hostname</td><th>State</th><th></th>";
187     
188     // if user can control PCU add table cells
189     if( $pcu_controller )
190       echo "<th></th>\n";
191     
192     echo "</tr></thead><tbody>\n";
193     
194     // for port numbers
195     $count= 0;
196     
197     foreach( $node_info as $node ) {
198       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>";
199
200       if( $pcu_controller )
201         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";
202
203       echo "</tr>\n";
204                         
205       $count++;
206         
207     }
208     echo "</tbody></table><br />\n";
209     
210   } else {
211     echo "<p>No nodes on PCU.";
212   }
213
214   
215   echo "<br /><p><a href='/db/sites/index.php?id=". $pcu_info[0]['site_id'] ."'>Back to Site</a>\n";
216
217 }
218
219
220
221 // Print footer
222 include 'plc_footer.php';
223
224 ?>