Skip to content

Commit

Permalink
Merge pull request #2 from nqminds/deploy
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
mereacre authored Feb 17, 2020
2 parents bfaea32 + 6fbd16a commit c2ba139
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 89 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ The format is based on
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2020-02-17

### Changed
- Added databot deploy command.
- Added shortening of the command line arguments.
- Added get app url command.

### Fixed

## [0.2.8] - 2020-02-11

### Changed
Expand Down
111 changes: 70 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,25 @@ The client app can be accessed by running the command ```tdxcli```.
Usage: tdxcli <command> [options]

Commands:
tdxcli signin [id] [secret] Sign in to tdx
tdxcli signout Sign out of tdx
tdxcli info [type] [id] Output current account info
tdxcli config Output tdx config
tdxcli list [type] List all configured aliases or secrets
tdxcli runapi <command> Run a tdx api command
tdxcli download <id> [filepath] Download resource
tdxcli upload <id> <filepath> Upload resource
tdxcli copyalias <aliasname> Makes a copy of an existing alias configuration
tdxcli modifyalias <aliasname> <configjson> Modifies an existing alias configuration
tdxcli removealias <aliasname> Removes an existing alias configuration
tdxcli databot <command> <id> [configjson] Starts, stops or aborts a databot instance
tdxcli token <command> Get or revoke a token for a give alias
tdxcli signin [id] [secret] Sign in to tdx
tdxcli signout Sign out of tdx
tdxcli info [type] [id] Output current account info
tdxcli config Output tdx config
tdxcli list [type] List all configured aliases or secrets
tdxcli token <command> Get or revoke a token for a give alias
tdxcli runapi <command> Run a tdx api command
tdxcli download <rid> [filepath] Download resource
tdxcli upload <rid> <filepath> Upload resource
tdxcli copyalias <name> Makes a copy of an existing alias configuration
tdxcli modifyalias <name> <config> Modifies an existing alias configuration
tdxcli removealias <name> Removes an existing alias configuration
tdxcli databot <command> <id> [config] Starts, stops or aborts a databot instance
tdxcli deploy <id> <rid> <config> <filepath> Deploys a databot stop->upload->start

Options:
-a, --alias Alias name [string]
-c, --credentials Input credentials in base64 [string]
-c, --credentials TDX credentials {id:"",secret:""} in base64 [string]
-t, --tdx-configs The path to the TDX config file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
```
Expand All @@ -74,6 +76,41 @@ tdxcli list credentials
```
The output of the last command will show the credentials under the alias name ```name```.

### Tdx config file
One can also pass a custom tdx config file with param ```--tdx-config``` as follows:
```bash
tdxcli commandtoexecute ...variousparams --alias=name --tdx-config=pathtoconfig
```

The config file contains the tdx configuration for each defined alias as follows:
```json
{
"nqminds": {
"tokenHref": "https://tbx.nqminds.com",
"config": {
"commandServer": "https://cmd.nqminds.com",
"ddpServer": "https://ddp.nqminds.com",
"queryServer": "https://q.nqminds.com",
"tdxServer": "https://tdx.nqminds.com",
"databotServer": "http://databot.nqminds.com",
"accessTokenTTL": 31622400
}
},
"nq_m": {
"tokenHref": "https://tbx.nq-m.com",
"config": {
"commandServer": "https://cmd.nq-m.com",
"ddpServer": "https://ddp.nq-m.com",
"queryServer": "https://q.nq-m.com",
"tdxServer": "https://tdx.nq-m.com",
"databotServer": "http://databot.nq-m.com",
"accessTokenTTL": 31622400
}
}
}
```
In the above example there are two defined aliases ```nqminds``` and ```nq-m```.

### ```signin```
Usage
```bash
Expand All @@ -89,32 +126,7 @@ tdxcli signin --alias=nqminds
tdxcli signin emailorsharetokenid thesecret --alias=nq_m
```

