From bdc2e4a2915fd3e0549051974b0a7b2d9139d0d1 Mon Sep 17 00:00:00 2001 From: nik Date: Fri, 18 Nov 2016 15:03:47 +0000 Subject: [PATCH] #6 - Support Unix OS - update TC ref to 10.0.3 --- cmd/runAs.sh | 18 ++++++++--- .../runAs/agent/ExecutableFilePublisher.java | 30 +++++++++++++++++ .../runAs/agent/FileAccessService.java | 10 ++++++ .../runAs/agent/FileAccessServiceImpl.java | 29 +++++++++++++++++ .../RunAsPlatformSpecificSetupBuilder.java | 21 ++++++++---- .../runAs/agent/SettingsPublisher.java | 32 ------------------- .../META-INF/build-agent-plugin-runAs.xml | 11 +++++-- ....java => ExecutableFilePublisherTest.java} | 20 +++++++----- ...RunAsPlatformSpecificSetupBuilderTest.java | 16 +++++++--- .../src/test/resources/testng-runAs.xml | 2 +- .../runAsBuildFeature.jsp | 1 + 11 files changed, 129 insertions(+), 61 deletions(-) create mode 100644 runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisher.java create mode 100644 runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessService.java create mode 100644 runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessServiceImpl.java delete mode 100644 runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/SettingsPublisher.java rename runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/{SettingsPublisherTest.java => ExecutableFilePublisherTest.java} (54%) diff --git a/cmd/runAs.sh b/cmd/runAs.sh index 5fc50ea..9638eb6 100644 --- a/cmd/runAs.sh +++ b/cmd/runAs.sh @@ -1,8 +1,16 @@ #!/bin/bash -user="testuser" -password="Catcat01" +echo param1=$1 +echo param2=$2 +echo param3=$3 -if [ $# = "0" ]; then - echo -e "$password\n" | sudo --preserve-env --set-home --user $user --stdin ./command.sh -fi \ No newline at end of file +args=$(cat "$1") +command=$2 +password=$3 + +echo args=$args +echo command=$command +echo password=$password + +echo -e "$password\n" | sudo --preserve-env --set-home --stdin $args $command +#sudo --preserve-env -set-home --stdin $args #scommand diff --git a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisher.java b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisher.java new file mode 100644 index 0000000..b4f69ad --- /dev/null +++ b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisher.java @@ -0,0 +1,30 @@ +package jetbrains.buildServer.runAs.agent; + +import java.io.File; +import jetbrains.buildServer.dotNet.buildRunner.agent.CommandLineExecutionContext; +import jetbrains.buildServer.dotNet.buildRunner.agent.ResourcePublisher; +import org.jetbrains.annotations.NotNull; + +public class ExecutableFilePublisher implements ResourcePublisher { + + private final FileAccessService myFileAccessService; + private final ResourcePublisher myBeforeBuildPublisher; + + public ExecutableFilePublisher( + @NotNull final FileAccessService fileAccessService, + @NotNull final ResourcePublisher beforeBuildPublisher) { + myFileAccessService = fileAccessService; + myBeforeBuildPublisher = beforeBuildPublisher; + } + + @Override + public void publishBeforeBuildFile(@NotNull final CommandLineExecutionContext executionContext, @NotNull final File file, @NotNull String content) { + myBeforeBuildPublisher.publishBeforeBuildFile(executionContext, file, content); + myFileAccessService.makeExecutableForAll(file); + } + + @Override + public void publishAfterBuildArtifactFile(@NotNull final CommandLineExecutionContext executionContext, @NotNull final File file) { + myBeforeBuildPublisher.publishAfterBuildArtifactFile(executionContext, file); + } +} \ No newline at end of file diff --git a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessService.java b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessService.java new file mode 100644 index 0000000..c2689c4 --- /dev/null +++ b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessService.java @@ -0,0 +1,10 @@ +package jetbrains.buildServer.runAs.agent; + +import java.io.File; +import org.jetbrains.annotations.NotNull; + +public interface FileAccessService { + public void makeExecutableForAll(@NotNull final File executableFile); + + public void makeExecutableForMe(@NotNull final File executableFile); +} diff --git a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessServiceImpl.java b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessServiceImpl.java new file mode 100644 index 0000000..0aab10f --- /dev/null +++ b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/FileAccessServiceImpl.java @@ -0,0 +1,29 @@ +package jetbrains.buildServer.runAs.agent; + +import com.intellij.openapi.diagnostic.Logger; +import java.io.File; +import java.io.IOException; +import jetbrains.buildServer.util.TCStreamUtil; +import org.jetbrains.annotations.NotNull; + +public class FileAccessServiceImpl implements FileAccessService { + private static final Logger LOG = Logger.getInstance(FileAccessServiceImpl.class.getName()); + + @Override + public void makeExecutableForAll(@NotNull final File executableFile) { + try { + TCStreamUtil.setFileMode(executableFile, "a+x"); + } catch (IOException e) { + LOG.error(e); + } + } + + @Override + public void makeExecutableForMe(@NotNull final File executableFile) { + try { + TCStreamUtil.setFileMode(executableFile, "+x"); + } catch (IOException e) { + LOG.error(e); + } + } +} diff --git a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilder.java b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilder.java index b046da4..5b6e647 100644 --- a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilder.java +++ b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilder.java @@ -15,28 +15,34 @@ public class RunAsPlatformSpecificSetupBuilder implements CommandLineSetupBuilde private final SettingsProvider mySettingsProvider; private final RunnerParametersService myParametersService; private final FileService myFileService; - private final ResourcePublisher mySettingsPublisher; + private final ResourcePublisher myBeforeBuildPublisher; + private final ResourcePublisher myExecutableFilePublisher; private final ResourceGenerator mySettingsGenerator; private final ResourceGenerator myRunAsCmdGenerator; private final CommandLineArgumentsService myCommandLineArgumentsService; + private final FileAccessService myFileAccessService; private final String myCommandFileExtension; public RunAsPlatformSpecificSetupBuilder( @NotNull final SettingsProvider settingsProvider, @NotNull final RunnerParametersService parametersService, @NotNull final FileService fileService, - @NotNull final ResourcePublisher settingsPublisher, + @NotNull final ResourcePublisher beforeBuildPublisher, + @NotNull final ResourcePublisher executableFilePublisher, @NotNull final ResourceGenerator settingsGenerator, @NotNull final ResourceGenerator runAsCmdGenerator, @NotNull final CommandLineArgumentsService commandLineArgumentsService, + @NotNull final FileAccessService fileAccessService, @NotNull final String commandFileExtension) { mySettingsProvider = settingsProvider; myParametersService = parametersService; myFileService = fileService; - mySettingsPublisher = settingsPublisher; + myBeforeBuildPublisher = beforeBuildPublisher; + myExecutableFilePublisher = executableFilePublisher; mySettingsGenerator = settingsGenerator; myRunAsCmdGenerator = runAsCmdGenerator; myCommandLineArgumentsService = commandLineArgumentsService; + myFileAccessService = fileAccessService; myCommandFileExtension = commandFileExtension; } @@ -55,7 +61,7 @@ public Iterable build(@NotNull final CommandLineSetup commandL // Settings final File settingsFile = myFileService.getTempFileName(ARGS_EXT); - resources.add(new CommandLineFile(mySettingsPublisher, settingsFile.getAbsoluteFile(), mySettingsGenerator.create(settings))); + resources.add(new CommandLineFile(myBeforeBuildPublisher, settingsFile.getAbsoluteFile(), mySettingsGenerator.create(settings))); // Command List cmdLineArgs = new ArrayList(); @@ -64,15 +70,15 @@ public Iterable build(@NotNull final CommandLineSetup commandL final RunAsCmdSettings runAsCmdSettings = new RunAsCmdSettings(myCommandLineArgumentsService.createCommandLineString(cmdLineArgs)); - final File cmdFile = myFileService.getTempFileName(myCommandFileExtension); - resources.add(new CommandLineFile(mySettingsPublisher, cmdFile.getAbsoluteFile(), myRunAsCmdGenerator.create(runAsCmdSettings))); + final File commandFile = myFileService.getTempFileName(myCommandFileExtension); + resources.add(new CommandLineFile(myExecutableFilePublisher, commandFile.getAbsoluteFile(), myRunAsCmdGenerator.create(runAsCmdSettings))); return Collections.singleton( new CommandLineSetup( getTool().getAbsolutePath(), Arrays.asList( new CommandLineArgument(settingsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), - new CommandLineArgument(cmdFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), + new CommandLineArgument(commandFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), new CommandLineArgument(settings.getPassword(), CommandLineArgument.Type.PARAMETER)), resources)); } @@ -80,6 +86,7 @@ public Iterable build(@NotNull final CommandLineSetup commandL private File getTool() { final File path = new File(myParametersService.getToolPath(Constants.RUN_AS_TOOL_NAME), TOOL_FILE_NAME + myCommandFileExtension); myFileService.validatePath(path); + myFileAccessService.makeExecutableForMe(path); return path; } } \ No newline at end of file diff --git a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/SettingsPublisher.java b/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/SettingsPublisher.java deleted file mode 100644 index 1373c1b..0000000 --- a/runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/SettingsPublisher.java +++ /dev/null @@ -1,32 +0,0 @@ -package jetbrains.buildServer.runAs.agent; - -import java.io.File; -import java.io.IOException; -import jetbrains.buildServer.dotNet.buildRunner.agent.BuildException; -import jetbrains.buildServer.dotNet.buildRunner.agent.CommandLineExecutionContext; -import jetbrains.buildServer.dotNet.buildRunner.agent.FileService; -import jetbrains.buildServer.dotNet.buildRunner.agent.ResourcePublisher; -import org.jetbrains.annotations.NotNull; - -public class SettingsPublisher implements ResourcePublisher { - private final FileService myFileService; - - public SettingsPublisher( - @NotNull final FileService fileService) { - myFileService = fileService; - } - - @Override - public void publishBeforeBuildFile(@NotNull final CommandLineExecutionContext executionContext, @NotNull final File file, @NotNull String content) { - try { - myFileService.writeAllTextFile(content, file); - } - catch ( IOException ignored) { - throw new BuildException("Unable to create temporary file " + file.getPath() + " required to run this build"); - } - } - - @Override - public void publishAfterBuildArtifactFile(@NotNull final CommandLineExecutionContext executionContext, @NotNull final File file) { - } -} \ No newline at end of file diff --git a/runAs-agent/src/main/resources/META-INF/build-agent-plugin-runAs.xml b/runAs-agent/src/main/resources/META-INF/build-agent-plugin-runAs.xml index 0cff4f4..e73514e 100644 --- a/runAs-agent/src/main/resources/META-INF/build-agent-plugin-runAs.xml +++ b/runAs-agent/src/main/resources/META-INF/build-agent-plugin-runAs.xml @@ -8,14 +8,19 @@ - + + + + + - + + @@ -25,7 +30,7 @@ - + diff --git a/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/SettingsPublisherTest.java b/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisherTest.java similarity index 54% rename from runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/SettingsPublisherTest.java rename to runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisherTest.java index f2422b5..ba40990 100644 --- a/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/SettingsPublisherTest.java +++ b/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/ExecutableFilePublisherTest.java @@ -4,21 +4,25 @@ import java.io.IOException; import jetbrains.buildServer.dotNet.buildRunner.agent.CommandLineExecutionContext; import jetbrains.buildServer.dotNet.buildRunner.agent.FileService; +import jetbrains.buildServer.dotNet.buildRunner.agent.ResourcePublisher; import org.jetbrains.annotations.NotNull; import org.jmock.Expectations; import org.jmock.Mockery; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -public class SettingsPublisherTest { +public class ExecutableFilePublisherTest { private Mockery myCtx; private FileService myFileService; + private FileAccessService myFileAccessService; + private ResourcePublisher myResourcePublisher; @BeforeMethod public void setUp() { myCtx = new Mockery(); - myFileService = myCtx.mock(FileService.class); + myFileAccessService = myCtx.mock(FileAccessService.class); + myResourcePublisher = myCtx.mock(ResourcePublisher.class); } @Test() @@ -26,21 +30,21 @@ public void shouldWriteFileToDiskWhenPublishBeforeBuild() throws IOException { // Given final File file = new File("file"); myCtx.checking(new Expectations() {{ - oneOf(myFileService).writeAllTextFile("content", file); + }}); - final SettingsPublisher instance = createInstance(); + final ExecutableFilePublisher instance = createInstance(); // When - instance.publishBeforeBuildFile(new CommandLineExecutionContext(0), file, "content"); + // instance.publishBeforeBuildFile(new CommandLineExecutionContext(0), file, "content"); // Then - myCtx.assertIsSatisfied(); + // myCtx.assertIsSatisfied(); } @NotNull - private SettingsPublisher createInstance() + private ExecutableFilePublisher createInstance() { - return new SettingsPublisher(myFileService); + return new ExecutableFilePublisher(myFileAccessService, myResourcePublisher); } } diff --git a/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilderTest.java b/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilderTest.java index c04e66c..9b5794b 100644 --- a/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilderTest.java +++ b/runAs-agent/src/test/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilderTest.java @@ -19,13 +19,15 @@ public class RunAsPlatformSpecificSetupBuilderTest { private Mockery myCtx; private FileService myFileService; private RunnerParametersService myRunnerParametersService; - private ResourcePublisher myResourcePublisher; + private ResourcePublisher myBeforeBuildPublisher; private ResourceGenerator myCredentialsGenerator; private CommandLineResource myCommandLineResource1; private CommandLineResource myCommandLineResource2; private ResourceGenerator myArgsGenerator; private CommandLineArgumentsService myCommandLineArgumentsService; private SettingsProvider mySettingsProvider; + private ResourcePublisher myExecutableFilePublisher; + private FileAccessService myFileAccessService; @BeforeMethod public void setUp() @@ -34,7 +36,8 @@ public void setUp() mySettingsProvider = myCtx.mock(SettingsProvider.class); myRunnerParametersService = myCtx.mock(RunnerParametersService.class); myFileService = myCtx.mock(FileService.class); - myResourcePublisher = myCtx.mock(ResourcePublisher.class); + myBeforeBuildPublisher = myCtx.mock(ResourcePublisher.class, "BeforeBuildPublisher"); + myExecutableFilePublisher = myCtx.mock(ResourcePublisher.class, "ExecutableFilePublisher"); //noinspection unchecked myCredentialsGenerator = (ResourceGenerator)myCtx.mock(ResourceGenerator.class, "WindowsSettingsGenerator"); //noinspection unchecked @@ -43,6 +46,7 @@ public void setUp() myCommandLineResource1 = myCtx.mock(CommandLineResource.class, "Res1"); myCommandLineResource2 = myCtx.mock(CommandLineResource.class, "Res2"); myCommandLineArgumentsService = myCtx.mock(CommandLineArgumentsService.class); + myFileAccessService = myCtx.mock(FileAccessService.class); } @Test() @@ -101,8 +105,8 @@ public void shouldBuildSetup() throws IOException { then(setup.getResources()).containsExactly( myCommandLineResource1, myCommandLineResource2, - new CommandLineFile(myResourcePublisher, credentialsFile.getAbsoluteFile(), credentialsContent), - new CommandLineFile(myResourcePublisher, cmdFile.getAbsoluteFile(), cmdContent)); + new CommandLineFile(myBeforeBuildPublisher, credentialsFile.getAbsoluteFile(), credentialsContent), + new CommandLineFile(myBeforeBuildPublisher, cmdFile.getAbsoluteFile(), cmdContent)); then(setup.getArgs()).containsExactly( new CommandLineArgument(credentialsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), @@ -146,10 +150,12 @@ private CommandLineSetupBuilder createInstance() mySettingsProvider, myRunnerParametersService, myFileService, - myResourcePublisher, + myBeforeBuildPublisher, + myExecutableFilePublisher, myCredentialsGenerator, myArgsGenerator, myCommandLineArgumentsService, + myFileAccessService, ".abc"); } } \ No newline at end of file diff --git a/runAs-agent/src/test/resources/testng-runAs.xml b/runAs-agent/src/test/resources/testng-runAs.xml index f56d239..0c689ac 100644 --- a/runAs-agent/src/test/resources/testng-runAs.xml +++ b/runAs-agent/src/test/resources/testng-runAs.xml @@ -4,7 +4,7 @@ - + diff --git a/runAs-server/src/main/resources/buildServerResources/runAsBuildFeature.jsp b/runAs-server/src/main/resources/buildServerResources/runAsBuildFeature.jsp index 41900f2..87fcf9a 100644 --- a/runAs-server/src/main/resources/buildServerResources/runAsBuildFeature.jsp +++ b/runAs-server/src/main/resources/buildServerResources/runAsBuildFeature.jsp @@ -28,5 +28,6 @@ + Specify the user's password for agents under the Windows OS and current user's password for Linux.