Skip to content

Instantly share code, notes, and snippets.

@tpokorra
Last active December 19, 2015 16:29
Show Gist options
  • Save tpokorra/5984248 to your computer and use it in GitHub Desktop.
Save tpokorra/5984248 to your computer and use it in GitHub Desktop.
<?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();
}
}
?>
message="Server will shutdown soon, please refresh if you still need it by visiting http://jb.solidcharity.com"
wall $message
for display in `ps xaf | grep x2goagent | grep -v grep | awk '{ print ( $(NF) ) }'`
do
user=`ps xaf | grep x2goagent | grep -v grep | grep "$display$" | awk ' { print ( $(NF-5) ) }' | awk ' { print ( $3 ) }' FS='/'`
echo "$user $display"
su - $user -c "zenity --info --display=$display --text='$message'&"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment