Skip to content

Commit

Permalink
Staging fix (#343)
Browse files Browse the repository at this point in the history
* Changed the deploy path for the experiments

* Added support for local builds without analytics

* Fixed analytics body conditional
  • Loading branch information
raj-vlabs authored Jan 17, 2024
1 parent 5ff8fca commit d079b05
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 344 deletions.
5 changes: 3 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const log = require("./logger.js");
const Lab = {
descriptor_name: "lab-descriptor",
build_dir: "build",
exp_dir: "exp",
exp_build_dir: "exprepos",
exp_deploy_dir: "exp",
deployment_dest: "/var/www/html",
stage_dir: "stage",
ui_template_name: "templates",
Expand All @@ -31,7 +32,7 @@ const Lab = {

}
]
}
};

const Experiment = {
descriptor_name: "experiment-descriptor.json",
Expand Down
1 change: 1 addition & 0 deletions exp_build/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class Task extends Unit {
plugins: exp_info.plugins,
page_plugins: page_plugins,
local: options.local,
addAnalytics: options.addAnalytics,
units: this.setCurrent(this.getMenu(exp_info.menu)),
experiment_name: exp_info.name,
meta: {
Expand Down
2 changes: 2 additions & 0 deletions exp_build/templates/partials/analytics-body.handlebars
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#if addAnalytics}}
{{#if production}}
<!--Google Tag Manager (noscript)-->
<noscript class="gtm">
Expand All @@ -13,3 +14,4 @@
</noscript>
<!--End Google Tag Manager (noscript)-->
{{/if}}
{{/if}}
2 changes: 2 additions & 0 deletions exp_build/templates/partials/analytics-head.handlebars
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#if addAnalytics}}
{{#if production}}
<!--Google Tag Manager-->
<script class="gtm">
Expand Down Expand Up @@ -44,4 +45,5 @@
})(window,document,'script','dataLayer','GTM-5NMQ9NQ');
</script>
<!--End Google Tag Manager-->
{{/if}}
{{/if}}
48 changes: 27 additions & 21 deletions lab_build/exp_utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");
const {toDirName} = require("./lab_utils.js");
const {run} = require("../exp_build/exp_gen.js");
const { toDirName } = require("./lab_utils.js");
const { run } = require("../exp_build/exp_gen.js");
const chalk = require("chalk");
const shell = require("shelljs");
const log = require("../logger.js");
Expand All @@ -23,17 +23,18 @@ function exp_clone(e, exp_dir) {
log.debug(`Cloning ${e.repo} to ${exp_dir}`);
const e_short_name = e["short-name"];
shell.mkdir("-p", path.resolve(exp_dir));
shell.mkdir("-p", path.resolve(exp_dir, e_short_name));
shell.rm("-rf", path.resolve(exp_dir, e_short_name));
shell.exec(
`git clone -b ${e.tag} --depth 1 ${e.repo} "${path.resolve(
exp_dir,
e_short_name
)}"`,
{silent: false}
{ silent: false }
);
}

function exp_build(e, ld, exp_dir) {
function exp_build(e, ld, exp_dir, build_options) {
const e_short_name = e["short-name"];
log.debug(`Building experiment ${e_short_name} at ${path.resolve(exp_dir, e_short_name)}`);
/*
Expand All @@ -46,53 +47,58 @@ function exp_build(e, ld, exp_dir) {
ld.exp_name = e.name;
ld.exp_short_name = e_short_name;

const build_options = {
env: BuildEnvs.PRODUCTION,
isValidate: false,
plugins: false
}
// const build_options = {
// env: BuildEnvs.PRODUCTION,
// isValidate: false,
// plugins: false
// };

run(path.resolve(exp_dir, e_short_name), ld, build_options);
}

function exp_stage(e, exp_dir, deployment_dest) {
const e_short_name = toDirName(e["short-name"]);
log.debug(`Staging experiment ${e_short_name} to ${path.resolve(deployment_dest, "stage", "exp", e_short_name)}`);
log.debug(`Staging experiment ${e_short_name} to ${path.resolve(deployment_dest, e_short_name)}`);

shell.rm("-rf", `${deployment_dest}/stage/exp/${e_short_name}/`);
shell.rm("-rf", `${deployment_dest}/${e_short_name}/`);
shell.mkdir(
"-p",
path.resolve(deployment_dest, "stage", "exp", e_short_name)
path.resolve(deployment_dest, e_short_name)
);
shell.cp(
"-rf",
`${exp_dir}/${e_short_name}/build/*`,
`${deployment_dest}/stage/exp/${e_short_name}/`
`${deployment_dest}/${e_short_name}/`
);
}

function loadExperiments(labpath) {
function loadExperiments(labpath, build_options) {
const ldpath = path.resolve(labpath, "lab-descriptor.json");
const lab_descriptor = require(ldpath);

const config = require("./lab_config.json");
const exp_dir = config["exp_dir"];
const deployment_dest = config["deployment_dest"];
//const config = require("./lab_config.json");
const config = require("../config.js");
const exp_dir = config.Lab.exp_build_dir;
const deployment_dest = config.Lab.deployment_dest;
const lab_dir_name = toDirName(lab_descriptor.lab);
const deployment_path = path.join(deployment_dest, lab_dir_name);
const lab_build_dir = config.Lab.build_dir;
const exp_deploy_dir = config.Lab.exp_deploy_dir;
const exp_deploy_path = path.join(labpath, lab_build_dir, exp_deploy_dir);
const experiments = expList(lab_descriptor);

const num_experiments = experiments.length;
let exp_count = 1;
experiments.forEach((e) => {
log.info(`Loading experiment ${e["short-name"]} (${exp_count}/${num_experiments})`);
exp_clone(e, exp_dir);
exp_build(e, lab_descriptor, exp_dir);
exp_stage(e, exp_dir, deployment_path);
exp_build(e, lab_descriptor, exp_dir, build_options);
// exp_stage(e, exp_dir, deployment_path);
exp_stage(e, exp_dir, exp_deploy_path);
});
}

module.exports = {
loadExperiments,
expList
loadExperiments,
expList
};
3 changes: 2 additions & 1 deletion lab_build/lab_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"lab-name.html",
"broad-area.html",
"sidebar.html",
"navbar.html"
"navbar.html",
"analytics-body.html"
],
"pages" : [
{
Expand Down
47 changes: 26 additions & 21 deletions lab_build/lab_gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require("fs");
const glob = require("glob");
const { nextVersion } = require("./tags.js");
const { loadExperiments, expList } = require("./exp_utils");
const { validateLabDescriptor } = require("../validation/validate_descriptor.js")
const { validateLabDescriptor } = require("../validation/validate_descriptor.js");
const config = require("./lab_config.json");
const log = require("../logger.js");
const {
Expand All @@ -18,11 +18,16 @@ const {
processLabDescriptor,
} = require("./lab_utils");
const { renderTemplate } = require("./template.js");
const { BuildEnvs } = require("../enums.js");

shell.config.silent = true;

function generateLab(labpath) {
const data = processLabDescriptor(path.join(labpath, "lab-descriptor.json"));
function generateLab(labpath, build_options) {
const data = processLabDescriptor(path.join(labpath, "lab-descriptor.json"), build_options);
data.options = { ...build_options };
data.options.production = (build_options.env === BuildEnvs.PRODUCTION);
data.options.testing = (build_options.env === BuildEnvs.TESTING);
data.options.local = (build_options.env === BuildEnvs.LOCAL);
const template_file = "skeleton.html";
const component_files = config.commonComponents;

Expand Down Expand Up @@ -65,20 +70,20 @@ function moveToDeployDir(labpath) {

elist.forEach((e) => {
if (e.deploy) {
log.debug(`Deploying experiment ${e["short-name"]} to ${path.resolve(deployment_path, "exp", e["short-name"])}`);
shell.mkdir("-p", path.resolve(deployment_path, "exp", e["short-name"]));
// shell.exec(`rsync -arv --exclude .git \
// '${deployment_path}/stage/exp/${e["short-name"]}/'* '${deployment_path}/exp/${e["short-name"]}'`);
// alternative
shell.rm(
"-rf",
path.resolve(deployment_path, "exp", e["short-name"], "**", ".git/")
);
shell.cp(
"-r",
`${deployment_path}/stage/exp/${e["short-name"]}/*`,
`${deployment_path}/exp/${e["short-name"]}`
);
log.debug(`Deploying experiment ${e["short-name"]} to ${path.resolve(deployment_path, "exp", e["short-name"])}`);
shell.mkdir("-p", path.resolve(deployment_path, "exp", e["short-name"]));
// shell.exec(`rsync -arv --exclude .git \
// '${deployment_path}/stage/exp/${e["short-name"]}/'* '${deployment_path}/exp/${e["short-name"]}'`);
// alternative
shell.rm(
"-rf",
path.resolve(deployment_path, "exp", e["short-name"], "**", ".git/")
);
shell.cp(
"-r",
`${deployment_path}/stage/exp/${e["short-name"]}/*`,
`${deployment_path}/exp/${e["short-name"]}`
);
}
});

Expand All @@ -98,7 +103,7 @@ function moveToDeployDir(labpath) {
);
}

function validation(labpath){
function validation(labpath) {
const lab_descriptor_path = path.resolve(labpath, "lab-descriptor.json");

log.info("Validating lab descriptor");
Expand Down Expand Up @@ -143,7 +148,7 @@ function deployLab(labpath, release_type) {
}
}

function buildLab(labpath) {
function buildLab(labpath, build_options) {
log.info(`Building lab at ${labpath}`);

// Generate lab
Expand All @@ -154,10 +159,10 @@ function buildLab(labpath) {
} else {
// 1 : Build all lab pages by rendering templates and loading components
log.info("Generating lab pages");
generateLab(labpath);
generateLab(labpath, build_options);
// 2 : Load all experiments in the lab (Clone, build, and stage)
log.info("Loading all experiments");
loadExperiments(labpath);
loadExperiments(labpath, build_options);
log.info("Lab build complete");
}
}
Expand Down
19 changes: 12 additions & 7 deletions lab_build/lab_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ const { buildPage } = require("./template.js");
const moment = require("moment");
const log = require("../logger.js");
const Config = require("../config.js");
const { BuildEnvs } = require("vlabs-buildexp/enums.js");

shell.config.silent = true;

function toDirName(n) {
return n.toLowerCase().trim().replace(//g, "").replace(/ +/g, "-");
}

function generateLink(baseUrl, expName, index_fn = "") {
const expUrl = new URL(`https://${baseUrl}/exp/${expName}/${index_fn}`);
function generateLink(baseUrl, expName, env, index_fn = "") {
const expUrl = (env === BuildEnvs.LOCAL) ?
`./exp/${expName}/${index_fn}`
: `https://${baseUrl}/exp/${expName}/${index_fn}`;
// : new URL(`https://${baseUrl}/exp/${expName}/${index_fn}`);
return expUrl;
}

Expand Down Expand Up @@ -74,7 +78,7 @@ function prepareStructure(labpath) {
shell.mkdir("-p", path.resolve(labpath, "build"));
shell.cp(
"-r",
path.resolve(Config.assets_path(),"*"),
path.resolve(Config.assets_path(), "*"),
path.resolve(labpath, "build")
);
}
Expand All @@ -92,14 +96,14 @@ function buildLabPages(pages, labpath, template_file, component_files) {
});
}

function processLabDescriptor(descriptor_path) {
function processLabDescriptor(descriptor_path, build_options) {
log.debug(`Processing lab descriptor`);
const lab_descriptor = JSON.parse(fs.readFileSync(descriptor_path));

if (lab_descriptor.experiments) {
lab_descriptor.experiments = lab_descriptor.experiments.map((e) => {
const exp_url = generateLink(lab_descriptor.baseUrl, e["short-name"]);
return { name: e.name, link: exp_url.toString() };
const exp_url = generateLink(lab_descriptor.baseUrl, e["short-name"], build_options.env);
return { name: e.name, link: exp_url };
});
return lab_descriptor;
} else {
Expand All @@ -113,9 +117,10 @@ function processLabDescriptor(descriptor_path) {
const exp_url = generateLink(
lab_descriptor.baseUrl,
e["short-name"],
build_options.env,
(index_fn = "index.html")
);
return { name: e.name, link: exp_url.toString() };
return { name: e.name, link: exp_url };
}),
};
});
Expand Down
10 changes: 10 additions & 0 deletions lab_build/page-templates/analytics-body.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#if options.addAnalytics}}
<!--Google Tag Manager-->
{{#if options.production}}
<iframe height="0" src="https://www.googletagmanager.com/ns.html?id=GTM-W59SWTR" style="display:none;visibility:hidden" width="0">
</iframe>
{{else if options.testing}}
<iframe height="0" src="https://www.googletagmanager.com/ns.html?id=GTM-5NMQ9NQ" style="display:none;visibility:hidden" width="0">
</iframe>
{{/if}}
{{/if}}
22 changes: 22 additions & 0 deletions lab_build/page-templates/head.handlebars
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{#if options.addAnalytics}}
<!--Google Tag Manager-->
{{#if options.production}}

var dataLayer = [{
{{#if escapeCharOnFlag }}
Expand All @@ -16,4 +18,24 @@
j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-W59SWTR');

{{else if options.testing}}
var dataLayer = [{
{{#if escapeCharOnFlag }}
'labName': '{{lab}}',
{{else}}
'labName': '{{{lab}}}',
{{/if}}
'discipline': '{{broadArea.name}}',
'college': '{{collegeName}}',
'phase': '{{phase}}',
}];

(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5NMQ9NQ');
{{/if}}
<!--End Google Tag Manager-->
{{/if}}
Loading

0 comments on commit d079b05

Please sign in to comment.