|
|
|
import { execSync, exec } from 'child_process'
|
|
|
|
import { isFolder } from '../files.js'
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
const AO_SERVER_PATH = path.join(process.env.HOME, 'ao-server')
|
|
|
|
|
|
|
|
// Returns one of: off, installed, enabled, running, synced, error
|
|
|
|
function serviceStatus() {
|
|
|
|
if(!aoIsInstalled()) {
|
|
|
|
return 'off'
|
|
|
|
}
|
|
|
|
let stdout
|
|
|
|
try {
|
|
|
|
stdout = execSync('systemctl status ao')
|
|
|
|
} catch(err) {
|
|
|
|
stdout = err.output.toString()
|
|
|
|
}
|
|
|
|
if(stdout.includes('Unit ao.service could not be found.')) {
|
|
|
|
return 'installed'
|
|
|
|
}
|
|
|
|
const isServiceRunning = stdout.includes('Active: active (running)')
|
|
|
|
if(isServiceRunning) return 'running'
|
|
|
|
else if(stdout.includes('error')) return 'error'
|
|
|
|
else if(stdout.includes('stopped')) return 'installed'
|
|
|
|
else if(stdout.includes('inactive (dead')) return 'installed'
|
|
|
|
return 'off'
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
try {
|
|
|
|
const stdout = execSync('cd ' + AO_SERVER_PATH + ' && git pull origin main 2>&1')
|
|
|
|
if(stdout.includes('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
|
|
|
|
}
|