2019-08-07 23:18:17 +02:00
|
|
|
import * as cp from "child_process";
|
|
|
|
import * as os from "os";
|
|
|
|
import * as path from "path";
|
2019-08-10 01:50:05 +02:00
|
|
|
import { Stream } from "stream";
|
2019-08-07 23:18:17 +02:00
|
|
|
import * as util from "util";
|
2019-08-10 01:50:05 +02:00
|
|
|
import { toVSBufferReadableStream } from "vs/base/common/buffer";
|
2019-08-07 23:18:17 +02:00
|
|
|
import { CancellationToken } from "vs/base/common/cancellation";
|
2019-08-10 01:50:05 +02:00
|
|
|
import { URI } from "vs/base/common/uri";
|
2019-08-07 23:18:17 +02:00
|
|
|
import * as pfs from "vs/base/node/pfs";
|
|
|
|
import { IConfigurationService } from "vs/platform/configuration/common/configuration";
|
|
|
|
import { IEnvironmentService } from "vs/platform/environment/common/environment";
|
2019-08-10 01:50:05 +02:00
|
|
|
import { IFileService } from "vs/platform/files/common/files";
|
2019-08-07 23:18:17 +02:00
|
|
|
import { ILogService } from "vs/platform/log/common/log";
|
2019-10-19 01:20:02 +02:00
|
|
|
import product from "vs/platform/product/common/product";
|
2019-08-10 01:50:05 +02:00
|
|
|
import { asJson, IRequestService } from "vs/platform/request/common/request";
|
2019-10-19 01:20:02 +02:00
|
|
|
import { AvailableForDownload, State, UpdateType } from "vs/platform/update/common/update";
|
2019-08-07 23:18:17 +02:00
|
|
|
import { AbstractUpdateService } from "vs/platform/update/electron-main/abstractUpdateService";
|
2019-10-05 01:14:07 +02:00
|
|
|
import { ipcMain } from "vs/server/src/node/ipc";
|
|
|
|
import { extract } from "vs/server/src/node/marketplace";
|
|
|
|
import { tmpdir } from "vs/server/src/node/util";
|
2019-08-10 01:50:05 +02:00
|
|
|
import * as zlib from "zlib";
|
2019-08-07 23:18:17 +02:00
|
|
|
|
|
|
|
interface IUpdate {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UpdateService extends AbstractUpdateService {
|
|
|
|
_serviceBrand: any;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@IConfigurationService configurationService: IConfigurationService,
|
|
|
|
@IEnvironmentService environmentService: IEnvironmentService,
|
|
|
|
@IRequestService requestService: IRequestService,
|
2019-08-10 01:50:05 +02:00
|
|
|
@ILogService logService: ILogService,
|
|
|
|
@IFileService private readonly fileService: IFileService,
|
2019-08-07 23:18:17 +02:00
|
|
|
) {
|
|
|
|
super(null, configurationService, environmentService, requestService, logService);
|
|
|
|
}
|
|
|
|
|
2019-10-08 23:23:39 +02:00
|
|
|
public async isLatestVersion(latest?: IUpdate | null): Promise<boolean | undefined> {
|
|
|
|
if (!latest) {
|
|
|
|
latest = await this.getLatestVersion();
|
|
|
|
}
|
|
|
|
if (latest) {
|
|
|
|
const latestMajor = parseInt(latest.name);
|
2019-10-19 01:20:02 +02:00
|
|
|
const currentMajor = parseInt(product.codeServerVersion);
|
2019-10-08 23:23:39 +02:00
|
|
|
return !isNaN(latestMajor) && !isNaN(currentMajor) &&
|
2019-10-19 01:20:02 +02:00
|
|
|
currentMajor <= latestMajor && latest.name === product.codeServerVersion;
|
2019-10-08 23:23:39 +02:00
|
|
|
}
|
|
|
|
return true;
|
2019-08-07 23:18:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 01:20:02 +02:00
|
|
|
protected buildUpdateFeedUrl(quality: string): string {
|
|
|
|
return `${product.updateUrl}/${quality}`;
|
2019-08-07 23:18:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 01:20:02 +02:00
|
|
|
public async doQuitAndInstall(): Promise<void> {
|
2019-08-07 23:18:17 +02:00
|
|
|
ipcMain.relaunch();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async doCheckForUpdates(context: any): Promise<void> {
|
|
|
|
this.setState(State.CheckingForUpdates(context));
|
|
|
|
try {
|
|
|
|
const update = await this.getLatestVersion();
|
2019-10-08 23:23:39 +02:00
|
|
|
if (!update || this.isLatestVersion(update)) {
|
2019-08-07 23:18:17 +02:00
|
|
|
this.setState(State.Idle(UpdateType.Archive));
|
|
|
|
} else {
|
|
|
|
this.setState(State.AvailableForDownload({
|
|
|
|
version: update.name,
|
|
|
|
productVersion: update.name,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.onRequestError(error, !!context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getLatestVersion(): Promise<IUpdate | null> {
|
|
|
|
const data = await this.requestService.request({
|
|
|
|
url: this.url,
|
2019-10-19 01:20:02 +02:00
|
|
|
headers: { "User-Agent": "code-server" },
|
2019-08-07 23:18:17 +02:00
|
|
|
}, CancellationToken.None);
|
|
|
|
return asJson(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async doDownloadUpdate(state: AvailableForDownload): Promise<void> {
|
2019-10-19 01:20:02 +02:00
|
|
|
this.setState(State.Downloading(state.update));
|
2019-08-07 23:18:17 +02:00
|
|
|
const target = os.platform();
|
|
|
|
const releaseName = await this.buildReleaseName(state.update.version);
|
|
|
|
const url = "https://github.com/cdr/code-server/releases/download/"
|
|
|
|
+ `${state.update.version}/${releaseName}`
|
|
|
|
+ `.${target === "darwin" ? "zip" : "tar.gz"}`;
|
|
|
|
const downloadPath = path.join(tmpdir, `${state.update.version}-archive`);
|
|
|
|
const extractPath = path.join(tmpdir, state.update.version);
|
|
|
|
try {
|
|
|
|
await pfs.mkdirp(tmpdir);
|
|
|
|
const context = await this.requestService.request({ url }, CancellationToken.None);
|
|
|
|
// Decompress the gzip as we download. If the gzip encoding is set then
|
|
|
|
// the request service already does this.
|
2019-08-10 01:50:05 +02:00
|
|
|
// HACK: This uses knowledge of the internals of the request service.
|
2019-08-07 23:18:17 +02:00
|
|
|
if (target !== "darwin" && context.res.headers["content-encoding"] !== "gzip") {
|
2019-08-10 01:50:05 +02:00
|
|
|
const stream = (context.res as any as Stream);
|
|
|
|
stream.removeAllListeners();
|
|
|
|
context.stream = toVSBufferReadableStream(stream.pipe(zlib.createGunzip()));
|
2019-08-07 23:18:17 +02:00
|
|
|
}
|
2019-08-10 01:50:05 +02:00
|
|
|
await this.fileService.writeFile(URI.file(downloadPath), context.stream);
|
2019-08-07 23:18:17 +02:00
|
|
|
await extract(downloadPath, extractPath, undefined, CancellationToken.None);
|
|
|
|
const newBinary = path.join(extractPath, releaseName, "code-server");
|
|
|
|
if (!pfs.exists(newBinary)) {
|
|
|
|
throw new Error("No code-server binary in extracted archive");
|
|
|
|
}
|
|
|
|
await pfs.unlink(process.argv[0]); // Must unlink first to avoid ETXTBSY.
|
|
|
|
await pfs.move(newBinary, process.argv[0]);
|
|
|
|
this.setState(State.Ready(state.update));
|
|
|
|
} catch (error) {
|
|
|
|
this.onRequestError(error, true);
|
|
|
|
}
|
|
|
|
await Promise.all([downloadPath, extractPath].map((p) => pfs.rimraf(p)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private onRequestError(error: Error, showNotification?: boolean): void {
|
|
|
|
this.logService.error(error);
|
2019-10-19 01:20:02 +02:00
|
|
|
this.setState(State.Idle(UpdateType.Archive, showNotification ? (error.message || error.toString()) : undefined));
|
2019-08-07 23:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async buildReleaseName(release: string): Promise<string> {
|
|
|
|
let target: string = os.platform();
|
|
|
|
if (target === "linux") {
|
2019-08-21 18:29:28 +02:00
|
|
|
const result = await util.promisify(cp.exec)("ldd --version").catch((error) => ({
|
|
|
|
stderr: error.message,
|
|
|
|
stdout: "",
|
|
|
|
}));
|
2019-10-21 21:09:04 +02:00
|
|
|
if (/musl/.test(result.stderr) || /musl/.test(result.stdout)) {
|
2019-08-07 23:18:17 +02:00
|
|
|
target = "alpine";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let arch = os.arch();
|
|
|
|
if (arch === "x64") {
|
|
|
|
arch = "x86_64";
|
|
|
|
}
|
|
|
|
return `code-server${release}-${target}-${arch}`;
|
|
|
|
}
|
|
|
|
}
|