2017-02-14 20:27:08 +00:00
'use strict'
2017-04-08 20:16:13 +00:00
// =====================================================
// Wiki.js
// Installation Script
// =====================================================
2017-02-14 20:27:08 +00:00
const Promise = require ( 'bluebird' )
2017-04-08 20:16:13 +00:00
const _ = require ( 'lodash' )
const colors = require ( 'colors/safe' )
2017-04-05 01:50:12 +00:00
const exec = require ( 'execa' )
2017-02-14 20:27:08 +00:00
const fs = Promise . promisifyAll ( require ( 'fs-extra' ) )
const https = require ( 'follow-redirects' ) . https
2017-04-08 20:16:13 +00:00
const inquirer = require ( 'inquirer' )
const os = require ( 'os' )
2017-02-14 20:27:08 +00:00
const path = require ( 'path' )
const pm2 = Promise . promisifyAll ( require ( 'pm2' ) )
const tar = require ( 'tar' )
const zlib = require ( 'zlib' )
2017-04-08 20:16:13 +00:00
const installDir = path . resolve ( _ _dirname , '../..' )
2017-06-10 17:00:19 +00:00
const isContainerBased = ( process . env . WIKI _JS _HEROKU || process . env . WIKI _JS _DOCKER )
let installMode = 'new'
2017-02-14 20:27:08 +00:00
2017-04-08 20:16:13 +00:00
// =====================================================
// INSTALLATION TASKS
// =====================================================
2017-04-05 01:50:12 +00:00
2017-04-08 20:16:13 +00:00
const tasks = {
2017-04-07 23:37:33 +00:00
/ * *
* Stop and delete existing instances
* /
2017-06-10 17:00:19 +00:00
stopAndDeleteInstances ( ) {
2017-04-08 20:16:13 +00:00
ora . text = 'Looking for running instances...'
return pm2 . connectAsync ( ) . then ( ( ) => {
return pm2 . describeAsync ( 'wiki' ) . then ( ( ) => {
ora . text = 'Stopping and deleting process from pm2...'
return pm2 . deleteAsync ( 'wiki' )
} ) . catch ( err => { // eslint-disable-line handle-callback-err
return true
} ) . finally ( ( ) => {
pm2 . disconnect ( )
} )
2017-06-10 17:00:19 +00:00
} ) . catch ( err => { // eslint-disable-line handle-callback-err
return true
2017-04-08 20:16:13 +00:00
} )
} ,
2017-04-07 23:37:33 +00:00
/ * *
* Check for sufficient memory
* /
2017-06-10 17:00:19 +00:00
checkRequirements ( ) {
2017-04-08 20:16:13 +00:00
ora . text = 'Checking system requirements...'
2017-06-10 17:00:19 +00:00
if ( os . totalmem ( ) < 1000 * 1000 * 768 ) {
2017-04-08 20:16:13 +00:00
return Promise . reject ( new Error ( 'Not enough memory to install dependencies. Minimum is 768 MB.' ) )
} else {
return Promise . resolve ( true )
}
} ,
2017-04-07 23:37:33 +00:00
/ * *
* Install via local tarball if present
* /
2017-06-10 17:00:19 +00:00
installFromLocal ( ) {
2017-04-08 20:16:13 +00:00
let hasTarball = true
let tbPath = path . join ( installDir , 'wiki-js.tar.gz' )
return fs . accessAsync ( tbPath )
. catch ( err => { // eslint-disable-line handle-callback-err
hasTarball = false
} ) . then ( ( ) => {
if ( hasTarball ) {
ora . text = 'Local tarball found. Extracting...'
return new Promise ( ( resolve , reject ) => {
fs . createReadStream ( tbPath ) . pipe ( zlib . createGunzip ( ) )
2017-06-10 17:00:19 +00:00
. pipe ( tar . extract ( { cwd : installDir } ) )
2017-04-08 20:16:13 +00:00
. on ( 'error' , err => reject ( err ) )
. on ( 'end' , ( ) => {
ora . text = 'Tarball extracted successfully.'
2017-07-27 01:40:34 +00:00
resolve ( tasks . installDependencies ( ) )
isContainerBased && console . info ( '>> Installing dependencies...' )
2017-04-08 20:16:13 +00:00
} )
} )
} else {
return false
}
} )
} ,
/ * *
* Install from GitHub release download
* /
2017-06-10 17:00:19 +00:00
installFromRemote ( ) {
2017-04-08 20:16:13 +00:00
// Fetch version from npm package
return fs . readJsonAsync ( 'package.json' ) . then ( ( packageObj ) => {
let versionGet = _ . chain ( packageObj . version ) . split ( '.' ) . take ( 4 ) . join ( '.' )
2017-07-27 01:40:34 +00:00
let remoteURLApp = _ . replace ( 'https://github.com/Requarks/wiki/releases/download/v{0}/wiki-js.tar.gz' , '{0}' , versionGet )
let remoteURLDeps = _ . replace ( 'https://github.com/Requarks/wiki/releases/download/v{0}/node_modules.tar.gz' , '{0}' , versionGet )
2017-04-07 23:37:33 +00:00
return new Promise ( ( resolve , reject ) => {
2017-07-27 01:40:34 +00:00
// Fetch app tarball
ora . text = 'Looking for app package...'
https . get ( remoteURLApp , resp => {
2017-04-08 20:16:13 +00:00
if ( resp . statusCode !== 200 ) {
return reject ( new Error ( 'Remote file not found' ) )
}
2017-07-27 01:40:34 +00:00
ora . text = 'Remote app tarball found. Downloading...'
isContainerBased && console . info ( '>> Extracting app to ' + installDir )
2017-04-08 20:16:13 +00:00
2017-07-27 01:40:34 +00:00
// Extract app tarball
2017-04-08 20:16:13 +00:00
resp . pipe ( zlib . createGunzip ( ) )
2017-06-10 17:00:19 +00:00
. pipe ( tar . extract ( { cwd : installDir } ) )
. on ( 'error' , err => reject ( err ) )
. on ( 'end' , ( ) => {
2017-07-27 01:40:34 +00:00
ora . text = 'App tarball extracted successfully.'
2017-06-10 17:00:19 +00:00
resolve ( true )
} )
2017-04-08 20:16:13 +00:00
} )
2017-07-27 01:40:34 +00:00
} ) . then ( ( ) => {
return new Promise ( ( resolve , reject ) => {
// Fetch deps tarball
ora . text = 'Looking for dependencies package...'
https . get ( remoteURLDeps , resp => {
if ( resp . statusCode !== 200 ) {
return reject ( new Error ( 'Remote file not found' ) )
}
ora . text = 'Remote dependencies tarball found. Downloading...'
isContainerBased && console . info ( '>> Extracting dependencies to ' + installDir )
// Extract deps tarball
resp . pipe ( zlib . createGunzip ( ) )
. pipe ( tar . extract ( { cwd : path . join ( installDir , 'node_modules' ) } ) )
. on ( 'error' , err => reject ( err ) )
. on ( 'end' , ( ) => {
ora . text = 'Dependencies tarball extracted successfully.'
resolve ( true )
} )
} )
} )
2017-04-07 23:37:33 +00:00
} )
2017-04-08 20:16:13 +00:00
} )
} ,
2017-02-16 17:49:56 +00:00
/ * *
2017-04-08 20:16:13 +00:00
* Create default config . yml file if new installation
2017-02-16 17:49:56 +00:00
* /
2017-06-10 17:00:19 +00:00
ensureConfigFile ( ) {
2017-04-08 20:16:13 +00:00
return fs . accessAsync ( path . join ( installDir , 'config.yml' ) ) . then ( ( ) => {
// Is Upgrade
2017-06-10 17:00:19 +00:00
ora . text = 'Existing config.yml found. Upgrade mode.'
installMode = 'upgrade'
2017-04-08 20:16:13 +00:00
return true
} ) . catch ( err => {
// Is New Install
if ( err . code === 'ENOENT' ) {
ora . text = 'First-time install, creating a new config.yml...'
2017-06-10 17:00:19 +00:00
installMode = 'new'
2017-05-15 00:17:08 +00:00
let sourceConfigFile = path . join ( installDir , 'config.sample.yml' )
2017-06-25 21:30:33 +00:00
if ( process . env . WIKI _JS _HEROKU || process . env . WIKI _JS _DOCKER ) {
sourceConfigFile = path . join ( _ _dirname , 'configs/config.passive.yml' )
2017-04-29 00:37:43 +00:00
}
2017-06-10 17:00:19 +00:00
return fs . copyAsync ( sourceConfigFile , path . join ( installDir , 'config.yml' ) )
2017-04-08 20:16:13 +00:00
} else {
return err
}
2017-02-14 20:27:08 +00:00
} )
2017-04-08 20:16:13 +00:00
} ,
2017-06-10 17:00:19 +00:00
/ * *
* Install npm dependencies
* /
installDependencies ( ) {
ora . text = 'Installing Wiki.js npm dependencies...'
2017-07-23 18:32:02 +00:00
return exec . stdout ( 'npm' , [ 'install' , '--only=production' , '--no-optional' ] , {
2017-06-10 17:00:19 +00:00
cwd : installDir
} ) . then ( results => {
ora . text = 'Wiki.js npm dependencies installed successfully.'
return true
} )
} ,
startConfigurationWizard ( ) {
ora . succeed ( 'Installation succeeded.' )
2017-05-15 04:09:44 +00:00
if ( process . env . WIKI _JS _HEROKU ) {
2017-06-10 17:00:19 +00:00
console . info ( '> Wiki.js has been installed and is configured to use Heroku configuration.' )
return true
2017-05-15 04:09:44 +00:00
} else if ( process . env . WIKI _JS _DOCKER ) {
console . info ( 'Docker environment detected. Skipping setup wizard auto-start.' )
2017-06-10 17:00:19 +00:00
return true
2017-05-15 04:09:44 +00:00
} else if ( process . stdout . isTTY ) {
2017-06-10 17:00:19 +00:00
if ( installMode === 'upgrade' ) {
console . info ( colors . yellow ( '\n!!! IMPORTANT !!!' ) )
console . info ( colors . yellow ( 'Running the configuration wizard is optional but recommended after an upgrade to ensure your config file is using the latest available settings.' ) )
console . info ( colors . yellow ( 'Note that the contents of your config file will be displayed during the configuration wizard. It is therefor highly recommended to run the wizard on a non-publicly accessible port or skip this step completely.\n' ) )
}
2017-04-05 01:50:12 +00:00
inquirer . prompt ( [ {
type : 'list' ,
name : 'action' ,
message : 'Continue with configuration wizard?' ,
default : 'default' ,
choices : [
{ name : 'Yes, run configuration wizard on port 3000 (recommended)' , value : 'default' , short : 'Yes' } ,
{ name : 'Yes, run configuration wizard on a custom port...' , value : 'custom' , short : 'Yes' } ,
{ name : 'No, I\'ll configure the config file manually' , value : 'exit' , short : 'No' }
]
} , {
type : 'input' ,
name : 'customport' ,
message : 'Custom port to use:' ,
default : 3000 ,
validate : ( val ) => {
val = _ . toNumber ( val )
return ( _ . isNumber ( val ) && val > 0 ) ? true : 'Invalid Port!'
} ,
when : ( ans ) => {
return ans . action === 'custom'
}
} ] ) . then ( ( ans ) => {
switch ( ans . action ) {
case 'default' :
2017-04-07 03:01:00 +00:00
console . info ( colors . bold . cyan ( '> Browse to http://your-server:3000/ to configure your wiki! (Replaced your-server with the hostname or IP of your server!)' ) )
2017-04-05 01:50:12 +00:00
ora = require ( 'ora' ) ( { text : 'I\'ll wait until you\'re done ;)' , color : 'yellow' , spinner : 'pong' } ) . start ( )
return exec . stdout ( 'node' , [ 'wiki' , 'configure' ] , {
cwd : installDir
} )
case 'custom' :
2017-04-07 03:01:00 +00:00
console . info ( colors . bold . cyan ( '> Browse to http://your-server:' + ans . customport + '/ to configure your wiki! (Replaced your-server with the hostname or IP of your server!)' ) )
2017-04-05 01:50:12 +00:00
ora = require ( 'ora' ) ( { text : 'I\'ll wait until you\'re done ;)' , color : 'yellow' , spinner : 'pong' } ) . start ( )
return exec . stdout ( 'node' , [ 'wiki' , 'configure' , ans . customport ] , {
cwd : installDir
} )
default :
2017-04-07 23:37:33 +00:00
console . info ( colors . bold . cyan ( '\n> You can run the configuration wizard using command:' ) + colors . bold . white ( ' node wiki configure' ) + colors . bold . cyan ( '.\n> Then start Wiki.js using command: ' ) + colors . bold . white ( 'node wiki start' ) )
return Promise . delay ( 7000 ) . then ( ( ) => {
process . exit ( 0 )
} )
2017-04-05 01:50:12 +00:00
}
} ) . then ( ( ) => {
2017-04-06 00:33:16 +00:00
ora . succeed ( colors . bold . green ( 'Wiki.js has been configured successfully. It is now starting up and should be accessible very soon!' ) )
2017-04-07 23:37:33 +00:00
return Promise . delay ( 3000 ) . then ( ( ) => {
console . info ( 'npm is finishing... please wait...' )
} )
2017-04-05 01:50:12 +00:00
} )
} else {
2017-05-15 04:09:44 +00:00
console . info ( colors . cyan ( '[WARNING] Non-interactive terminal detected. You must manually start the configuration wizard using the command: node wiki configure' ) )
2017-04-05 01:50:12 +00:00
}
2017-04-08 20:16:13 +00:00
}
}
// =====================================================
// INSTALL SEQUENCE
// =====================================================
2017-06-10 17:00:19 +00:00
if ( ! isContainerBased ) {
2017-04-19 00:23:42 +00:00
console . info ( colors . yellow (
' __ __ _ _ _ _ \n' +
'/ / /\\ \\ (_) | _(_) (_)___ \n' +
'\\ \\/ \\/ / | |/ / | | / __| \n' +
' \\ /\\ /| | <| |_ | \\__ \\ \n' +
' \\/ \\/ |_|_|\\_\\_(_)/ |___/ \n' +
' |__/\n' ) )
2017-05-15 00:17:08 +00:00
} else {
2017-05-15 04:09:44 +00:00
console . info ( '> WIKI.JS [Installing...]' )
2017-04-19 00:23:42 +00:00
}
2017-04-08 20:16:13 +00:00
let ora = require ( 'ora' ) ( { text : 'Initializing...' , spinner : 'dots12' } ) . start ( )
Promise . join (
tasks . stopAndDeleteInstances ( ) ,
tasks . checkRequirements ( )
) . then ( ( ) => {
2017-06-10 17:00:19 +00:00
isContainerBased && console . info ( '>> Fetching tarball...' )
2017-04-08 20:16:13 +00:00
return tasks . installFromLocal ( ) . then ( succeeded => {
return ( ! succeeded ) ? tasks . installFromRemote ( ) : true
2017-02-14 20:27:08 +00:00
} )
2017-04-08 20:16:13 +00:00
} ) . then ( ( ) => {
2017-06-10 17:00:19 +00:00
isContainerBased && console . info ( '>> Creating config file...' )
2017-04-08 20:16:13 +00:00
return tasks . ensureConfigFile ( )
} ) . then ( ( ) => {
return tasks . startConfigurationWizard ( )
2017-02-14 20:27:08 +00:00
} ) . catch ( err => {
2017-06-10 17:00:19 +00:00
isContainerBased && console . error ( err )
2017-02-14 20:27:08 +00:00
ora . fail ( err )
} )