Skip to content

Commit

Permalink
Merge pull request #142 from edge/develop
Browse files Browse the repository at this point in the history
v1.8.7
  • Loading branch information
annybs authored Aug 15, 2023
2 parents 27e0620 + 7b5deff commit f71951a
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .ci/docker/build
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ARG NODE=node16
ENV PKG_CACHE_PATH=/pkg-cache

# Pre-fetch Node base binaries to avoid build time issues
RUN npm install -g pkg-fetch
# https://github.com/vercel/pkg-fetch/releases/tag/v3.4
RUN npm install -g [email protected]
RUN pkg-fetch -n ${NODE} -p linux -a $ARCH
RUN pkg-fetch -n ${NODE} -p macos -a $ARCH
RUN pkg-fetch -n ${NODE} -p win -a $ARCH
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@edge/cli",
"version": "1.8.5",
"version": "1.8.7",
"description": "Command line interface for the Edge network",
"private": true,
"author": "Edge Network <[email protected]>",
Expand Down Expand Up @@ -38,7 +38,7 @@
"typescript": "^4.9.4"
},
"dependencies": {
"@edge/index-utils": "^0.3.5",
"@edge/index-utils": "^0.7.2",
"@edge/log": "^1.3.0",
"@edge/stargate-utils": "^0.5.0",
"@edge/wallet-utils": "^0.14.2",
Expand Down
9 changes: 9 additions & 0 deletions src/api/xe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a GNU GPL-style license
// that can be found in the LICENSE.md file. All rights reserved.

import * as index from '@edge/index-utils'
import * as xe from '@edge/xe-utils'
import { Context } from '../main'
import { SuperAgentRequest } from 'superagent'
Expand Down Expand Up @@ -34,6 +35,13 @@ const client = (ctx: Context) => {
return data
}

const stakeViaIndex = async (stake: string) => {
log.debug('stake query via index', { host, address: stake })
const data = await index.stake.stake(ctx.network.index.host, stake, cb)
log.debug('stake response', { data })
return data
}

const stakes = async (address: string) => {
log.debug('stakes', { host, address })
const data = await xe.stake.stakes(host, address, cb)
Expand Down Expand Up @@ -65,6 +73,7 @@ const client = (ctx: Context) => {
return {
createTransaction,
pendingTransactions,
stakeViaIndex,
stakes,
wallet,
walletWithNextNonce,
Expand Down
6 changes: 4 additions & 2 deletions src/device/cli/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import { errorHandler } from '../../cli'
*/
export const action = (ctx: Context) => async (): Promise<void> => {
const opts = {
...cli.docker.readPrefix(ctx.cmd)
...cli.docker.readPrefix(ctx.cmd),
...cli.stake.read(ctx.cmd)
}

const device = ctx.device(opts.prefix)
const docker = device.docker()
const node = await device.node()
const node = await device.node(opts.stake)

const info = await node.container()
if (info === undefined) {
Expand All @@ -36,6 +37,7 @@ export const action = (ctx: Context) => async (): Promise<void> => {
export const command = (ctx: Context): Command => {
const cmd = new Command('restart').description('restart node').addHelpText('after', help)
cli.docker.configurePrefix(cmd)
cli.stake.configure(cmd)
cmd.action(errorHandler(ctx, checkVersionHandler(ctx, action({ ...ctx, cmd }))))
return cmd
}
Expand Down
6 changes: 4 additions & 2 deletions src/device/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ export const action = (ctx: Context) => async (): Promise<void> => {
...cli.docker.readAllEnv(ctx.cmd),
...cli.docker.readAllExtraHosts(ctx.cmd, ctx.network),
...cli.docker.readNetworks(ctx.cmd),
...cli.docker.readPrefix(ctx.cmd)
...cli.docker.readPrefix(ctx.cmd),
...cli.stake.read(ctx.cmd)
}

const log = ctx.log()
const device = ctx.device(opts.prefix)
const docker = device.docker()
const node = await device.node()
const node = await device.node(opts.stake)

let info = await node.container()
if (info !== undefined) {
Expand Down Expand Up @@ -69,6 +70,7 @@ export const command = (ctx: Context): Command => {
cli.docker.configurePrefix(cmd)
cli.docker.configureStargate(cmd)
cli.docker.configureTarget(cmd)
cli.stake.configure(cmd)
cmd.action(errorHandler(ctx, checkVersionHandler(ctx, action({ ...ctx, cmd }))))
return cmd
}
Expand Down
6 changes: 4 additions & 2 deletions src/device/cli/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import { errorHandler } from '../../cli'
*/
export const action = (ctx: Context) => async (): Promise<void> => {
const opts = {
...cli.docker.readPrefix(ctx.cmd)
...cli.docker.readPrefix(ctx.cmd),
...cli.stake.read(ctx.cmd)
}

const device = ctx.device(opts.prefix)
const node = await device.node()
const node = await device.node(opts.stake)
const info = await node.container()
if (info === undefined) repl.echo(`${node.name} is not running`)
else repl.echo(`${node.name} is running`)
Expand All @@ -30,6 +31,7 @@ export const action = (ctx: Context) => async (): Promise<void> => {
export const command = (ctx: Context): Command => {
const cmd = new Command('status').description('display node status').addHelpText('after', help)
cli.docker.configurePrefix(cmd)
cli.stake.configure(cmd)
cmd.action(errorHandler(ctx, checkVersionHandler(ctx, action({ ...ctx, cmd }))))
return cmd
}
Expand Down
6 changes: 4 additions & 2 deletions src/device/cli/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import { errorHandler } from '../../cli'
*/
export const action = (ctx: Context) => async (): Promise<void> => {
const opts = {
...cli.docker.readPrefix(ctx.cmd)
...cli.docker.readPrefix(ctx.cmd),
...cli.stake.read(ctx.cmd)
}

const log = ctx.log()
const device = ctx.device(opts.prefix)
const docker = device.docker()
const node = await device.node()
const node = await device.node(opts.stake)

const info = await node.container()
if (info === undefined) {
Expand All @@ -41,6 +42,7 @@ export const action = (ctx: Context) => async (): Promise<void> => {
export const command = (ctx: Context): Command => {
const cmd = new Command('stop').description('stop node').addHelpText('after', help)
cli.docker.configurePrefix(cmd)
cli.stake.configure(cmd)
cmd.action(errorHandler(ctx, checkVersionHandler(ctx, action({ ...ctx, cmd }))))
return cmd
}
Expand Down
6 changes: 4 additions & 2 deletions src/device/cli/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import { EndpointsConfig, ImageInspectInfo } from 'dockerode'
*/
export const action = (ctx: Context) => async (): Promise<void> => {
const opts = {
...cli.docker.readPrefix(ctx.cmd)
...cli.docker.readPrefix(ctx.cmd),
...cli.stake.read(ctx.cmd)
}

const log = ctx.log()
const device = ctx.device(opts.prefix)
const docker = device.docker()
const node = await device.node()
const node = await device.node(opts.stake)

repl.echo(`Checking ${node.name} version...`)
const { target } = await cli.docker.readTarget(ctx, node.stake.type)
Expand Down Expand Up @@ -100,6 +101,7 @@ export const command = (ctx: Context): Command => {
cli.docker.configureTarget(cmd)
cli.docker.configurePrefix(cmd)
cli.docker.configureAuth(cmd)
cli.stake.configure(cmd)
cmd.action(errorHandler(ctx, checkVersionHandler(ctx, action({ ...ctx, cmd }))))
return cmd
}
Expand Down
29 changes: 23 additions & 6 deletions src/device/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,31 @@ const device = (ctx: Context, prefix: string | undefined) => {
}

// get device node information including the assigned stake and, if running, the Docker container
const node = async () => {
const address = await ctx.wallet().address()
const deviceWallet = await (await volume()).read()
const node = async (stakeId?: string) => {
const stake = await (async () => {
if (stakeId) {
const deviceWallet = await (await volume()).read()
log.debug('finding stake via index', { stake: stakeId, deviceAddress: deviceWallet.address, network: deviceWallet.network })
try {
const stake = await ctx.xeClient().stakeViaIndex(stakeId)
return stake
}
catch {
throw new Error('Stake not found on the index. It might take several minutes until your stake appears.')
}
}
else {
const address = await ctx.wallet().address()
const deviceWallet = await (await volume()).read()

log.debug('finding node', { address, deviceAddress: deviceWallet.address, network: deviceWallet.network })
log.debug('finding stake via local wallet', { address, deviceAddress: deviceWallet.address, network: deviceWallet.network })

const stake = Object.values(await ctx.xeClient().stakes(address)).find(s => s.device === deviceWallet.address)
if (stake === undefined) throw new Error('device is not assigned to a stake')
return stake
}
})()

const stake = Object.values(await ctx.xeClient().stakes(address)).find(s => s.device === deviceWallet.address)
if (stake === undefined) throw new Error('device is not assigned to a stake')

const image = ctx.network.registry.imageName(stake.type, arch())
const name = toUpperCaseFirst(stake.type)
Expand Down
38 changes: 26 additions & 12 deletions src/update/cli/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import * as cli from '../../cli'
import * as repl from '../../repl'
import { Command } from 'commander'
import { arch } from 'os'
import { errorHandler } from '../../cli'
import { normalizedPlatform } from '../../helpers'
import path from 'path'
import { Context, Network } from '../../main'
import { chmodSync, copyFileSync, renameSync, unlinkSync } from 'fs'
import { currentVersion, download, latestVersion } from '..'
import { currentVersion, download, ext, latestVersion } from '..'

/** Update CLI. */
export const action = (ctx: Context, argv: string[]) => async (): Promise<void> => {
Expand All @@ -30,21 +32,33 @@ export const action = (ctx: Context, argv: string[]) => async (): Promise<void>

if (/node$/.test(selfPath)) throw new Error('path to binary appears to be node path')

repl.echo(`Downloading v${lv}`)
const { file } = await download(ctx)
const tmpFilename = `${path.dirname(file)}/tmp-${Date.now}`
let tmpFilename = ''
try {
repl.echo(`Downloading v${lv}`)
const { file } = await download(ctx)
tmpFilename = `${path.dirname(file)}/tmp-${Date.now()}`

// After downloading the file, we move the current binary to a temporary
// location, move the new file to the current binary location, and then
// attempt to remove the previous binary. This may fail on Windows.
repl.echo(`Updating from v${cv} to v${lv}`)
chmodSync(file, 0o755)
renameSync(selfPath, tmpFilename)
copyFileSync(file, selfPath)
// After downloading the file, we move the current binary to a temporary
// location, move the new file to the current binary location, and then
// attempt to remove the previous binary. This may fail on Windows.
repl.echo(`Updating from v${cv} to v${lv}`)
chmodSync(file, 0o755)
renameSync(selfPath, tmpFilename)
copyFileSync(file, selfPath)
}
catch (err) {
log.error(`Failed to download Edge CLI v${cv}`, { err })
const buildURL = ctx.network.files.latestBuildURL(normalizedPlatform(), arch(), ext())
repl.nl()
repl.echo('If you have difficulty updating Edge CLI via this method, you can download it manually from the following URL:')
repl.nl()
repl.echo(buildURL)
return
}

// Try to remove file but ignore any errors.
try {
unlinkSync(tmpFilename)
if (tmpFilename) unlinkSync(tmpFilename)
}
catch (err) {
if (opts.debug) log.error('Unable to remove download', { err })
Expand Down
2 changes: 1 addition & 1 deletion src/update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const download = async ({ network, ...ctx }: Context): Promise<DownloadIn
}
}

const ext = (): string => normalizedPlatform() === 'windows' ? '.exe' : ''
export const ext = (): string => normalizedPlatform() === 'windows' ? '.exe' : ''

export const latestVersion = async ({ network, ...ctx }: Context): Promise<SemVer> => {
const log = ctx.log('update.version.get')
Expand Down

0 comments on commit f71951a

Please sign in to comment.