Skip to content

Commit

Permalink
Merge branch 'master' of bitbucket.org:samkos/ludion-published
Browse files Browse the repository at this point in the history
  • Loading branch information
samkos committed Nov 15, 2020
2 parents 1fe5f1d + 2a070b2 commit c51e85f
Show file tree
Hide file tree
Showing 21 changed files with 695 additions and 13 deletions.
3 changes: 3 additions & 0 deletions API/unix/.gitignore
@@ -0,0 +1,3 @@
node_modules
package-lock.json
aws-exports.js
1 change: 1 addition & 0 deletions API/unix/getService
96 changes: 96 additions & 0 deletions API/unix/getService.js
@@ -0,0 +1,96 @@
// Copyright 2020 Samuel KORTAS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

const { commandLineBase, checkArgv, SERVICE_REQUIRED } = require('./helpers');

var argv = commandLineBase()
.usage('Usage: getService --service <serviceName> --instance <instanceName>'
+ '\n [ --parameter param1[,param2,..] | --all-parameters ]')
.command('getService', 'get current parameter register a service in Ludion')
.example('$0 -s Jupyter -i myBook', '-> gives all-parameter of a Jupyter note book service')
.alias('a', 'all-parameters').nargs('all-parameters', 0)
.describe('all-parameters', 'returns the value of all parameters')
.alias('p', 'parameter').nargs('parameter', 1)
.describe('parameter', 'return the value of parameter listed')
.argv;

checkArgv(argv, [ SERVICE_REQUIRED ]);

var os = require("os");
var hostname = argv.hostname ? argv.hostname : os.hostname();

var service = argv.service ? argv.service : null;
var instance = argv.instance ? argv.instance : null;
var id = argv.id ? argv.id : null;

if (argv.debug) {
console.log(`argv`,argv);
}


var AWS = require("aws-sdk");

AWS.config.update({region: "us-east-1"});
AWS.config.loadFromPath('./service-updater.json');

// Create the Service interface for DynamoDB
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});


var params;

if (id) {
params = {
Key: {
"id": {"S": id},
},
TableName: "Service"
};
dynamodb.getItem(params, function(err, data) {
if (err) {
console.error("Can't get Service parameters.\n"+err);

} else {
if (argv.debug) {
console.log("data",data)
}
if (argv["all-parameters"]) {
var answer = {};
Object.keys(data.Item).map(x => { answer[x] = data.Item[x].S ;});
console.log(answer);
} else if (!argv["parameters"]) {
console.log(data.Item.status.S);
} else {
var answer = {};
argv.parameters.split(",").map( x => { answer[x] = data.Item[x].S ;});
console.log(answer);

}

}
});

} else {
params = {
KeyConditionExpression: 'service = :service AND instance = :instance',
ExpressionAttributeValues: {
':service': {'S': service},
':instance': {'S': instance}
},
TableName: "Service"
};
dynamodb.query(params, function(err, data) {
if (err) {
console.error("Can't get Service parameters.\n"+err);

} else {
console.log(JSON.stringify(data));
}
});


}



131 changes: 131 additions & 0 deletions API/unix/helpers.js
@@ -0,0 +1,131 @@
// Copyright 2020 Samuel KORTAS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

export const CHECK_USER = 'CHECK_USER';
export const SERVICE_REQUIRED = 'SERVICE_REQUIRED'

const LUDION_ADMINISTRATORS = ',samy,kortass,';
// const LUDION_ADMINISTRATORS = '';
var is_admin = LUDION_ADMINISTRATORS.indexOf(`,${process.env.USER},`)>-1;


// Load the AWS SDK for JS
var AWS = require("aws-sdk");

AWS.config.update({region: "us-east-1"});
AWS.config.loadFromPath('./service-updater.json');

AWS.config.apiVersions = {
cognitoidentityserviceprovider: '2016-04-18',
// other service API versions
};


// -----------------------------------------
// Create the document client interface for DynamoDB
const documentClient = new AWS.DynamoDB.DocumentClient();

// Create the Service interface for DynamoDB
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider;


var params = {
UserPoolId: 'us-east-1_62kpV0ohT', /* required */
// AttributesToGet: null,
AttributesToGet: ['email'],
// Filter: 'Username = "kortass"',
// Filter:"",
// Limit: 50,
// PaginationToken: 'x'
};

async function getMail(user) {
return new Promise(function(resolve,reject) {
cognitoidentityserviceprovider.listUsers(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
reject(err);
}
else {
var user_data = data.Users.find( x => x.Username === user);
var email = user_data.Attributes[0].Value;
resolve(email);
}
}
)
}
)
}

