Public Access
b491241a58
Universal: Sync Feature Branch Versions / Sync feature branches with dev (push) Has been skipped
# Conflicts: # .mokogitea/CLAUDE.md # .mokogitea/ISSUE_TEMPLATE/config.yml # .mokogitea/ISSUE_TEMPLATE/documentation.md # .mokogitea/ISSUE_TEMPLATE/feature_request.md # .mokogitea/ISSUE_TEMPLATE/security.md # .mokogitea/branch-protection.yml # .mokogitea/bulk-repo-sync.yml # .mokogitea/pr-branch-check.yml # .mokogitea/renovate.yml # .mokogitea/sync-wikis.yml # .mokogitea/workflows/auto-bump.yml # .mokogitea/workflows/auto-release.yml # .mokogitea/workflows/ci-platform.yml # .mokogitea/workflows/cleanup.yml # .mokogitea/workflows/gitleaks.yml # .mokogitea/workflows/issue-branch.yml # .mokogitea/workflows/notify.yml # .mokogitea/workflows/pre-release.yml # .mokogitea/workflows/repo-health.yml # .mokogitea/workflows/security-audit.yml # .script-registry.json # CHANGELOG.md # PLUGIN_SCRIPTS.md # README.md # analysis/index.md # automation/bulk_joomla_template.php # automation/bulk_sync.php # automation/enrich_manifest_xml.php # automation/enrich_mokostandards_xml.php # automation/index.md # automation/migrate_to_gitea.php # automation/push_files.php # automation/push_manifest_xml.php # automation/push_mokostandards_xml.php # automation/repo_cleanup.php # bin/moko # cli/archive_repo.php # cli/audit_query.php # cli/badge_update.php # cli/branch_rename.php # cli/bulk_workflow_push.php # cli/bulk_workflow_trigger.php # cli/changelog_promote.php # cli/changelog_prune.php # cli/client_dashboard.php # cli/client_health_check.php # cli/client_inventory.php # cli/client_provision.php # cli/completion.php # cli/create_project.php # cli/create_repo.php # cli/deploy_joomla.php # cli/dev_branch_reset.php # cli/grafana_dashboard.php # cli/joomla_build.php # cli/joomla_compat_check.php # cli/joomla_metadata_validate.php # cli/joomla_release.php # cli/license_manage.php # cli/manifest_element.php # cli/manifest_licensing.php # cli/manifest_read.php # cli/package_build.php # cli/platform_detect.php # cli/release.php # cli/release_body_update.php # cli/release_cascade.php # cli/release_create.php # cli/release_manage.php # cli/release_mirror.php # cli/release_notes.php # cli/release_package.php # cli/release_promote.php # cli/release_publish.php # cli/release_validate.php # cli/release_verify.php # cli/scaffold_client.php # cli/sync_rulesets.php # cli/theme_lint.php # cli/updates_xml_build.php # cli/updates_xml_sync.php # cli/version_auto_bump.php # cli/version_bump.php # cli/version_bump_remote.php # cli/version_check.php # cli/version_read.php # cli/version_reset_dev.php # cli/version_set_platform.php # cli/wiki_sync.php # cli/workflow_sync.php # composer.json # deploy/backup-before-deploy.php # deploy/deploy-dolibarr.php # deploy/deploy-joomla.php # deploy/deploy-sftp.php # deploy/health-check.php # deploy/rollback-joomla.php # deploy/sync-joomla.php # fix/fix_line_endings.php # fix/fix_permissions.php # fix/fix_tabs.php # fix/fix_trailing_spaces.php # fix/index.md # index.md # lib/CliBase.php # lib/Common.php # lib/Enterprise/AbstractProjectPlugin.php # lib/Enterprise/ApiClient.php # lib/Enterprise/AuditLogger.php # lib/Enterprise/CheckpointManager.php # lib/Enterprise/CliFramework.php # lib/Enterprise/Config.php # lib/Enterprise/ConfigValidator.php # lib/Enterprise/EnterpriseReadinessValidator.php # lib/Enterprise/ErrorRecovery.php # lib/Enterprise/FileFixUtility.php # lib/Enterprise/GitHubAdapter.php # lib/Enterprise/GitPlatformAdapter.php # lib/Enterprise/InputValidator.php # lib/Enterprise/ManifestParser.php # lib/Enterprise/ManifestReader.php # lib/Enterprise/MetricsCollector.php # lib/Enterprise/MokoGiteaAdapter.php # lib/Enterprise/PackageBuilder.php # lib/Enterprise/PlatformAdapterFactory.php # lib/Enterprise/PluginFactory.php # lib/Enterprise/PluginRegistry.php # lib/Enterprise/Plugins/ApiPlugin.php # lib/Enterprise/Plugins/DocumentationPlugin.php # lib/Enterprise/Plugins/DolibarrPlugin.php # lib/Enterprise/Plugins/GenericPlugin.php # lib/Enterprise/Plugins/JoomlaPlugin.php # lib/Enterprise/Plugins/McpServerPlugin.php # lib/Enterprise/Plugins/MobilePlugin.php # lib/Enterprise/Plugins/NodeJsPlugin.php # lib/Enterprise/Plugins/PythonPlugin.php # lib/Enterprise/Plugins/TerraformPlugin.php # lib/Enterprise/Plugins/WordPressPlugin.php # lib/Enterprise/ProjectConfigValidator.php # lib/Enterprise/ProjectMetricsCollector.php # lib/Enterprise/ProjectPluginInterface.php # lib/Enterprise/ProjectTypeDetector.php # lib/Enterprise/RecoveryError.php # lib/Enterprise/RecoveryManager.php # lib/Enterprise/RepositoryHealthChecker.php # lib/Enterprise/RepositorySynchronizer.php # lib/Enterprise/RetryHelper.php # lib/Enterprise/SecurityValidator.php # lib/Enterprise/SourceResolver.php # lib/Enterprise/SynchronizationException.php # lib/Enterprise/TransactionManager.php # lib/Enterprise/UnifiedValidation.php # lib/index.md # lib/plugins/Joomla/UpdateXmlGenerator.php # maintenance/index.md # maintenance/pin_action_shas.php # maintenance/repo_inventory.php # maintenance/rotate_secrets.php # maintenance/setup_labels.php # maintenance/sync_dolibarr_readmes.php # maintenance/update_repo_inventory.php # maintenance/update_sha_hashes.php # maintenance/update_version_from_readme.php # mcp/config.example.json # mcp/package.json # mcp/src/config.ts # mcp/src/index.ts # mcp/src/runner.ts # mcp/src/types.ts # phpcs.xml # plugin_health_check.php # plugin_list.php # plugin_metrics.php # plugin_readiness.php # plugin_validate.php # release/generate_dolibarr_version_txt.php # release/generate_joomla_update_xml.php # src/functions.php # templates/configs/README.md # templates/configs/index.md # templates/configs/manifest.xml.template # templates/configs/manifest.yml.template # templates/configs/mokostandards.xml.template # templates/configs/mokostandards.yml.template # templates/configs/phpcs.xml # templates/docs/README.md # templates/docs/extra/README.md # templates/docs/extra/index.md # templates/docs/index.md # templates/docs/required/GOVERNANCE.md # templates/docs/required/README.md # templates/docs/required/index.md # templates/docs/required/template-CONTRIBUTING.md # templates/docs/required/template-README.md # templates/docs/required/template-SECURITY.md # templates/index.md # templates/licenses/README.md # templates/licenses/index.md # templates/makefiles/README.md # templates/mokogitea/CLAUDE.dolibarr.md.template # templates/mokogitea/CLAUDE.joomla.md.template # templates/mokogitea/CLAUDE.md.template # templates/mokogitea/ISSUE_TEMPLATE/config.yml # templates/mokogitea/ISSUE_TEMPLATE/documentation.md # templates/mokogitea/ISSUE_TEMPLATE/dolibarr_module_id_request.md # templates/mokogitea/ISSUE_TEMPLATE/feature_request.md # templates/mokogitea/ISSUE_TEMPLATE/security.md # templates/mokogitea/README.md # templates/mokogitea/copilot-instructions.dolibarr.md.template # templates/mokogitea/copilot-instructions.joomla.md.template # templates/mokogitea/copilot-instructions.md.template # templates/mokogitea/dependabot.yml.template # templates/mokogitea/override.tf.template # templates/required/README.md # templates/schemas/README.md # templates/schemas/manifest-schema.xsd # templates/schemas/moko-platform-schema.xsd # templates/schemas/mokostandards-schema.xsd # templates/schemas/schemas/README.md # templates/schemas/template-repository-structure.xml # templates/scripts/README.md # templates/scripts/common/CliBase.template.php # templates/scripts/fix/index.md # templates/scripts/index.md # templates/scripts/release/index.md # templates/scripts/release/package_dolibarr.php # templates/scripts/release/package_joomla.php # templates/scripts/sftp-config/README.md # templates/scripts/validate/dolibarr_module.php # templates/scripts/validate/index.md # templates/scripts/validate/validate_manifest.php # templates/scripts/validate/validate_structure.php # templates/security/README.md # templates/security/index.php # templates/stubs/dolibarr.php # templates/stubs/joomla.php # templates/web/index.php # tests/Enterprise/GitPlatformAdapterTest.php # tests/Unit/VersionBumpTest.php # tests/Unit/VersionReadTest.php # tests/index.md # tests/test_circuit_breaker_handling.php # tests/test_enterprise_libraries.php # validate/SECURITY_SCANNING.md # validate/auto_detect_platform.php # validate/check_changelog.php # validate/check_client_theme.php # validate/check_composer_deps.php # validate/check_dolibarr_module.php # validate/check_enterprise_readiness.php # validate/check_file_integrity.php # validate/check_joomla_manifest.php # validate/check_language_structure.php # validate/check_license_headers.php # validate/check_no_secrets.php # validate/check_paths.php # validate/check_php_syntax.php # validate/check_repo_health.php # validate/check_structure.php # validate/check_tabs.php # validate/check_version_consistency.php # validate/check_wiki_health.php # validate/check_xml_wellformed.php # validate/index.md # validate/scan_drift.php # wrappers/auto_detect_platform.php # wrappers/bulk_sync.php # wrappers/check_changelog.php # wrappers/check_dolibarr_module.php # wrappers/check_enterprise_readiness.php # wrappers/check_joomla_manifest.php # wrappers/check_language_structure.php # wrappers/check_license_headers.php # wrappers/check_no_secrets.php # wrappers/check_paths.php # wrappers/check_php_syntax.php # wrappers/check_repo_health.php # wrappers/check_structure.php # wrappers/check_tabs.php # wrappers/check_version_consistency.php # wrappers/check_xml_wellformed.php # wrappers/deploy_sftp.php # wrappers/fix_line_endings.php # wrappers/fix_permissions.php # wrappers/fix_tabs.php # wrappers/fix_trailing_spaces.php # wrappers/gen_wrappers.php # wrappers/index.md # wrappers/pin_action_shas.php # wrappers/plugin_health_check.php # wrappers/plugin_list.php # wrappers/plugin_metrics.php # wrappers/plugin_readiness.php # wrappers/plugin_validate.php # wrappers/scan_drift.php # wrappers/setup_labels.php # wrappers/sync_dolibarr_readmes.php # wrappers/update_sha_hashes.php # wrappers/update_version_from_readme.php
500 lines
14 KiB
PHP
500 lines
14 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
|
*
|
|
* This file is part of a Moko Consulting project.
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* FILE INFORMATION
|
|
<<<<<<< HEAD
|
|
* DEFGROUP: MokoCLI.CLI
|
|
* INGROUP: MokoCLI
|
|
* REPO: https://git.mokoconsulting.tech/MokoConsulting/mokocli
|
|
* PATH: /cli/client_provision.php
|
|
* VERSION: 09.25.05
|
|
=======
|
|
* DEFGROUP: mokoplatform.CLI
|
|
* INGROUP: mokoplatform
|
|
* REPO: https://git.mokoconsulting.tech/MokoConsulting/mokoplatform
|
|
* PATH: /cli/client_provision.php
|
|
* VERSION: 09.32.00
|
|
>>>>>>> main
|
|
* BRIEF: Provision a new client environment end-to-end
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../lib/Enterprise/CliFramework.php';
|
|
|
|
use MokoCli\CliFramework;
|
|
|
|
class ClientProvisionCli extends CliFramework
|
|
{
|
|
private string $giteaUrl = 'https://git.mokoconsulting.tech';
|
|
private string $giteaToken = '';
|
|
private string $grafanaUrl = '';
|
|
private string $grafanaToken = '';
|
|
private string $configFile = '';
|
|
private string $step = '';
|
|
/** @var array<string, mixed> */
|
|
private array $config = [];
|
|
private string $org = '';
|
|
private string $repoName = '';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this->setDescription('Provision a new client environment end-to-end');
|
|
$this->addArgument('--config', 'Client config JSON', '');
|
|
$this->addArgument('--step', 'Run one step: repo, variables, secrets, monitoring, summary', '');
|
|
}
|
|
|
|
protected function run(): int
|
|
{
|
|
$this->configFile = $this->getArgument('--config');
|
|
$this->step = $this->getArgument('--step');
|
|
|
|
if ($this->configFile === '') {
|
|
$this->log('ERROR', '--config is required.');
|
|
return 1;
|
|
}
|
|
|
|
if (!file_exists($this->configFile)) {
|
|
$this->log('ERROR', "Not found: {$this->configFile}");
|
|
return 1;
|
|
}
|
|
|
|
$json = file_get_contents($this->configFile);
|
|
$this->config = json_decode($json, true);
|
|
|
|
if (!is_array($this->config)) {
|
|
$this->log('ERROR', 'Invalid JSON in config file.');
|
|
return 1;
|
|
}
|
|
|
|
$this->giteaToken = $this->config['gitea_token']
|
|
?? getenv('MOKOGITEA_TOKEN') ?: '';
|
|
$this->grafanaUrl = $this->config['grafana_url']
|
|
?? getenv('GRAFANA_URL') ?: '';
|
|
$this->grafanaToken = $this->config['grafana_token']
|
|
?? getenv('GRAFANA_TOKEN') ?: '';
|
|
$this->giteaUrl = $this->config['gitea_url']
|
|
?? $this->giteaUrl;
|
|
|
|
if ($this->giteaToken === '') {
|
|
$this->log('ERROR', 'gitea_token or MOKOGITEA_TOKEN required.');
|
|
return 1;
|
|
}
|
|
|
|
$this->org = $this->config['org'] ?? '';
|
|
$clientName = $this->config['name'] ?? '';
|
|
|
|
if ($this->org === '' || $clientName === '') {
|
|
$this->log('ERROR', '"org" and "name" required in config.');
|
|
return 1;
|
|
}
|
|
|
|
$this->repoName = 'client-waas-' . $clientName;
|
|
|
|
$this->log('INFO', "=== Client Provisioning: {$clientName} ===");
|
|
$this->log('INFO', " Org: {$this->org}");
|
|
$this->log('INFO', " Repo: {$this->repoName}");
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', ' Mode: DRY RUN');
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
$steps = [
|
|
'repo' => 'createRepo',
|
|
'variables' => 'setVariables',
|
|
'secrets' => 'setSecrets',
|
|
'monitoring' => 'setupMonitoring',
|
|
'summary' => 'printSummary',
|
|
];
|
|
|
|
$exitCode = 0;
|
|
|
|
foreach ($steps as $name => $method) {
|
|
if ($this->step !== '' && $this->step !== $name) {
|
|
continue;
|
|
}
|
|
|
|
$result = $this->$method();
|
|
|
|
if ($result !== 0) {
|
|
$exitCode = 1;
|
|
}
|
|
}
|
|
|
|
return $exitCode;
|
|
}
|
|
|
|
private function createRepo(): int
|
|
{
|
|
$this->log('INFO', '[1/5] Creating repository...');
|
|
|
|
$check = $this->giteaApi(
|
|
'GET',
|
|
"/api/v1/repos/{$this->org}/{$this->repoName}"
|
|
);
|
|
|
|
if ($check['code'] === 200) {
|
|
$this->log('INFO', ' SKIP: repo already exists');
|
|
return 0;
|
|
}
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', " WOULD CREATE: {$this->org}/{$this->repoName}");
|
|
return 0;
|
|
}
|
|
|
|
$payload = json_encode([
|
|
'owner' => $this->org,
|
|
'name' => $this->repoName,
|
|
'description' => ($this->config['name'] ?? '') . ' WaaS site',
|
|
'private' => true,
|
|
'git_content' => true,
|
|
'topics' => true,
|
|
'labels' => true,
|
|
]);
|
|
|
|
$resp = $this->giteaApi(
|
|
'POST',
|
|
'/api/v1/repos/MokoConsulting/'
|
|
. 'Template-Client-WaaS/generate',
|
|
$payload
|
|
);
|
|
|
|
if ($resp['code'] < 200 || $resp['code'] >= 300) {
|
|
$this->log('ERROR', "HTTP {$resp['code']}");
|
|
return 1;
|
|
}
|
|
|
|
$this->log('INFO', ' OK: Repo created');
|
|
|
|
$this->giteaApi(
|
|
'POST',
|
|
"/api/v1/repos/{$this->org}/{$this->repoName}/branches",
|
|
json_encode([
|
|
'new_branch_name' => 'dev',
|
|
'old_branch_name' => 'main',
|
|
])
|
|
);
|
|
|
|
$this->log('INFO', ' OK: dev branch created');
|
|
|
|
return 0;
|
|
}
|
|
|
|
private function setVariables(): int
|
|
{
|
|
$this->log('INFO', '[2/5] Setting repo variables...');
|
|
|
|
$vars = $this->config['variables'] ?? [];
|
|
|
|
if (empty($vars)) {
|
|
$this->log('INFO', ' SKIP: No variables in config');
|
|
return 0;
|
|
}
|
|
|
|
$errors = 0;
|
|
$api = "/api/v1/repos/{$this->org}/{$this->repoName}"
|
|
. "/actions/variables";
|
|
|
|
foreach ($vars as $name => $value) {
|
|
if ($this->dryRun) {
|
|
$display = strlen($value) > 40
|
|
? substr($value, 0, 37) . '...' : $value;
|
|
$this->log('INFO', " WOULD SET: {$name} = {$display}");
|
|
continue;
|
|
}
|
|
|
|
$ok = $this->setOrCreateVariable($api, $name, $value);
|
|
|
|
if ($ok) {
|
|
$this->log('INFO', " OK: {$name}");
|
|
} else {
|
|
$this->log('ERROR', " {$name}");
|
|
$errors++;
|
|
}
|
|
}
|
|
|
|
return $errors > 0 ? 1 : 0;
|
|
}
|
|
|
|
private function setSecrets(): int
|
|
{
|
|
$this->log('INFO', '[3/5] Setting repo secrets...');
|
|
|
|
$secrets = $this->config['secrets'] ?? [];
|
|
|
|
if (empty($secrets)) {
|
|
$this->log('INFO', ' SKIP: No secrets in config');
|
|
return 0;
|
|
}
|
|
|
|
$errors = 0;
|
|
$api = "/api/v1/repos/{$this->org}/{$this->repoName}"
|
|
. "/actions/secrets";
|
|
|
|
foreach ($secrets as $name => $value) {
|
|
if (str_starts_with($value, '@')) {
|
|
$keyPath = substr($value, 1);
|
|
|
|
if (!file_exists($keyPath)) {
|
|
$this->log('ERROR', " {$name} file not found: {$keyPath}");
|
|
$errors++;
|
|
continue;
|
|
}
|
|
|
|
$value = file_get_contents($keyPath);
|
|
}
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', " WOULD SET: {$name} (len: " . strlen($value) . ")");
|
|
continue;
|
|
}
|
|
|
|
$resp = $this->giteaApi(
|
|
'PUT',
|
|
"{$api}/{$name}",
|
|
json_encode(['data' => $value])
|
|
);
|
|
|
|
if ($resp['code'] >= 200 && $resp['code'] < 300) {
|
|
$this->log('INFO', " OK: {$name}");
|
|
} else {
|
|
$this->log('ERROR', " {$name} (HTTP {$resp['code']})");
|
|
$errors++;
|
|
}
|
|
}
|
|
|
|
return $errors > 0 ? 1 : 0;
|
|
}
|
|
|
|
private function setupMonitoring(): int
|
|
{
|
|
$this->log('INFO', '[4/5] Setting up monitoring...');
|
|
|
|
$mon = $this->config['monitoring'] ?? [];
|
|
|
|
if (empty($mon)) {
|
|
$this->log('INFO', ' SKIP: No monitoring config');
|
|
return 0;
|
|
}
|
|
|
|
$dashFile = $mon['grafana_dashboard'] ?? '';
|
|
|
|
if (
|
|
$dashFile !== '' && $this->grafanaUrl !== ''
|
|
&& $this->grafanaToken !== ''
|
|
) {
|
|
$this->pushGrafanaDashboard(
|
|
$dashFile,
|
|
$mon['grafana_folder'] ?? 'Clients'
|
|
);
|
|
}
|
|
|
|
$urls = $mon['urls'] ?? [];
|
|
$domains = $mon['domains'] ?? [];
|
|
$api = "/api/v1/repos/{$this->org}/{$this->repoName}"
|
|
. "/actions/variables";
|
|
|
|
if (!empty($urls)) {
|
|
$urlStr = implode("\n", $urls);
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', ' WOULD SET: MONITORED_URLS');
|
|
} else {
|
|
$this->setOrCreateVariable($api, 'MONITORED_URLS', $urlStr);
|
|
$this->log('INFO', ' OK: MONITORED_URLS');
|
|
}
|
|
}
|
|
|
|
if (!empty($domains)) {
|
|
$domainStr = implode("\n", $domains);
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', ' WOULD SET: MONITORED_DOMAINS');
|
|
} else {
|
|
$this->setOrCreateVariable($api, 'MONITORED_DOMAINS', $domainStr);
|
|
$this->log('INFO', ' OK: MONITORED_DOMAINS');
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private function pushGrafanaDashboard(string $file, string $folder): void
|
|
{
|
|
if (!file_exists($file)) {
|
|
$this->warning("Dashboard not found: {$file}");
|
|
return;
|
|
}
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', " WOULD PUSH: dashboard to \"{$folder}\"");
|
|
return;
|
|
}
|
|
|
|
$dashboard = json_decode(file_get_contents($file), true);
|
|
|
|
if (!is_array($dashboard)) {
|
|
$this->log('ERROR', 'Invalid dashboard JSON');
|
|
return;
|
|
}
|
|
|
|
$folderId = $this->resolveGrafanaFolder($folder);
|
|
$dashboard['id'] = null;
|
|
|
|
$resp = $this->grafanaApi(
|
|
'POST',
|
|
'/api/dashboards/db',
|
|
json_encode([
|
|
'dashboard' => $dashboard,
|
|
'folderId' => $folderId,
|
|
'overwrite' => true,
|
|
])
|
|
);
|
|
|
|
if ($resp['code'] === 200) {
|
|
$data = json_decode($resp['body'], true);
|
|
$this->log('INFO', " OK: Dashboard (uid: " . ($data['uid'] ?? '?') . ")");
|
|
} else {
|
|
$this->log('ERROR', " Dashboard push (HTTP {$resp['code']})");
|
|
}
|
|
}
|
|
|
|
private function resolveGrafanaFolder(string $title): int
|
|
{
|
|
$resp = $this->grafanaApi('GET', '/api/folders');
|
|
|
|
if ($resp['code'] !== 200) {
|
|
return 0;
|
|
}
|
|
|
|
$folders = json_decode($resp['body'], true);
|
|
|
|
if (!is_array($folders)) {
|
|
return 0;
|
|
}
|
|
|
|
foreach ($folders as $f) {
|
|
if (strcasecmp($f['title'] ?? '', $title) === 0) {
|
|
return (int) ($f['id'] ?? 0);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private function printSummary(): int
|
|
{
|
|
$vars = $this->config['variables'] ?? [];
|
|
$secrets = $this->config['secrets'] ?? [];
|
|
|
|
echo "\n";
|
|
$this->log('INFO', '[5/5] Provisioning summary');
|
|
echo str_repeat('=', 60) . "\n";
|
|
echo " Repo: {$this->giteaUrl}/{$this->org}/{$this->repoName}\n";
|
|
echo ' Variables: ' . count($vars) . " set\n";
|
|
echo ' Secrets: ' . count($secrets) . " set\n";
|
|
echo "\n";
|
|
echo "Next steps:\n";
|
|
echo " 1. Clone and customize the Joomla template\n";
|
|
echo " 2. Push to dev to trigger dev deployment\n";
|
|
echo " 3. Merge dev -> main for production release\n";
|
|
echo str_repeat('=', 60) . "\n";
|
|
|
|
return 0;
|
|
}
|
|
|
|
private function setOrCreateVariable(
|
|
string $api,
|
|
string $name,
|
|
string $value
|
|
): bool {
|
|
$resp = $this->giteaApi(
|
|
'PUT',
|
|
"{$api}/{$name}",
|
|
json_encode(['value' => $value])
|
|
);
|
|
|
|
if ($resp['code'] === 404) {
|
|
$resp = $this->giteaApi(
|
|
'POST',
|
|
$api,
|
|
json_encode(['name' => $name, 'value' => $value])
|
|
);
|
|
}
|
|
|
|
return $resp['code'] >= 200 && $resp['code'] < 300;
|
|
}
|
|
|
|
private function giteaApi(
|
|
string $method,
|
|
string $endpoint,
|
|
?string $body = null
|
|
): array {
|
|
return $this->httpRequest(
|
|
$this->giteaUrl . $endpoint,
|
|
$method,
|
|
"token {$this->giteaToken}",
|
|
$body
|
|
);
|
|
}
|
|
|
|
private function grafanaApi(
|
|
string $method,
|
|
string $endpoint,
|
|
?string $body = null
|
|
): array {
|
|
return $this->httpRequest(
|
|
$this->grafanaUrl . $endpoint,
|
|
$method,
|
|
"Bearer {$this->grafanaToken}",
|
|
$body
|
|
);
|
|
}
|
|
|
|
private function httpRequest(
|
|
string $url,
|
|
string $method,
|
|
string $auth,
|
|
?string $body = null
|
|
): array {
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
'Accept: application/json',
|
|
"Authorization: {$auth}",
|
|
]);
|
|
|
|
if ($body !== null) {
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
|
}
|
|
|
|
$responseBody = curl_exec($ch);
|
|
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if (curl_errno($ch)) {
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
return ['code' => 0, 'body' => "cURL error: {$error}"];
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
return ['code' => $httpCode, 'body' => $responseBody];
|
|
}
|
|
}
|
|
|
|
$app = new ClientProvisionCli();
|
|
exit($app->execute());
|