user_syndicate page, use tag and add action to set it
[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-syndicate': {
176             $account_name = $_POST['account_name'];
177             $enable_syndicate = $_POST['enable_syndicate'];
178             set_slice_tag($api, $account_name, "enable_syndicate", $enable_syndicate);
179             if ($enable_syndicate) {
180                 drupal_set_message('Syndicate shared filesystem enabled. It may take a few minutes for the volume to show up in your slice');
181             } else {
182                 drupal_set_message('Syndicate shared filesystem disabled.');
183             }
184             plc_redirect(l_gacks_syndicate());
185             break;
186         }
187
188         case 'set-account': {
189             $account_name = $_POST['account_name'];
190             $url = $_POST['url'];
191
192             // XXX we need a permission check here.
193
194             update_gacks_session(array("account_name" => $account_name));
195             Header('Location: ' . $url);
196             break;
197         }
198
199         case 'set-service': {
200             $account_name = $_POST['account_name'];
201             $level = $_POST['level'];
202             $autoRenew = $_POST['autoRenew'];
203             $enableBestEffort = $_POST['enableBestEffort'];
204             $confirmed = $_POST['confirmed'];
205             $billingContacts = $_POST['billingContacts'];
206
207             // XXX we need a permission check here.
208
209             $fields = array();
210             $fields["name"] = gacksid($account_name);
211             $fields["kind"] = "user";
212
213             if (($level != NULL) and ($level!="")) {
214                 $fields['level'] = $level;
215                 if ($confirmed!='yes') {
216                     plc_redirect(l_gacks_confirm_service($account_name, $level, $autoRenew, $enableBestEffort));
217                     return;
218                 }
219             }
220
221             if ($billingContacts !== NULL) {
222                 $fields['billingContacts'] = $billingContacts;
223             }
224
225             if ($autoRenew !== NULL) {
226                 $fields['autoRenew'] = $autoRenew;
227             }
228
229             if ($enableBestEffort !== NULL) {
230                 $fields['freezeUnreserved'] = !$enableBestEffort;
231             }
232
233             $gacksapi->update_account($fields, "restricted");
234
235             drupal_set_message ('Settings updated');
236
237             plc_redirect(l_gacks_service_level());
238             break;
239         }
240
241         case 'request-service': {
242             $account_name = $_POST['account_name'];
243             $level = $_POST['level'];
244             $email = $plc->person['email'];
245             $to = array("smbaker@gmail.com");   // list Vicci admins here
246
247             $headers = "Content-type: text/plain\r\n";
248             $headers .= "From: $email\r\n";
249             $headers .= "Reply-To: $email\r\n";
250             $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
251
252             $subject = "Service Update Request: $account_name, $level";
253
254             $message = $subject . "\r\n";
255             $message .= "\r\n";
256             $message .= "Account $account_name requests service upgrade to $level.\r\n";
257
258             mail(implode(", ", $to), $subject, $message, $headers);
259
260             print "Your request has been submitted.";
261
262             break;
263         }
264
265         case 'submit-reservation': {
266             $account_name = $_POST['account_name'];
267             $resource_id = $_POST['resource_id'];
268             $timezone_offset_minutes = $_POST['timezone_offset_minutes'];
269             $date_y = $_POST['date_y'];
270             $date_m = $_POST['date_m'];
271             $date_d = $_POST['date_d'];
272             $time = $_POST['time'];
273             $duration = $_POST['duration'];
274             $kind = $_POST['kind'];
275
276             $timeStart = mktime($time, 0, 0, $date_m, $date_d, $date_y) - $timezone_offset_minutes*60;
277             $timeStop = $timeStart + $duration * 60 * 60;
278
279             if ($kind=="grouped") {
280                 $resource_offsets = $_POST['resource_offsets'];
281                 $rspecs = array();
282                 foreach ($resource_offsets as $offset) {
283                    $unitQuantity = $_POST['quantity_per'];
284                    $unitStart = $offset >> 16;
285                    $unitStop = $unitStart + ($offset & 0xFFFF);
286                    $rspecs []= array("id"=>$resource_id,
287                                     "timeStart"=>$timeStart,
288                                     "timeStop"=>$timeStop,
289                                     "unitStart"=>intval($unitStart),
290                                     "unitStop"=>intval($unitStop),
291                                     "unitQuantity"=>intval($unitQuantity),
292                                     "isLastAllocator"=>"plc.arizona.grm");
293
294                    $rspec = array("aggregate"=> $rspecs);
295                 }
296             } else {  // kind== "continuous"
297                 $unitQuantity = $_POST['quantity'];
298                 $rspec = array("id"=>$resource_id,
299                                  "timeStart"=>$timeStart,
300                                  "timeStop"=>$timeStop,
301                                  "unitQuantity"=>intval($unitQuantity));
302             }
303
304 //            print_r($rspec);
305
306             $allocatorHRN = slice_to_hrn($account_name);
307             $consumerHRN = slice_to_hrn($account_name);
308
309             $gacksapi->hrn_object = $allocatorHRN;
310             $gacksapi->grm_reserve($rspec, $consumerHRN, False, False);
311
312             drupal_set_message ('Reservation submitted');
313
314             plc_redirect(l_gacks_user_reservations());
315
316             break;
317         }
318
319         case 'delete-reservation': {
320             $timeStart = get_query_arg("timeStart");
321             $timeStop = get_query_arg("timeStop");
322             $resource_id = get_query_arg("resource_id");
323             $lastAllocator = get_query_arg("lastAllocator");
324
325             //print "args: $resource_id, $timeStart, $timeStop, $lastAllocator<br>";
326
327             $records = $gacksapi->query_exact($resource_id, Null, Null, $timeStart, $timeStop, NULL, $lastAllocator);
328
329             //print "records: ";
330             //print_r($records);
331
332             $handles = array();
333             foreach ($records as $record) {
334                 $handles []= handle_as_string($record);
335             }
336
337             print "<br>handles: ";
338             //print_r($handles);
339
340             $gacksapi->grm_delete($handles);
341
342             drupal_set_message ('Reservation deleted');
343
344             plc_redirect(l_gacks_user_reservations());
345
346
347             break;
348         }
349
350         case 'download-nodelist':
351             $account_name = get_query_arg('account_name');
352             $slices=$api->GetSlices($account_name);
353             if ($slices) {
354                 $slice = $slices[0];
355                 $slice_nodes=$api->GetNodes(array('node_id'=>$slice['node_ids']),array("hostname"));
356                 header("Content-type: text/ascii");
357                 header('Content-Disposition: attachment; filename="nodelist.csv"');
358                 foreach ($slice_nodes as $node) {
359                     print $node["hostname"];
360                 }
361                 exit();
362             }
363             break;
364
365         case 'set-advanced': {
366             $value = get_query_arg("value");
367             $url = get_query_arg("url");
368
369             plc_set_advanced($value);
370
371             plc_redirect($url);
372
373             break;
374         }
375
376         default: {
377             plc_error ("Unknown action $action in actions.php");\r
378             return;\r
379         }
380     }
381 } catch (GacksClientException $e) {
382     // The action failed on the client side; let the user know something went
383     // wrong.
384     $msg = $e->getMessage();
385     $msg = "<pre>" . $msg . "</pre>";
386     drupal_set_message($msg, 'error');
387     // TODO: Tell the user what to do about it. Give him an email address.
388 }
389
390 ?>
391
392