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

feat: show repo migration (go-ipfs 0.12.0) #1982

Merged
merged 9 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 9 additions & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,14 @@
"message": "Ongoing operation is taking more resources than expected. Do you wish to abort, and forcefully reload the interface?",
"forceReload": "Yes, reload",
"doNothing": "Do nothing"
},
"migrationDialog": {
"title": "Migration In Progress",
"message": "One moment! IPFS Desktop needs to run the latest data store migrations:",
"closeAndContinue": "Close and continue in background"
},
"migrationFailedDialog": {
"title": "Migration Has Failed",
"message": "IPFS has encountered an error and migration could not be completed:"
}
}
29 changes: 7 additions & 22 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"electron-updater": "4.6.1",
"fix-path": "3.0.0",
"fs-extra": "^10.0.0",
"go-ipfs": "0.11.0",
"go-ipfs": "0.12.0",
"i18next": "^21.6.10",
"i18next-electron-language-detector": "0.0.10",
"i18next-fs-backend": "1.1.4",
Expand Down
101 changes: 88 additions & 13 deletions src/daemon/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const Ctl = require('ipfsd-ctl')
const i18n = require('i18next')
const { showDialog } = require('../dialogs')
const logger = require('../common/logger')
const { applyDefaults, migrateConfig, checkCorsConfig, checkPorts, configExists, rmApiFile, apiFileExists } = require('./config')
const { getCustomBinary } = require('../custom-ipfs-binary')
const { applyDefaults, migrateConfig, checkCorsConfig, checkPorts, configExists, rmApiFile, apiFileExists } = require('./config')
const showMigrationPrompt = require('./migration-prompt')

