Commit 9bfd43b9 authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

serviceDumper & templateEvaluer

parent b9c65d71
......@@ -3,9 +3,22 @@ function main() {
var iarg = 2
var args = process.argv
var cmds = []
const usage = `node ${args[1]} [-h|--help] [--image-type [beaker|jupyter|creedo|remotevis]] [webserver|watcher]
`
const usage = `node ${args[1]} [-h|--help] [--image-type [beaker|jupyter|creedo|remotevis]] [webserver|watcher|apiserver]
node ${args[1]} serviceDumper <serviceListFile>
`
var imageType = undefined
if (iarg < args.length) {
if (args[iarg] == 'serviceDumper') {
cmds.push('serviceDumper')
require('./app/service-dumper').serviceDumper(args.slice(iarg + 1))
return;
} else if (args[iarg] == 'templateEvaluer') {
require('./app/template-evaluer').templateEvaluer(args.slice(iarg + 1))
return;
}
}
if (cmds.length == 0)
while (iarg < args.length) {
var arg = args[iarg]
iarg += 1
......@@ -29,6 +42,7 @@ function main() {
throw new Error(`unknown command line argument '${arg}'.\n${usage}`)
}
}
if (imageType)
process.env["NODE_APP_INSTANCE"] = imageType;
const config = require('config')
......@@ -41,6 +55,10 @@ function main() {
logger.error(`UncaughtException: ${stringify(err)}`)
})
}
if (cmds.includes('serviceDumper')) {
require('./app/service-dumper').serviceDumper(args.slice(iarg + 1))
return;
}
const watcherRequired = cmds.includes("watcher")
const webserverRequired = cmds.includes("webserver")
const apiserverRequired = cmds.includes("apiserver")
......
......@@ -5,6 +5,7 @@ const fs = require('fs');
const components = require('./components');
const yaml = require('js-yaml')
const logger = require('./logger')
const url = require('url');
// cache pod name -> host & port
const resolveCache = require('../safe-memory-cache/map.js')({
......@@ -277,6 +278,8 @@ function resolvePod(repl, next) {
}
}
function deletePod(podName, next) {
k8.ns(config.k8component.namespace).pods.delete({ name: podName }, function (err, result) {
resolveCache.set(podName, undefined)
......@@ -290,6 +293,41 @@ function deletePod(podName, next) {
})
}
// returns info about services indexed by service
function getServiceInfo(namespace, next, {details = false} = {}) {
k8.ns(namespace).services.get({}, function(err, res) {
if (err) {
next(err, null)
} else {
let services = {}
let master = config.k8Api.url
let masterHostname = new url.URL(master).hostname
let node = config.k8Api.node
if (res.items)
for (let is in res.items) {
let s = res.items[is]
let service = {
name: s.metadata.name,
namespace: s.metadata.namespace,
ports: [],
master: master,
masterHostname: masterHostname,
node: node
}
if (s.spec.clusterIP && s.spec.clusterIP != 'None')
service.clusterIP = s.spec.clusterIP
if (s.spec.ports)
service.ports = s.spec.ports
if (services[service.name])
services[service.name].push(service)
else
services[service.name]=[service]
}
next(null, services)
}
})
}
module.exports = {
getPods: getPods,
jsonApiPods: jsonApiPods,
......@@ -297,5 +335,6 @@ module.exports = {
guaranteeDir: guaranteeDir,
guaranteeUserDir: guaranteeUserDir,
resolvePod: resolvePod,
deletePod: deletePod
deletePod: deletePod,
getServiceInfo: getServiceInfo
}
const yaml = require('js-yaml')
const logger = require('./logger')
const fs = require('fs')
const k8D = require('./k8-data')
function mergeServices(s1,s2) {
for (let k in s2) {
if (!s1[k])
s1[k] = []
let vv = s1[k]
for (let s in s2[k])
vv.push(s)
}
}
exports.serviceDumper = function(args) {
let iarg = 0
let inFile = ''
let outFile = ''
let namespace = 'default'
let services = []
const usage = `node ${args[1]} serviceDumper [--namespace <namespace>] [--in-file <input.yaml>] [--out-file <servicePorts.yaml>]
input should be a yaml dictionary with the following keys:
- namespace: the namespaces to dump (defaults to ['default'])
- services: a list of the services to extract (defaults to all if not given)
`
while (iarg < args.length) {
var arg = args[iarg]
iarg += 1
if (arg == '--help') {
console.log(usage)
return;
} else if (arg == '--in-file') {
if (iarg >= args.length) {
console.log(`Expected in file after --in-file, ${usage}`)
return;
}
inFile = args[iarg]
iarg += 1
} else if (arg == '--out-file') {
if (iarg >= args.length) {
console.log(`Expected out file after --out-file, ${usage}`)
return;
}
outFile = args[iarg]
iarg += 1
} else if (arg == '--namespace') {
if (iarg >= args.length) {
console.log(`Expected namespace after --namespace, ${usage}`)
return;
}
namespace = args[iarg]
iarg += 1
} else {
console.log(`unexpected argument ${arg}, ${usage}`)
}
}
if (inFile.length > 0) {
let inF = yaml.safeLoad(fs.readFileSync(inFile))
if (inF.namespace)
namespace = inF.namespace
if (inF.services)
services = inF.services
}
k8D.getServiceInfo(namespace, function(err, ss){
if (err)
logger.warn(`error getting services for namespace ${namespace}`)
else {
let sss = ss
if (services.length != 0){
sss = {}
for (let sName in services) {
if (ss[sName])
sss[sName] = ss[sName]
else
sss[sName] = []
}
}
let res = yaml.safeDump(sss, {sortKeys: true})
if (outFile.length > 0)
fs.writeFileSync(outFile, res, {encoding:'utf8'})
else
console.log(res)
process.exit(0)
}
})
}
const yaml = require('js-yaml')
const logger = require('./logger')
const fs = require('fs')
const k8D = require('./k8-data')
const stringify = require('json-stringify-safe')
const components = require('./components')
function mergeServices(s1,s2) {
for (let k in s2) {
if (!s1[k])
s1[k] = []
let vv = s1[k]
s2[k].forEach(function(s) {
vv.push(s) })
}
}
exports.templateEvaluer = function(args) {
let iarg = 0
let inFile = ''
let outFile = ''
let replacements = {}
const usage = `node ${args[1]} templateEvaluer [--template <template>] [--replacements <repl1.yaml> [--replacements <repl2.yaml>]...] [--out-file <evaluatedTemplate>]
`
while (iarg < args.length) {
var arg = args[iarg]
iarg += 1
if (arg == '--help') {
console.log(usage)
return;
} else if (arg == '--template') {
if (iarg >= args.length) {
console.log(`Expected in file after --template, ${usage}`)
return;
}
inFile = args[iarg]
iarg += 1
} else if (arg == '--replacements') {
if (iarg >= args.length) {
console.log(`Expected a replacements file after --replacements, ${usage}`)
return;
}
let repl = yaml.safeLoad(fs.readFileSync(args[iarg]))
mergeServices(replacements, repl)
iarg += 1
} else if (arg == '--out-file') {
if (iarg >= args.length) {
console.log(`Expected out file after --out-file, ${usage}`)
return;
}
outFile = args[iarg]
iarg += 1
} else {
console.log(`unexpected argument ${arg}, ${usage}`)
return;
}
}
if (inFile.length > 0) {
let inF = fs.readFileSync(inFile, {encoding:'utf8'})
let outF = components.templatize(inF)(replacements)
if (outFile.lenght > 0)
fs.writeFileSync(outFile, outF, { encoding: 'utf8'})
else
console.log(outF)
}
process.exit(0)
}
......@@ -82,10 +82,10 @@ k8component: {
}
userInfo: {
demoToWatch: "/nomad/nomadlab/demos"
basePathToWatch: "/nomad/nomadlab/user-data",
privateDir: "/nomad/nomadlab/user-data/private",
sharedDir: "/nomad/nomadlab/user-data/shared",
privateDirInContainer: "/data/private",
sharedDirInContainer: "/data/shared",
basePathToWatch: "/nomad/nomadlab/user-data"
privateDir: "/nomad/nomadlab/user-data/private"
sharedDir: "/nomad/nomadlab/user-data/shared"
privateDirInContainer: "/data/private"
sharedDirInContainer: "/data/shared"
mySharedDirInContainer: "/data/my-shared"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment