use of com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger in project jib by google.
the class ContainerBuilders method create.
/**
* Creates a {@link JibContainerBuilder} depending on the base image specified.
*
* @param baseImageReference base image reference
* @param platforms platforms for multi-platform support in build command
* @param commonCliOptions common cli options
* @param logger console logger
* @return a {@link JibContainerBuilder}
* @throws InvalidImageReferenceException if the baseImage reference cannot be parsed
* @throws FileNotFoundException if credential helper file cannot be found
*/
public static JibContainerBuilder create(String baseImageReference, Set<Platform> platforms, CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
if (baseImageReference.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
return Jib.from(DockerDaemonImage.named(baseImageReference.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
}
if (baseImageReference.startsWith(TAR_IMAGE_PREFIX)) {
return Jib.from(TarImage.at(Paths.get(baseImageReference.replaceFirst(TAR_IMAGE_PREFIX, ""))));
}
ImageReference imageReference = ImageReference.parse(baseImageReference.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
RegistryImage registryImage = RegistryImage.named(imageReference);
DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
Credentials.getFromCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
JibContainerBuilder containerBuilder = Jib.from(registryImage);
if (!platforms.isEmpty()) {
containerBuilder.setPlatforms(platforms);
}
return containerBuilder;
}
use of com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger in project jib by google.
the class Containerizers method create.
private static Containerizer create(CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
String imageSpec = commonCliOptions.getTargetImage();
if (imageSpec.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
// TODO: allow setting docker env and docker executable (along with path/env)
return Containerizer.to(DockerDaemonImage.named(imageSpec.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
}
if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
return Containerizer.to(TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, ""))).named(commonCliOptions.getName()));
}
ImageReference imageReference = ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
RegistryImage registryImage = RegistryImage.named(imageReference);
DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
Credentials.getToCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
return Containerizer.to(registryImage);
}
use of com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger in project jib by google.
the class Jar method call.
@Override
public Integer call() {
commonCliOptions.validate();
SingleThreadedExecutor executor = new SingleThreadedExecutor();
ConsoleLogger logger = CliLogger.newLogger(commonCliOptions.getVerbosity(), commonCliOptions.getHttpTrace(), commonCliOptions.getConsoleOutput(), spec.commandLine().getOut(), spec.commandLine().getErr(), executor);
Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
try {
JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
GlobalConfig globalConfig = GlobalConfig.readConfig();
updateCheckFuture = JibCli.newUpdateChecker(globalConfig, commonCliOptions.getVerbosity(), logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage()));
if (!Files.exists(jarFile)) {
logger.log(LogEvent.Level.ERROR, "The file path provided does not exist: " + jarFile);
return 1;
}
if (Files.isDirectory(jarFile)) {
logger.log(LogEvent.Level.ERROR, "The file path provided is for a directory. Please provide a path to a JAR: " + jarFile);
return 1;
}
if (!commonContainerConfigCliOptions.getEntrypoint().isEmpty() && !jvmFlags.isEmpty()) {
logger.log(LogEvent.Level.WARN, "--jvm-flags is ignored when --entrypoint is specified");
}
Path jarFileParentDir = Verify.verifyNotNull(jarFile.toAbsolutePath().getParent());
CacheDirectories cacheDirectories = CacheDirectories.from(commonCliOptions, jarFileParentDir);
ArtifactProcessor processor = ArtifactProcessors.fromJar(jarFile, cacheDirectories, this, commonContainerConfigCliOptions);
JibContainerBuilder containerBuilder = JarFiles.toJibContainerBuilder(processor, this, commonCliOptions, commonContainerConfigCliOptions, logger);
Containerizer containerizer = Containerizers.from(commonCliOptions, logger, cacheDirectories);
// Enable registry mirrors
Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
JibContainer jibContainer = containerBuilder.containerize(containerizer);
JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
} catch (InterruptedException ex) {
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
Thread.currentThread().interrupt();
return 1;
} catch (Exception ex) {
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
return 1;
} finally {
JibCli.finishUpdateChecker(logger, updateCheckFuture);
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
}
return 0;
}
use of com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger in project jib by GoogleContainerTools.
the class CliLoggerTest method createLoggerAndSendMessages.
private void createLoggerAndSendMessages(Verbosity verbosity, ConsoleOutput consoleOutput) {
SingleThreadedExecutor executor = new SingleThreadedExecutor();
ConsoleLogger logger = CliLogger.newLogger(verbosity, HttpTraceLevel.off, consoleOutput, mockOut, mockErr, executor);
logger.log(Level.DEBUG, "debug");
logger.log(Level.INFO, "info");
logger.log(Level.LIFECYCLE, "lifecycle");
logger.log(Level.PROGRESS, "progress");
logger.log(Level.WARN, "warn");
logger.log(Level.ERROR, "error");
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
}
use of com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger in project jib by GoogleContainerTools.
the class Build method call.
@Override
public Integer call() {
commonCliOptions.validate();
Path buildFile = getBuildFile();
SingleThreadedExecutor executor = new SingleThreadedExecutor();
ConsoleLogger logger = CliLogger.newLogger(commonCliOptions.getVerbosity(), commonCliOptions.getHttpTrace(), commonCliOptions.getConsoleOutput(), spec.commandLine().getOut(), spec.commandLine().getErr(), executor);
Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
try {
JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
GlobalConfig globalConfig = GlobalConfig.readConfig();
updateCheckFuture = JibCli.newUpdateChecker(globalConfig, commonCliOptions.getVerbosity(), logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage()));
if (!Files.isReadable(buildFile)) {
logger.log(LogEvent.Level.ERROR, "The Build File YAML either does not exist or cannot be opened for reading: " + buildFile);
return 1;
}
if (!Files.isRegularFile(buildFile)) {
logger.log(LogEvent.Level.ERROR, "Build File YAML path is a not a file: " + buildFile);
return 1;
}
CacheDirectories cacheDirectories = CacheDirectories.from(commonCliOptions, contextRoot);
Containerizer containerizer = Containerizers.from(commonCliOptions, logger, cacheDirectories);
JibContainerBuilder containerBuilder = BuildFiles.toJibContainerBuilder(contextRoot, buildFile, this, commonCliOptions, logger);
// Enable registry mirrors
Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
JibContainer jibContainer = containerBuilder.containerize(containerizer);
JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
} catch (InterruptedException ex) {
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
Thread.currentThread().interrupt();
return 1;
} catch (Exception ex) {
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
return 1;
} finally {
JibCli.finishUpdateChecker(logger, updateCheckFuture);
executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
}
return 0;
}
Aggregations