|
<?php |
|
// get from here: https://github.com/tfnet/jiffy-api-php-client/blob/master/JiffyBoxApi.class.php |
|
require "JiffyBoxApi.class.php"; |
|
$token = "INSERT YOUR TOKEN HERE"; |
|
$id=INSERT YOU jiffybox id HERE; |
|
$planid=20; |
|
$hours=2; |
|
|
|
if (!empty($argc) && strstr($argv[0], basename(__FILE__))) |
|
{ |
|
if (!file_exists("refresh.txt")) |
|
{ |
|
die(); |
|
die("no server running"); |
|
} |
|
$jb = new JiffyBox ($token, $id); |
|
if ($jb->getStatus () == "") |
|
{ |
|
die("problems connecting to jiffybox"); |
|
} |
|
if ($argv[1] == "freeze") |
|
{ |
|
echo freeze($jb); |
|
} |
|
else |
|
{ |
|
# check how old the last request is |
|
# freeze or start |
|
$timestamp = file_get_contents("refresh.txt"); |
|
if ((time()-$timestamp)<60*60*$hours) |
|
{ |
|
refresh($jb, $planid); |
|
} |
|
else |
|
{ |
|
freeze($jb); |
|
unlink("refresh.txt"); |
|
} |
|
} |
|
die("command line"); |
|
} |
|
else if ($_GET["operation"] == "refresh") |
|
{ |
|
$jb = new JiffyBox ($token, $id); |
|
if ($jb->getStatus () == "") |
|
{ |
|
die("problems connecting to jiffybox"); |
|
} |
|
|
|
# save to file current timestamp |
|
file_put_contents("refresh.txt", time()); |
|
|
|
refresh($jb, $planid); |
|
$shutdowntime= date('Y-m-d H:i e', strtotime('+'.$hours.' hours')); |
|
die("machine will run until ".$shutdowntime." if it is not refreshed again" ); |
|
} |
|
die ("exit"); |
|
?><pre><? |
|
|
|
#echo "status: ".($jb->getStatus ())."<br/>"; |
|
#echo "public IP: ".$jb->getInfo()->ips->public[0]."<br/>"; |
|
#print_r($jb->getInfo()); |
|
#print_r($jb->start()); |
|
#print_r($jb->stop()); |
|
#print_r($jb->freeze()); |
|
#print_r($jb->thaw($planid)); |
|
#print_r($jb->getPlans()); |
|
|
|
?></pre><? |
|
#echo freeze($jb); |
|
# status: READY FROZEN STOPPING UPDATING |
|
function freeze($jb) |
|
{ |
|
echo "stopping..."; |
|
$jb->stop(); |
|
while ($jb->getStatus () != "READY") |
|
{ |
|
sleep(20); |
|
} |
|
echo "freezing..."; |
|
$jb->freeze(); |
|
while ($jb->getStatus () != "FROZEN") |
|
{ |
|
sleep(20); |
|
} |
|
return $jb->getStatus (); |
|
} |
|
|
|
function refresh($jb, $planid) |
|
{ |
|
if ($jb->getStatus () == "FROZEN") |
|
{ |
|
$msg=$jb->thaw($planid); |
|
while ($jb->getStatus () != "READY") |
|
{ |
|
echo "waiting"; |
|
sleep(20); |
|
} |
|
echo $jb->getStatus (); |
|
} |
|
if ($jb->getStatus () == "READY") |
|
{ |
|
$jb->start(); |
|
} |
|
} |
|
?> |