-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from playground/refactor
Refactor
- Loading branch information
Showing
15 changed files
with
460 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.