Fix issues with configuration directories
- Move the old data directory if possible. - Fix extension path to not use a hard-coded path and instead use the data directory. - Create every part of the path during startup. - Create each path when a connection is made as well in case they are deleted while the server is running. - Create every part of the path before saving settings or writing a file using the resource endpoint.
This commit is contained in:
@ -97,3 +97,22 @@ export const parse = (arg: string): any => { // tslint:disable-line no-any
|
||||
|
||||
return arg ? convert(JSON.parse(arg)) : arg;
|
||||
};
|
||||
|
||||
export const mkdirP = async (path: string): Promise<void> => {
|
||||
// Since our fills require this file, we can't import them up top or we get
|
||||
// circular dependency issue.
|
||||
const { mkdir } = require("fs") as typeof import("fs");
|
||||
const { promisify } = require("util") as typeof import("util");
|
||||
const split = path.replace(/^\/*|\/*$/g, "").split("/");
|
||||
let dir = "";
|
||||
while (split.length > 0) {
|
||||
dir += "/" + split.shift();
|
||||
try {
|
||||
await promisify(mkdir)(dir);
|
||||
} catch (error) {
|
||||
if (error.code !== "EEXIST" && error.code !== "EISDIR") {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1,16 +1,15 @@
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { mkdir } from "fs";
|
||||
import { promisify } from "util";
|
||||
import { logger, field } from "@coder/logger";
|
||||
import { Pong, ClientMessage, WorkingInitMessage, ServerMessage } from "../proto";
|
||||
import { evaluate, ActiveEvaluation } from "./evaluate";
|
||||
import { ForkProvider } from "../common/helpers";
|
||||
import { ReadWriteConnection } from "../common/connection";
|
||||
import { mkdirP } from "../common/util";
|
||||
|
||||
export interface ServerOptions {
|
||||
readonly workingDirectory: string;
|
||||
readonly dataDirectory: string;
|
||||
readonly cacheDirectory: string;
|
||||
readonly builtInExtensionsDirectory: string;
|
||||
readonly fork?: ForkProvider;
|
||||
}
|
||||
@ -42,24 +41,11 @@ export class Server {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure the data directory exists.
|
||||
const mkdirP = async (path: string): Promise<void> => {
|
||||
const split = path.replace(/^\/*|\/*$/g, "").split("/");
|
||||
let dir = "";
|
||||
while (split.length > 0) {
|
||||
dir += "/" + split.shift();
|
||||
try {
|
||||
await promisify(mkdir)(dir);
|
||||
} catch (error) {
|
||||
if (error.code !== "EEXIST" && error.code !== "EISDIR") {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Promise.all([ mkdirP(path.join(this.options.dataDirectory, "User", "workspaceStorage")) ]).then(() => {
|
||||
logger.info("Created data directory");
|
||||
}).catch((error) => {
|
||||
Promise.all([
|
||||
mkdirP(this.options.cacheDirectory),
|
||||
mkdirP(this.options.dataDirectory),
|
||||
mkdirP(this.options.workingDirectory),
|
||||
]).catch((error) => {
|
||||
logger.error(error.message, field("error", error));
|
||||
});
|
||||
|
||||
|
@ -4,10 +4,12 @@ describe("Server", () => {
|
||||
const dataDirectory = "/tmp/example";
|
||||
const workingDirectory = "/working/dir";
|
||||
const builtInExtensionsDirectory = "/tmp/example";
|
||||
const cacheDirectory = "/tmp/cache";
|
||||
const client = createClient({
|
||||
builtInExtensionsDirectory,
|
||||
cacheDirectory,
|
||||
dataDirectory,
|
||||
workingDirectory,
|
||||
builtInExtensionsDirectory,
|
||||
});
|
||||
|
||||
it("should get init msg", (done) => {
|
||||
|
Reference in New Issue
Block a user