Skip to content

Commit

Permalink
Merge pull request #43 from bcgov-nr/feat/addPlaybookGen
Browse files Browse the repository at this point in the history
Feat/add playbook gen
  • Loading branch information
andrwils authored Apr 30, 2024
2 parents d1bca47 + a093ea8 commit 3a3bec3
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 5 deletions.
57 changes: 52 additions & 5 deletions generators/gh-maven-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yosay from 'yosay';
import chalk from 'chalk';

/**
* Generate the CI workflow and NR Broker intention files needed Java/Tomcat Maven builds in GitHub
* Generate the CI workflow and NR Broker intention files needed for Java/Tomcat Maven builds in GitHub
*/
export default class extends Generator {
async prompting() {
Expand Down Expand Up @@ -52,6 +52,11 @@ export default class extends Generator {
"The path to where your pom file is located relative to the repository's root",
),
);
this.log(
' ' +
chalk.bold('GitHub Owner with repo path: ') +
chalk.dim('The Github owner with repo path (e.g. bcgov-nr/edqa-war) '),
);
this.log('');

this.log(chalk.bold('Prompts'));
Expand Down Expand Up @@ -87,15 +92,14 @@ export default class extends Generator {
{
type: 'confirm',
name: 'gitHubPackages',
message: 'Publish to GitHub Packages?',
message: 'Publish to GitHub Packages:',
default: false,
store: true,
},
{
type: 'input',
name: 'gitHubOwnerPack',
message: 'GitHub Owner with repo path (eg. bcgov-nr-labs/edqa-war):',
default: 'bcgov-nr-labs/edqa-war',
message: 'GitHub Owner with repo path (e.g. bcgov-nr/results-war):',
store: true,
when: (answers) => answers.gitHubPackages,
},
Expand All @@ -118,10 +122,48 @@ export default class extends Generator {
{
type: 'confirm',
name: 'deployOnPrem',
message: 'Deploy on-prem?',
message: 'Deploy on-prem:',
default: false,
store: true,
},
{
type: 'input',
name: 'playbookPath',
message: 'Playbook path:',
default: 'playbooks',
store: true,
when: (answers) => answers.deployOnPrem,
},
{
type: 'input',
name: 'tomcatContext',
message: 'Tomcat Context (e.g. ext#results):',
store: true,
when: (answers) => answers.deployOnPrem,
},
{
type: 'confirm',
name: 'useAltAppDirName',
message: 'Use alternative webapp directory:',
default: false,
store: true,
when: (answers) => answers.deployOnPrem,
},
{
type: 'input',
name: 'altAppDirName',
message: 'Alternative webapp directory name:',
store: true,
when: (answers) => answers.useAltAppDirName,
},
{
type: 'confirm',
name: 'addWebadeConfig',
message: 'Add Webade configuration:',
default: false,
store: true,
when: (answers) => answers.deployOnPrem,
}
]);

this.props = answers;
Expand Down Expand Up @@ -177,6 +219,11 @@ export default class extends Generator {
serviceName: this.props.serviceName,
},
);
const playbook_args = [this.props.projectName, this.props.serviceName, this.props.playbookPath,
this.props.tomcatContext, this.props.altAppDirName
]
const playbook_options = { addWebadeConfig: this.props.addWebadeConfig, altAppDirName: this.props.altAppDirName }
this.composeWith('nr-repository-composer:pd-ansible-playbook', playbook_args, playbook_options)
}

this.config.save();
Expand Down
80 changes: 80 additions & 0 deletions generators/pd-ansible-playbook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';
import Generator from 'yeoman-generator';
import yosay from 'yosay';
import chalk from 'chalk';
import path from 'path';
import * as fs from 'node:fs';

/**
* Generate the Ansible playbook and variable files needed for Tomcat webapp deployments
*/
export default class extends Generator {
constructor(args, opts) {
super(args, opts);

this.argument('projectName', {
type: String,
required: true,
description: 'Project Name',
});
this.argument('serviceName', {
type: String,
required: true,
description: 'Service Name',
});
this.argument('playbookPath', {
type: String,
required: true,
description: 'Playbook Path',
});
this.argument('tomcatContext', {
type: String,
required: true,
description: 'Tomcat Context',
});
this.option('altAppDirName', {
type: String,
description: 'Alternative webapp directory name',
});
this.option('addWebadeConfig', {
type: String,
description: 'Add Webade configuration',
});
}

// Generate GitHub workflows and NR Broker intention files
writing() {
this.log('Generating playbook files');
this.fs.copyTpl(
this.templatePath('playbook.yaml'),
this.destinationPath(`${this.options.playbookPath}/playbook.yaml`),
{
projectName: this.options.projectName,
serviceName: this.options.serviceName,
},
);
this.fs.copyTpl(
this.templatePath('vars/standard/**'),
this.destinationPath(`${this.options.playbookPath}/vars/standard`),
{
projectName: this.options.projectName,
serviceName: this.options.serviceName,
projectNameUpperCase: this.options.projectName.toUpperCase(),
tomcatContext: this.options.tomcatContext,
altAppDirName: this.options.altAppDirName,
addWebadeConfig: this.options.addWebadeConfig,
},
);
// Initialize empty files that can be used for custom variables
// Skip copying templates if any custom files already exist
if (!fs.existsSync(this.destinationPath(`${this.options.playbookPath}/vars/custom`))) {
this.fs.copyTpl(
this.templatePath('vars/custom/**'),
this.destinationPath(`${this.options.playbookPath}/vars/custom`),
{},
);
}

this.config.save();
}
}
74 changes: 74 additions & 0 deletions generators/pd-ansible-playbook/templates/playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- name: <%= projectName %>/<%= serviceName %>
hosts: tomcat-servers
collections:
- polaris.deploy
vars_files:
- "vars/standard/all.yaml"
- "vars/standard/{{env_vars}}.yaml"
- "vars/custom/all.yaml"
- "vars/custom/{{env_vars}}.yaml"

