Skip to content

Commit

Permalink
#6 - Support Unix OS - update TC ref to 10.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
nik authored and nik committed Nov 18, 2016
1 parent 77b0706 commit bdc2e4a
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 61 deletions.
18 changes: 13 additions & 5 deletions cmd/runAs.sh
Original file line number Diff line number Diff line change
@@ -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
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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Settings> mySettingsGenerator;
private final ResourceGenerator<RunAsCmdSettings> 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<Settings> settingsGenerator,
@NotNull final ResourceGenerator<RunAsCmdSettings> 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;
}

Expand All @@ -55,7 +61,7 @@ public Iterable<CommandLineSetup> 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<CommandLineArgument> cmdLineArgs = new ArrayList<CommandLineArgument>();
Expand All @@ -64,22 +70,23 @@ public Iterable<CommandLineSetup> 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));
}

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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
<bean class="jetbrains.buildServer.runAs.agent.CommandLineExecutorImpl" />
<bean class="jetbrains.buildServer.runAs.agent.RunAsPropertiesExtension" />
<bean class="jetbrains.buildServer.runAs.agent.RunAsToolProvider" />
<bean class="jetbrains.buildServer.runAs.agent.SettingsPublisher" id="settingsPublisher"/>
<bean class="jetbrains.buildServer.runAs.agent.FileAccessServiceImpl"/>
<bean class="jetbrains.buildServer.dotNet.buildRunner.agent.BeforeBuildPublisher" id="beforeBuildPublisher"/>
<bean class="jetbrains.buildServer.runAs.agent.ExecutableFilePublisher" id="executableFilePublisher">
<constructor-arg ref="beforeBuildPublisher"/>
</bean>
<bean class="jetbrains.buildServer.runAs.agent.SettingsProviderImpl" />

<!-- Windows -->
<bean class="jetbrains.buildServer.runAs.agent.RunAsCmdGenerator" id="runAsCmdGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.WindowsSettingsGenerator" id="windowsSettingsGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.RunAsPlatformSpecificSetupBuilder" id="runAsWindowsSetupBuilder">
<constructor-arg ref="settingsPublisher"/>
<constructor-arg ref="beforeBuildPublisher"/>
<constructor-arg ref="executableFilePublisher"/>
<constructor-arg ref="windowsSettingsGenerator"/>
<constructor-arg ref="runAsCmdGenerator"/>
<constructor-arg type="java.lang.String" value=".cmd"/>
Expand All @@ -25,7 +30,7 @@
<bean class="jetbrains.buildServer.runAs.agent.RunAsShGenerator" id="runAsShGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.UnixSettingsGenerator" id="unixSettingsGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.RunAsPlatformSpecificSetupBuilder" id="runAsUnixSetupBuilder">
<constructor-arg ref="settingsPublisher"/>
<constructor-arg ref="beforeBuildPublisher"/>
<constructor-arg ref="unixSettingsGenerator"/>
<constructor-arg ref="runAsShGenerator"/>
<constructor-arg type="java.lang.String" value=".sh"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@
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()
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ public class RunAsPlatformSpecificSetupBuilderTest {
private Mockery myCtx;
private FileService myFileService;
private RunnerParametersService myRunnerParametersService;
private ResourcePublisher myResourcePublisher;
private ResourcePublisher myBeforeBuildPublisher;
private ResourceGenerator<Settings> myCredentialsGenerator;
private CommandLineResource myCommandLineResource1;
private CommandLineResource myCommandLineResource2;
private ResourceGenerator<RunAsCmdSettings> myArgsGenerator;
private CommandLineArgumentsService myCommandLineArgumentsService;
private SettingsProvider mySettingsProvider;
private ResourcePublisher myExecutableFilePublisher;
private FileAccessService myFileAccessService;

@BeforeMethod
public void setUp()
Expand All @@ -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<Settings>)myCtx.mock(ResourceGenerator.class, "WindowsSettingsGenerator");
//noinspection unchecked
Expand All @@ -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()
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -146,10 +150,12 @@ private CommandLineSetupBuilder createInstance()
mySettingsProvider,
myRunnerParametersService,
myFileService,
myResourcePublisher,
myBeforeBuildPublisher,
myExecutableFilePublisher,
myCredentialsGenerator,
myArgsGenerator,
myCommandLineArgumentsService,
myFileAccessService,
".abc");
}
}
2 changes: 1 addition & 1 deletion runAs-agent/src/test/resources/testng-runAs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<classes>
<class name="jetbrains.buildServer.runAs.agent.WindowsWindowsSettingsGeneratorTest"/>
<class name="jetbrains.buildServer.runAs.agent.RunAsCmdGeneratorTest"/>
<class name="jetbrains.buildServer.runAs.agent.SettingsPublisherTest"/>
<class name="jetbrains.buildServer.runAs.agent.ExecutableFilePublisherTest"/>
<class name="jetbrains.buildServer.runAs.agent.RunAsPlatformSpecificSetupBuilderTest"/>
<class name="jetbrains.buildServer.runAs.agent.RunAsToolProviderTest"/>
<class name="jetbrains.buildServer.runAs.agent.SettingsProviderTest"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
<props:passwordProperty name="${bean.runAsPasswordKey}" className="longField"/>
</div>
<span class="error" id="error_${bean.runAsPasswordKey}"></span>
<span class="smallNote">Specify the user's password for agents under the Windows OS and current user's password for Linux.</span>
</td>
</tr>

0 comments on commit bdc2e4a

Please sign in to comment.