2018-03-06 04:51:42 +00: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 17:21:17 +00:00
|
|
|
import { waitForMastodonUiToStart, waitForMastodonApiToStart } from './wait-for-mastodon-to-start'
|
2018-03-06 04:53:52 +00:00
|
|
|
import mkdirpCB from 'mkdirp'
|
2018-03-06 04:51:42 +00:00
|
|
|
|
|
|
|
const exec = childProcessPromise.exec
|
|
|
|
const spawn = childProcessPromise.spawn
|
|
|
|
const mkdirp = pify(mkdirpCB)
|
2018-02-18 19:53:50 +00:00
|
|
|
const stat = pify(fs.stat.bind(fs))
|
2018-02-18 18:42:27 +00:00
|
|
|
const writeFile = pify(fs.writeFile.bind(fs))
|
2018-03-06 04:51:42 +00:00
|
|
|
const dir = __dirname
|
2018-02-18 18:42:27 +00:00
|
|
|
|
2018-04-08 21:43:24 +00:00
|
|
|
const GIT_URL = 'https://github.com/nolanlawson/mastodon'
|
|
|
|
const GIT_BRANCH = 'for-pinafore'
|
|
|
|
|
2018-02-18 18:42:27 +00:00
|
|
|
const envFile = `
|
|
|
|
PAPERCLIP_SECRET=foo
|
|
|
|
SECRET_KEY_BASE=bar
|
|
|
|
OTP_SECRET=foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
|
2018-04-10 02:02:33 +00:00
|
|
|
DB_PORT=${process.env.PG_PORT || 5432}
|
|
|
|
DB_USER=nolan
|
2018-02-18 18:42:27 +00:00
|
|
|
`
|
|
|
|
|
2018-02-18 19:53:50 +00:00
|
|
|
const mastodonDir = path.join(dir, '../mastodon')
|
|
|
|
|
2018-03-07 05:32:56 +00:00
|
|
|
let childProc
|
2018-02-18 19:53:50 +00:00
|
|
|
|
2018-02-18 23:30:42 +00:00
|
|
|
async function cloneMastodon () {
|
2018-02-18 19:53:50 +00:00
|
|
|
try {
|
|
|
|
await stat(mastodonDir)
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Cloning mastodon...')
|
2018-04-08 21:43:24 +00:00
|
|
|
await exec(`git clone ${GIT_URL} "${mastodonDir}"`)
|
|
|
|
await exec(`git checkout ${GIT_BRANCH}`, {cwd: mastodonDir})
|
2018-02-18 18:42:27 +00:00
|
|
|
await writeFile(path.join(dir, '../mastodon/.env'), envFile, 'utf8')
|
|
|
|
}
|
2018-02-18 19:53:50 +00:00
|
|
|
}
|
|
|
|
|
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...')
|
2018-02-18 19:53:50 +00:00
|
|
|
try {
|
|
|
|
await exec('dropdb mastodon_development', {cwd: mastodonDir})
|
|
|
|
} catch (e) { /* ignore */ }
|
|
|
|
await exec('createdb mastodon_development', {cwd: mastodonDir})
|
|
|
|
|
|
|
|
let dumpFile = path.join(dir, '../fixtures/dump.sql')
|
|
|
|
await exec(`pg_restore -Fc -d mastodon_development "${dumpFile}"`, {cwd: mastodonDir})
|
|
|
|
|
|
|
|
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-18 23:30:42 +00:00
|
|
|
async function runMastodon () {
|
2018-02-18 19:53:50 +00:00
|
|
|
console.log('Running mastodon...')
|
|
|
|
let cmds = [
|
2018-03-09 02:08:14 +00:00
|
|
|
'gem install bundler foreman',
|
2018-02-18 19:53:50 +00:00
|
|
|
'bundle install',
|
|
|
|
'yarn --pure-lockfile'
|
|
|
|
]
|
|
|
|
|
|
|
|
for (let cmd of cmds) {
|
|
|
|
console.log(cmd)
|
|
|
|
await exec(cmd, {cwd: mastodonDir})
|
|
|
|
}
|
2018-03-07 05:32:56 +00:00
|
|
|
const promise = spawn('foreman', ['start'], {cwd: mastodonDir})
|
2018-02-24 22:49:28 +00:00
|
|
|
const log = fs.createWriteStream('mastodon.log', {flags: 'a'})
|
2018-03-07 05:32:56 +00:00
|
|
|
childProc = promise.childProcess
|
|
|
|
childProc.stdout.pipe(log)
|
|
|
|
childProc.stderr.pipe(log)
|
2018-02-18 18:42:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 23:30:42 +00:00
|
|
|
async function main () {
|
2018-02-18 19:53:50 +00:00
|
|
|
await cloneMastodon()
|
2018-03-06 04:51:42 +00:00
|
|
|
await setupMastodonDatabase()
|
2018-02-18 19:53:50 +00:00
|
|
|
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-02-18 19:53:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 07:57:06 +00:00
|
|
|
function shutdownMastodon () {
|
2018-03-07 05:32:56 +00:00
|
|
|
if (childProc) {
|
|
|
|
console.log('killing child process')
|
|
|
|
childProc.kill()
|
2018-02-18 19:53:50 +00:00
|
|
|
}
|
2018-03-06 17:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
shutdownMastodon()
|
2018-02-18 19:53:50 +00:00
|
|
|
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)
|
|
|
|
})
|