roles:
# Establish the port number
- name: port_manager

# prepare the installation environment by creating the necessary folders
- name: create_project_directories

# individual JDK installation
- name: jdk
vars:
jdk_version:
type: 'openjdk'
major_version: 8
url: '{{ artifactory_url }}/ext-binaries-local/openjdk/java-se-8u40-ri.tar.gz'
filename: 'java-se-8u40-ri.tar.gz'
checksum: 'sha1:606984531e3eeddba6be5ac626a229ef283c5de0'
cacerts_path: 'jre/lib/security/cacerts'
use_proxy: false

# create a self signed certificate to allow for HTTPS
- name: self_signed_cert

# install & configure the Tomcat container
- name: tomcat

# deploy the webapp
- name: webapp
vars:
webapp_war:
context: "{{ context }}"
proxy_env: "{{ env_vars }}"

# create a WebADE connection jar for the container
- name: webade_connection_jar
# Uncomment the following line if using the MOF Webade datastore
# vars:
# webade_datastore: "ca.bc.gov.webade.mof.MOFOrganizationDatastore"
become: yes
become_user: "{{ install_user }}"

tasks:
# The following task is an example for adding custom webapp configuration files
# You can modify it according to your needs or remove it if custom configuration is not needed
- name: configure nonstandard files
template:
src: "{{ playbook_dir }}/templates/{{ item.src }}"
dest: "{{ item.dest }}"
mode: "0775"
become: yes
become_user: "{{ install_user }}"
with_items:
- {
src: "web.xml.j2",
dest: "{{ pd_prop_service_install_directory }}/webapps/{{ alt_app_dir_name or context }}/WEB-INF/web.xml"
}
- {
src: "JMSConfig.xml.j2",
dest: "{{ pd_prop_service_install_directory }}/webapps/{{ alt_app_dir_name or context }}/WEB-INF/classes/JMSConfig.xml"
}
- {
src: "tasks.xml.j2",
dest: "{{ pd_prop_service_install_directory }}/webapps/{{ alt_app_dir_name or context }}/WEB-INF/classes/tasks.xml"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use this file for custom variables related to your webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use this file for custom variables related to your webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use this file for custom variables related to your webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use this file for custom variables related to your webapp
29 changes: 29 additions & 0 deletions generators/pd-ansible-playbook/templates/vars/standard/all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The following properties are required by all services
# Ansible connection properties
ssh_user: "{{ lookup('ansible.builtin.env', 'PODMAN_CD_USER') }}"
ssh_pass: "{{ lookup('ansible.builtin.env', 'PODMAN_CD_PASS') }}"
ansible_user: "{{ lookup('ansible.builtin.env', 'PODMAN_CD_USER') }}"
ansible_become_password: "{{ lookup('ansible.builtin.env', 'PODMAN_CD_PASS') }}"
ansible_ssh_extra_args: "-o StrictHostKeyChecking=no"

# General properties
pd_prop_project_name: "<%= projectNameUpperCase %>"
pd_prop_service_name: "<%= serviceName %>"
pd_prop_project_version: "{{ lookup('ansible.builtin.env', 'PODMAN_PROJECT_VERSION') }}"
pd_prop_artifact_name: "{{ lookup('ansible.builtin.env', 'PODMAN_ARTIFACT_NAME') }}"
pd_prop_artifact_sha256: "{{ lookup('ansible.builtin.env', 'PODMAN_ARTIFACT_SHA256') }}"
pd_prop_build_number: "{{ lookup('ansible.builtin.env', 'PODMAN_BUILD_NUMBER') }}"
pd_prop_project_version_run: "{{ pd_prop_project_version }}-{{ pd_prop_build_number }}"

# The following properties are custom to each particular service
context: <%= tomcatContext %>
<% if (altAppDirName) { -%>
alt_app_dir_name: <%= altAppDirName %>
<% } -%>

# By default, the Tomcat port is set dynamically by using the port_manager role
# If you need to set a static port, replace the dynamic port assignment value with your static value
tomcat_https_port: "{{ portmanager_assignments[ansible_host] }}"

# If you need to use a particular version of Tomcat, set the version here
tomcat_version_number: "8.5.51"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Use this file for properties specific to the development (integration, delivery) environment
<% if (addWebadeConfig === 'true') { -%>
webade_jdbc_url: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_jdbc_url') }}"
webade_db_user: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_username') }}"
webade_db_pass: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_password') }}"
webade_env: DEV
<% } -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Use this file for properties specific to the production environment
<% if (addWebadeConfig === 'true') { -%>
webade_jdbc_url: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_jdbc_url') }}"
webade_db_user: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_username') }}"
webade_db_pass: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_password') }}"
webade_env: PROD
<% } -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Use this file for properties specific to the test environment
<% if (addWebadeConfig === 'true') { -%>
webade_jdbc_url: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_jdbc_url') }}"
webade_db_user: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_username') }}"
webade_db_pass: "{{ lookup('ansible.builtin.env', 'PODMAN_webade_password') }}"
webade_env: TEST
<% } -%>

0 comments on commit 3a3bec3

Please sign in to comment.