Skip to content

Commit

Permalink
feat: add new reset command & clean/prune arguments
Browse files Browse the repository at this point in the history
The reset command allows you to delete all Atlassian host product data, which will now be persisted unless you use the new clean/prune options
  • Loading branch information
remie committed Apr 17, 2024
1 parent e68d814 commit e18c4a9
Show file tree
Hide file tree
Showing 25 changed files with 249 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/applications/amps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class AMPS {
const nodes = AMPS.getNodes('//*[local-name()=\'packaging\']');
return nodes.some(item => item.textContent === 'atlassian-plugin');
} catch (err) {
console.log(err);
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/applications/bamboo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Bamboo extends Base {

// ------------------------------------------------------------------------------------------ Protected Methods

protected getService = (): Service => {
protected getService(): Service {

const volumes = this.getVolumes();
const environment = this.getEnvironmentVariables();
Expand Down
39 changes: 27 additions & 12 deletions src/applications/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { spawn } from 'child_process';
import { downAll, execCompose, ps, upAll } from 'docker-compose/dist/v2.js';
import { downAll, execCompose, ps, stop, upAll } from 'docker-compose/dist/v2.js';
import EventEmitter from 'events';
import { gracefulExit } from 'exit-hook';
import { existsSync, mkdirSync } from 'fs';
Expand Down Expand Up @@ -37,7 +37,10 @@ export abstract class Base extends EventEmitter {
// ------------------------------------------------------------------------------------------ Properties

protected get baseUrl(): string {
const baseUrl = `http://localhost:${this.options.port}`;
let baseUrl = `http://localhost`;
if (this.options.port) {
baseUrl += `:${this.options.port}`;
}
return this.options.contextPath ? `${baseUrl}/${this.options.contextPath}` : baseUrl;
}

Expand All @@ -52,15 +55,32 @@ export abstract class Base extends EventEmitter {
}

async start() {
await this.stop();
if (this.options.clean) {
await this.down();
}
await this.build(this.options.version);

await this.database.start(this.name, this.options.version);
await this.database.start(this.options.clean);
await this.up();
}

async stop() {
await this.database.stop();
async stop(): Promise<void> {
await this.database.stop(this.options.prune);
if (this.options.prune) {
await this.down();
} else {
const configAsString = dump(this.getDockerComposeConfig());
await stop({
cwd: cwd(),
configAsString,
log: true
});
}
this.emit(`${this.name}:stopped`);
}

async reset() {
await this.database.stop(true);
await this.down();
}

Expand Down Expand Up @@ -140,15 +160,12 @@ export abstract class Base extends EventEmitter {

private async down() {
const configAsString = dump(this.getDockerComposeConfig());

await downAll({
cwd: cwd(),
configAsString,
commandOptions: [ '-v', '--remove-orphans', '--rmi', 'local' ],
log: true
});

this.emit(`${this.name}:stopped`);
}

private async getServiceState() {
Expand Down Expand Up @@ -205,10 +222,8 @@ export abstract class Base extends EventEmitter {
const docker = spawn(
'docker',
[ 'build', '-t', `dcdx/${this.name}:${version}`, '--build-arg', `${this.name.toUpperCase()}_VERSION=${version}`, '.'],
{ cwd: checkoutPath }
{ cwd: checkoutPath, stdio: 'inherit' }
);
docker.stdout.on('data', (lines: Buffer) => { console.log(lines.toString('utf-8').trim()); });
docker.stderr.on('data', (lines: Buffer) => { console.log(lines.toString('utf-8').trim()); });
docker.on('exit', (code) => (code === 0) ? resolve() : reject(new Error(`Docker exited with code ${code}`)));
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/applications/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Bitbucket extends Base {

// ------------------------------------------------------------------------------------------ Protected Methods

protected getService = (): Service => {
protected getService(): Service {

const volumes = this.getVolumes();
const environment = this.getEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion src/applications/confluence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Confluence extends Base {

// ------------------------------------------------------------------------------------------ Protected Methods

protected getService = (): Service => {
protected getService(): Service {

const volumes = this.getVolumes();
const environment = this.getEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion src/applications/jira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Jira extends Base {

// ------------------------------------------------------------------------------------------ Protected Methods

protected getService = (): Service => {
protected getService(): Service {
const volumes = this.getVolumes();
const environment = this.getEnvironmentVariables();

Expand Down
4 changes: 4 additions & 0 deletions src/commands/database-mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { MSSQL, MSSQLOptions } from '../databases/mssql';
.addOption(new Option('-e, --edition <edition>', 'The edition of Microsoft SQL Server').choices([ 'Developer', 'Express', 'Standard', 'Enterprise', 'EnterpriseCore' ]).default('Developer'))
.addOption(new Option('-p, --port <port>', 'The port on which the database will be accessible').default('1433'))
.addOption(new Option('-P, --password <password>', 'The value passed to MSSQL_SA_PASSWORD environment variable. MS SQL Server password policy applies.').default('DataCenterDX!'))
.addOption(new Option('--clean', 'Remove data files before starting the database').default(false))
.addOption(new Option('--prune', 'Remove data files when stopping the database').default(false))
.parse(process.argv)
.opts();

Expand All @@ -20,6 +22,8 @@ import { MSSQL, MSSQLOptions } from '../databases/mssql';
edition: options.edition,
port: Number(options.port),
password: options.password,
clean: options.clean,
prune: options.prune,
logging: true
} as MSSQLOptions);

Expand Down
4 changes: 4 additions & 0 deletions src/commands/database-mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { MySQL } from '../databases/mysql';
.addOption(new Option('-p, --port <port>', 'The port on which the database will be accessible').default('3306'))
.addOption(new Option('-U, --username <username>', 'The value passed to MYSQL_USER environment variable').default('dcdx'))
.addOption(new Option('-P, --password <password>', 'The value passed to MYSQL_PASSWORD environment variable').default('dcdx'))
.addOption(new Option('--clean', 'Remove data files before starting the database').default(false))
.addOption(new Option('--prune', 'Remove data files when stopping the database').default(false))
.parse(process.argv)
.opts();

Expand All @@ -22,6 +24,8 @@ import { MySQL } from '../databases/mysql';
port: Number(options.port),
username: options.username,
password: options.password,
clean: options.clean,
prune: options.prune,
logging: true
});

Expand Down
4 changes: 4 additions & 0 deletions src/commands/database-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Postgres } from '../databases/postgres';
.addOption(new Option('-p, --port <port>', 'The port on which the database will be accessible').default('5432'))
.addOption(new Option('-U, --username <username>', 'The value passed to POSTGRES_USER environment variable').default('dcdx'))
.addOption(new Option('-P, --password <password>', 'The value passed to POSTGRES_PASSWORD environment variable').default('dcdx'))
.addOption(new Option('--clean', 'Remove data files before starting the database').default(false))
.addOption(new Option('--prune', 'Remove data files when stopping the database').default(false))
.parse(process.argv)
.opts();

Expand All @@ -22,6 +24,8 @@ import { Postgres } from '../databases/postgres';
port: Number(options.port),
username: options.username,
password: options.password,
clean: options.clean,
prune: options.prune,
logging: true
})

Expand Down
30 changes: 30 additions & 0 deletions src/commands/reset-bamboo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { Option, program } from 'commander';
import { gracefulExit } from 'exit-hook';

import { AMPS } from '../applications/amps';
import { Bamboo } from '../applications/bamboo';

const version = AMPS.getApplicationVersion() || '9.6.1';

(async () => {
const options = program
.showHelpAfterError(true)
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '9.4.3' ]).default(version))
.addOption(new Option('-d, --database <name>', 'The database engine to remove data from').choices([ 'postgresql', 'mysql', 'mssql' ]).default('postgresql'))
.parse(process.argv)
.opts();

const instance = new Bamboo({
version: options.version,
database: options.database
});

await instance.reset();
})();

process.on('SIGINT', () => {
console.log(`Received term signal, trying to stop gracefully 💪`);
gracefulExit();
});
30 changes: 30 additions & 0 deletions src/commands/reset-bitbucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { Option, program } from 'commander';
import { gracefulExit } from 'exit-hook';

import { AMPS } from '../applications/amps';
import { Bitbucket } from '../applications/bitbucket';

const version = AMPS.getApplicationVersion() || '9.4.3';

(async () => {
const options = program
.showHelpAfterError(true)
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '9.4.3' ]).default(version))
.addOption(new Option('-d, --database <name>', 'The database engine to remove data from').choices([ 'postgresql', 'mysql', 'mssql' ]).default('postgresql'))
.parse(process.argv)
.opts();

const instance = new Bitbucket({
version: options.version,
database: options.database
});

await instance.reset();
})();

process.on('SIGINT', () => {
console.log(`Received term signal, trying to stop gracefully 💪`);
gracefulExit();
});
30 changes: 30 additions & 0 deletions src/commands/reset-confluence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { Option, program } from 'commander';
import { gracefulExit } from 'exit-hook';

import { AMPS } from '../applications/amps';
import { Confluence } from '../applications/confluence';

const version = AMPS.getApplicationVersion() || '8.9.0';

(async () => {
const options = program
.showHelpAfterError(true)
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '8.9.0' ]).default(version))
.addOption(new Option('-d, --database <name>', 'The database engine to remove data from').choices([ 'postgresql', 'mysql', 'mssql' ]).default('postgresql'))
.parse(process.argv)
.opts();

const instance = new Confluence({
version: options.version,
database: options.database
});

await instance.reset();
})();

process.on('SIGINT', () => {
console.log(`Received term signal, trying to stop gracefully 💪`);
gracefulExit();
});
30 changes: 30 additions & 0 deletions src/commands/reset-jira.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { Option, program } from 'commander';
import { gracefulExit } from 'exit-hook';

import { AMPS } from '../applications/amps';
import { Jira } from '../applications/jira';

const version = AMPS.getApplicationVersion() || '9.15.0';

(async () => {
const options = program
.showHelpAfterError(true)
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '9.15.0' ]).default(version))
.addOption(new Option('-d, --database <name>', 'The database engine to remove data from').choices([ 'postgresql', 'mysql', 'mssql' ]).default('postgresql'))
.parse(process.argv)
.opts();

const instance = new Jira({
version: options.version,
database: options.database,
});

await instance.reset();
})();

