add network configuration screen
[raven.git] / apps / gacksadmin / action.php
1 <?php
2 \r
3 // Require login\r
4 require_once 'plc_login.php';\r
5 \r
6 // Get session and API handles\r
7 require_once 'plc_session.php';\r
8 global $plc, $api;\r
9 \r
10 //print header\r
11 require_once 'plc_drupal.php';
12
13 require_once 'plc_functions.php';
14 require_once 'gacks_functions.php';
15 require_once 'gacksclient.php';
16
17 $known_actions=array();
18 $known_actions []= "delete-account";
19 $known_actions []= "create-account";
20 $known_actions []= "update-account";
21 $known_actions []= "add-charge";
22 $known_actions []= "node-wizard";
23
24 function get_query_arg($name, $default=NULL)
25 {
26    if (isset($_GET[$name])) {
27        return $_GET[$name];
28    } else if (isset($_POST[$name])) {
29        return $_POST[$name];
30    } else {
31        return $default;
32    }
33 }
34
35 $action=get_query_arg("action");
36 if (!$action) {
37   foreach ($known_actions as $known_action) {\r
38     if ($_POST[$known_action]) {\r
39       $action=$known_action;\r
40       break;\r
41     }
42   }
43 }
44
45 $person_id = $_POST['person_id'];       // usually needed
46
47 if ( ! $action ) {
48   drupal_set_message ("actions.php: action not set or not in known_actions");\r
49   plc_debug('POST',$_POST);\r
50   return;\r
51  }
52
53 $gacksapi = new GACKSAPI();
54
55 try {
56
57     switch ($action) {
58
59         case 'update-account': {
60             $account_name = $_POST['account_name'];
61             $account_kind = $_POST['account_kind'];
62
63             $fields = array();
64             $fields['name'] = gacksid($account_name);
65             $fields['kind'] = $account_kind;
66             $fields['level'] = $_POST['level'];
67             $fields['multiplier'] = $_POST['multiplier'];
68             $fields['goodStanding'] = $_POST['goodStanding'];
69             $fields['billingContacts'] = $_POST['billingContacts'];
70             $fields['freezeUnreserved'] = ! $_POST['enableBestEffort'];
71
72             $gacksapi->update_account($fields);
73
74             drupal_set_message ('Account updated');
75
76             plc_redirect(l_gacks_account($account_name, $account_kind));
77             break;
78         }
79
80         case 'create-account': {
81             $account_name = $_POST['account_name'];
82             $account_kind = $_POST['account_kind'];
83
84             $fields = array();
85             $fields['name'] = gacksid_guess_prefix($account_name);
86             $fields['kind'] = $account_kind;
87             $fields['level'] = $_POST['level'];
88             $fields['multiplier'] = $_POST['multiplier'];
89             $fields['freezeUreserved'] = ! $_POST['enableBestEffort'];
90
91             $gacksapi->create_account($fields);
92
93             drupal_set_message ('Account created');
94
95             plc_redirect(l_gacks_account($account_name, $account_kind));
96             break;
97         }
98
99         case 'delete-account': {
100             $account_name = $_POST['account_name'];
101             $account_kind = $_POST['account_kind'];
102
103             $gacksapi->delete_account(gacksid($account_name), $account_kind);
104
105             drupal_set_message ('Account deleted');
106
107             plc_redirect(l_gacks_accounts());
108             break;
109         }
110
111         case 'add-charge': {
112             $account_name = $_POST['account_name'];
113             $account_kind = $_POST['account_kind'];
114             $kind = intval($_POST['kind']);
115             $desc = $_POST['description'];
116             $amount = floatval($_POST['amount']);
117             $date = time();
118
119             $gacksapi->add_charge(gacksid($account_name), $desc, $kind, $date, $amount, 1);
120
121             drupal_set_message ('Charge applied');
122
123             plc_redirect(l_gacks_account($account_name, $account_kind));
124             break;
125         }
126
127         case 'node-wizard': {
128             $account_name = $_POST['account_name'];
129             $resource_name = $_POST['resource_name'];
130             $resource_group = $_POST['resource_group'];
131             $expand = intval($_POST['expand']);
132             $amount = intval($_POST['amount']);
133             $image = $_POST['image'];
134
135             // XXX we need a permission check here.
136
137             $options = array("image" => $image);
138
139             $gacksapi->hrn_object = slice_to_hrn($account_name);
140             $result = $gacksapi->pick($resource_name, $resource_group, $amount, $expand, $options);
141
142             $status = $result['status'];
143
144             if (!$status) {
145                 drupal_set_message('Failed to Pick Nodes');
146                 return;
147             } else {
148                 print "added the following nodes:<br>";
149                 foreach ($result['addSetNames'] as $name) {
150                     print " " . $name . "<br>";
151                 }
152                 print "<br>removed the following nodes:<br>";
153                 foreach ($result['delSetNames'] as $name) {
154                     print " " .$name . "<br>";
155                 }
156                 if (array_key_exists("headNode", $result)) {
157                 print "<br>head node (for Hadoop or MPI slices):<br>";
158                 print $result['headNode'];
159                 }
160             }
161
162             drupal_set_message('Nodes Picked');
163             break;
164         }
165
166         case 'set-image': {
167             $account_name = $_POST['account_name'];
168             $image = $_POST['image'];
169             set_slice_image($api, $account_name, $image);
170             drupal_set_message('Image Set');
171             plc_redirect(l_gacks_image());
172             break;
173         }
174
175         case 'set-network': {
176             $account_name = $_POST['account_name'];
177             $network_method = $_POST['network_method'];
178             $api->SliceAutoConfig($account_name, array("network_method" => $network_method));
179             drupal_set_message('Network Set');
180             plc_redirect(l_gacks_network());
181             break;
182         }
183
184         case 'set-syndicate': {
185             $account_name = $_POST['account_name'];
186             $enable_syndicate = $_POST['enable_syndicate'];
187             set_slice_tag($api, $account_name, "enable_syndicate", $enable_syndicate);
188             if ($enable_syndicate) {
189                 drupal_set_message('Syndicate shared filesystem enabled. It may take a few minutes for the volume to show up in your slice');
190             } else {
191                 drupal_set_message('Syndicate shared filesystem disabled.');
192             }
193             plc_redirect(l_gacks_syndicate());
194             break;
195         }
196
197         case 'set-account': {
198             $account_name = $_POST['account_name'];
199             $url = $_POST['url'];
200
201             // XXX we need a permission check here.
202
203             update_gacks_session(array("account_name" => $account_name));
204             Header('Location: ' . $url);
205             break;
206         }
207
208         case 'set-service': {
209             $account_name = $_POST['account_name'];
210             $level = $_POST['level'];
211             $autoRenew = $_POST['autoRenew'];
212             $enableBestEffort = $_POST['enableBestEffort'];
213             $confirmed = $_POST['confirmed'];
214             $billingContacts = $_POST['billingContacts'];
215
216             // XXX we need a permission check here.
217
218             $fields = array();
219             $fields["name"] = gacksid($account_name);
220             $fields["kind"] = "user";
221
222             if (($level != NULL) and ($level!="")) {
223                 $fields['level'] = $level;
224                 if ($confirmed!='yes') {
225                     plc_redirect(l_gacks_confirm_service($account_name, $level, $autoRenew, $enableBestEffort));
226                     return;
227                 }
228             }
229
230             if ($billingContacts !== NULL) {
231                 $fields['billingContacts'] = $billingContacts;
232             }
233
234             if ($autoRenew !== NULL) {
235                 $fields['autoRenew'] = $autoRenew;
236             }
237
238             if ($enableBestEffort !== NULL) {
239                 $fields['freezeUnreserved'] = !$enableBestEffort;
240             }
241
242             $gacksapi->update_account($fields, "restricted");
243
244             drupal_set_message ('Settings updated');
245
246             plc_redirect(l_gacks_service_level());
247             break;
248         }
249
250         case 'request-service': {
251             $account_name = $_POST['account_name'];
252             $level = $_POST['level'];
253             $email = $plc->person['email'];
254             $to = array("smbaker@gmail.com");   // list Vicci admins here
255
256             $headers = "Content-type: text/plain\r\n";
257             $headers .= "From: $email\r\n";
258             $headers .= "Reply-To: $email\r\n";
259             $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
260
261             $subject = "Service Update Request: $account_name, $level";
262
263             $message = $subject . "\r\n";
264             $message .= "\r\n";
265             $message .= "Account $account_name requests service upgrade to $level.\r\n";
266
267             mail(implode(", ", $to), $subject, $message, $headers);
268
269             print "Your request has been submitted.";
270
271             break;
272         }
273
274         case 'submit-reservation': {
275             $account_name = $_POST['account_name'];
276             $resource_id = $_POST['resource_id'];
277             $timezone_offset_minutes = $_POST['timezone_offset_minutes'];
278             $date_y = $_POST['date_y'];
279             $date_m = $_POST['date_m'];
280             $date_d = $_POST['date_d'];
281             $time = $_POST['time'];
282             $duration = $_POST['duration'];
283             $kind = $_POST['kind'];
284
285             $timeStart = mktime($time, 0, 0, $date_m, $date_d, $date_y) - $timezone_offset_minutes*60;
286             $timeStop = $timeStart + $duration * 60 * 60;
287
288             if ($kind=="grouped") {
289                 $resource_offsets = $_POST['resource_offsets'];
290                 $rspecs = array();
291                 foreach ($resource_offsets as $offset) {
292                    $unitQuantity = $_POST['quantity_per'];
293                    $unitStart = $offset >> 16;
294                    $unitStop = $unitStart + ($offset & 0xFFFF);
295                    $rspecs []= array("id"=>$resource_id,
296                                     "timeStart"=>$timeStart,
297                                     "timeStop"=>$timeStop,
298                                     "unitStart"=>intval($unitStart),
299                                     "unitStop"=>intval($unitStop),
300                                     "unitQuantity"=>intval($unitQuantity),
301                                     "isLastAllocator"=>"plc.arizona.grm");
302
303                    $rspec = array("aggregate"=> $rspecs);
304                 }
305             } else {  // kind== "continuous"
306                 $unitQuantity = $_POST['quantity'];
307                 $rspec = array("id"=>$resource_id,
308                                  "timeStart"=>$timeStart,
309                                  "timeStop"=>$timeStop,
310                                  "unitQuantity"=>intval($unitQuantity));
311             }
312
313 //            print_r($rspec);
314
315             $allocatorHRN = slice_to_hrn($account_name);
316             $consumerHRN = slice_to_hrn($account_name);
317
318             $gacksapi->hrn_object = $allocatorHRN;
319             $gacksapi->grm_reserve($rspec, $consumerHRN, False, False);
320
321             drupal_set_message ('Reservation submitted');
322
323             plc_redirect(l_gacks_user_reservations());
324
325             break;
326         }
327
328         case 'delete-reservation': {
329             $timeStart = get_query_arg("timeStart");
330             $timeStop = get_query_arg("timeStop");
331             $resource_id = get_query_arg("resource_id");
332             $lastAllocator = get_query_arg("lastAllocator");
333
334             //print "args: $resource_id, $timeStart, $timeStop, $lastAllocator<br>";
335
336             $records = $gacksapi->query_exact($resource_id, Null, Null, $timeStart, $timeStop, NULL, $lastAllocator);
337
338             //print "records: ";
339             //print_r($records);
340
341             $handles = array();
342             foreach ($records as $record) {
343                 $handles []= handle_as_string($record);
344             }
345
346             print "<br>handles: ";
347             //print_r($handles);
348
349             $gacksapi->grm_delete($handles);
350
351             drupal_set_message ('Reservation deleted');
352
353             plc_redirect(l_gacks_user_reservations());
354
355
356             break;
357         }
358
359         case 'download-nodelist':
360             $account_name = get_query_arg('account_name');
361             $slices=$api->GetSlices($account_name);
362             if ($slices) {
363                 $slice = $slices[0];
364                 $slice_nodes=$api->GetNodes(array('node_id'=>$slice['node_ids']),array("hostname"));
365                 header("Content-type: text/ascii");
366                 header('Content-Disposition: attachment; filename="nodelist.csv"');
367                 foreach ($slice_nodes as $node) {
368                     print $node["hostname"];
369                 }
370                 exit();
371             }
372             break;
373
374         case 'set-advanced': {
375             $value = get_query_arg("value");
376             $url = get_query_arg("url");
377
378             plc_set_advanced($value);
379
380             plc_redirect($url);
381
382             break;
383         }
384
385         default: {
386             plc_error ("Unknown action $action in actions.php");\r
387             return;\r
388         }
389     }
390 } catch (GacksClientException $e) {
391     // The action failed on the client side; let the user know something went
392     // wrong.
393     $msg = $e->getMessage();
394     $msg = "<pre>" . $msg . "</pre>";
395     drupal_set_message($msg, 'error');
396     // TODO: Tell the user what to do about it. Give him an email address.
397 }
398
399 ?>
400
401