Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for windows #1812

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion digdag-docs/src/operators/sh.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@ The shell defaults to `/bin/sh`. If an alternate shell such as `zsh` is desired,
+step1:
sh>: tasks/step2.sh

On Windows, you can set PowerShell.exe to the `shell` option:
On Windows, you can set PowerShell.exe to the `shell` option.
Since ver.0.10.6, it is correct to specify ["powershell.exe"],
but digdag works with ["powershell.exe", "-"] in ver.0.9.46
or earlier.

>= 0.10.6
_export:
sh:
shell: ["powershell.exe"]

+step1:
sh>: step1.exe

+step2:
sh>: step2.ps1

<= 0.9.46

_export:
sh:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,41 @@ private void runCode(final Config state)
}
}

private List<String> getShell(final Config params, final Boolean winOS){
// Until digdag ver. 0.9, it was necessary to write ["powershell.exe","-"] to specify shell,
// but since ver. 0.10, jobs are not executed unless ["powershell.exe"] is written.
// To resolve this incompatibility, change the shell specification ["powershell.exe","-"]
// to ["powershell.exe"].
List<String> temp_shell;
if (params.has("shell")) {
temp_shell = params.getListOrEmpty("shell", String.class);
if (temp_shell.get(0).toLowerCase().contains("powershell") && temp_shell.size() == 2){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains("powershell") will match to strings like 'powershell-batch.bat`. Is it intentional ?

if (temp_shell.get(1).contains("-")){
Copy link
Contributor

@yoyama yoyama Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto. It will match like -option or abc-def.txt.

temp_shell.remove(1);
logger.info("removed \"-\" from shell specification --- from [\"powershell.exe\",\"-\"] to [\"powershell.exe\"] .");
}
}
}
else {
temp_shell = ImmutableList.of(winOS ? "PowerShell.exe" : "/bin/sh");
}
return temp_shell;
}

private CommandStatus runCommand(final Config params, final CommandContext commandContext)
throws IOException, InterruptedException
{
final Path tempDir = workspace.createTempDir(String.format("digdag-sh-%d-", request.getTaskId()));
final Path workingDirectory = workspace.getPath(); // absolute
final Path runnerPath = tempDir.resolve("runner.sh"); // absolute
final Boolean winOS = isWindowsPlatform();
final Path runnerPath = tempDir.resolve(winOS ? "runner.ps1" : "runner.sh"); // absolute

final List<String> shell;
if (params.has("shell")) {
shell = params.getListOrEmpty("shell", String.class);
}
else {
shell = ImmutableList.of("/bin/sh");
}
final List<String> shell = getShell(params, winOS);

final ImmutableList.Builder<String> cmdline = ImmutableList.builder();
if (params.has("shell")) {
cmdline.addAll(shell);
}
else {
cmdline.addAll(shell);
}

cmdline.addAll(shell);

cmdline.add(workingDirectory.relativize(runnerPath).toString()); // relative

final String shScript = UserSecretTemplate.of(params.get("_command", String.class))
Expand Down Expand Up @@ -223,5 +236,14 @@ private CommandRequest buildCommandRequest(final CommandContext commandContext,
.ioDirectory(ioDirectory)
.build();
}

private boolean isWindowsPlatform()
{
final String os = System.getProperty("os.name").toLowerCase();
if ( os.startsWith("windows")) {
return true;
}
return false;
}
}
}