2018-03-06 05:51:42 +01:00
|
|
|
import { restoreMastodonData } from './restore-mastodon-data'
|
2019-01-20 00:06:25 +01:00
|
|
|
import { promisify } from 'util'
|
2018-03-06 05:51:42 +01:00
|
|
|
import childProcessPromise from 'child-process-promise'
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2018-03-06 18:21:17 +01:00
|
|
|
import { waitForMastodonUiToStart, waitForMastodonApiToStart } from './wait-for-mastodon-to-start'
|
2020-02-23 01:45:32 +01:00
|
|
|
import mkdirp from 'mkdirp'
|
2020-11-23 21:45:01 +01:00
|
|
|
import cloneMastodon from './clone-mastodon'
|
|
|
|
import { DB_USER, DB_PASS, DB_NAME, DB_HOST, DB_PORT } from './mastodon-config'
|
2018-03-06 05:51:42 +01:00
|
|
|
|
|
|
|
const exec = childProcessPromise.exec
|
|
|
|
const spawn = childProcessPromise.spawn
|
2019-01-20 00:06:25 +01:00
|
|
|
const stat = promisify(fs.stat)
|
|
|
|
const writeFile = promisify(fs.writeFile)
|
2018-03-06 05:51:42 +01:00
|
|
|
const dir = __dirname
|
2018-02-18 20:53:50 +01:00
|
|
|
const mastodonDir = path.join(dir, '../mastodon')
|
|
|
|
|
2018-03-07 06:32:56 +01:00
|
|
|
let childProc
|
2018-02-18 20:53:50 +01:00
|
|
|
|
2018-03-06 08:56:48 +01:00
|
|
|
async function setupMastodonDatabase () {
|
2018-03-05 19:10:50 +01:00
|
|
|
console.log('Setting up mastodon database...')
|
2018-02-18 20:53:50 +01:00
|
|
|
try {
|
2018-04-11 03:53:22 +02:00
|
|
|
await exec(`psql -d template1 -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}' CREATEDB;"`)
|
2018-04-11 03:39:56 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
|
|
|
try {
|
2018-04-11 03:53:22 +02:00
|
|
|
await exec(`dropdb -h 127.0.0.1 -U ${DB_USER} -w ${DB_NAME}`, {
|
|
|
|
cwd: mastodonDir,
|
2018-08-30 06:42:57 +02:00
|
|
|
env: Object.assign({ PGPASSWORD: DB_PASS }, process.env)
|
2018-04-11 03:53:22 +02:00
|
|
|
})
|
2018-02-18 20:53:50 +01:00
|
|
|
} catch (e) { /* ignore */ }
|
2018-04-11 03:53:22 +02:00
|
|
|
await exec(`createdb -h 127.0.0.1 -U ${DB_USER} -w ${DB_NAME}`, {
|
|
|
|
cwd: mastodonDir,
|
2018-08-30 06:42:57 +02:00
|
|
|
env: Object.assign({ PGPASSWORD: DB_PASS }, process.env)
|
2018-04-11 03:53:22 +02:00
|
|
|
})
|
2018-02-18 20:53:50 +01:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
const dumpFile = path.join(dir, '../tests/fixtures/dump.sql')
|
2018-04-11 03:53:22 +02:00
|
|
|
await exec(`psql -h 127.0.0.1 -U ${DB_USER} -w -d ${DB_NAME} -f "${dumpFile}"`, {
|
|
|
|
cwd: mastodonDir,
|
2018-08-30 06:42:57 +02:00
|
|
|
env: Object.assign({ PGPASSWORD: DB_PASS }, process.env)
|
2018-04-11 03:53:22 +02:00
|
|
|
})
|
2018-02-18 20:53:50 +01:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
const tgzFile = path.join(dir, '../tests/fixtures/system.tgz')
|
|
|
|
const systemDir = path.join(mastodonDir, 'public/system')
|
2018-02-18 20:53:50 +01:00
|
|
|
await mkdirp(systemDir)
|
2018-08-30 06:42:57 +02:00
|
|
|
await exec(`tar -xzf "${tgzFile}"`, { cwd: systemDir })
|
2018-02-18 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:30:42 +01:00
|
|
|
async function runMastodon () {
|
2018-02-18 20:53:50 +01:00
|
|
|
console.log('Running mastodon...')
|
2019-08-03 22:49:37 +02:00
|
|
|
const env = Object.assign({}, process.env, {
|
2018-06-08 00:26:47 +02:00
|
|
|
RAILS_ENV: 'development',
|
|
|
|
NODE_ENV: 'development',
|
|
|
|
DB_NAME,
|
|
|
|
DB_USER,
|
|
|
|
DB_PASS,
|
|
|
|
DB_HOST,
|
|
|
|
DB_PORT
|
|
|
|
})
|
2019-08-03 22:49:37 +02:00
|
|
|
const cwd = mastodonDir
|
|
|
|
const cmds = [
|
2018-03-09 03:08:14 +01:00
|
|
|
'gem install bundler foreman',
|
2020-11-24 01:29:03 +01:00
|
|
|
'bundle install --frozen',
|
2018-06-08 00:26:47 +02:00
|
|
|
'bundle exec rails db:migrate',
|
2018-04-11 04:32:47 +02:00
|
|
|
'yarn --pure-lockfile'
|
2018-02-18 20:53:50 +01:00
|
|
|
]
|
|
|
|
|
2018-11-11 20:12:47 +01:00
|
|
|
const installedFile = path.join(mastodonDir, 'installed.txt')
|
|
|
|
try {
|
|
|
|
await stat(installedFile)
|
|
|
|
console.log('Already installed Mastodon')
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Installing Mastodon...')
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const cmd of cmds) {
|
2018-11-11 20:12:47 +01:00
|
|
|
console.log(cmd)
|
|
|
|
await exec(cmd, { cwd, env })
|
|
|
|
}
|
|
|
|
await writeFile(installedFile, '', 'utf8')
|
2018-02-18 20:53:50 +01:00
|
|
|
}
|
2018-08-30 06:42:57 +02:00
|
|
|
const promise = spawn('foreman', ['start'], { cwd, env })
|
2020-11-23 21:45:01 +01:00
|
|
|
// don't bother writing to mastodon.log in CI; we can't read the file anyway
|
|
|
|
const logFile = process.env.CIRCLECI ? '/dev/null' : 'mastodon.log'
|
2020-05-03 04:58:58 +02:00
|
|
|
const log = fs.createWriteStream(logFile, { flags: 'a' })
|
2018-03-07 06:32:56 +01:00
|
|
|
childProc = promise.childProcess
|
|
|
|
childProc.stdout.pipe(log)
|
|
|
|
childProc.stderr.pipe(log)
|
2018-04-11 04:43:36 +02:00
|
|
|
promise.catch(err => {
|
|
|
|
console.error('foreman start failed, see mastodon.log for details')
|
|
|
|
console.error(err)
|
|
|
|
shutdownMastodon()
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2018-02-18 19:42:27 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:30:42 +01:00
|
|
|
async function main () {
|
2018-02-18 20:53:50 +01:00
|
|
|
await cloneMastodon()
|
2018-03-06 05:51:42 +01:00
|
|
|
await setupMastodonDatabase()
|
2018-02-18 20:53:50 +01:00
|
|
|
await runMastodon()
|
2018-03-06 06:21:28 +01:00
|
|
|
await waitForMastodonApiToStart()
|
2018-03-06 18:04:09 +01:00
|
|
|
await restoreMastodonData()
|
2018-03-06 06:58:29 +01:00
|
|
|
await waitForMastodonUiToStart()
|
2018-02-18 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 08:57:06 +01:00
|
|
|
function shutdownMastodon () {
|
2018-03-07 06:32:56 +01:00
|
|
|
if (childProc) {
|
|
|
|
console.log('killing child process')
|
|
|
|
childProc.kill()
|
2018-02-18 20:53:50 +01:00
|
|
|
}
|
2018-03-06 18:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
shutdownMastodon()
|
2018-02-18 20:53:50 +01:00
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
|
2018-02-18 19:42:27 +01:00
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
2018-03-06 18:03:59 +01:00
|
|
|
shutdownMastodon()
|
2018-02-18 19:42:27 +01:00
|
|
|
process.exit(1)
|
|
|
|
})
|