Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions app/Traits/ServersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,156 @@ protected function displayServerInfo(array $info): void

$this->io->displayDeets(['Services' => $services]);
$this->io->writeln('');

// Display Caddy information if available
if (isset($info['caddy']) && is_array($info['caddy']) && ($info['caddy']['available'] ?? false) === true) {
$caddyItems = [];

if (isset($info['caddy']['version']) && $info['caddy']['version'] !== 'unknown') {
/** @var string $version */
$version = $info['caddy']['version'];
$caddyItems[] = 'Version: '.$version;
}

if (isset($info['caddy']['uptime_seconds'])) {
/** @var int|string|float $rawUptime */
$rawUptime = $info['caddy']['uptime_seconds'];
/** @var int $uptimeSeconds */
$uptimeSeconds = (int) $rawUptime;
$caddyItems[] = 'Uptime: '.$this->formatUptime($uptimeSeconds);
}

if (isset($info['caddy']['total_requests'])) {
/** @var int|string|float $rawTotalReq */
$rawTotalReq = $info['caddy']['total_requests'];
/** @var int $totalReq */
$totalReq = (int) $rawTotalReq;
$caddyItems[] = 'Total Requests: '.number_format($totalReq);
}

if (isset($info['caddy']['active_requests'])) {
/** @var int|string $activeRequests */
$activeRequests = $info['caddy']['active_requests'];
$caddyItems[] = 'Active Requests: '.$activeRequests;
}

if (isset($info['caddy']['memory_mb']) && $info['caddy']['memory_mb'] !== '0') {
/** @var string $memoryMb */
$memoryMb = $info['caddy']['memory_mb'];
$caddyItems[] = 'Memory: '.$memoryMb.' MB';
}

if (count($caddyItems) > 0) {
$this->io->displayDeets(['Caddy' => $caddyItems]);
$this->io->writeln('');
}
}

// Display PHP-FPM information if available
if (isset($info['php_fpm']) && is_array($info['php_fpm']) && ($info['php_fpm']['available'] ?? false) === true) {
$phpFpmItems = [];

if (isset($info['php_fpm']['pool']) && $info['php_fpm']['pool'] !== 'unknown') {
/** @var string $pool */
$pool = $info['php_fpm']['pool'];
$phpFpmItems[] = 'Pool: '.$pool;
}

if (isset($info['php_fpm']['process_manager']) && $info['php_fpm']['process_manager'] !== 'unknown') {
/** @var string $processManager */
$processManager = $info['php_fpm']['process_manager'];
$phpFpmItems[] = 'Process Manager: '.$processManager;
}

if (isset($info['php_fpm']['active_processes'])) {
/** @var int|string $activeProcesses */
$activeProcesses = $info['php_fpm']['active_processes'];
$phpFpmItems[] = 'Active: '.$activeProcesses.' processes';
}

if (isset($info['php_fpm']['idle_processes'])) {
/** @var int|string $idleProcesses */
$idleProcesses = $info['php_fpm']['idle_processes'];
$phpFpmItems[] = 'Idle: '.$idleProcesses.' processes';
}

if (isset($info['php_fpm']['total_processes'])) {
/** @var int|string $totalProcesses */
$totalProcesses = $info['php_fpm']['total_processes'];
$phpFpmItems[] = 'Total: '.$totalProcesses.' processes';
}

if (isset($info['php_fpm']['listen_queue'])) {
/** @var int|string|float $rawQueue */
$rawQueue = $info['php_fpm']['listen_queue'];
/** @var int $queue */
$queue = (int) $rawQueue;
$queueDisplay = $queue > 0 ? "<fg=yellow>{$queue} waiting</>" : '0 waiting';
$phpFpmItems[] = 'Queue: '.$queueDisplay;
}

if (isset($info['php_fpm']['accepted_conn'])) {
/** @var int|string|float $rawAccepted */
$rawAccepted = $info['php_fpm']['accepted_conn'];
/** @var int $accepted */
$accepted = (int) $rawAccepted;
$phpFpmItems[] = 'Accepted: '.number_format($accepted);
}

if (isset($info['php_fpm']['max_children_reached'])) {
/** @var int|string|float $rawMaxChildren */
$rawMaxChildren = $info['php_fpm']['max_children_reached'];
/** @var int $maxChildren */
$maxChildren = (int) $rawMaxChildren;
if ($maxChildren > 0) {
$phpFpmItems[] = "<fg=yellow>Max Children Reached: {$maxChildren}</>";
}
}

if (isset($info['php_fpm']['slow_requests'])) {
/** @var int|string|float $rawSlowReqs */
$rawSlowReqs = $info['php_fpm']['slow_requests'];
/** @var int $slowReqsInt */
$slowReqsInt = (int) $rawSlowReqs;
if ($slowReqsInt > 0) {
$slowReqs = number_format($slowReqsInt);
$phpFpmItems[] = "<fg=yellow>Slow Requests: {$slowReqs}</>";
}
}

if (count($phpFpmItems) > 0) {
$this->io->displayDeets(['PHP-FPM' => $phpFpmItems]);
$this->io->writeln('');
}
}
}

