fix self update in Debian systems

This commit is contained in:
cupcakearmy 2019-12-22 14:25:52 +01:00
parent f940f23338
commit 22d93f0b9c
2 changed files with 24 additions and 16 deletions

View File

@ -1,9 +1,10 @@
import axios from 'axios' import { chmodSync, renameSync, unlinkSync } from 'fs'
import { Writer } from 'clitastic'
import { unlinkSync } from 'fs'
import { tmpdir } from 'os' import { tmpdir } from 'os'
import { join, resolve } from 'path' import { join, resolve } from 'path'
import axios from 'axios'
import { Writer } from 'clitastic'
import { config, INSTALL_DIR, VERSION } from './autorestic' import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend' import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend'
import { backupAll } from './backup' import { backupAll } from './backup'
@ -147,7 +148,7 @@ const handlers: Handlers = {
checkIfResticIsAvailable() checkIfResticIsAvailable()
console.log('Restic is already installed') console.log('Restic is already installed')
return return
} catch (e) { } catch {
} }
const w = new Writer('Checking latest version... ⏳') const w = new Writer('Checking latest version... ⏳')
@ -164,9 +165,7 @@ const handlers: Handlers = {
} }
w.replaceLn('Downloading binary... 🌎') w.replaceLn('Downloading binary... 🌎')
const name = `${json.name.replace(' ', '_')}_${process.platform}_${ const name = `${json.name.replace(' ', '_')}_${process.platform}_${archMap[process.arch]}.bz2`
archMap[process.arch]
}.bz2`
const dl = json.assets.find((asset: any) => asset.name === name) const dl = json.assets.find((asset: any) => asset.name === name)
if (!dl) if (!dl)
return console.log( return console.log(
@ -184,8 +183,8 @@ const handlers: Handlers = {
unlinkSync(tmp) unlinkSync(tmp)
w.replaceLn(`Moving to ${INSTALL_DIR} 🚙`) w.replaceLn(`Moving to ${INSTALL_DIR} 🚙`)
exec('chmod', ['+x', extracted]) chmodSync(extracted, 0o755)
exec('mv', [extracted, INSTALL_DIR + '/restic']) renameSync(extracted, INSTALL_DIR + '/restic')
w.done( w.done(
`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉', `\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉',

View File

@ -1,8 +1,8 @@
import { spawnSync, SpawnSyncOptions } from 'child_process' import { spawnSync, SpawnSyncOptions } from 'child_process'
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import { createWriteStream } from 'fs' import { createWriteStream, unlinkSync, renameSync } from 'fs'
import { dirname, isAbsolute, join, resolve } from 'path' import { dirname, isAbsolute, join, resolve } from 'path'
import { homedir } from 'os' import { homedir, tmpdir } from 'os'
import axios from 'axios' import axios from 'axios'
import { Duration, Humanizer } from 'uhrwerk' import { Duration, Humanizer } from 'uhrwerk'
@ -44,13 +44,14 @@ export const execPlain = (command: string, opt: SpawnSyncOptions = {}) => {
export const checkIfResticIsAvailable = () => export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable( checkIfCommandIsAvailable(
'restic', 'restic',
'Restic is not installed'.red + 'restic is not installed'.red +
' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases', '\nEither run ' + 'autorestic install'.green +
'\nOr go to https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
) )
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => { export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
if (require('child_process').spawnSync(cmd).error) if (spawnSync(cmd).error)
throw new Error(errorMsg ? errorMsg : `"${errorMsg}" is not installed`.red) throw new Error(errorMsg ? errorMsg : `"${cmd}" is not installed`.red)
} }
export const makeObjectKeysLowercase = (object: Object): any => export const makeObjectKeysLowercase = (object: Object): any =>
@ -83,11 +84,19 @@ export const downloadFile = async (url: string, to: string) =>
responseType: 'stream', responseType: 'stream',
}) })
const stream = createWriteStream(to) const tmp = join(tmpdir(), rand(64))
const stream = createWriteStream(tmp)
const writer = file.pipe(stream) const writer = file.pipe(stream)
writer.on('close', () => { writer.on('close', () => {
stream.close() stream.close()
try {
// Delete file if already exists. Needed if the binary wants to replace itself.
// Unix does not allow to overwrite a file that is being executed, but you can remove it and save other one at its place
unlinkSync(to)
} catch {
}
renameSync(tmp, to)
res() res()
}) })
}) })