The aliases configurations are stored in ```config.json```:
```json
"tdxConfigs": {
"nqminds": {
"tokenHref": "https://tbx.nqminds.com",
"config": {
"commandServer": "https://cmd.nqminds.com",
"ddpServer": "https://ddp.nqminds.com",
"queryServer": "https://q.nqminds.com",
"tdxServer": "https://tdx.nqminds.com",
"accessTokenTTL": 31622400
}
},
"nq_m": {
"tokenHref": "https://tbx.nq-m.com",
"config": {
"commandServer": "https://cmd.nq-m.com",
"ddpServer": "https://ddp.nq-m.com",
"queryServer": "https://q.nq-m.com",
"tdxServer": "https://tdx.nq-m.com",
"accessTokenTTL": 31622400
}
}
}
```
An new alias can be copied from an existing alias, it can be modified or removed.
Note, the aliases configurations are stored in ```config.json``` in home folder ```.tdxcli``` of the user. A new alias can be copied from an existing alias, it can be modified or removed.

The ```tdxcli signin``` allows storing access tokens and secrets for every configured alias. So, that the user can change among them by providing the ```tdxcli signin --alias=name``` option.

Expand Down Expand Up @@ -142,6 +154,7 @@ tdxcli info
tdxcli info account
tdxcli info serverfolderid appid
tdxcli info databotsid
tdxcli info appurl instanceid
```
The above command can also be run with the ```--alias``` option.

Expand All @@ -151,6 +164,8 @@ The above command can also be run with the ```--alias``` option.

```tdxcli info databotsid``` will return all databot ids.

```tdxcli info appurl instanceid``` will return the app url for databot with instance ```instanceid```.

## ```config```
Usage
```bash
Expand Down Expand Up @@ -281,4 +296,18 @@ Usage
tdxcli token get
```

The command returns the access token for a the default alias or an alias passed with ```--alias```.
The command returns the access token for a the default alias or an alias passed with ```--alias```.

## ```deploy```
Usage
```bash
tdxcli deploy databotid resourceid databot.json filetoupload
```

The above command will deploy a databot with the following steps:

[1] Will stop a running databot instance with the databot instance id from config file ```databot.json```.

[2] Will upload the file ```filetoupload``` to tdx resource id ```resourceid```.

[3] Will start a new databot instance id for the databot ```databotid```.
5 changes: 4 additions & 1 deletion config.app.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"scraperTimeout": 5000
"scraperTimeout": 5000,
"tdxcliFoldername": ".tdxcli",
"envFilename": ".env",
"configFilename": "config.json"
}
2 changes: 2 additions & 0 deletions config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ddpServer": "https://ddp.nqminds.com",
"queryServer": "https://q.nqminds.com",
"tdxServer": "https://tdx.nqminds.com",
"databotServer": "http://databot.nqminds.com",
"accessTokenTTL": 31622400
}
},
Expand All @@ -16,6 +17,7 @@
"ddpServer": "https://ddp.nq-m.com",
"queryServer": "https://q.nq-m.com",
"tdxServer": "https://tdx.nq-m.com",
"databotServer": "http://databot.nq-m.com",
"accessTokenTTL": 31622400
}
}
Expand Down
94 changes: 67 additions & 27 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,39 @@ const {
const CommandHandler = require("./src");

const homePath = os.homedir();
const tdxcliConfigPath = path.join(homePath, ".tdxcli");
const envPath = path.join(tdxcliConfigPath, ".env");
const configPath = path.join(tdxcliConfigPath, "config.json");
const tdxcliConfigPath = path.join(homePath, appConfig.tdxcliFoldername);
const envPath = path.join(tdxcliConfigPath, appConfig.envFilename);
const configPath = path.join(tdxcliConfigPath, appConfig.configFilename);

require("dotenv").config({path: envPath});

async function getConfigs(commandlineConfigPath, configPath) {
if (commandlineConfigPath) {
return readJsonFromFile(commandlineConfigPath);
} else {
try {
const output = await readJsonFromFile(configPath);
return output;
} catch (error) {
return {};
}
}
}

async function argumentHandler(argv) {
const command = argv._[0];
const commandProps = {
alias: numberToString(argv.alias || ""),
id: numberToString(argv.id || ""),
secret: numberToString(argv.secret || ""),
resourceId: numberToString(argv.rid || ""),
type: numberToString(argv.type || ""),
command: numberToString(argv.command || ""),
filepath: numberToString(argv.filepath || ""),
aliasName: numberToString(argv.aliasname || ""),
configJson: numberToString(argv.configjson || ""),
instanceId: numberToString(argv.instanceid || ""),
databotId: numberToString(argv.databotid || ""),
aliasName: numberToString(argv.name || ""),
configJson: numberToString(argv.config || ""),
credentials: numberToString(argv.credentials || ""),
commandlineConfigPath: numberToString(argv["tdx-configs"] || ""),
apiArgs: filterObjectByIdentifier(argv, "@"),
apiArgsStringify: filterListByIdentifier(argv._.slice(1), "@"),
};
Expand All @@ -61,23 +74,23 @@ async function run(commandName, commandProps) {
let alias = commandProps.alias;
let credentials = commandProps.credentials;
const {
id, secret, type, command, filepath,
aliasName, configJson, apiArgs, apiArgsStringify,
id, secret, type, command, filepath, commandlineConfigPath,
aliasName, configJson, apiArgs, apiArgsStringify, resourceId,
} = commandProps;

try {
await mkdir(tdxcliConfigPath);
await createFile(envPath);
await createFile(configPath, JSON.stringify(defaultConfig, null, 2));

if (alias === "") alias = envToAlias(process.env[TDX_CURRENT_ALIAS] || "");
if (credentials === "") credentials = process.env[TDX_CREDENTIALS] || "";

if (commandName !== "list" && !checkValidAlias(alias)) {
throw Error("No alias or wrong alias name. Only allowed [a-zA-Z0-9_]");
}

const tdxConfigs = await readJsonFromFile(configPath);
const tdxConfigs = await getConfigs(commandlineConfigPath, configPath);
if (!(alias in tdxConfigs) && (!["signin", "modifyalias"].includes(commandName))) {
throw Error(`No configuration found for alias=${alias}`);
}

const argumentSecret = {id, secret};
const configArgs = {tdxConfig: tdxConfigs[alias] || {}, timeout: appConfig.scraperTimeout};
let commandHandler;
Expand All @@ -99,11 +112,20 @@ async function run(commandName, commandProps) {
let output;
switch (commandName) {
case "signin":
// Create the .tdxcli folder in the home directory
await mkdir(tdxcliConfigPath);
await createFile(envPath);
await createFile(configPath, JSON.stringify(defaultConfig, null, 2));
const newTdxConfigs = await readJsonFromFile(configPath);

commandHandler.setTdxConfig(newTdxConfigs[alias]);
await commandHandler.signin(argumentSecret);

setEnv({key: TDX_CURRENT_ALIAS, value: aliasToEnv(alias), envPath});
// Store the argument secret
if (argumentSecret.id) setEnv({key: getSecretAliasName(alias), value: jsonToBase64(argumentSecret), envPath});
if (argumentSecret.id) {
setEnv({key: getSecretAliasName(alias), value: jsonToBase64(argumentSecret), envPath});
}
setEnv({key: getTokenAliasName(alias), value: commandHandler.getToken(), envPath});
output = "OK";
break;
Expand All @@ -113,7 +135,11 @@ async function run(commandName, commandProps) {
setEnv({key: getSecretAliasName(alias), value: "", envPath});
break;
case "info":
output = await commandHandler.getInfo({id, type});
output = await commandHandler.getInfo({
id,
type,
tdxConfig: tdxConfigs[alias] || {},
});
break;
case "config":
output = tdxConfigs[alias] || {};
Expand All @@ -131,10 +157,10 @@ async function run(commandName, commandProps) {
output = JSON.stringify(output, null, 2);
break;
case "download":
await commandHandler.download(id, filepath);
await commandHandler.download(resourceId, filepath);
break;
case "upload":
output = await commandHandler.upload(id, filepath);
output = await commandHandler.upload(resourceId, filepath);
break;
case "copyalias":
await copyAliasConfig({tdxConfigs, alias, copyAliasName: aliasName, configPath});
Expand All @@ -146,7 +172,9 @@ async function run(commandName, commandProps) {
output = "OK";
break;
case "removealias":
if (alias === aliasName) throw Error("Can't remove the running alias.");
if (alias === aliasName) {
throw Error(`Can't remove the running alias=${alias}.`);
}
await removeAliasConfig({tdxConfigs, aliasName, configPath});
output = "OK";
break;
Expand All @@ -156,6 +184,10 @@ async function run(commandName, commandProps) {
case "token":
output = await commandHandler.runTokenCommand(command);
break;
case "deploy":
output = await commandHandler.deploy({id, resourceId, configJson, filepath});
output = JSON.stringify(output, null, 2);
break;
}

