diff --git a/src/Grains.fs b/src/Grains.fs index 54c1447..9d07e7b 100644 --- a/src/Grains.fs +++ b/src/Grains.fs @@ -6,6 +6,7 @@ open System.Threading open System.Threading.Tasks open Bidello.Datatypes +open Bidello.Shell type IShellGrain = @@ -18,14 +19,26 @@ type ShellGrain() = interface IShellGrain with member _.schedule (ct) (jobs: ChainOfJobs) = task { - // printfn "Grain view: --------" - // printfn "HEAD= %A" jobs.head - // jobs.rest |> List.iter (printfn "%A") - // printfn "--------" let! rc = jobs.head - |> Shell.run_job - match rc with - | Ok rc -> printfn "rc = OK %A" rc - | Error rc -> printfn "rc = Error %A" rc - } + |> run_job +(* + let rec run = function + | [] -> () + | x::xs -> + let! rc = run_job x + + match rc with + | NoShell reason | Unknown reason -> + printfn "Greve: %A" reason + | Success stdout -> printfn "rc = Ok %A" stdout + | NoPermissionOnFolder -> printfn "NO perms on folder" + | NoPrivilegeToUser -> printfn "NO privilege to user" + | Failure (_rc, stderr) -> printfn "rc ERror = = stderr %A" stderr + + run xs + + run (jobs.head::jobs.rest) + *) + failwith "todoo" +} diff --git a/src/Shell.fs b/src/Shell.fs index bbec54f..67b99b8 100644 --- a/src/Shell.fs +++ b/src/Shell.fs @@ -29,6 +29,33 @@ let private shell_ () = let shell = Lazy.Create shell_ +let private has_permission_on_folder (shell: string) (folder: string) (user: string) = task { + try + let! rc = + Builder + .UseShell(shell) + .UseExecutable("true") + .UseStartInfoTransform(fun si -> + si.WorkingDirectory <- folder + si.UserName <- user) + .ExecuteAsync () + return true + with _ -> return false +} + +let private can_switch_to_user (shell: string) (user: string) = task { + try + let! rc = + Builder + .UseShell(shell) + .UseExecutable("true") + .UseStartInfoTransform(fun si -> si.UserName <- user) + .ExecuteAsync () + return true + with _ -> return false +} + + let which (executable: string) = let run (shell: string) = let rc = @@ -56,6 +83,7 @@ let which (executable: string) = type RunResult = | Success of string | Failure of (int * string) | Unknown of string | NoShell of string + | NoPermissionOnFolder | NoPrivilegeToUser let run_job (cj: CronJob) = task { @@ -92,6 +120,18 @@ let run_job (cj: CronJob) = task { match shell.Value with | Error e -> return NoShell e | Ok shell -> - printfn $"Running: {executable}|{shell}|{workdir}|{user}" - return! run shell + let! rc = run shell + + match rc with + | Unknown u -> + let! can_switch = can_switch_to_user shell user + if not can_switch then + return NoPrivilegeToUser + else + let! can_cwd = has_permission_on_folder shell workdir user + if not can_cwd then + return NoPermissionOnFolder + else + return Unknown u + | rc -> return rc } diff --git a/tests/UnitTest1.fs b/tests/UnitTest1.fs index 4aef5f1..8b7ab5b 100644 --- a/tests/UnitTest1.fs +++ b/tests/UnitTest1.fs @@ -98,3 +98,19 @@ let should_fail_no_host () = Cron.sort_jobs now requirements |> Result.isError |> Assert.is_true + +[] +let job_deps_chain () = + + let requirements = [ + {bj with job_name = "j1"} + {bj with job_name = "j2_after_j1"; ``when``="@after j2"} + {bj with job_name = "j3_after_j2"; ``when``="@after j2_after_j1"} + ] + + let cjs = run_function requirements + + let expected = [[("h1", "j1"); ("h1", "j1_after")]; + [("h1", "j2"); ("h1", "j2_after")]; + [("h2", "j1")]] + Assert.are_seq_equal expected cjs