Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All nodes and cluster client versions #233

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client-keys/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const consola = require('consola')
const inquirer = require('inquirer')
const config = require('../config')
const {
coppyToClipboard,
copyToClipboard,
fetcher,
formatOutput,
formatErrorResponse
Expand Down Expand Up @@ -56,7 +56,7 @@ async function createClientKey({ user, accessToken, json }) {
consola.warn(
'You will NOT be able to see your client secret again. Remember to copy it and keep it safe.'
)
coppyToClipboard(res.data.clientSecret, 'Client secret')
copyToClipboard(res.data.clientSecret, 'Client secret')
consola.success(`Generated new client keys:`)
}

Expand Down
4 changes: 2 additions & 2 deletions src/clusters/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
const jwtDecode = require('jwt-decode')

const config = require('../config')
const { coppyToClipboard } = require('../utils')
const { copyToClipboard } = require('../utils')

const CLUSTER_MIN_CAPACITY = 2
const CLUSTER_MAX_CAPACITY = 10
Expand Down Expand Up @@ -89,7 +89,7 @@ async function createCluster(params) {

if (!isJson) {
consola.success(`Initialized new cluster from service ${serviceId}\n`)
coppyToClipboard(data.id, 'Cluster id')
copyToClipboard(data.id, 'Cluster id')
}
})
}
Expand Down
154 changes: 88 additions & 66 deletions src/clusters/list.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
'use strict'

require('console.table')

const consola = require('consola')
const lodash = require('lodash')
const { fetcher, formatErrorResponse, formatOutput } = require('../utils')
require('console.table')

const {
fetcher,
formatErrorResponse,
formatOutput,
getArchiveStatus,
getClientVersion
} = require('../utils')
const config = require('../config')

