use of io.fabric8.openshift.api.model.Project in project docker-maven-plugin by fabric8io.
the class BuildService method buildImage.
/**
* Build an image
*
* @param imageConfig the image configuration
* @param params mojo params for the project
* @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
* @param buildArgs
* @throws DockerAccessException
* @throws MojoExecutionException
*/
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs) throws DockerAccessException, MojoExecutionException {
String imageName = imageConfig.getName();
ImageName.validate(imageName);
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
String oldImageId = null;
CleanupMode cleanupMode = buildConfig.cleanupMode();
if (cleanupMode.isRemove()) {
oldImageId = queryService.getImageId(imageName);
}
long time = System.currentTimeMillis();
if (buildConfig.getDockerArchive() != null) {
docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params));
log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
return;
}
File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));
Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
// auto is now supported by docker, consider switching?
BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).buildArgs(mergedBuildMap);
String newImageId = doBuildImage(imageName, dockerArchive, opts);
log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
if (oldImageId != null && !oldImageId.equals(newImageId)) {
try {
docker.removeImage(oldImageId, true);
log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
} catch (DockerAccessException exp) {
if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(), (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
} else {
throw exp;
}
}
}
}
use of io.fabric8.openshift.api.model.Project in project docker-maven-plugin by fabric8io.
the class DockerAssemblyManagerTest method assemblyFiles.
@Test
public void assemblyFiles(@Injectable final MojoParameters mojoParams, @Injectable final MavenProject project, @Injectable final Assembly assembly) throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException, MojoExecutionException, AssemblyReadException, IllegalAccessException {
ReflectionUtils.setVariableValueInObject(assemblyManager, "trackArchiver", trackArchiver);
new Expectations() {
{
mojoParams.getOutputDirectory();
result = "target/";
times = 3;
mojoParams.getProject();
project.getBasedir();
result = ".";
assemblyReader.readAssemblies((AssemblerConfigurationSource) any);
result = Arrays.asList(assembly);
}
};
BuildImageConfiguration buildConfig = createBuildConfig();
assemblyManager.getAssemblyFiles("testImage", buildConfig, mojoParams, new AnsiLogger(new SystemStreamLog(), true, true));
}
use of io.fabric8.openshift.api.model.Project in project docker-maven-plugin by fabric8io.
the class DockerAssemblyManagerTest method mockMojoParams.
private MojoParameters mockMojoParams(MavenProject project) {
Settings settings = new Settings();
ArtifactRepository localRepository = new MockUp<ArtifactRepository>() {
@Mock
public String getBasedir() {
return "repository";
}
}.getMockInstance();
@SuppressWarnings("deprecation") MavenSession session = new MavenSession(null, settings, localRepository, null, null, Collections.<String>emptyList(), ".", null, null, new Date());
return new MojoParameters(session, project, null, null, null, settings, "src", "target", Collections.singletonList(project));
}
use of io.fabric8.openshift.api.model.Project in project docker-maven-plugin by fabric8io.
the class AbstractDockerMojo method execute.
/**
* Entry point for this plugin. It will set up the helper class and then calls
* {@link #executeInternal(ServiceHub)}
* which must be implemented by subclass.
*
* @throws MojoExecutionException
* @throws MojoFailureException
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!skip) {
log = new AnsiLogger(getLog(), useColor, verbose, !settings.getInteractiveMode(), getLogPrefix());
authConfigFactory.setLog(log);
LogOutputSpecFactory logSpecFactory = new LogOutputSpecFactory(useColor, logStdout, logDate);
ConfigHelper.validateExternalPropertyActivation(project, images);
// The 'real' images configuration to use (configured images + externally resolved images)
this.minimalApiVersion = initImageConfiguration(getBuildTimestamp());
DockerAccess access = null;
try {
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
access = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
ServiceHub serviceHub = serviceHubFactory.createServiceHub(project, session, access, log, logSpecFactory);
executeInternal(serviceHub);
} catch (DockerAccessException | ExecException exp) {
logException(exp);
throw new MojoExecutionException(log.errorMessage(exp.getMessage()), exp);
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
} finally {
if (access != null) {
access.shutdown();
}
}
}
}
use of io.fabric8.openshift.api.model.Project in project fabric8 by jboss-fuse.
the class OpenShiftPomDeployerTest method doTest.
protected void doTest(String folder, String[] artifactUrls, String[] repoUrls, String expectedCamelDependencyScope, String expectedHawtioDependencyScope) throws Exception {
File sourceDir = new File(baseDir, "src/test/resources/" + folder);
assertDirectoryExists(sourceDir);
File pomSource = new File(sourceDir, "pom.xml");
assertFileExists(pomSource);
File outputDir = new File(baseDir, "target/" + getClass().getName() + "/" + folder);
outputDir.mkdirs();
assertDirectoryExists(outputDir);
File pom = new File(outputDir, "pom.xml");
Files.copy(pomSource, pom);
assertFileExists(pom);
git = Git.init().setDirectory(outputDir).setGitDir(new File(outputDir, ".git")).call();
assertDirectoryExists(new File(outputDir, ".git"));
git.add().addFilepattern("pom.xml").call();
git.commit().setMessage("Initial import").call();
// now we have the git repo setup; lets run the update
OpenShiftPomDeployer deployer = new OpenShiftPomDeployer(git, outputDir, deployDir, webAppDir);
System.out.println("About to update the pom " + pom + " with artifacts: " + Arrays.asList(artifactUrls));
List<Parser> artifacts = new ArrayList<Parser>();
for (String artifactUrl : artifactUrls) {
artifacts.add(Parser.parsePathWithSchemePrefix(artifactUrl));
}
List<MavenRepositoryURL> repos = new ArrayList<MavenRepositoryURL>();
for (String repoUrl : repoUrls) {
repos.add(new MavenRepositoryURL(repoUrl));
}
deployer.update(artifacts, repos);
System.out.println("Completed the new pom is: ");
System.out.println(Files.toString(pom));
Document xml = XmlUtils.parseDoc(pom);
Element plugins = assertXPathElement(xml, "project/profiles/profile[id = 'openshift']/build/plugins");
Element cleanExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-clean-plugin']/executions/execution[id = 'fuse-fabric-clean']");
Element dependencySharedExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-dependency-plugin']/executions/execution[id = 'fuse-fabric-deploy-shared']");
Element dependencyWebAppsExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-dependency-plugin']/executions/execution[id = 'fuse-fabric-deploy-webapps']");
Element warPluginWarName = xpath("plugin[artifactId = 'maven-war-plugin']/configuration/warName").element(plugins);
if (warPluginWarName != null) {
String warName = warPluginWarName.getTextContent();
System.out.println("WarName is now: " + warName);
assertTrue("Should not have ROOT war name", !"ROOT".equals(warName));
}
Element dependencies = assertXPathElement(xml, "project/dependencies");
Element repositories = assertXPathElement(xml, "project/repositories");
for (Parser artifact : artifacts) {
// lets check there's only 1 dependency for group & artifact and it has the right version
String group = groupId(artifact);
String artifactId = artifact.getArtifact();
Element dependency = assertSingleDependencyForGroupAndArtifact(dependencies, group, artifactId);
Element version = assertXPathElement(dependency, "version");
assertEquals("Version", artifact.getVersion(), version.getTextContent());
}
// lets check we either preserve scope, add provided or don't add a scope if there's none present in the underlying pom
assertDependencyScope(dependencies, "org.apache.camel", "camel-core", expectedCamelDependencyScope);
assertDependencyScope(dependencies, "org.drools", "drools-wb-distribution-wars", "provided");
assertDependencyScope(dependencies, "io.hawt", "hawtio-web", expectedHawtioDependencyScope);
assertRepositoryUrl(repositories, "https://maven.repository.redhat.com/ga/");
assertRepositoryUrl(repositories, "https://repo.fusesource.com/nexus/content/groups/ea/");
}
Aggregations