| @@ 456-500 (lines=45) @@ | ||
| 453 | * @param array $data |
|
| 454 | * @return bool |
|
| 455 | */ |
|
| 456 | public function updateContainer($id, array $data) |
|
| 457 | { |
|
| 458 | $validator = Validator::make($data, [ |
|
| 459 | 'image' => 'required|string', |
|
| 460 | ]); |
|
| 461 | ||
| 462 | // Run validator, throw catchable and displayable exception if it fails. |
|
| 463 | // Exception includes a JSON result of failed validation rules. |
|
| 464 | if ($validator->fails()) { |
|
| 465 | throw new DisplayValidationException($validator->errors()); |
|
| 466 | } |
|
| 467 | ||
| 468 | DB::beginTransaction(); |
|
| 469 | try { |
|
| 470 | $server = Models\Server::findOrFail($id); |
|
| 471 | ||
| 472 | $server->image = $data['image']; |
|
| 473 | $server->save(); |
|
| 474 | ||
| 475 | $node = Models\Node::getByID($server->node); |
|
| 476 | $client = Models\Node::guzzleRequest($server->node); |
|
| 477 | ||
| 478 | $client->request('PATCH', '/server', [ |
|
| 479 | 'headers' => [ |
|
| 480 | 'X-Access-Server' => $server->uuid, |
|
| 481 | 'X-Access-Token' => $node->daemonSecret, |
|
| 482 | ], |
|
| 483 | 'json' => [ |
|
| 484 | 'build' => [ |
|
| 485 | 'image' => $server->image, |
|
| 486 | ], |
|
| 487 | ], |
|
| 488 | ]); |
|
| 489 | ||
| 490 | DB::commit(); |
|
| 491 | ||
| 492 | return true; |
|
| 493 | } catch (\GuzzleHttp\Exception\TransferException $ex) { |
|
| 494 | DB::rollBack(); |
|
| 495 | throw new DisplayException('An error occured while attempting to update the container image.', $ex); |
|
| 496 | } catch (\Exception $ex) { |
|
| 497 | DB::rollBack(); |
|
| 498 | throw $ex; |
|
| 499 | } |
|
| 500 | } |
|
| 501 | ||
| 502 | /** |
|
| 503 | * [changeBuild description]. |
|
| @@ 975-1017 (lines=43) @@ | ||
| 972 | } |
|
| 973 | } |
|
| 974 | ||
| 975 | public function updateSFTPPassword($id, $password) |
|
| 976 | { |
|
| 977 | $server = Models\Server::findOrFail($id); |
|
| 978 | $node = Models\Node::findOrFail($server->node); |
|
| 979 | ||
| 980 | $validator = Validator::make([ |
|
| 981 | 'password' => $password, |
|
| 982 | ], [ |
|
| 983 | 'password' => 'required|regex:/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/', |
|
| 984 | ]); |
|
| 985 | ||
| 986 | if ($validator->fails()) { |
|
| 987 | throw new DisplayValidationException(json_encode($validator->errors())); |
|
| 988 | } |
|
| 989 | ||
| 990 | DB::beginTransaction(); |
|
| 991 | $server->sftp_password = Crypt::encrypt($password); |
|
| 992 | ||
| 993 | try { |
|
| 994 | $server->save(); |
|
| 995 | ||
| 996 | $client = Models\Node::guzzleRequest($server->node); |
|
| 997 | $client->request('POST', '/server/password', [ |
|
| 998 | 'headers' => [ |
|
| 999 | 'X-Access-Token' => $node->daemonSecret, |
|
| 1000 | 'X-Access-Server' => $server->uuid, |
|
| 1001 | ], |
|
| 1002 | 'json' => [ |
|
| 1003 | 'password' => $password, |
|
| 1004 | ], |
|
| 1005 | ]); |
|
| 1006 | ||
| 1007 | DB::commit(); |
|
| 1008 | ||
| 1009 | return true; |
|
| 1010 | } catch (\GuzzleHttp\Exception\TransferException $ex) { |
|
| 1011 | DB::rollBack(); |
|
| 1012 | throw new DisplayException('There was an error while attmping to contact the remote service to change the password.', $ex); |
|
| 1013 | } catch (\Exception $ex) { |
|
| 1014 | DB::rollBack(); |
|
| 1015 | throw $ex; |
|
| 1016 | } |
|
| 1017 | } |
|
| 1018 | } |
|
| 1019 | ||