Skip to content

Commit

Permalink
fetch the list of validator image tags, so users can update
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Dowideit <[email protected]>
  • Loading branch information
SvenDowideit committed Jul 15, 2022
1 parent 081a051 commit 32e2933
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
"dompurify": "^2.3.8",
"electron-cfg": "^1.2.7",
"electron-debug": "^3.2.0",
"electron-fetch": "^1.7.4",
"electron-log": "^4.4.6",
"electron-promise-ipc": "^2.2.4",
"electron-updater": "^5.0.1",
Expand Down
40 changes: 36 additions & 4 deletions src/main/ipc/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Dockerode from 'dockerode';
import * as shell from 'shelljs';

import * as newStream from 'stream';
import { get } from 'https';
import { execAsync } from '../const';

import { logger } from '../logger';
Expand Down Expand Up @@ -195,6 +196,40 @@ async function execAmman() {
log('Exiting execAmman');
return p;
}
type ImageTagsResult = {
count: number;
results: HubImageTag[];
};
type HubImageTag = {
name: string;
};

async function getImageTags(imageName: string): Promise<string[]> {
const url = `https://hub.docker.com/v2/repositories/${imageName}/tags/`;

const promise = new Promise<string>((resolve, reject) => {
let data = '';
get(url, (res) => {
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
res.on('error', reject);
});
});
const result = await promise; // wait until the promise resolves

const images: ImageTagsResult = JSON.parse(result);

const tags = images.results.map((res: HubImageTag) => {
return `${res.name}`;
});

log(`Found ${imageName}: image tags: ${JSON.stringify(tags)}`);
return tags;
}

// Need to import the file and call a function (from the main process) to get the IPC promise to exist.
export function initDockerPromises() {
Expand All @@ -204,10 +239,7 @@ export function initDockerPromises() {
logger.silly(`main: called DOCKER-GetImageTags, ${name}, ${event}`);
log(`main: called DOCKER-GetImageTags, ${name}, ${event}`);

// TODO: this would use something like https://github.com/jessestuart/docker-hub-utils
// start with just ... curl https://hub.docker.com/v2/repositories/cryptoworkbench/solana-amman/tags/ | jq '.results[].name'

return ['v1.9.29', 'v1.10.30', 'v1.11.2'];
return getImageTags('cryptoworkbench/solana-amman');
}
);

Expand Down
4 changes: 2 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ipcMain.on(
'main',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async (event: Electron.IpcMainEvent, method: string, msg: any) => {
logger.info('IPC event', { method, ...msg });
// logger.info('IPC event', { method, ...msg });
let res = {};
try {
switch (method) {
Expand All @@ -60,7 +60,7 @@ ipcMain.on(
if (typeof loggedRes === 'string') {
loggedRes = { res: `${loggedRes.slice(0, MAX_STRING_LOG_LENGTH)}...` };
}
logger.info('OK', { method, ...loggedRes });
// logger.info('OK', { method, ...loggedRes });
event.reply('main', { method, res });
} catch (e) {
const error = e as Error;
Expand Down
33 changes: 20 additions & 13 deletions src/renderer/nav/Validator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { toast } from 'react-toastify';

import { useEffect, useRef, useState } from 'react';
import { useQuery } from 'react-query';

import {
Button,
FormControl,
Expand Down Expand Up @@ -37,9 +39,25 @@ const Validator = () => {
const validator = useAppSelector(selectValidatorNetworkState);
const validatorImageName = 'cryptoworkbench/solana-amman';
const [validatorImageTag, setValidatorImageTag] = useState<string>('');
const [validatorImageTags, setValidatorImageTags] = useState<string[]>([]);
// const [validatorImageTags, setValidatorImageTags] = useState<string[]>([]);
const [containerInspect, setContainerInspect] = useState<any>({});

const {
/* isLoading: validatorImageTagsIsLoading, */
/* error: validatorImageTagsError, */
data: validatorImageTagsData,
} = useQuery('validatorImageTags', () =>
window.promiseIpc.send('DOCKER-GetImageTags', validatorImageName)
);

const validatorImageTags = validatorImageTagsData || [
'Image list loading....',
];

// TODO: not sure how to tell the user if we fail to get the list of image tags...
// if (isLoading) return 'Loading...'
// if (error) return 'An error has occurred: ' + error.message

useInterval(() => {
if (validator.net === Net.Localhost) {
window.promiseIpc
Expand Down Expand Up @@ -71,16 +89,6 @@ const Validator = () => {
});
}, 222);

useEffect(() => {
window.promiseIpc
.send('DOCKER-GetImageTags', validatorImageName)
.then((tags: string[]) => {
setValidatorImageTags(tags);
return tags;
})
.catch(logger.error);
}, [validatorImageName]);

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const listener = (resp: any) => {
Expand Down Expand Up @@ -154,8 +162,7 @@ const Validator = () => {
window.promiseIpc
.send('DOCKER-CreateValidatorContainer', validatorImageTag)
.then((info: any) => {
// setValidatorImageTags(tags);
logger.info(`hooray ${JSON.stringify(info)}`);
logger.info(`create ${JSON.stringify(info)}`);
return info;
})
.then(() => {
Expand Down

0 comments on commit 32e2933

Please sign in to comment.