process.on('SIGINT', () => {
console.log(`Received term signal, trying to stop gracefully 💪`);
gracefulExit();
});
28 changes: 28 additions & 0 deletions src/commands/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

import { Option, program } from 'commander';

import { AMPS } from '../applications/amps';
import { SupportedApplications } from '../types/SupportedApplications';

// Check if there is a command in the arguments
const isDefaultCommand = !process.argv.some(item => Object.values(SupportedApplications).includes(item as SupportedApplications));
// If there is no command, check if we are running this within the context of an Atlassian Plugin project
if (isDefaultCommand) {
const application = AMPS.getApplication();
if (application) {
const args = [ application, ...process.argv.splice(2) ];
process.argv = [ ...process.argv.slice(0, 2), ...args ];
}
}

program
.name('dcdx reset')
.addOption(new Option('-P, --activate-profiles <arg>', 'Comma-delimited list of profiles to activate'))
.command('bamboo', 'Remove all data (incl. database) for Atlassian Bamboo (standalone)', { executableFile: './reset-bamboo.js'})
.command('bitbucket', 'Remove all data (incl. database) for Atlassian Bitbucket (standalone)', { executableFile: './reset-bitbucket.js'})
.command('confluence', 'Remove all data (incl. database) for Atlassian Confluence (standalone)', { executableFile: './reset-confluence.js'})
.command('jira', 'Remove all data (incl. database) for Atlassian Jira (standalone)', { executableFile: './reset-jira.js'})
.showHelpAfterError(true);

