|
|
|
import { execSync, exec } from 'child_process'
|
|
|
|
import { isFolder } from '../files.js'
|
|
|
|
import path from 'path'
|
|
|
|
import { unlink } from 'node:fs'
|
|
|
|
import SystemServiceManager from '../services.js'
|
|
|
|
|
|
|
|
const AO_SERVER_PATH = path.join(process.env.HOME, 'ao-server')
|
|
|
|
|
|
|
|
const aoServerServiceTemplate =
|
|
|
|
`[Unit]
|
|
|
|
Description=ao-server daemon
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
WorkingDirectory=${process.env.HOME}/ao-server
|
|
|
|
ExecStart=npm run build && node --loader ts-node/esm ${process.env.HOME}/ao-server/src/server/app.ts
|
|
|
|
User=${process.env.USER}
|
|
|
|
Type=simple
|
|
|
|
Restart=on-failure
|
|
|
|
PrivateTmp=true
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target`
|
|
|
|
|
|
|
|
const aoServerServiceManager = new SystemServiceManager('ao-server', aoServerServiceTemplate)
|
|
|
|
|
|
|
|
// Returns one of: off, installed, enabled, running, synced, error
|
|
|
|
function serviceStatus() {
|
|
|
|
if(!aoIsInstalled()) {
|
|
|
|
return 'off'
|
|
|
|
}
|
|
|
|
if(aoServerServiceManager.isInstalled()) {
|
|
|
|
const isRunning = aoServerServiceManager.isRunning()
|
|
|
|
switch(isRunning) {
|
|
|
|
case true:
|
|
|
|
return 'running'
|
|
|
|
case 'error':
|
|
|
|
return 'error'
|
|
|
|
}
|
|
|
|
return 'enabled'
|
|
|
|
} else {
|
|
|
|
return 'installed'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Downloads ao-server to ~/ao-server. Returns false if it fails, which usually means the folder already exists (update instead).
|
|
|
|
export function downloadAoServer() {
|
|
|
|
try {
|
|
|
|
execSync('git clone http://git.coalitionofinvisiblecolleges.org:3009/autonomousorganization/ao-server.git 2>&1')
|
|
|
|
} catch(err) {
|
|
|
|
switch(err.code) {
|
|
|
|
case 128:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the ~/ao-server/.git folder exists
|
|
|
|
export function aoIsInstalled() {
|
|
|
|
return isFolder(path.join(AO_SERVER_PATH, '.git'))
|
|
|
|
}
|
|
|
|
|
|
|
|
function installAo(version) {
|
|
|
|
if(!version) {
|
|
|
|
version = aoEnv('AO_VERSION')
|
|
|
|
if(!version) {
|
|
|
|
version = 'ao-svelte'
|
|
|
|
setAoEnv('AO_VERSION', 'ao-svelte')
|
|
|
|
console.log('No AO server/frontend version specified, defaulting to ao-svelte.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
downloadAoServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateAoServer() {
|
|
|
|
console.log('Updating ao-server. If you are a developer, you may be asked for your SSH key now.')
|
|
|
|
try {
|
|
|
|
const stdout = execSync('cd ' + AO_SERVER_PATH + ' && git pull origin main 2>&1')
|
|
|
|
if(stdout.includes('Already up to date.')) {
|
|
|
|
console.log('Already up to date.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log('\nao-server was updated.')
|
|
|
|
} catch(error) {
|
|
|
|
console.log('git pull failed with error:', error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
description: 'AO server instance on this computer',
|
|
|
|
status: serviceStatus,
|
|
|
|
install: installAo,
|
|
|
|
isInstalled: aoIsInstalled,
|
|
|
|
update: updateAoServer,
|
|
|
|
submodules: [ aoServerServiceManager ]
|
|
|
|
}
|