pinafore/bin/run-mastodon.js

149 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-03-06 05:51:42 +01:00
import { restoreMastodonData } from './restore-mastodon-data'
import pify from 'pify'
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'
2018-03-06 05:53:52 +01:00
import mkdirpCB from 'mkdirp'
2018-03-06 05:51:42 +01:00
const exec = childProcessPromise.exec
const spawn = childProcessPromise.spawn
const mkdirp = pify(mkdirpCB)
const stat = pify(fs.stat.bind(fs))
2018-02-18 19:42:27 +01:00
const writeFile = pify(fs.writeFile.bind(fs))
2018-03-06 05:51:42 +01:00
const dir = __dirname
2018-02-18 19:42:27 +01:00
const GIT_URL = 'https://github.com/tootsuite/mastodon.git'
const GIT_TAG = 'v2.5.0'
const DB_NAME = 'pinafore_development'
const DB_USER = 'pinafore'
2018-04-11 03:53:22 +02:00
const DB_PASS = 'pinafore'
const DB_PORT = process.env.PGPORT || 5432
const DB_HOST = '127.0.0.1'
2018-04-08 23:43:24 +02:00
2018-02-18 19:42:27 +01:00
const envFile = `
PAPERCLIP_SECRET=foo
SECRET_KEY_BASE=bar
OTP_SECRET=foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
2018-04-11 01:45:51 +02:00
DB_USER=${DB_USER}
DB_NAME=${DB_NAME}
2018-04-11 03:53:22 +02:00
DB_PASS=${DB_PASS}
2018-02-18 19:42:27 +01:00
`
const mastodonDir = path.join(dir, '../mastodon')
let childProc
2018-02-19 00:30:42 +01:00
async function cloneMastodon () {
try {
await stat(mastodonDir)
} catch (e) {
console.log('Cloning mastodon...')
await exec(`git clone --single-branch --branch master ${GIT_URL} "${mastodonDir}"`)
await exec(`git checkout ${GIT_TAG}`, { cwd: mastodonDir })
2018-02-18 19:42:27 +01:00
await writeFile(path.join(dir, '../mastodon/.env'), envFile, 'utf8')
}
}
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...')
try {
2018-04-11 03:53:22 +02:00
await exec(`psql -d template1 -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}' CREATEDB;"`)
} 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,
env: Object.assign({ PGPASSWORD: DB_PASS }, process.env)
2018-04-11 03:53:22 +02: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,
env: Object.assign({ PGPASSWORD: DB_PASS }, process.env)
2018-04-11 03:53:22 +02:00
})
let dumpFile = path.join(dir, '../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,
env: Object.assign({ PGPASSWORD: DB_PASS }, process.env)
2018-04-11 03:53:22 +02:00
})
let tgzFile = path.join(dir, '../fixtures/system.tgz')
let systemDir = path.join(mastodonDir, 'public/system')
await mkdirp(systemDir)
await exec(`tar -xzf "${tgzFile}"`, { cwd: systemDir })
}
2018-02-19 00:30:42 +01:00
async function runMastodon () {
console.log('Running mastodon...')
let env = Object.assign({}, process.env, {
RAILS_ENV: 'development',
NODE_ENV: 'development',
DB_NAME,
DB_USER,
DB_PASS,
DB_HOST,
DB_PORT
})
let cwd = mastodonDir
let cmds = [
2018-03-09 03:08:14 +01:00
'gem install bundler foreman',
'bundle install',
'bundle exec rails db:migrate',
2018-04-11 04:32:47 +02: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 (let cmd of cmds) {
console.log(cmd)
await exec(cmd, { cwd, env })
}
await writeFile(installedFile, '', 'utf8')
}
const promise = spawn('foreman', ['start'], { cwd, env })
const log = fs.createWriteStream('mastodon.log', { flags: 'a' })
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 () {
await cloneMastodon()
2018-03-06 05:51:42 +01:00
await setupMastodonDatabase()
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-03-07 08:57:06 +01:00
function shutdownMastodon () {
if (childProc) {
console.log('killing child process')
childProc.kill()
}
2018-03-06 18:03:59 +01:00
}
process.on('SIGINT', function () {
shutdownMastodon()
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)
})