Supervisor Manager

PHP

Plesk extension for managing Supervisor processes and workers with monitoring, process control, service management, and deployment automation.

Stars
16
Forks
1
Downloads
N/A
Open Issues
0
Files main

Repository Files

Loading file structure...
plib/views/scripts/index/index.phtml
<div class="sm-shell">
    <?php
    $domainQuery = !empty($this->domainQuery) ? $this->domainQuery : '';
    $domainParam = strpos($domainQuery, 'site_id=') !== false ? 'site_id' : 'dom_id';
    $infoUrl = pm_Context::getActionUrl('index', 'info') . $domainQuery;
    $extensionInfo = !empty($this->extensionInfo) ? $this->extensionInfo : array();
    $supervisorInstalled = !empty($this->diagnostics['installed']);
    $supervisorHealthy = !empty($this->diagnostics['healthy']);
    $hasAccess = !empty($this->hasAccess);
    ?>
    <?php if ($hasAccess || !empty($this->isAdmin)): ?>
        <div class="sm-commandbar">
            <div class="sm-toolbar">
                <?php if (!$supervisorInstalled): ?>
                    <?php if (!empty($this->canInstall)): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'install')); ?>" onsubmit="return confirm('Install Supervisor on this server now?');">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn btn-primary" type="submit">Install</button>
                        </form>
                    <?php endif; ?>
                    <a class="btn" href="<?php echo $this->escape($infoUrl); ?>">Info</a>
                <?php else: ?>
                    <?php if (!$supervisorHealthy && !empty($this->canSetup)): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'setup')); ?>" onsubmit="return confirm('Repair Supervisor service and socket setup now?');">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn btn-primary" type="submit">Repair Supervisor</button>
                        </form>
                    <?php endif; ?>
                    <?php if (!empty($this->canCreate)): ?>
                        <a class="btn" href="<?php echo $this->escape(pm_Context::getActionUrl('index', 'add') . $domainQuery); ?>">Add Program</a>
                    <?php endif; ?>
                    <?php if (!empty($this->canReread)): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'reload')); ?>">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn" type="submit">Reread</button>
                        </form>
                    <?php endif; ?>
                    <a class="btn" href="<?php echo $this->escape($infoUrl); ?>">Info</a>
                    <a class="btn" href="<?php echo $this->escape(pm_Context::getActionUrl('index', 'index') . $domainQuery); ?>">Refresh</a>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>

<?php if (!empty($this->notice)): ?>
    <div class="sm-message sm-message-info"><?php echo $this->escape($this->notice); ?></div>
<?php endif; ?>

<?php if (!empty($this->error)): ?>
    <div class="sm-message sm-message-error"><?php echo $this->escape($this->error); ?></div>
