Last active
September 23, 2019 11:11
-
-
Save finalwebsites/162e168b3539b12005141113277b5edd to your computer and use it in GitHub Desktop.
Start your VPS on a different host node using PHP and the UpCloud API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Enter here your server UID and the login / password | |
$uid = 'xxx-xxx-xxx-xxx-xxx'; | |
$host = 'https://api.upcloud.com/1.2/server/'.$uid; | |
$username = 'XXX'; | |
$password = 'XXXXXXXX'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'conf.php'; | |
// Do a server detail request and obtain the current host id | |
$ch = curl_init($host); | |
$headers = array( | |
'Content-Type:application/json', | |
'Authorization: Basic '. base64_encode($username.':'.$password) | |
); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
$array = json_decode($return, true); | |
// Add the current host id to avoid this one during the start request | |
echo $array['server']['host']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'conf.php'; | |
$start_data['server']['avoid_host'] = 'PLACE HERE THE HOST ID'; | |
$start_data_string = json_encode($start_data); | |
// start the server | |
$ch = curl_init($host.'/start'); | |
$headers = array( | |
'Content-Type:application/json', | |
'Authorization: Basic '. base64_encode($username.':'.$password), | |
'Content-Length: ' . strlen($start_data_string) | |
); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $start_data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($start_data_string) | |
) | |
); | |
$response = curl_exec($ch); | |
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'conf.php'; | |
// Shutdown the server | |
$stop_data['stop_server'] = array('stop_type' => 'soft', 'timeout' => '60'); | |
$stop_data_string = json_encode($stop_data); | |
$ch = curl_init($host.'/stop'); | |
$headers = array( | |
'Content-Type:application/json', | |
'Authorization: Basic '. base64_encode($username.':'.$password), | |
'Content-Length: ' . strlen($stop_data_string) | |
); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $stop_data_string); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$resp = curl_exec($ch); | |
curl_close($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before you can use this script you need to enable API access via the UpCloud controle panel.
I suggest to run these PHP scripts via your laptop's terminal application.