function cannotConnectDialog (addr) {
showDialog({
Expand Down Expand Up @@ -57,29 +58,103 @@ async function spawn ({ flags, path }) {
return { ipfsd, isRemote: false }
}

module.exports = async function (opts) {
const { ipfsd, isRemote } = await spawn(opts)
if (!isRemote) await checkPorts(ipfsd)
function listenToIpfsLogs (ipfsd, callback) {
let stdout, stderr

const listener = data => {
callback(data.toString())
}

const interval = setInterval(() => {
if (!ipfsd.subprocess) {
return
}

stdout = ipfsd.subprocess.stdout
stderr = ipfsd.subprocess.stderr

stdout.on('data', listener)
stderr.on('data', listener)

clearInterval(interval)
}, 100)

const stop = () => {
clearInterval(interval)

if (stdout) stdout.removeListener('data', listener)
if (stderr) stderr.removeListener('data', listener)
}

return stop
}

async function startIpfsWithLogs (ipfsd) {
let err, id, migrationPrompt
let isMigrating, isErrored
let logs = ''

const stopListening = listenToIpfsLogs(ipfsd, data => {
logs += data.toString()

isMigrating = isMigrating || logs.toLowerCase().includes('migration')
isErrored = isErrored || logs.toLowerCase().includes('error')

if (!isMigrating) {
return
}

if (!migrationPrompt) {
logger.info('[daemon] ipfs data store is migrating')
migrationPrompt = showMigrationPrompt(logs, isErrored)
return
}

if (isErrored) {
migrationPrompt.updateShow(logs, true)
} else {
migrationPrompt.update(logs)
}
})

try {
await ipfsd.start()
const { id } = await ipfsd.api.id()
logger.info(`[daemon] PeerID is ${id}`)
logger.info(`[daemon] Repo is at ${ipfsd.path}`)
} catch (err) {
if (!err.message.includes('ECONNREFUSED') && !err.message.includes('ERR_CONNECTION_REFUSED')) {
throw err
const idRes = await ipfsd.api.id()
id = idRes.id
} catch (e) {
err = e
}

stopListening()

return {
err, id, logs
}
}

module.exports = async function (opts) {
const { ipfsd, isRemote } = await spawn(opts)
if (!isRemote) {
await checkPorts(ipfsd)
}

let errLogs = await startIpfsWithLogs(ipfsd)

if (errLogs.err) {
if (!errLogs.err.message.includes('ECONNREFUSED') && !errLogs.err.message.includes('ERR_CONNECTION_REFUSED')) {
return { ipfsd, err: errLogs.err, logs: errLogs.logs }
}

if (!configExists(ipfsd)) {
cannotConnectDialog(ipfsd.apiAddr.toString())
throw err
return { ipfsd, err: errLogs.err, logs: errLogs.logs }
}

logger.info('[daemon] removing api file')
rmApiFile(ipfsd)
await ipfsd.start()

errLogs = await startIpfsWithLogs(ipfsd)
}

return ipfsd
return { ipfsd, err: errLogs.err, logs: errLogs.logs, id: errLogs.id }
}
35 changes: 20 additions & 15 deletions src/daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,29 @@ module.exports = async function (ctx) {
const config = store.get('ipfsConfig')
updateStatus(STATUS.STARTING_STARTED)

try {
ipfsd = await createDaemon(config)
const { id } = await ipfsd.api.id()

// Update the path if it was blank previously.
// This way we use the default path when it is
// not set.
if (!config.path || typeof config.path !== 'string') {
config.path = ipfsd.path
store.set('ipfsConfig', config)
}
const res = await createDaemon(config)

log.end()
updateStatus(STATUS.STARTING_FINISHED, id)
} catch (err) {
log.fail(err)
if (res.err) {
log.fail(res.err)
updateStatus(STATUS.STARTING_FAILED)
return
}

ipfsd = res.ipfsd

logger.info(`[daemon] PeerID is ${res.id}`)
logger.info(`[daemon] Repo is at ${ipfsd.path}`)

// Update the path if it was blank previously.
// This way we use the default path when it is
// not set.
if (!config.path || typeof config.path !== 'string') {
config.path = ipfsd.path
store.set('ipfsConfig', config)
}

log.end()
updateStatus(STATUS.STARTING_FINISHED, res.id)
}

const stopIpfs = async () => {
Expand Down
134 changes: 134 additions & 0 deletions src/daemon/migration-prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const { BrowserWindow } = require('electron')
const i18n = require('i18next')
const crypto = require('crypto')
const dock = require('../utils/dock')
const { styles, getBackgroundColor } = require('../dialogs/prompt/styles')
const { generateErrorIssueUrl } = require('../dialogs/errors')
const { IS_MAC } = require('../common/consts')

const template = (logs, script, title, message, buttons) => {
if (IS_MAC) {
buttons.reverse()
}

return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>${title}</title>
</head>
<body>
<p>${message}</p>
<pre id="logs">${logs}</pre>
<div id="buttons">
${buttons.join('\n')}
</div>
</body>
<style>
${styles}
pre {
height: 350px;
background: black;
color: white;
overflow-y: auto;
overflow-x: hidden;
overflow-anchor: none;
white-space: break-spaces;
padding: 10px;
}
</style>
<script>
function scrollToBottom (id) {
const el = document.getElementById(id);
el.scrollTop = el.scrollHeight - el.clientHeight;
}

scrollToBottom('logs')

${script}
</script>
</html>`
}

const inProgressTemplate = (logs, id) => {
const title = i18n.t('migrationDialog.title')
const message = i18n.t('migrationDialog.message')
const buttons = [`<button class="default" onclick="javascript:window.close()">${i18n.t('migrationDialog.closeAndContinue')}</button>`]
const script = `const { ipcRenderer } = require('electron')

ipcRenderer.on('${id}', (event, logs) => {
document.getElementById('logs').innerText = logs
scrollToBottom('logs')
})`
return template(logs, script, title, message, buttons)
}

const errorTemplate = (logs) => {
const title = i18n.t('migrationFailedDialog.title')
const message = i18n.t('migrationFailedDialog.message')
const buttons = [
`<button class="default" onclick="javascript:window.close()">${i18n.t('close')}</button>`,
`<button onclick="javascript:openIssue()">${i18n.t('reportTheError')}</button>`
]

const script = `
const { shell } = require('electron')

function openIssue () {
shell.openExternal('${generateErrorIssueUrl(new Error(logs))}')
}
`
return template(logs, script, title, message, buttons)
}

module.exports = (logs, error = false) => {
let window = new BrowserWindow({
show: false,
width: 800,
height: 438,
useContentSize: true,
resizable: false,
autoHideMenuBar: true,
fullscreenable: false,
backgroundColor: getBackgroundColor(),
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})

window.on('close', () => {
dock.hide()
window = null
})

window.once('ready-to-show', () => {
dock.show()
window.show()
})

// Generate random id
const id = crypto.randomBytes(16).toString('hex')

const loadWindow = (error, logs) => {
const page = error ? errorTemplate(logs) : inProgressTemplate(logs, id)
const data = `data:text/html;base64,${Buffer.from(page, 'utf8').toString('base64')}`
window.loadURL(data)
}

loadWindow(error, logs)

return {
update: logs => {
if (window) {
window.webContents.send(id, logs)
return true
}

return false
},
updateShow: (logs, error = false) => {
loadWindow(logs, error)
}
}
}
Loading