if (output) console.log(output);
Expand All @@ -175,14 +207,15 @@ const argv = require("yargs")
.command("info [type] [id]", "Output current account info", {}, argumentHandler)
.command("config", "Output tdx config", {}, argumentHandler)
.command("list [type]", "List all configured aliases or secrets", {}, argumentHandler)
.command("runapi <command>", "Run a tdx api command", {}, argumentHandler)
.command("download <id> [filepath]", "Download resource", {}, argumentHandler)
.command("upload <id> <filepath>", "Upload resource", {}, argumentHandler)
.command("copyalias <aliasname>", "Makes a copy of an existing alias configuration", {}, argumentHandler)
.command("modifyalias <aliasname> <configjson>", "Modifies an existing alias configuration", {}, argumentHandler)
.command("removealias <aliasname>", "Removes an existing alias configuration", {}, argumentHandler)
.command("databot <command> <id> [configjson]", "Starts, stops or aborts a databot instance", {}, argumentHandler)
.command("token <command>", "Get or revoke a token for a give alias", {}, argumentHandler)
.command("runapi <command>", "Run a tdx api command", {}, argumentHandler)
.command("download <rid> [filepath]", "Download resource", {}, argumentHandler)
.command("upload <rid> <filepath>", "Upload resource", {}, argumentHandler)
.command("copyalias <name>", "Makes a copy of an existing alias configuration", {}, argumentHandler)
.command("modifyalias <name> <config>", "Modifies an existing alias configuration", {}, argumentHandler)
.command("removealias <name>", "Removes an existing alias configuration", {}, argumentHandler)
.command("databot <command> <id> [config]", "Starts, stops or aborts a databot instance", {}, argumentHandler)
.command("deploy <id> <rid> <config> <filepath>", "Deploys a databot stop->upload->start", {}, argumentHandler)
.demandCommand(1, 1, "You need at least one command to run.")
.option("a", {
alias: "alias",
Expand All @@ -194,7 +227,14 @@ const argv = require("yargs")
.option("c", {
alias: "credentials",
nargs: 1,
describe: "Input credentials in base64",
describe: "TDX credentials {id:\"\",secret:\"\"} in base64",
type: "string",
requiresArg: true,
})
.option("t", {
alias: "tdx-configs",
nargs: 1,
describe: "The path to the TDX config file",
type: "string",
requiresArg: true,
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nqminds/nqm-tdx-terminal-cli",
"version": "0.2.8",
"version": "1.0.0",
"description": "Command-line interface for accessing the TDX API",
"main": "main.js",
"directories": {
Expand Down
Loading

0 comments on commit c2ba139

Please sign in to comment.