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){
}
}
+9 -5
View File
@@ -252,7 +252,7 @@ class Provision extends BaseController
//echo "<pre>$output</pre>";
//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){