/**
* Retrieves cluster
*
* @param {Object} params object
* @param {string} params.accessToken Account access token
* @param {boolean} params.all Flag defining if it should show killed clusters
* @param {string} params.allClusters List clusters from all users
* @param {string} params.sort Key used to sort the output
* @param {boolean} [params.all] Flag defining if it should show killed clusters
* @param {string} [params.allClusters] List clusters from all users
* @param {string} [params.json] Format output as JSON
* @param {string} [params.sort] Key used to sort the output

* @returns {Promise} The information cluster promise
*/
async function listClusters({ accessToken, all, allClusters, sort, json }) {
function listClusters({ accessToken, all, allClusters, sort, json }) {
const isJson = typeof json !== 'undefined'
!isJson && consola.info('Retrieving clusters...')

Expand All @@ -27,73 +35,87 @@ async function listClusters({ accessToken, all, allClusters, sort, json }) {
allClusters ? '/clusters' : '/users/me/clusters'
}`

return fetcher(url, 'GET', accessToken).then(res => {
if (!res.ok) {
formatErrorResponse(
isJson,
`Error retrieving all clusters: ${res.status}`
)
return
}

let body = res.data
if (!body) {
return
}

if (!body.length) {
const user = `${config.get('user')}`
formatErrorResponse(isJson, `No clusters were found for user ${user}`)
return
}

body = body.map(function ({
alias,
capacity,
chain,
createdAt,
healthCount = 0,
id,
name,
network,
serviceData = {},
state,
stoppedAt,
updatingService,
user
}) {
const cluster = {
id,
chain,
network,
'name/alias': alias || name,
'state': state === 'started' && updatingService ? 'updating' : state,
'health': `${Math.round((healthCount / capacity) * 100)}%`,
createdAt,
'version': serviceData.software,
'performance': serviceData.performance
return fetcher(url, 'GET', accessToken)
.then(res => {
if (!res.ok) {
formatErrorResponse(
isJson,
`Error retrieving all clusters: ${res.status}`
)
return
}

if (all) {
cluster.stoppedAt = stoppedAt
const { data } = res
if (!data) {
return
}
if (allClusters) {
cluster.user = user.email
if (!data.length) {
const user = `${config.get('user')}`
formatErrorResponse(isJson, `No clusters were found for user ${user}`)
return
}

return cluster
})
// eslint-disable-next-line consistent-return
return Promise.all(
data.map(clusterRow =>
Promise.all([
getClientVersion(clusterRow).catch(() => 'error'),
getArchiveStatus(clusterRow)
]).then(function ([clientVersion, archive]) {
const {
alias,
capacity,
chain,
createdAt,
healthCount = 0,
id,
name,
network,
serviceData = {},
state,
stoppedAt,
updatingService,
user
} = clusterRow

const cluster = {
id,
'alias/name': alias ? `${alias} (${name})` : name,
chain,
network,
'version': `${
clientVersion && clientVersion !== 'error'
? clientVersion
: serviceData.software
}${archive !== null ? ` (${archive ? 'archive' : 'full'})` : ''}`,
'performance': serviceData.performance,
'cap': capacity,
'state':
state === 'started' && updatingService ? 'updating' : state,
'health': `${Math.round((healthCount / capacity) * 100)}%`,
createdAt
}

if (!all) {
body = body.filter(n => n.state !== 'stopped')
}
if (all) {
cluster.stoppedAt = stoppedAt
}
if (allClusters) {
cluster.user = user.email
}
return cluster
})
)
)
})
.then(function (clusters) {
const body = all ? clusters : clusters.filter(n => n.state !== 'stopped')

!isJson && consola.success(`Got ${body.length} clusters:\n`)
formatOutput(
isJson,
lodash.sortBy(body, sort ? sort.split(',') : 'createdAt')
)
})
!isJson && consola.success(`Got ${body.length} clusters:\n`)
formatOutput(
isJson,
lodash.sortBy(body, sort ? sort.split(',') : 'createdAt')
)
})
}

module.exports = listClusters
4 changes: 2 additions & 2 deletions src/commands/client-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const inquirer = require('inquirer')
const { Command, flags } = require('@oclif/command')

const config = require('../config')
const { coppyToClipboard } = require('../utils')
const { copyToClipboard } = require('../utils')

class ClientTokenCommand extends Command {
async run() {
Expand Down Expand Up @@ -88,7 +88,7 @@ class ClientTokenCommand extends Command {
config.set('refreshToken', data.refreshToken)
}

coppyToClipboard(data.accessToken, 'Client access token')
copyToClipboard(data.accessToken, 'Client access token')
})
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/commands/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,33 @@ class NodesCommand extends Command {

NodesCommand.description = 'Manage your Bloq nodes'
NodesCommand.flags = {
serviceId: flags.string({ char: 's', description: 'service id' }),
authType: flags.enum({
char: 't',
description: 'auth type (jwt or basic)',
default: 'basic',
options: ['jwt', 'basic']
}),
all: flags.boolean({
char: 'a',
description: 'list all nodes',
default: false,
required: false
}),
allUsers: flags.boolean({
char: 'A',
description: 'list nodes from all user (admins only)',
default: false,
required: false
}),
authType: flags.enum({
char: 't',
description: 'auth type (jwt or basic)',
default: 'basic',
options: ['jwt', 'basic']
}),
json: flags.boolean({ char: 'j', description: 'JSON output' }),
lines: flags.integer({ char: 'l', description: 'max lines to retrieve' }),
nodeId: flags.string({ char: 'i', description: 'node id' }),
serviceId: flags.string({ char: 's', description: 'service id' }),
yes: flags.boolean({
char: 'y',
description: 'answer "yes" to prompts',
default: false
}),
lines: flags.integer({ char: 'l', description: 'max lines to retrieve' })
})
}

NodesCommand.args = [
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
const jwtDecode = require('jwt-decode')

const config = require('../config')
const { coppyToClipboard } = require('../utils')
const { copyToClipboard } = require('../utils')

/**
* Creates a node from a service ID (Valid for admin users)
Expand Down Expand Up @@ -86,7 +86,7 @@ async function createNode({ accessToken, serviceId, authType, json }) {

if (!isJson) {
consola.success(`Initialized new node from service ${serviceId}\n`)
coppyToClipboard(res.data.id, 'Node id')
copyToClipboard(res.data.id, 'Node id')
}
})
}
Expand Down
32 changes: 20 additions & 12 deletions src/nodes/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ const config = require('../config')
*
* @param {Object} params object
* @param {Object} params.accessToken Account access token
* @param {Object} params.all Boolean defining if it should show killed nodes
* @param {Object} [params.all] Boolean defining if it should show killed nodes
* @param {string} [params.allUsers] List nodes from all users
* @param {string} [params.json] Format output as JSON
* @returns {Promise} The information nodes promise
*/
async function listNodes({ accessToken, all, json }) {
async function listNodes({ accessToken, all, allUsers, json }) {
const isJson = typeof json !== 'undefined'

!isJson && consola.info('Retrieving all nodes\n')

const env = config.get('env') || 'prod'
const url = `${config.get(`services.${env}.nodes.url`)}/users/me/nodes`
const url = `${config.get(`services.${env}.nodes.url`)}${
allUsers ? '/nodes' : '/users/me/nodes'
}`

return fetcher(url, 'GET', accessToken).then(res => {
if (!res.ok) {
Expand All @@ -43,29 +47,33 @@ async function listNodes({ accessToken, all, json }) {
return
}
body = body.map(function ({
id,
chain,
state,
network,
ip,
createdAt,
id,
ip,
network,
serviceData,
stoppedAt
state,
stoppedAt,
user
}) {
const node = {
id,
ip,
chain,
network,
ip,
state,
createdAt,
version: serviceData.software,
performance: serviceData.performance
performance: serviceData.performance,
state,
createdAt
}

if (all) {
node.stoppedAt = stoppedAt || 'N/A'
}
if (allUsers) {
node.user = user
}
return node
})

Expand Down
Loading