program.parse(process.argv);
6 changes: 5 additions & 1 deletion src/commands/run-bamboo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { Bamboo } from '../applications/bamboo';
(async () => {
const options = program
.showHelpAfterError(true)
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '9.4.3' ]).default('9.4.3'))
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '9.4.3' ]).default('9.6.1'))
.addOption(new Option('-d, --database <name>', 'The database engine on which the host application will run').choices([ 'postgresql', 'mysql', 'mssql' ]).default('postgresql'))
.addOption(new Option('-p, --port <port>', 'The HTTP port on which the host application will be accessible').default('80'))
.addOption(new Option('-c, --contextPath <contextPath>', 'The context path on which the host application will be accessible'))
.addOption(new Option('-qr, --quickReload <path_to_watch>', 'Add support for QuickReload and add the provided path to the watch list'))
.addOption(new Option('--clean', 'Remove data files before starting the database').default(false))
.addOption(new Option('--prune', 'Remove data files when stopping the database').default(false))
.addOption(new Option('--debug', 'Add support for JVM debugger on port 5005'))
.parse(process.argv)
.opts();
Expand All @@ -23,6 +25,8 @@ import { Bamboo } from '../applications/bamboo';
port: Number(options.port),
contextPath: options.contextPath,
quickReload: options.qr,
clean: options.clean,
prune: options.prune,
debug: options.debug
});

Expand Down
6 changes: 5 additions & 1 deletion src/commands/run-bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { Bitbucket } from '../applications/bitbucket';
(async () => {
const options = program
.showHelpAfterError(true)
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '8.9.0' ]).default('8.9.0'))
.addOption(new Option('-v, --version <version>', 'The version of the host application').choices([ '8.19.1' ]).default('8.19.1'))
.addOption(new Option('-d, --database <name>', 'The database engine on which the host application will run').choices([ 'postgresql', 'mysql', 'mssql' ]).default('postgresql'))
.addOption(new Option('-p, --port <port>', 'The HTTP port on which the host application will be accessible').default('80'))
.addOption(new Option('-qr, --quickReload <path_to_watch>', 'Add support for QuickReload and add the provided path to the watch list'))
.addOption(new Option('--clean', 'Remove data files before starting the database').default(false))
.addOption(new Option('--prune', 'Remove data files when stopping the database').default(false))
.addOption(new Option('--debug', 'Add support for JVM debugger on port 5005'))
.parse(process.argv)
.opts();
Expand All @@ -21,6 +23,8 @@ import { Bitbucket } from '../applications/bitbucket';
database: options.database,
port: Number(options.port),
quickReload: options.qr,
clean: options.clean,
prune: options.prune,
debug: options.debug
});

Expand Down
Loading

0 comments on commit e18c4a9

Please sign in to comment.