#!/usr/bin/env node /** * Komodo Setup Script for komodo integration * * Prerequisites: * npm install komodo_client * * Environment variables: * - KOMODO_HOST (default: komodo.internal) * - KOMODO_PORT (default: 9120) * - KOMODO_KEY (required) * - KOMODO_SECRET (required) */ // If running without komodo_client installed, use simple fetch const USE_SIMPLE_FETCH = true; const KOMODO_HOST = process.env.KOMODO_HOST || 'komodo.internal'; const KOMODO_PORT = process.env.KOMODO_PORT || '9120'; const KOMODO_URL = `http://${KOMODO_HOST}:${KOMODO_PORT}`; const KOMODO_KEY = process.env.KOMODO_KEY; const KOMODO_SECRET = process.env.KOMODO_SECRET; if (!KOMODO_KEY || !KOMODO_SECRET) { console.error('Error: KOMODO_KEY and KOMODO_SECRET environment variables must be set'); process.exit(1); } console.log('======================================'); console.log('Komodo Setup Script'); console.log('======================================'); console.log(`Komodo URL: ${KOMODO_URL}`); console.log(''); // Simple API wrapper using fetch async function komodoApi(method, endpoint, data = {}) { const response = await fetch(`${KOMODO_URL}/${endpoint}/${method}`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': KOMODO_KEY, 'x-api-secret': KOMODO_SECRET }, body: JSON.stringify(data) }); if (!response.ok) { throw new Error(`API request failed: ${response.status} ${response.statusText}`); } return await response.json(); } async function main() { try { // Test connection console.log('Testing connection to Komodo...'); const version = await komodoApi('GetVersion', 'read'); console.log(`✓ Connected to Komodo: ${JSON.stringify(version)}`); console.log(''); // List servers console.log('Step 1: Checking for available servers...'); const servers = await komodoApi('ListServers', 'read'); if (servers && servers.length > 0) { servers.forEach(server => { console.log(` - ${server.name} (${server.id})`); }); } else { console.log(' No servers found'); } console.log(''); console.log('Note: You\'ll need a server ID for the Stack resource.'); console.log('If no servers are listed, add one in Komodo UI or config.'); console.log(''); // Create Build resource console.log('Step 2: Creating Build resource \'komodo-build\'...'); try { const buildConfig = { name: 'komodo-build', description: 'Build Docker image for komodo app', tags: ['demo', 'gitea'], config: { git_provider: 'gitea.straymoog.xyz', git_account: 'stray', repo: 'stray/komodo', branch: 'main', build_path: '.', dockerfile_path: 'Dockerfile' } }; const buildResult = await komodoApi('CreateBuild', 'write', buildConfig); console.log(' ✓ Build resource created'); } catch (error) { console.log(` ⚠ Build might already exist or error occurred: ${error.message}`); } console.log(''); // Create Stack resource console.log('Step 3: Creating Stack resource \'komodo-stack\'...'); console.log(' → This requires a valid server_id'); console.log(' → Skipping automatic creation - please create via UI or update script with server_id'); console.log(''); // Create Resource Sync console.log('Step 4: Creating Resource Sync \'komodo-sync\'...'); try { const syncConfig = { name: 'komodo-sync', description: 'Sync Komodo resources from Git', tags: ['demo', 'gitea'], config: { git_provider: 'gitea.straymoog.xyz', git_account: 'stray', repo: 'stray/komodo', branch: 'main', resource_path: '.komodo', commit_to_repo: false } }; const syncResult = await komodoApi('CreateResourceSync', 'write', syncConfig); console.log(' ✓ Resource Sync created'); console.log(' → You can now execute the sync to import all resources from .komodo/resources.toml'); } catch (error) { console.log(` ⚠ Sync might already exist or error occurred: ${error.message}`); } console.log(''); console.log('======================================'); console.log('Setup Summary'); console.log('======================================'); console.log('Gitea webhooks: ✓ Created (3 webhooks)'); console.log(' - Build webhook'); console.log(' - Stack deploy webhook'); console.log(' - Sync webhook'); console.log(''); console.log('Komodo resources:'); console.log(' - Git provider: Configure in core.config.toml'); console.log(' - Build: Created (or attempted)'); console.log(' - Stack: Manual creation required (needs server_id)'); console.log(' - Sync: Created (or attempted)'); console.log(''); console.log('Next steps:'); console.log('1. Add git provider config to core.config.toml (see komodo-gitea.config.toml)'); console.log('2. Restart Komodo core to load git provider'); console.log('3. Execute the Resource Sync to import all resources'); console.log('4. Test by pushing to the Git repository'); console.log(''); console.log('Repository: https://gitea.straymoog.xyz/stray/komodo'); console.log('======================================'); } catch (error) { console.error('Error:', error.message); process.exit(1); } } main();