/**
* Format uptime seconds into human-readable string.
*/
private function formatUptime(int $seconds): string
{
if ($seconds < 60) {
return "{$seconds}s";
}

if ($seconds < 3600) {
$minutes = floor($seconds / 60);

return "{$minutes}m";
}

if ($seconds < 86400) {
$hours = floor($seconds / 3600);
$minutes = floor(($seconds % 3600) / 60);

return "{$hours}h {$minutes}m";
}

$days = floor($seconds / 86400);
$hours = floor(($seconds % 86400) / 3600);

return "{$days}d {$hours}h";
}

//
Expand Down
58 changes: 37 additions & 21 deletions playbooks/demo-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#
# Demo Site Setup Playbook - Ubuntu/Debian Only
#
# Provision demo site (requires deployer user pre-configured)
# Provision demo site (requires deployer user and Caddy structure pre-configured)
# ----
#
# This playbook only supports Ubuntu and Debian distributions (debian family).
# Requires server-install to have run first (creates /etc/caddy/conf.d/sites/ directory).
#
# Required Environment Variables:
# DEPLOYER_OUTPUT_FILE - Output file path
Expand All @@ -16,7 +17,7 @@
# - status: success
# - demo_site_path: /home/deployer/demo/public
# - deployer_user: existing
# - caddy_configured: true
# - demo_site_configured: true
#

set -o pipefail
Expand All @@ -26,7 +27,7 @@ export DEBIAN_FRONTEND=noninteractive
[[ -z $DEPLOYER_PERMS ]] && echo "Error: DEPLOYER_PERMS required" && exit 1
export DEPLOYER_PERMS

#
# ----
# Helpers
# ----

Expand All @@ -41,8 +42,16 @@ run_cmd() {
fi
}

#
# ----
# Setup Functions
# ----

#
# Prerequisites
# ----

#
# Verify deployer user and home directory exist

require_deployer_user() {
if ! id -u deployer > /dev/null 2>&1; then
Expand All @@ -56,6 +65,13 @@ require_deployer_user() {
fi
}

#
# Demo Site Setup
# ----

#
# Create demo site directory structure and files

setup_demo_site() {
echo "✓ Setting up demo site..."

Expand Down Expand Up @@ -106,8 +122,15 @@ setup_demo_site() {
fi
}

configure_caddy() {
echo "✓ Configuring Caddy..."
#
# Caddy Configuration
# ----

#
# Configure Caddy for demo site

configure_demo_site() {
echo "✓ Configuring demo site..."

# PHP-FPM socket path (debian family)
local php_fpm_socket='/run/php/php8.4-fpm.sock'
Expand All @@ -125,15 +148,8 @@ configure_caddy() {
exit 1
fi

# Create Caddyfile with logging - using the simplest PHP configuration
if ! run_cmd tee /etc/caddy/Caddyfile > /dev/null <<- EOF; then
{
log {
output file /var/log/caddy/access.log
format json
}
}

# Create demo.caddy - demo site configuration
if ! run_cmd tee /etc/caddy/conf.d/sites/demo.caddy > /dev/null <<- EOF; then
# Demo site - HTTP only (can't get HTTPS cert for IP address)
http://:80 {
root * /home/deployer/demo/public
Expand All @@ -148,11 +164,11 @@ configure_caddy() {
format json
}

# This single line handles everything: PHP files, index.php routing, and static files
php_fastcgi unix/${php_fpm_socket}
# This single line handles everything: PHP files, index.php routing, and static files
php_fastcgi unix/${php_fpm_socket}
}
EOF
echo "Error: Failed to create Caddyfile" >&2
echo "Error: Failed to create demo.caddy" >&2
exit 1
fi

Expand All @@ -177,22 +193,22 @@ configure_caddy() {
fi
}

#
# ----
# Main Execution
# ----

main() {
# Execute setup tasks
require_deployer_user
setup_demo_site
configure_caddy
configure_demo_site

# Write output YAML
if ! cat > "$DEPLOYER_OUTPUT_FILE" <<- EOF; then
status: success
demo_site_path: /home/deployer/demo/public
deployer_user: existing
caddy_configured: true
demo_site_configured: true
EOF
echo "Error: Failed to write output file" >&2
exit 1
Expand Down
Loading