From 5909d8cd9a2d03e39a96eda8ed70a19420da78df Mon Sep 17 00:00:00 2001 From: jamaljsr <1356600+jamaljsr@users.noreply.github.com> Date: Mon, 17 Feb 2020 23:08:00 -0500 Subject: [PATCH] feat(images): add migration to set a blank docker command for each node --- src/lib/docker/dockerService.ts | 8 ++- src/lib/docker/nodeTemplates.spec.ts | 5 +- src/store/models/app.ts | 2 + src/utils/migrations.ts | 79 +++++++++++++++------------- 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/src/lib/docker/dockerService.ts b/src/lib/docker/dockerService.ts index 449a578..b83a1d4 100644 --- a/src/lib/docker/dockerService.ts +++ b/src/lib/docker/dockerService.ts @@ -189,9 +189,13 @@ class DockerService implements DockerLibrary { if (await exists(path)) { const json = await read(path); - const data = JSON.parse(json); + let data = JSON.parse(json); info(`loaded ${data.networks.length} networks from '${path}'`); - return data.version === APP_VERSION ? data : migrateNetworksFile(data); + if (data.version !== APP_VERSION) { + data = migrateNetworksFile(data); + await this.saveNetworks(data); + } + return data; } else { info(`skipped loading networks because the file '${path}' doesn't exist`); return { version: APP_VERSION, networks: [], charts: {} }; diff --git a/src/lib/docker/nodeTemplates.spec.ts b/src/lib/docker/nodeTemplates.spec.ts index b0c553c..69d1353 100644 --- a/src/lib/docker/nodeTemplates.spec.ts +++ b/src/lib/docker/nodeTemplates.spec.ts @@ -3,7 +3,7 @@ import { bitcoind, lnd } from './nodeTemplates'; describe('nodeTemplates', () => { it('should create a valid bitcoind config', () => { - const node = bitcoind('mynode', 'polar-mynode', '0.18.1', 18443, 28334, 29335); + const node = bitcoind('mynode', 'polar-mynode', '0.18.1', 18443, 28334, 29335, ''); expect(node.image).toContain('bitcoind'); expect(node.container_name).toEqual('polar-mynode'); expect(node.volumes[0]).toContain('mynode'); @@ -14,14 +14,13 @@ describe('nodeTemplates', () => { 'mynode', 'polar-mynode', defaultRepoState.images.LND.latest, - 'btcnode1', 8080, 10009, 9735, + '', ); expect(node.image).toContain('lnd'); expect(node.container_name).toEqual('polar-mynode'); - expect(node.command).toContain('btcnode1'); expect(node.volumes[0]).toContain('mynode'); }); }); diff --git a/src/store/models/app.ts b/src/store/models/app.ts index 8e368b7..b536072 100644 --- a/src/store/models/app.ts +++ b/src/store/models/app.ts @@ -1,5 +1,6 @@ import { getI18n } from 'react-i18next'; import { shell } from 'electron'; +import { warn } from 'electron-log'; import { notification } from 'antd'; import { ArgsProps } from 'antd/lib/notification'; import { push } from 'connected-react-router'; @@ -219,6 +220,7 @@ const appModel: AppModel = { message: message, description: description || error.message, }); + warn(message, error); } }), navigateTo: thunk((actions, route, { dispatch }) => { diff --git a/src/utils/migrations.ts b/src/utils/migrations.ts index 23b0dcc..0144101 100644 --- a/src/utils/migrations.ts +++ b/src/utils/migrations.ts @@ -1,7 +1,7 @@ import { debug } from 'electron-log'; import { join } from 'path'; import { IChart } from '@mrblenny/react-flow-chart'; -import { BitcoinNode, LightningNode } from 'shared/types'; +import { LndNode } from 'shared/types'; import { Network, NetworksFile } from 'types'; import { networksPath } from './config'; import { APP_VERSION } from './constants'; @@ -15,13 +15,17 @@ import { getLndFilePaths } from './network'; */ const migrateCharts = (charts: Record): Record => { Object.entries(charts).forEach(([id, chart]) => { + debug(`Migrating chart for network with id #${id}`); Object.values(chart.nodes).forEach(node => { if (node.type === 'bitcoin') { - debug(`Migrating chart for network ID #${id}`); - debug(`adding peer-left port to ${node.id}`); - node.ports['peer-left'] = { id: 'peer-left', type: 'left' }; - debug(`adding peer-right port to ${node.id}`); - node.ports['peer-right'] = { id: 'peer-right', type: 'right' }; + if (!node.ports['peer-left']) { + debug(`[v0.2.0] adding peer-left port to ${node.id} in chart`); + node.ports['peer-left'] = { id: 'peer-left', type: 'left' }; + } + if (!node.ports['peer-right']) { + debug(`[v0.2.0] adding peer-right port to ${node.id} in chart`); + node.ports['peer-right'] = { id: 'peer-right', type: 'right' }; + } } }); }); @@ -29,50 +33,51 @@ const migrateCharts = (charts: Record): Record = }; /** - * In the upgrade from v0.1.0 to v0.2.0, the path where network data is stored + * v0.1.0 -> v0.2.0: the path where network data is stored * was moved to a different path. This function ensures that the paths set for * the network and the nodes are correct, as the networks folder could have * been moved manually + * v0.2.1 -> 0.3.0: the docker property was added to bitcoin and lightning + * nodes to save a custom startup command * @param networks the list of networks to migrate */ const migrateNetworks = (networks: Network[]): Network[] => { - const migrateBitcoinNodes = (network: Network): BitcoinNode[] => { - return network.nodes.bitcoin.map(node => { - debug(`updated Bitcoin node peers for ${node.name}`); - return { - ...node, - peers: [], - }; + networks.forEach(network => { + debug(`Migrating network '${network.name}' id #${network.id}`); + const newPath = join(networksPath, network.id.toString()); + if (network.path !== newPath) { + debug(`[v0.2.0] updating network path from '${network.path}' to '${newPath}'`); + network.path = newPath; + } + network.nodes.bitcoin.forEach(node => { + if (!node.peers) { + debug(`[v0.2.0] set default peers for Bitcoin node ${node.name}`); + node.peers = []; + } + if (!node.docker) { + debug(`[v0.3.0] set docker command for Bitcoin node ${node.name}`); + node.docker = { command: '' }; + } }); - }; - const migrateLightningNodes = (network: Network): LightningNode[] => { - return network.nodes.lightning.map(node => { + network.nodes.lightning.forEach(node => { + if (!node.docker) { + debug(`[v0.3.0] set docker command for ${node.implementation} node ${node.name}`); + node.docker = { command: '' }; + } if (node.implementation === 'LND') { - debug(`updated LND node paths for ${node.name}`); - return { - ...node, - paths: getLndFilePaths(node.name, network), - }; + const newPaths = getLndFilePaths(node.name, network); + if ((node as LndNode).paths.tlsCert !== newPaths.tlsCert) { + debug(`[v0.2.0] updated LND node paths for ${node.name}`); + (node as LndNode).paths = newPaths; + } } - return node; }); - }; - - return networks.map(n => { - debug(`Migrating paths in network ${n.name} #${n.id}`); - const network = { - ...n, - path: join(networksPath, n.id.toString()), - }; - debug(`updated network path from '${n.path}' to '${network.path}'`); - network.nodes.bitcoin = migrateBitcoinNodes(network); - network.nodes.lightning = migrateLightningNodes(network); - return network; }); + return networks; }; /** - * Migrates network and chart data that was created in v0.1.0 to work with v0.2.0 + * Migrates network and chart data from a previous app version * @param file the data from the `networks.json` file */ export const migrateNetworksFile = (file: NetworksFile): NetworksFile => { @@ -82,6 +87,6 @@ export const migrateNetworksFile = (file: NetworksFile): NetworksFile => { networks: migrateNetworks(file.networks), charts: migrateCharts(file.charts), }; - debug('migrations complete'); + debug('Migrations complete'); return migrated; };