<?php endif; ?>

    <?php if ($this->isAdmin): ?>
        <div class="sm-summary">
            <div class="sm-summary-item">
                <span>Managed Processes</span>
                <strong><?php echo count($this->programs); ?></strong>
            </div>
            <?php if (!empty($this->domainContext)): ?>
                <div class="sm-summary-item">
                    <span>Domain</span>
                    <strong><?php echo $this->escape($this->domainName ?: ('ID ' . $this->domainId)); ?></strong>
                </div>
            <?php endif; ?>
            <div class="sm-summary-item">
                <span>Supervisor</span>
                <strong><?php echo $supervisorInstalled ? ($supervisorHealthy ? 'Ready' : 'Needs setup') : 'Not installed'; ?></strong>
            </div>
            <div class="sm-summary-item">
                <span>OS</span>
                <strong><?php echo $this->escape(isset($this->diagnostics['os']) ? $this->diagnostics['os'] : 'Unknown'); ?></strong>
            </div>
            <div class="sm-summary-item">
                <span>Config Directory</span>
                <strong><?php echo $this->escape(isset($this->diagnostics['config_dir']) ? $this->diagnostics['config_dir'] : '/etc/supervisor/conf.d'); ?></strong>
            </div>
            <div class="sm-summary-item">
                <span>Service</span>
                <strong><?php echo $this->escape(isset($this->diagnostics['service']) ? $this->diagnostics['service'] : 'supervisor'); ?><?php echo !empty($this->diagnostics['service_active']) ? ' active' : ' inactive'; ?></strong>
            </div>
        </div>
        <?php if ($supervisorInstalled && !$supervisorHealthy): ?>
            <section class="sm-install-card">
                <div class="sm-install-head">
                    <div>
                        <span>Supervisor needs setup</span>
                        <h2>Repair the Supervisor service and socket</h2>
                        <p><?php echo $this->escape(isset($this->diagnostics['status_output']) && trim($this->diagnostics['status_output']) !== '' ? trim($this->diagnostics['status_output']) : 'Supervisor is installed, but supervisorctl cannot connect to the running service.'); ?></p>
                    </div>
                    <strong>Needs setup</strong>
                </div>
                <div class="sm-install-grid">
                    <div>
                        <span>Detected OS</span>
                        <strong><?php echo $this->escape(isset($this->diagnostics['os']) ? $this->diagnostics['os'] : 'Unknown'); ?></strong>
                    </div>
                    <div>
                        <span>Socket</span>
                        <strong><?php echo $this->escape(isset($this->diagnostics['socket']) ? $this->diagnostics['socket'] : '/var/run/supervisor.sock'); ?></strong>
                    </div>
                    <div>
                        <span>Main config</span>
                        <strong><?php echo $this->escape(isset($this->diagnostics['main_config']) ? $this->diagnostics['main_config'] : 'Not detected'); ?></strong>
                    </div>
                </div>
                <div class="sm-install-actions">
                    <?php if (!empty($this->canSetup)): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'setup')); ?>" onsubmit="return confirm('Repair Supervisor service and socket setup now?');">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn btn-primary" type="submit">Repair Supervisor</button>
                        </form>
                    <?php endif; ?>
                    <a class="btn" href="<?php echo $this->escape(pm_Context::getActionUrl('index', 'index') . $domainQuery); ?>">Refresh</a>
                </div>
            </section>
        <?php endif; ?>
        <?php if (!$supervisorInstalled): ?>
            <section class="sm-install-card">
                <div class="sm-install-head">
                    <div>
                        <span>Supervisor is required</span>
                        <h2>Install Supervisor to start managing processes</h2>
                        <p>The extension detected your server OS and prepared the correct installation details for this panel.</p>
                    </div>
                    <strong>Not installed</strong>
                </div>
                <div class="sm-install-grid">
                    <div>
                        <span>Detected OS</span>
                        <strong><?php echo $this->escape(isset($this->diagnostics['os']) ? $this->diagnostics['os'] : 'Unknown'); ?></strong>
                    </div>
                    <div>
                        <span>Config directory</span>
                        <strong><?php echo $this->escape(isset($this->diagnostics['config_dir']) ? $this->diagnostics['config_dir'] : '/etc/supervisor/conf.d'); ?></strong>
                    </div>
                    <div>
                        <span>Service name</span>
                        <strong><?php echo $this->escape(isset($this->diagnostics['service']) ? $this->diagnostics['service'] : 'supervisor'); ?></strong>
                    </div>
                </div>
                <?php if (!empty($this->diagnostics['error'])): ?>
                    <div class="sm-install-warning">
                        Diagnostic check returned: <code><?php echo $this->escape($this->diagnostics['error']); ?></code>
                    </div>
                <?php endif; ?>
                <?php if (!empty($this->diagnostics['supports_install'])): ?>
                    <div class="sm-install-command">
                        <span>Install command</span>
                        <code><?php echo $this->escape($this->diagnostics['install_command']); ?></code>
                    </div>
                    <div class="sm-install-command">
                        <span>Manual SSH command</span>
                        <code>sudo <?php echo $this->escape($this->diagnostics['install_command']); ?></code>
                    </div>
                <?php else: ?>
                    <div class="sm-install-warning">
                        Automatic install is not available for this OS. Install Supervisor manually, then open this page again.
                    </div>
                <?php endif; ?>
                <div class="sm-install-actions">
                    <?php if (!empty($this->canInstall)): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'install')); ?>" onsubmit="return confirm('Install Supervisor on this server now?');">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn btn-primary" type="submit">Install Supervisor</button>
                        </form>
                    <?php endif; ?>
                    <a class="btn" href="<?php echo $this->escape($infoUrl); ?>">Extension Info</a>
                </div>
            </section>
        <?php endif; ?>
    <?php endif; ?>

    <?php if (!$hasAccess && empty($this->isAdmin)): ?>
    <?php elseif (!$supervisorInstalled): ?>
    <?php elseif (empty($this->programs)): ?>
        <div class="sm-empty">
            <h3>No Supervisor programs assigned</h3>
            <p>
                <?php if ($this->isAdmin): ?>
                    Add a program and assign it to a hosted domain.
                <?php elseif (!empty($this->canCreate)): ?>
                    Add a program for a domain that has Supervisor Manager enabled.
                <?php else: ?>
                    Ask the server administrator to assign Supervisor programs for your domains.
                <?php endif; ?>
            </p>
        </div>
    <?php else: ?>
        <div class="sm-list">
        <?php foreach ($this->programs as $program): ?>
            <?php
            $statusName = !empty($program['supervisor_name']) ? $program['supervisor_name'] : $program['name'];
            $status = isset($this->statuses[$statusName]) ? $this->statuses[$statusName] : array('state' => 'UNKNOWN', 'detail' => 'Not checked.');
            $stateClass = 'sm-status-' . strtolower($status['state']);
            $configExists = !empty($this->configExists[$program['id']]);
            $permissions = isset($this->programPermissions[$program['id']]) ? $this->programPermissions[$program['id']] : array('control' => false, 'logs' => false, 'manage' => false);
            ?>
            <section class="sm-row">
                <div class="sm-row-main">
                    <div class="sm-row-title">
                        <h3><?php echo $this->escape($program['display_name']); ?></h3>
                        <p class="sm-muted"><?php echo $this->escape($program['domain_name']); ?></p>
                    </div>
                    <div class="sm-row-status">
                        <span class="sm-status <?php echo $this->escape($stateClass); ?>"><?php echo $this->escape($status['state']); ?></span>
                        <span class="sm-mini <?php echo $configExists ? 'sm-mini-ok' : 'sm-mini-bad'; ?>"><?php echo $configExists ? 'config ok' : 'config missing'; ?></span>
                    </div>
                    <p class="sm-status-line"><?php echo $this->escape($status['detail']); ?></p>
                    <dl class="sm-facts">
                        <?php if (!empty($program['command'])): ?>
                            <div>
                                <dt>Command</dt>
                                <dd><code><?php echo $this->escape($program['command']); ?></code></dd>
                            </div>
                        <?php endif; ?>
                        <?php if (!empty($program['working_directory'])): ?>
                            <div>
                                <dt>Project Root</dt>
                                <dd><code><?php echo $this->escape($program['working_directory']); ?></code></dd>
                            </div>
                        <?php endif; ?>
                        <?php if (!empty($program['config_path'])): ?>
                            <div>
                                <dt>Config</dt>
                                <dd><code><?php echo $this->escape($program['config_path']); ?></code></dd>
                            </div>
                        <?php endif; ?>
                        <?php if (!empty($program['log_path'])): ?>
                            <div>
                                <dt>Log</dt>
                                <dd><code><?php echo $this->escape($program['log_path']); ?></code></dd>
                            </div>
                        <?php endif; ?>
                    </dl>
                </div>
                <?php if (!empty($program['description'])): ?>
                    <p class="sm-notes"><?php echo nl2br($this->escape($program['description'])); ?></p>
                <?php endif; ?>
                <div class="sm-actions">
                    <?php if (!empty($permissions['control'])): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'action')); ?>">
                            <input type="hidden" name="id" value="<?php echo $this->escape($program['id']); ?>">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <input type="hidden" name="program_action" value="start">
                            <button class="btn" type="submit" <?php echo empty($program['enabled']) ? 'disabled' : ''; ?>>Start</button>
                        </form>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'action')); ?>">
                            <input type="hidden" name="id" value="<?php echo $this->escape($program['id']); ?>">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <input type="hidden" name="program_action" value="stop">
                            <button class="btn" type="submit" <?php echo empty($program['enabled']) ? 'disabled' : ''; ?>>Stop</button>
                        </form>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'action')); ?>">
                            <input type="hidden" name="id" value="<?php echo $this->escape($program['id']); ?>">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <input type="hidden" name="program_action" value="restart">
                            <button class="btn" type="submit" <?php echo empty($program['enabled']) ? 'disabled' : ''; ?>>Restart</button>
                        </form>
                    <?php endif; ?>
                    <?php if (!empty($permissions['logs'])): ?>
                        <a class="btn" href="<?php echo $this->escape(pm_Context::getActionUrl('index', 'logs') . '?id=' . urlencode($program['id']) . (!empty($this->domainContext) ? '&' . ltrim($domainQuery, '?') : '')); ?>" target="_blank" rel="noopener noreferrer">Logs</a>
                    <?php endif; ?>
                    <?php if (!empty($permissions['manage'])): ?>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'generate')); ?>">
                            <input type="hidden" name="id" value="<?php echo $this->escape($program['id']); ?>">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn" type="submit">Generate Config</button>
                        </form>
                        <a class="btn" href="<?php echo $this->escape(pm_Context::getActionUrl('index', 'edit') . '?id=' . urlencode($program['id']) . (!empty($this->domainContext) ? '&' . ltrim($domainQuery, '?') : '')); ?>">Edit</a>
                        <form method="post" action="<?php echo $this->escape(pm_Context::getActionUrl('index', 'delete')); ?>" onsubmit="return confirm('Remove this program assignment?');">
                            <input type="hidden" name="id" value="<?php echo $this->escape($program['id']); ?>">
                            <?php if (!empty($this->domainContext)): ?>
                                <input type="hidden" name="context_domain_id" value="<?php echo (int) $this->domainId; ?>">
                                <input type="hidden" name="context_domain_param" value="<?php echo $this->escape($domainParam); ?>">
                            <?php endif; ?>
                            <button class="btn" type="submit">Delete</button>
                        </form>
                    <?php endif; ?>
                </div>
            </section>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
    <footer class="sm-footer">
        Proudly developed by
        <a href="<?php echo $this->escape(isset($extensionInfo['profile_url']) ? $extensionInfo['profile_url'] : 'https://github.com/ghostcompiler'); ?>" target="_blank" rel="noopener noreferrer">Ghost Compiler</a>
    </footer>
</div>