Skip to content

Commit

Permalink
fix(instance): clean up config usage around application (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine authored Jul 2, 2017
1 parent b6ae853 commit 22c804c
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 42 deletions.
2 changes: 0 additions & 2 deletions extensions/nginx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class NginxExtension extends cli.Extension {
}

this.instance = this.system.getInstance();
this.instance.loadConfig();

cmd.addStage('nginx', this.setupNginx.bind(this));
cmd.addStage('ssl', this.setupSSL.bind(this));
Expand Down Expand Up @@ -219,7 +218,6 @@ class NginxExtension extends cli.Extension {

uninstall() {
this.instance = this.system.getInstance();
this.instance.loadConfig();

if (!this.instance.cliConfig.get('extension.nginx', false)) {
return;
Expand Down
1 change: 0 additions & 1 deletion extensions/systemd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class SystemdExtension extends cli.Extension {

setup(cmd, argv) {
let instance = this.system.getInstance();
instance.loadConfig();

if (!argv.local && instance.config.get('process') === 'systemd') {
cmd.addStage('systemd', this._setup.bind(this));
Expand Down
1 change: 0 additions & 1 deletion lib/commands/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ConfigCommand extends Command {
super(ui, system);

let instance = this.system.getInstance();
instance.loadConfig(true);
this.config = instance.config;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LogCommand extends Command {
// relative to that
process.chdir(instance.dir);

instance.loadRunningConfig(true);
instance.loadRunningEnvironment(true);

// Check if logging file transport is set in config
if (!includes(instance.config.get('logging.transports', []), 'file')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RestartCommand extends Command {
return Promise.reject(new Error('Ghost instance is not currently running.'));
}

instance.loadRunningConfig(true, true);
instance.loadRunningEnvironment(true, true);

return this.runCommand(StopCommand, argv).then(() => this.runCommand(StartCommand, argv));
}
Expand Down
1 change: 0 additions & 1 deletion lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class RunCommand extends Command {
}

let instance = this.system.getInstance();
instance.loadConfig(true);

process.env.paths__contentPath = path.join(process.cwd(), 'content');

Expand Down
1 change: 0 additions & 1 deletion lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class SetupCommand extends Command {
title: 'Setting up instance',
task: () => {
let instance = this.system.getInstance();
instance.loadConfig();
instance.name = argv.pname || url.parse(instance.config.get('url')).hostname.replace(/\./g, '-');
this.system.addInstance(instance);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StartCommand extends Command {
return Promise.reject(new Error('Ghost is already running.'));
}

instance.loadConfig();
instance.checkEnvironment();

return this.ui.listr(startupChecks.concat({
title: 'Running database migrations',
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class StopCommand extends Command {
return Promise.reject(new errors.SystemError('No running Ghost instance found here.'));
}

instance.loadRunningConfig();
instance.loadRunningEnvironment(true);

let stop = () => Promise.resolve(instance.process.stop(process.cwd())).then(() => {
instance.running = null;
Expand Down
3 changes: 1 addition & 2 deletions lib/commands/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ class UninstallCommand extends Command {
return Promise.resolve();
}

instance.loadRunningConfig(true, true);
instance.loadRunningEnvironment(true, true);

// If the instance is currently running we need to make
// sure it gets stopped
return this.runCommand(StopCommand);
}).then(() => {
this.system.setEnvironment(!fs.existsSync('config.production.json'));
instance.loadConfig(true);

return this.ui.run(this.system.hook('uninstall'), 'Removing related configuration');
}).then(() => this.ui.run(() => {
Expand Down
6 changes: 5 additions & 1 deletion lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class UpdateCommand extends Command {
context.installPath = path.join(process.cwd(), 'versions', context.version);
}

instance.running ? instance.loadRunningConfig(true, true) : instance.loadConfig();
if (instance.running) {
instance.loadRunningEnvironment(true, true);
}

instance.checkEnvironment();

// TODO: add meaningful update checks after this task
let tasks = [{
Expand Down
63 changes: 34 additions & 29 deletions lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ class Instance {
return false;
}

// Ordinarily we'd use this.loadRunningConfig(), but stack overflow
// (the error not the programming bible) happens if we do that.
let env = this.cliConfig.get('running');
this.config = new Config(path.join(this.dir, `config.${env}.json`));
this.loadProcess();
this.loadRunningEnvironment();

if (!this.process.isRunning(this.dir)) {
this.cliConfig.set('running', null).save();
Expand All @@ -48,47 +44,56 @@ class Instance {
return true;
}

get config() {
let currentEnv = this.system.environment;

if (!this._config || this._config.environment !== currentEnv) {
this._config = new Config(path.join(this.dir, `config.${this.system.environment}.json`));
this._config.environment = this.system.environment;
}

return this._config;
}

get process() {
let name = this.config.get('process', 'local');

if (!this._process || name !== this._process.name) {
let manager = this.system.getProcessManager(name);
this._process = new manager.Class(this.ui, this.system, this);
this._process.name = name;
}

return this._process;
}

constructor(ui, system, dir) {
this.ui = ui;
this.system = system;
this.dir = dir;
}

loadConfig(skipEnvCheck) {
// If we are starting in production mode but a development config exists and a production config doesn't,
// we want to start in development mode anyways.
checkEnvironment() {
if (
!skipEnvCheck && this.system.production &&
this.system.production &&
Config.exists(path.join(this.dir, 'config.development.json')) &&
!Config.exists(path.join(this.dir, 'config.production.json'))
!Config.exists(path.join('config.production.json'))
) {
this.ui.log('Found a development config but not a production config, running in development mode instead', 'yellow');
this.system.setEnvironment(true, true);
}
this.config = new Config(path.join(this.dir, `config.${this.system.environment}.json`));
this.loadProcess();
}

loadRunningConfig(setEnv, setNodeEnv) {
if (!this.running) {
loadRunningEnvironment(setEnv, setNodeEnv) {
let env = this.cliConfig.get('running');

if (!env) {
throw new Error('This instance is not running.');
}

let env = this.cliConfig.get('running');
if (setEnv) {
this.system.setEnvironment(env === 'development', setNodeEnv);
}

this.config = new Config(path.join(this.dir, `config.${env}.json`));
this.loadProcess();
return env;
}

loadProcess() {
let name = this.config.get('process', 'local');
let manager = this.system.getProcessManager(name);
this.process = new manager.Class(this.ui, this.system, this);
this.processName = manager.name;
}

summary() {
Expand All @@ -101,17 +106,17 @@ class Instance {
};
}

let env = this.loadRunningConfig();
this.loadRunningEnvironment(true);

return {
name: this.name,
dir: this.dir.replace(os.homedir(), '~'),
running: true,
version: this.cliConfig.get('active-version'),
mode: env,
mode: this.system.environment,
url: this.config.get('url'),
port: this.config.get('server.port'),
process: this.processName
process: this._process.name
};
}

Expand Down

0 comments on commit 22c804c

Please sign in to comment.