new proviison

This commit is contained in:
CHIEFSOFT\ameye
2025-01-26 09:16:28 -05:00
parent b4e3e3f7bd
commit a967dda061
2 changed files with 72 additions and 5 deletions
+63
View File
@@ -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){
}
}