diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index a4d9705..1620017 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -102,4 +102,67 @@ abstract class BaseController extends Controller curl_close($curl); return json_decode($result, true); } + + public function ansibleLogin():string { + + $result = "Unhandled exception"; + $base_url = "http://172.16.4.90:3000"; + $cookies = tempnam('/tmp','cookie.txt'); + + # 1. Login + $url = $base_url . "/api/auth/login"; + $ch = curl_init( $url ); + # Setup request to send json via POST. + $payload = json_encode( array( "auth"=> "admin", "password" => "may12002" ) ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); + # Return response instead of printing. + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + # Fetch the headers, not the body content: + //curl_setopt($ch, CURLOPT_HEADER, true); // we want headers + //curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body + # Cookie jar + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); + + # Send request. + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $result = 'HTTP code: ' . $httpcode; + + curl_close($ch); + + if ($httpcode != "204") { + return "Login failed! $result"; + } + + # 2. Get token + $url = $base_url . "/api/user/tokens"; + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $token = null; + $arr = json_decode($result, true); + foreach ($arr as $item) { + if ($item["expired"]===false) { + $token = $item["id"]; + break; + } + + } + $result = "Found token: $token"; + + # Print response. + return $token; + } + + public function ansibleProvision($ansibleToken,$params){ + + + } } diff --git a/app/Controllers/Provision.php b/app/Controllers/Provision.php index 227a819..d63a695 100644 --- a/app/Controllers/Provision.php +++ b/app/Controllers/Provision.php @@ -252,7 +252,7 @@ class Provision extends BaseController //echo "
$output
"; //log_message('critical', "Test poath -> ".$output); //ANSIBLE/templates/A000001.yml - + $ansibleToken = $this->ansibleLogin(); $mysql = "SELECT id AS plan_id, uid, provision_id, play_file from provision_plans ORDER BY updated ASC LIMIT 1 "; $query = $this->db->query($mysql); $provision_list = $query->getResult(); @@ -263,11 +263,15 @@ class Provision extends BaseController $this->db->query("UPDATE provision_plans SET updated = now() WHERE id = $planId"); $this->db->query("UPDATE members_products SET p_file = p_file + 1 WHERE id = $provisionId"); - $this->runAnsibleShell($playFile); + $params = [ + "template_id"=> 1, + "debug"=> false, + "dry_run"=> false, + "playbook"=> 'A000001.yml', + "environment"=> "{}" + ]; + $this->ansibleProvision($ansibleToken, $params); } - - - return 0; } public function runAnsibleShell($provisionFile){