Skip to content

Commit

Permalink
Merge pull request #25 from playground/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
playground authored Jan 20, 2022
2 parents 7ba5a66 + 8ff9223 commit f153f64
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 34 deletions.
85 changes: 85 additions & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#! /usr/bin/env node
const path = require('path');
const cp = require('child_process'),
exec = cp.exec;
const fs = require('fs');

const task = process.env.npm_config_task;
let userName = process.env.npm_config_username;
let imageName = process.env.npm_config_imagename;
let version = process.env.npm_config_imageversion;
const hznConfig = process.env.npm_config_hznconfig;
let org = process.env.npm_config_org || 'biz';

console.log('current directory: ', process.cwd());
let build = {
getConfig: () => {
if(fs.existsSync(hznConfig)) {
let hznJson = JSON.parse(fs.readFileSync(hznConfig).toString());
let envVars = hznJson[org]['envVars'];
userName = envVars.YOUR_DOCKERHUB_ID;
imageName = envVars.SERVICE_NAME;
version = envVars.SERVICE_VERSION;
}
},
dockerImage: () => {
if(hznConfig) {
build.getConfig();
}
if(userName && imageName && version) {
let arg = `hzn architecture`
exec(arg, {maxBuffer: 1024 * 2000}, (err, stdout, stderr) => {
if(!err) {
let arch = stdout.replace(/\r?\n|\r/g, '');
arg = `docker build -t ${userName}/${imageName}_${arch}:${version} -f Dockerfile-${arch} .`;
exec(arg, {maxBuffer: 1024 * 3500}, (err, stdout, stderr) => {
if(!err) {
console.log(stdout)
console.log(`done building image ${imageName}`);
} else {
console.log(`failed to build image ${imageName}`, err);
}
});
} else {
console.log('failed to identify arch', err);
}
});
} else {
console.log('docker username, imagename and imageversion are required...');
process.exit(0);
}
},
pushImage: () => {
if(hznConfig) {
build.getConfig();
}
if(userName && imageName && version) {
let arg = `hzn architecture`
exec(arg, {maxBuffer: 1024 * 2000}, (err, stdout, stderr) => {
if(!err) {
let arch = stdout.replace(/\r?\n|\r/g, '');
arg = `docker push ${userName}/${imageName}_${arch}:${version}`;
exec(arg, {maxBuffer: 1024 * 3500}, (err, stdout, stderr) => {
if(!err) {
console.log(stdout)
console.log(`done publishing image ${imageName}`);
} else {
console.log(`failed to publish image ${imageName}`, err);
}
});
} else {
console.log('failed to identify arch', err);
}
});
} else {
console.log('docker username, imagename and imageversion are required...');
process.exit(0);
}
},
default: () => {
console.log('command not found.');
process.exit(0);
}
}

build[task] ? build[task]() : build.default();
93 changes: 93 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#! /usr/bin/env node
const fs = require('fs');
const os = require('os');
const jsonfile = require('jsonfile');
const hznConfig = '/etc/default/config';

const template = {
envHzn: {
"envVars": {
"ANAX": "",
"SERVICE_NAME": "saved-model-service",
"SERVICE_CONTAINER_NAME": "saved-model-service",
"SERVICE_VERSION": "1.0.0",
"SERVICE_CONTAINER_CREDS": "",
"VOLUME_MOUNT": "/mms-shared",
"MMS_SHARED_VOLUME": "demo_model_mms_helper_shared_volume",
"MMS_CONTAINER_CREDS": "",
"MMS_CONTAINER_NAME": "mms-service",
"MMS_SERVICE_NAME": "mms-service",
"MMS_SERVICE_VERSION": "1.0.0",
"MMS_OBJECT_TYPE": "object_detection",
"MMS_OBJECT_ID": "config.json",
"MMS_OBJECT_FILE": "config/config.json",
"UPDATE_FILE_NAME": "model.zip"
},
"metaVars": {
}
},
envLocal: {
"YOUR_DOCKERHUB_ID": "",
"HZN_EXCHANGE_USER_AUTH": "",
"HZN_EXCHANGE_URL": "https://cp-console.ieam42-edge-8e873dd4c685acf6fd2f13f4cdfb05bb-0000.us-south.containers.appdomain.cloud/edge-exchange/v1",
"HZN_FSS_CSSURL": "https://cp-console.ieam42-edge-8e873dd4c685acf6fd2f13f4cdfb05bb-0000.us-south.containers.appdomain.cloud/edge-css",
"HZN_CUSTOM_NODE_ID": "",
"DEFAULT_ORG": ""
}
}

const getPropsFromFile = (file) => {
let props = {};
try {
if(fs.existsSync(file)) {
let data = fs.readFileSync(file).toString().split('\n');
data.forEach((el, i) => {
if(el.length > 0) {
let prop = el.split('=');
if(prop && prop.length > 0) {
if(prop[0] === 'HZN_CUSTOM_NODE_ID' && (!prop[1] || prop[1].length == 0)) {
prop[1] = os.hostname();
}
props[prop[0]] = prop[1];
}
}
});
}
} catch(e) {
console.log(e)
props = [];
}
return props;
}

const postInstall = () => {
if(fs.existsSync(`${hznConfig}/.env-local`)) {
let props = getPropsFromFile(`${hznConfig}/.env-local`);
Object.keys(template.envLocal).forEach((key) => {
if(!props[key]) {
props[key] = template.envLocal[key]
}
})
let content = '';
Object.keys(props).forEach((key) => {
content += `${key}=${props[key]}\n`;
})
fs.writeFileSync(`${hznConfig}/.env-local`, content);
}
if(fs.existsSync(`${hznConfig}/.env-hzn.json`)) {
let json = jsonfile.readFileSync(`${hznConfig}/.env-hzn.json`);
Object.keys(template.envHzn).forEach((child) => {
let node = template.envHzn[child];
Object.keys(json).forEach((org) => {
Object.keys(node).forEach((key) => {
if(!json[org][child][key]) {
json[org][child][key] = node[key];
}
})
})
})
jsonfile.writeFileSync(`${hznConfig}/.env-hzn.json`, json, {spaces: 2});
}
};

postInstall()
10 changes: 5 additions & 5 deletions build/commands/deploy.js

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

2 changes: 1 addition & 1 deletion build/commands/deploy.js.map

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

3 changes: 3 additions & 0 deletions build/common/src/hzn.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export declare class Hzn {
unregisterAgent(): Observable<unknown>;
registerAgent(): Observable<unknown>;
publishMMSObject(): Observable<unknown>;
buildAndPublish(): Observable<unknown>;
allInOneMMS(): Observable<unknown>;
showHznInfo(): Observable<unknown>;
updateHznInfo(): Observable<unknown>;
Expand All @@ -50,5 +51,7 @@ export declare class Hzn {
installHznCli(): Observable<unknown>;
uninstallHorizon(): Observable<unknown>;
preInstallHznCli(): Observable<unknown>;
setupManagementHub(): Observable<unknown>;
setupRedHat(): Observable<unknown>;
getIpAddress(): Observable<unknown>;
}
Loading

0 comments on commit f153f64

Please sign in to comment.