// function giving current date and time
function currentDateTime() {
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
return date+'/'+time;
}

const yargs = require('yargs');

// command line per default
function commandLineBase(user) {
// return yargs;
var commandLine = yargs
.alias('s', 'service').nargs('s', 1).describe('s', 'Name of the service')
.alias('i', 'instance').nargs('i', 1).describe('i', 'Name of the instance')
.alias('d','debug').nargs('debug',0).describe('debug', 'Adds debug trace')
.help('h').alias('h', 'help')
.epilog('Ludion Unix Command line - copyright 2020');

if (is_admin) {
commandLine = commandLine
.describe('u', 'given user | all [ ADMIN ONLY ]').nargs('u',1).alias('u','user')
.describe('Z', 'act as regular user, abolish admin privileges [ ADMIN ONLY ]').nargs('Z',0).alias('Z','not-admin') ;

}
return commandLine;
}

function haveToCheck(option, criteria) {
return ("," + option.join(",") + ",").indexOf(`,${criteria},`)>-1;
}

// checking argument passed are compatible with Ludion command
function checkArgv(argv, options =[]) {
if (argv.debug) {
console.log(`argv`,argv);
}

argv.user_as_param = argv.user;
if (argv["not-admin"]) {
is_admin = false;
}

argv.is_admin = is_admin;

if (!is_admin && argv.user ) {
console.log("ERROR: --user option is not possible if --not-admin option is set.");
process.exit(1);
}

argv.user = argv.user ? argv.user : process.env.USER;

if (haveToCheck(options, SERVICE_REQUIRED)) {
if (!argv.id & (!argv.service | !argv.instance )) {
console.log("ERROR: at least id or service AND instance are expected as parameters");
process.exit(1);
}
const {service, instance, user } = argv;
argv.id = `${service}:${instance}:${user}`;
}

}

module.exports.getMail = getMail;
module.exports.documentClient = documentClient;
module.exports.currentDateTime = currentDateTime;
module.exports.commandLineBase = commandLineBase;
module.exports.checkArgv = checkArgv;
1 change: 1 addition & 0 deletions API/unix/listServices
64 changes: 64 additions & 0 deletions API/unix/listServices.js
@@ -0,0 +1,64 @@
// Copyright 2020 Samuel KORTAS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

const { commandLineBase, checkArgv } = require('./helpers');

var argv = commandLineBase()
.usage('Usage: $0 [ --long ] [--json] '+
'\n [ --service <serviceName> ] [ --instance <instanceName> ]')
.alias('l', 'long').nargs('l', 0).describe('l', 'long format')
.alias('j', 'json').nargs('j', 0).describe('j', 'json format')
.command('listServices', 'list services registered in Ludion')
.example('$0 -u johndoe', '-> list all services belonging to user johndoe')
.argv;

checkArgv(argv);

const { id, user, service, instance } = argv;

import Amplify from 'aws-amplify'
import aws_exports from './aws-exports'

Amplify.configure(aws_exports)

import { API, graphqlOperation } from 'aws-amplify'

var filter = id ? `id:{eq:"${id}"}` : `id:{beginsWith:""}` ;
if (user && !id && user !=="all") { filter = filter + `, user:{eq:"${user}"}`}
if (service && !id) { filter = filter + `, service:{eq:"${service}"}`}
if (instance && !id) { filter = filter + `, instance:{eq:"${instance}"}`}

const query = `
query ls{
listServices(filter:{
${filter}
})
{
items {
${ argv.long && argv.is_admin ? "id" : ""}
machine
service
instance
status
step
${ argv.user_as_param ? "user" : ""}
${ argv.long ? "endpoint\njobid\ndescription" : ""}
}
}
}
`;

if (argv.debug) { console.log('query',query);}

const jsonToTable = require('json-to-table');
const tableToTxt = require('text-table');

API.graphql(graphqlOperation(query))
.then(data =>
console.log(argv.json ? data.data.listServices.items :
tableToTxt(jsonToTable(data.data.listServices.items))))
.catch(err => console.log({err}))


42 changes: 42 additions & 0 deletions API/unix/package.json
@@ -0,0 +1,42 @@
{
"name": "ludion",
"version": "0.1.1",
"private": true,
"dependencies": {
"aws-amplify": "^3.0.21",
"aws-appsync": "^4.0.0",
"es6-promise": "^4.2.8",
"esm": "^3.2.25",
"isomorphic-fetch": "^2.2.1",
"json-to-table": "^4.2.0",
"text-table": "^0.2.0",
"ws": "^7.3.1",
"yargs": "^15.4.1"
},
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es2018"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

0 comments on commit c51e85f

Please sign in to comment.