Skip to content

Commit

Permalink
Merge pull request #22 from arifszn/mysq-service
Browse files Browse the repository at this point in the history
Add MySQL in the service offerings
  • Loading branch information
arifszn authored Aug 6, 2023
2 parents e76253c + d4684ef commit 0afe43d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 30 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
},
"files": [
"src",
"runtimes"
"runtimes",
"stubs"
],
"dependencies": {
"child_process": "^1.0.2",
"inquirer": "^8.2.6"
"inquirer": "^8.2.6",
"js-yaml": "^4.1.0"
},
"devDependencies": {
"eslint": "^8.46.0",
Expand Down
36 changes: 17 additions & 19 deletions src/console/init-command.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const fs = require('fs');
const path = require('path');
const { displayErrorMessage, displaySuccessMessage } = require('../utils');
const {
displayErrorMessage,
displaySuccessMessage,
writeDockerComposeFileWithService,
} = require('../utils');
const inquirer = require('inquirer');
const { NODE_VERSIONS, NODE_16 } = require('../constants/services');
const {
NODE_VERSIONS,
NODE_16,
SERVICES,
SERVICES_WITH_VOLUME,
} = require('../constants/services');

const initCommand = async () => {
const dockerComposePath = path.resolve(process.cwd(), 'docker-compose.yml');
Expand Down Expand Up @@ -79,7 +88,7 @@ const initCommand = async () => {
const ports = portsAnswer.ports.split(',').map((port) => port.trim());

// Ask the user to select the services
/* const servicesAnswer = await inquirer.prompt([
const servicesAnswer = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select the services you want to include:',
Expand All @@ -89,7 +98,7 @@ const initCommand = async () => {
},
]);

const selectedServices = servicesAnswer.services; */
const selectedServices = servicesAnswer.services;

let dockerfileLocation = `./node_modules/vail/runtimes/${selectedPackageManager}`;
if (!fs.existsSync(dockerfileLocation)) {
Expand Down Expand Up @@ -120,23 +129,12 @@ networks:
driver: bridge
`;

/* if (selectedServices.includes(MY_SQL)) {
dockerComposeContent += `
mysql:
# Add MySQL service configuration here
`;
}
if (selectedServices.includes('minio')) {
dockerComposeContent += `
minio:
# Add Minio service configuration here
`;
} */

// Write docker-compose.yml with the selected services
// Write docker-compose.yml
fs.writeFileSync(dockerComposePath, dockerComposeContent);

// Call writeDockerComposeFile function with selected services and volumes
writeDockerComposeFileWithService(selectedServices, SERVICES_WITH_VOLUME);

displaySuccessMessage('Vail successfully initialized.');
};

Expand Down
8 changes: 4 additions & 4 deletions src/constants/services.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const MY_SQL = 'mysql';
const MINIO = 'minio';

const NODE_14 = '14';
const NODE_16 = '16';
const NODE_18 = '18';

const SERVICES = [MY_SQL, MINIO];
const SERVICES = [MY_SQL];

const SERVICES_WITH_VOLUME = [MY_SQL];

const NODE_VERSIONS = [NODE_14, NODE_16, NODE_18];

// export all
module.exports = {
MY_SQL,
MINIO,
NODE_VERSIONS,
NODE_14,
NODE_16,
NODE_18,
SERVICES,
SERVICES_WITH_VOLUME,
};
57 changes: 56 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');

const {
RED_COLOR_CODE,
RESET_COLOR_CODE,
Expand All @@ -12,4 +16,55 @@ const displaySuccessMessage = (message) => {
console.log(`${GREEN_COLOR_CODE}${message}${RESET_COLOR_CODE}`);
};

module.exports = { displayErrorMessage, displaySuccessMessage };
const writeDockerComposeFileWithService = (
selectedServices,
availableVolumes,
) => {
const dockerComposePath = path.resolve(process.cwd(), 'docker-compose.yml');

// Read the current docker-compose.yml file
const dockerComposeContent = fs.readFileSync(dockerComposePath, 'utf8');

// Parse the YAML content into a JavaScript object
const parsedCompose = yaml.load(dockerComposeContent);

// Check if 'volumes' property exists in 'parsedCompose' object
if (!parsedCompose.volumes) {
parsedCompose.volumes = {};
}

// Add selected services to the services section
selectedServices.forEach((service) => {
if (!(service in parsedCompose.services)) {
const serviceStubPath = `./node_modules/vail/stubs/${service}.stub`;
const serviceStubContent = fs.readFileSync(serviceStubPath, 'utf8');

const serviceStub = yaml.load(serviceStubContent);
parsedCompose.services[service] = serviceStub[service];
}
});

// Remove volumes if no services are selected
if (selectedServices.length === 0) {
delete parsedCompose.volumes;
} else {
// Add selected volumes to the volumes section
availableVolumes.forEach((volume) => {
if (!(volume in parsedCompose.volumes)) {
parsedCompose.volumes[`vail-${volume}`] = { driver: 'local' };
}
});
}

// Convert the JavaScript object back to YAML format
const updatedComposeContent = yaml.dump(parsedCompose);

// Write the updated YAML content to docker-compose.yml
fs.writeFileSync(dockerComposePath, updatedComposeContent, 'utf8');
};

module.exports = {
displayErrorMessage,
displaySuccessMessage,
writeDockerComposeFileWithService,
};
20 changes: 20 additions & 0 deletions stubs/mysql.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'vail-mysql:/var/lib/mysql'
networks:
- vail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s

0 comments on commit 0afe43d

Please sign in to comment.