clean up code
This commit is contained in:
@@ -56,18 +56,20 @@ abstract class BaseController extends Controller
|
|||||||
$this->db = \Config\Database::connect();
|
$this->db = \Config\Database::connect();
|
||||||
// E.g.: $this->session = service('session');
|
// E.g.: $this->session = service('session');
|
||||||
}
|
}
|
||||||
public function APIcall($method, $url, $data) {
|
|
||||||
|
public function APIcall($method, $url, $data)
|
||||||
|
{
|
||||||
// $curl = curl_init();
|
// $curl = curl_init();
|
||||||
$curl = curl_init($url);
|
$curl = curl_init($url);
|
||||||
switch ($method) {
|
switch ($method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
$params2 = '';
|
$params2 = '';
|
||||||
foreach($data as $key2=>$value2)
|
foreach ($data as $key2 => $value2)
|
||||||
$params2 .= $key2.'='.$value2.'&';
|
$params2 .= $key2 . '=' . $value2 . '&';
|
||||||
|
|
||||||
$params2 = trim($params2, '&');
|
$params2 = trim($params2, '&');
|
||||||
$url = $url.'?'.$params2;// add param to URL
|
$url = $url . '?' . $params2;// add param to URL
|
||||||
log_message('critical', "API URL FINAL =>".$url );
|
log_message('critical', "API URL FINAL =>" . $url);
|
||||||
//curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
|
//curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
|
||||||
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||||
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
|
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||||
@@ -96,72 +98,84 @@ abstract class BaseController extends Controller
|
|||||||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||||
$result = curl_exec($curl);
|
$result = curl_exec($curl);
|
||||||
|
|
||||||
if(!$result) {
|
if (!$result) {
|
||||||
echo("Connection failure!");
|
echo("Connection failure!");
|
||||||
}
|
}
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
return json_decode($result, true);
|
return json_decode($result, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ansibleLogin():string {
|
public function ansibleLogin(): string
|
||||||
|
{
|
||||||
$result = "Unhandled exception";
|
|
||||||
$base_url = $_ENV['ANSIBLE_AUTOMATION_SERVER_LOCATION'];
|
|
||||||
// $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;
|
$token = null;
|
||||||
$arr = json_decode($result, true);
|
try {
|
||||||
foreach ($arr as $item) {
|
// Code that might throw an exception
|
||||||
if ($item["expired"]===false) {
|
$result = "Unhandled exception";
|
||||||
$token = $item["id"];
|
$base_url = $_ENV['ANSIBLE_AUTOMATION_SERVER_LOCATION'];
|
||||||
break;
|
// $base_url = "http://172.16.4.90:3000";
|
||||||
|
$cookies = tempnam('/tmp', 'cookie.txt');
|
||||||
|
|
||||||
|
# 1. Login
|
||||||
|
$url = $base_url . "/api/auth/login";
|
||||||
|
log_message('critical', "***** ***** Provision LOGIN URL:: " . $url);
|
||||||
|
|
||||||
|
$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.
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
log_message('critical', "***** ***** Provision LOGIN FAILED :: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
$result = "Found token: $token";
|
|
||||||
# Print response.
|
|
||||||
return $token;
|
return $token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function ansibleProvision($ansibleToken,$params){
|
public function ansibleProvision($ansibleToken, $params)
|
||||||
|
{
|
||||||
// log_message('critical', "***** ***** Provision CALL:: ansibleProvision() Token ".$ansibleToken);
|
// log_message('critical', "***** ***** Provision CALL:: ansibleProvision() Token ".$ansibleToken);
|
||||||
// log_message('critical', "***** ***** Provision CALL:: ansibleProvision() Token ". serialize( $params));
|
// log_message('critical', "***** ***** Provision CALL:: ansibleProvision() Token ". serialize( $params));
|
||||||
|
|
||||||
@@ -172,19 +186,19 @@ abstract class BaseController extends Controller
|
|||||||
$PROJECT_ID = $_ENV['ANSIBLE_AUTOMATION_PROJECT_ID'];
|
$PROJECT_ID = $_ENV['ANSIBLE_AUTOMATION_PROJECT_ID'];
|
||||||
|
|
||||||
$url = $base_url . "/api/project/$PROJECT_ID/tasks";
|
$url = $base_url . "/api/project/$PROJECT_ID/tasks";
|
||||||
log_message('critical', "***** ***** Provision CALL URL:: ". $url);
|
log_message('critical', "***** ***** Provision CALL URL:: " . $url);
|
||||||
|
|
||||||
$headers = [
|
$headers = [
|
||||||
'Content-Type:application/json',
|
'Content-Type:application/json',
|
||||||
'Accept: application/json',
|
'Accept: application/json',
|
||||||
'Authorization: Bearer ' . $ansibleToken
|
'Authorization: Bearer ' . $ansibleToken
|
||||||
];
|
];
|
||||||
$ch = curl_init( $url );
|
$ch = curl_init($url);
|
||||||
$payload = json_encode( $params );
|
$payload = json_encode($params);
|
||||||
// $payload = json_encode( array( "template_id"=> 1, "debug" => false, "dry_run" => false, "playbook" => "first-playbook.yml", "environment" => "{}" ) );
|
// $payload = json_encode( array( "template_id"=> 1, "debug" => false, "dry_run" => false, "playbook" => "first-playbook.yml", "environment" => "{}" ) );
|
||||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
$res = curl_exec($ch);
|
$res = curl_exec($ch);
|
||||||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$result .= "\n" . 'Launch task HTTP code: ' . $httpcode;
|
$result .= "\n" . 'Launch task HTTP code: ' . $httpcode;
|
||||||
@@ -193,9 +207,9 @@ abstract class BaseController extends Controller
|
|||||||
|
|
||||||
# 4. Get tasks
|
# 4. Get tasks
|
||||||
$url = $base_url . "/api/project/$PROJECT_ID/tasks";
|
$url = $base_url . "/api/project/$PROJECT_ID/tasks";
|
||||||
$ch = curl_init( $url );
|
$ch = curl_init($url);
|
||||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
$res = curl_exec($ch);
|
$res = curl_exec($ch);
|
||||||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$result .= "\n" . 'Get tasks HTTP code: ' . $httpcode;
|
$result .= "\n" . 'Get tasks HTTP code: ' . $httpcode;
|
||||||
|
|||||||
Reference in New Issue
Block a user