pinafore/bin/run-mastodon.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

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