Skip to content

Commit

Permalink
Adds --disable-sandbox to esy b (and esy-build-package)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Aug 1, 2019
1 parent adeffd5 commit df516af
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 30 deletions.
8 changes: 6 additions & 2 deletions bin/Project.re
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ let makeProject = (makeSolved, projcfg: ProjectConfig.t) => {

RunAsync.ofBosError(
EsyBuildPackage.Config.make(
~disableSandbox=false,
~storePath,
~localStorePath=EsyInstall.SandboxSpec.storePath(projcfg.spec),
~projectPath=projcfg.spec.path,
Expand Down Expand Up @@ -709,13 +710,15 @@ let buildDependencies =
};
};

let buildPackage = (~quiet, ~buildOnly, projcfg, sandbox, plan, pkg) => {
let buildPackage =
(~quiet, ~disableSandbox, ~buildOnly, projcfg, sandbox, plan, pkg) => {
checkSymlinks();
let () =
Logs.info(m =>
m(
"running:@[<v>@;%s build-package \\@;%a@]",
"running:@[<v>@;%s build-package (disable-sandbox: %s)\\@;%a@]",
projcfg.ProjectConfig.mainprg,
string_of_bool(disableSandbox),
PackageId.pp,
pkg.Package.id,
)
Expand All @@ -725,6 +728,7 @@ let buildPackage = (~quiet, ~buildOnly, projcfg, sandbox, plan, pkg) => {
~force=true,
~quiet,
~buildOnly,
~disableSandbox,
sandbox,
plan,
pkg.id,
Expand Down
1 change: 1 addition & 0 deletions bin/Project.rei
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ let buildDependencies:
let buildPackage:
(
~quiet: bool,
~disableSandbox: bool,
~buildOnly: bool,
ProjectConfig.t,
BuildSandbox.t,
Expand Down
34 changes: 29 additions & 5 deletions bin/esy.re
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ let build =
~skipStalenessCheck=false,
mode,
pkgarg,
disableSandbox,
cmd,
proj: Project.t,
) => {
Expand All @@ -485,6 +486,7 @@ let build =
Project.buildPackage(
~quiet=true,
~buildOnly,
~disableSandbox,
proj.projcfg,
fetched.Project.sandbox,
plan,
Expand Down Expand Up @@ -547,9 +549,10 @@ let execEnv = (asJson, pkgarg, proj: Project.t) =>
(),
);

let exec = (mode, chdir, pkgarg, cmd, proj: Project.t) => {
let exec = (mode, chdir, pkgarg, disableSandbox, cmd, proj: Project.t) => {
open RunAsync.Syntax;
let%bind () = build(~buildOnly=false, mode, PkgArg.root, None, proj);
let%bind () =
build(~buildOnly=false, mode, PkgArg.root, disableSandbox, None, proj);
let f = pkg =>
Project.execCommand(
~checkIfDependenciesAreBuilt=false, /* not needed as we build an entire sandbox above */
Expand Down Expand Up @@ -1370,11 +1373,12 @@ let printHeader = (~spec=?, name) =>

let default = (chdir, cmdAndPkg, proj: Project.t) => {
open RunAsync.Syntax;
let disableSandbox = false;
let%lwt fetched = Project.fetched(proj);
switch (fetched, cmdAndPkg) {
| (Ok(_), None) =>
let%lwt () = printHeader(~spec=proj.projcfg.spec, "esy");
build(BuildDev, PkgArg.root, None, proj);
build(BuildDev, PkgArg.root, disableSandbox, None, proj);
| (Ok(_), Some((None, cmd))) =>
switch (Scripts.find(Cmd.getTool(cmd), proj.scripts)) {
| Some(script) => runScript(script, Cmd.getArgs(cmd), proj)
Expand All @@ -1389,7 +1393,7 @@ let default = (chdir, cmdAndPkg, proj: Project.t) => {
let%bind () = solveAndFetch(proj);
let%bind (proj, files) = Project.make(proj.projcfg);
let%bind () = Project.write(proj, files);
build(BuildDev, PkgArg.root, None, proj);
build(BuildDev, PkgArg.root, disableSandbox, None, proj);
| (Error(_) as err, Some((None, cmd))) =>
switch (Scripts.find(Cmd.getTool(cmd), proj.scripts)) {
| Some(script) => runScript(script, Cmd.getArgs(cmd), proj)
Expand Down Expand Up @@ -1487,7 +1491,16 @@ let commandsConfig = {

let commands = {
let buildCommand = {
let run = (mode, pkgarg, install, skipStalenessCheck, cmd, proj) => {
let run =
(
mode,
pkgarg,
disableSandbox,
install,
skipStalenessCheck,
cmd,
proj,
) => {
let () =
switch (cmd) {
| None =>
Expand All @@ -1502,6 +1515,7 @@ let commandsConfig = {
~skipStalenessCheck,
mode,
pkgarg,
disableSandbox,
cmd,
proj,
);
Expand All @@ -1517,6 +1531,11 @@ let commandsConfig = {
const(run)
$ modeTerm
$ pkgTerm
$ Arg.(
value
& flag
& info(["disable-sandbox"], ~doc="Disables sandbox")
)
$ Arg.(
value
& flag
Expand Down Expand Up @@ -1581,6 +1600,11 @@ let commandsConfig = {
$ modeTerm
$ chdirTerm
$ pkgTerm
$ Arg.(
value
& flag
& info(["disable-sandbox"], ~doc="Disables sandbox")
)
$ Cli.cmdTerm(
~doc="Command to execute within the sandbox environment.",
~docv="COMMAND",
Expand Down
2 changes: 1 addition & 1 deletion esy-build-package/Build.re
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ let configureBuild = (~cfg: Config.t, plan: Plan.t) => {
};
Ok({Sandbox.allowWrite: allowWrite});
};
Sandbox.init(config);
Sandbox.init(config, ~noSandbox=cfg.disableSandbox);
};

return({
Expand Down
5 changes: 3 additions & 2 deletions esy-build-package/Config.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type t = {
projectPath: EsyLib.Path.t,
storePath: EsyLib.Path.t,
localStorePath: EsyLib.Path.t,
disableSandbox: bool,
};

type config = t;
Expand Down Expand Up @@ -42,7 +43,7 @@ let rec configureStorePath = cfg => {
return(path);
};

let make = (~storePath, ~projectPath, ~localStorePath, ()) => {
let make = (~storePath, ~projectPath, ~localStorePath, ~disableSandbox, ()) => {
open Run;
let%bind storePath = configureStorePath(storePath);
let%bind () =
Expand All @@ -61,7 +62,7 @@ let make = (~storePath, ~projectPath, ~localStorePath, ()) => {
};
};
let%bind () = initStore(localStorePath);
return({projectPath, storePath, localStorePath});
return({projectPath, storePath, localStorePath, disableSandbox});
};

let render = (cfg, v) => {
Expand Down
2 changes: 2 additions & 0 deletions esy-build-package/Config.rei
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ type t =
projectPath: Fpath.t,
storePath: Fpath.t,
localStorePath: Fpath.t,
disableSandbox: bool,
};

let pp: Fmt.t(t);
Expand All @@ -21,6 +22,7 @@ let make:
~storePath: storePathConfig,
~projectPath: Fpath.t,
~localStorePath: Fpath.t,
~disableSandbox: bool,
unit
) =>
Run.t(t, _);
Expand Down
17 changes: 12 additions & 5 deletions esy-build-package/Sandbox.re
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,21 @@ module NoSandbox = {
};
};

let init = (config: config) =>
switch (EsyLib.System.Platform.host) {
| Windows => Windows.sandboxExec(config)
| Darwin => Darwin.sandboxExec(config)
| _ => NoSandbox.sandboxExec(config)
let init = (config: config, ~noSandbox) =>
if (noSandbox) {
NoSandbox.sandboxExec(config);
} else {
switch (EsyLib.System.Platform.host) {
| Windows => Windows.sandboxExec(config)
| Darwin => Darwin.sandboxExec(config)
| _ => NoSandbox.sandboxExec(config)
};
};

let exec = (~env, sandbox: sandbox, cmd) => {
/* print_endline(Astring.String.Map.get("PATH", env)); */
/* print_endline(Astring.String.Map.get("LD_LIBRARY_PATH", env)); */
/* print_endline(Astring.String.Map.get("PKG_CONFIG_PATH", env)); */
let result = sandbox(~env, cmd);
(result: result(_, err) :> Run.t(_, _));
};
2 changes: 1 addition & 1 deletion esy-build-package/Sandbox.rei
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type config = {allowWrite: list(pattern)};
type sandbox;

/* Init sandbox */
let init: config => Run.t(sandbox, _);
let init: (config, ~noSandbox: bool) => Run.t(sandbox, _);

/* Exec command in the sandbox. */
let exec:
Expand Down
32 changes: 25 additions & 7 deletions esy-build-package/bin/esyBuildPackageCommand.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type commonOpts = {
localStorePath: option(Fpath.t),
projectPath: option(Fpath.t),
logLevel: option(Logs.level),
disableSandbox: bool,
};

let setupLog = (style_renderer, level) => {
Expand All @@ -26,7 +27,7 @@ let setupLog = (style_renderer, level) => {

let createConfig = (copts: commonOpts) => {
open Run;
let {storePath, localStorePath, projectPath, _} = copts;
let {storePath, localStorePath, projectPath, disableSandbox, _} = copts;
let%bind currentPath = Bos.OS.Dir.current();
let projectPath = Option.orDefault(~default=currentPath, projectPath);
let storePath =
Expand All @@ -39,6 +40,7 @@ let createConfig = (copts: commonOpts) => {
~localStorePath=
Option.orDefault(~default=projectPath / "_store", localStorePath),
~projectPath,
~disableSandbox,
(),
);
};
Expand Down Expand Up @@ -236,18 +238,33 @@ let () = {
& info(["plan", "p"], ~env, ~docs, ~docv="PATH", ~doc)
);
};
let disableSandbox = {
let doc = "Disables sandboxing and builds the package without. CAUTION: this can be dangerous";
Arg.(value & flag & info(["disable-sandbox"], ~docs, ~doc));
};
let setupLogT =
Term.(
const(setupLog)
$ Fmt_cli.style_renderer()
$ Logs_cli.level(~env=Arg.env_var("ESY__LOG"), ())
);
let parse = (projectPath, storePath, localStorePath, planPath, logLevel) => {
projectPath,
storePath,
localStorePath,
planPath,
logLevel,
let parse =
(
projectPath,
storePath,
localStorePath,
planPath,
logLevel,
disableSandbox,
) => {
{
projectPath,
storePath,
localStorePath,
planPath,
logLevel,
disableSandbox,
};
};
Term.(
const(parse)
Expand All @@ -256,6 +273,7 @@ let () = {
$ localStorePath
$ planPath
$ setupLogT
$ disableSandbox
);
};
/* Command terms */
Expand Down
33 changes: 29 additions & 4 deletions esy-build/BuildSandbox.re
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,8 @@ let makeSymlinksToStore = (sandbox, task) => {
return();
};

let buildTask = (~quiet=?, ~buildOnly=?, ~logPath=?, sandbox, task) => {
let buildTask =
(~quiet=?, ~buildOnly=?, ~logPath=?, ~disableSandbox=?, sandbox, task) => {
open RunAsync.Syntax;
let%lwt () = Logs_lwt.debug(m => m("build %a", Task.pp, task));
let plan = Task.plan(task);
Expand All @@ -1108,6 +1109,7 @@ let buildTask = (~quiet=?, ~buildOnly=?, ~logPath=?, sandbox, task) => {
~quiet?,
~buildOnly?,
~logPath?,
~disableSandbox?,
sandbox.cfg,
plan,
)
Expand All @@ -1125,18 +1127,41 @@ let buildTask = (~quiet=?, ~buildOnly=?, ~logPath=?, sandbox, task) => {
};

let buildOnly =
(~force, ~quiet=?, ~buildOnly=?, ~logPath=?, sandbox, plan, id) =>
(
~force,
~quiet=?,
~buildOnly=?,
~logPath=?,
~disableSandbox=?,
sandbox,
plan,
id,
) =>
RunAsync.Syntax.(
switch (Plan.get(plan, id)) {
| Some(task) =>
if (!force) {
if%bind (isBuilt(sandbox, task)) {
return();
} else {
buildTask(~quiet?, ~buildOnly?, ~logPath?, sandbox, task);
buildTask(
~quiet?,
~buildOnly?,
~logPath?,
~disableSandbox?,
sandbox,
task,
);
};
} else {
buildTask(~quiet?, ~buildOnly?, ~logPath?, sandbox, task);
buildTask(
~quiet?,
~buildOnly?,
~logPath?,
~disableSandbox?,
sandbox,
task,
);
}
| None => RunAsync.return()
}
Expand Down
1 change: 1 addition & 0 deletions esy-build/BuildSandbox.rei
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ let buildOnly:
~quiet: bool=?,
~buildOnly: bool=?,
~logPath: Path.t=?,
~disableSandbox: bool=?,
t,
Plan.t,
PackageId.t
Expand Down
Loading

0 comments on commit df516af

Please sign in to comment.