diff --git a/ANSIBLE/templates/A000001.yml b/ANSIBLE/templates/A000001.yml index e2cf745..bc00db1 100644 --- a/ANSIBLE/templates/A000001.yml +++ b/ANSIBLE/templates/A000001.yml @@ -1 +1,20 @@ -A000001 \ No newline at end of file +--- +- hosts: WHAT_HOST_IN_USE + + tasks: + - name: Creates directory DESTINATION_FOLDER + ansible.builtin.file: + path: DESTINATION_FOLDER + state: directory + mode: 0775 + recurse: yes + + - name: Set up docker-compose file + template: + src: "WHAT_PART_LOCAL/docker-compose.yml" + dest: "DESTINATION_FOLDER" + + - name: docker compose up + shell: + chdir: "DESTINATION_FOLDER/" + cmd: docker-compose up -d \ No newline at end of file diff --git a/app/Config/Routes.php b/app/Config/Routes.php index a7b8ca2..4ada8b5 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -7,4 +7,5 @@ use CodeIgniter\Router\RouteCollection; */ $routes->get('/', 'Home::index'); -$routes->get('/provision/update', 'Home::provision'); +$routes->get('/provision/prepare', 'Provision::prepareProvision'); +$routes->get('/provision/update', 'Provision::updateProvision'); diff --git a/app/Controllers/Provision.php b/app/Controllers/Provision.php new file mode 100644 index 0000000..7bf8da2 --- /dev/null +++ b/app/Controllers/Provision.php @@ -0,0 +1,85 @@ +allocatePortNo(); // allocte port for the conyainer use + + return 0; + } + public function updateProvision(): string + { + $public_path = FCPATH; + $ansible_folder = str_replace("public/", "ANSIBLE", $public_path); + $partMachineLocal = "/home/chiefsoft/MermsProvision/ANSIBLE/parts/"; + if (!is_dir($ansible_folder . "/parts")) { + mkdir($ansible_folder . "/parts", 0700); + } + + $mysql = "SELECT id, uid, internal_url,product_id,status,provision_port FROM members_products WHERE provision_port = 0 "; + $query = $this->db->query($mysql); + // $num = $query->num_rows(); + $provision_list = $query->getResult(); + //$provision_list =[]; + foreach ($provision_list as $pr) { + $provisionPort = $pr->provision_port; + $productId = $pr->product_id; + $provisionUID = $pr->uid; + $ContainerName = $productId . str_pad($pr->id, 10, "0", STR_PAD_LEFT); + $partFolder = $ansible_folder . "/parts/" . $provisionUID; + + $allocatedPort = 8011; + + if (!is_dir($partFolder)) { + mkdir($partFolder, 0700); + } + + // THIS PROVISONING COMPOSERS + $composer_template_path = $ansible_folder . "/templates/composers/" . $productId . "/docker-compose.yml"; + $composer_template_path_local = $partMachineLocal . $provisionUID; + + $prov_name = str_replace(".", "_", $pr->internal_url) . ".yml"; + $template_file = $ansible_folder . "/templates/" . $pr->product_id . ".yml"; + //Get the domposer template file + $composer_template_contents = file_get_contents($composer_template_path); + // Do the processing + $composer_template_contents = str_replace("WHAT_CONTAINER_NAME", $ContainerName, $composer_template_contents); + $destinationHomeFolder = "/home/chiefsoft/SITES/" . $ContainerName; + $composer_template_contents = str_replace("DESTINATION_FOLDER", $destinationHomeFolder, $composer_template_contents); + $composer_template_contents = str_replace("ALLOCATED_PORT", $allocatedPort, $composer_template_contents); + $composer_template_contents = str_replace("CNT_DB_NAME", $ContainerName, $composer_template_contents); + $composer_template_contents = str_replace("CNT_DB_PASS", $provisionUID, $composer_template_contents); + + //Save to the specific path + $destination_composer_path = $ansible_folder . "/parts/" . $provisionUID . "/docker-compose.yml"; + $composerFile = fopen($destination_composer_path, "w") or die("Unable to open file!"); + fwrite($composerFile, $composer_template_contents); + fclose($composerFile); + + // GET THE TEMPLATE FOR THIS PROVISIONING + $template_content = file_get_contents($template_file); + $template_content_processed = str_replace("WHAT_HOST_IN_USE", $productId . "_SERVER", $template_content); + $template_content_processed = str_replace("WHAT_PART_FOLDER", $partFolder, $template_content_processed); + $template_content_processed = str_replace("DESTINATION_FOLDER", $destinationHomeFolder, $template_content_processed); + $template_content_processed = str_replace("WHAT_PART_LOCAL", $composer_template_path_local, $template_content_processed); // depends on the local machine + // CREATE PROVISOINING FILE + $myfile = fopen($ansible_folder . "/" . $prov_name, "w") or die("Unable to open file!"); + fwrite($myfile, $template_content_processed); + fclose($myfile); + } + + return view('welcome_message'); + } + + private function allocatePortNo(){ + + // return; + } + +}