function handle_status_update_event($event) { dbg_log("Received event " . print_r($event, true)); $prefix = substr($event["appId"], 1, strlen(get_config()->app_prefix)); dbg_log("prefix is {$prefix}"); if ($prefix == get_config()->app_prefix && $event["taskStatus"] == "TASK_RUNNING") { dbg_log("Handling event"); $taskId = $event["taskId"]; //then we need to add the pipework //to do that we need to identify the container //we have the marathon id but we need the docker id $host = $event["host"]; $containers_details = docker_get_container_details_for_all($host); $container = $containers_details[$taskId]; $containerID = $container["Id"]; $ip = ip_pool_allocate(array("taskID" => "{$taskId}", "containerID" => "{$containerID}", "host" => $host)); if ($ip == NULL) { throw new Exception("Could not allocate an ip address!"); } dbg_log("Allocating ip {$ip} to docker id {$containerID} and mesos id {$taskId}"); docker_add_container_public_ip($host, $containerID, $ip); } if ($prefix == get_config()->app_prefix && ($event["taskStatus"] == "TASK_KILLED" || $event["taskStatus"] == "TASK_LOST" || $event["taskStatus"] == "TASK_FAILED")) { dbg_log("Handling event"); $taskID = $event["taskId"]; $state = ip_pool_get_state(); #var_dump($state); $publicIP = "none"; $containerID = "none"; foreach ($state["meta"] as $ip => $metadata) { if ($metadata["taskID"] == $taskID) { $containerID = $metadata["containerID"]; $publicIP = $ip; break; } } if ($publicIP != "none") { dbg_log("Deallocating ip {$publicIP} from {$taskID}"); ip_pool_deallocate($publicIP); } else { dbg_log("Could not find allocated ip for container {$taskID}"); } } }
<?php include_once dirname(__FILE__) . "/../lib/ippool.php"; include_once dirname(__FILE__) . "/../lib/utilities.php"; var_dump(ip_pool_get_state()); $ip1 = ip_pool_allocate("ip1"); $ip2 = ip_pool_allocate("ip2"); var_dump($ip1); var_dump($ip2); $meta2 = ip_pool_deallocate($ip2); $meta1 = ip_pool_deallocate($ip1); var_dump(ip_pool_get_state());