Search in sources :

Example 6 with FailoverHttpClient

use of com.google.cloud.tools.jib.http.FailoverHttpClient in project jib by GoogleContainerTools.

the class UpdateChecker method performUpdateCheck.

@VisibleForTesting
static Optional<String> performUpdateCheck(Path configDir, String currentVersion, String versionUrl, String toolName, Consumer<LogEvent> log) {
    Path lastUpdateCheck = configDir.resolve(LAST_UPDATE_CHECK_FILENAME);
    try {
        // Check time of last update check
        if (Files.exists(lastUpdateCheck)) {
            try {
                String fileContents = new String(Files.readAllBytes(lastUpdateCheck), StandardCharsets.UTF_8);
                Instant modifiedTime = Instant.parse(fileContents);
                if (modifiedTime.plus(Duration.ofDays(1)).isAfter(Instant.now())) {
                    return Optional.empty();
                }
            } catch (DateTimeParseException | IOException ex) {
                // If reading update time failed, file might be corrupt, so delete it
                log.accept(LogEvent.debug("Failed to read lastUpdateCheck; " + ex.getMessage()));
                Files.delete(lastUpdateCheck);
            }
        }
        // Check for update
        FailoverHttpClient httpClient = new FailoverHttpClient(true, false, ignored -> {
        });
        try {
            Response response = httpClient.get(new URL(versionUrl), Request.builder().setHttpTimeout(3000).setUserAgent("jib " + currentVersion + " " + toolName).build());
            VersionJsonTemplate version = JsonTemplateMapper.readJson(response.getBody(), VersionJsonTemplate.class);
            Path lastUpdateCheckTemp = Files.createTempFile(configDir, LAST_UPDATE_CHECK_FILENAME, null);
            lastUpdateCheckTemp.toFile().deleteOnExit();
            Files.write(lastUpdateCheckTemp, Instant.now().toString().getBytes(StandardCharsets.UTF_8));
            Files.move(lastUpdateCheckTemp, lastUpdateCheck, StandardCopyOption.REPLACE_EXISTING);
            if (currentVersion.equals(version.latest)) {
                return Optional.empty();
            }
            return Optional.of(version.latest);
        } finally {
            httpClient.shutDown();
        }
    } catch (IOException ex) {
        log.accept(LogEvent.debug("Update check failed; " + ex.getMessage()));
    }
    return Optional.empty();
}
Also used : Path(java.nio.file.Path) Response(com.google.cloud.tools.jib.http.Response) DateTimeParseException(java.time.format.DateTimeParseException) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) Instant(java.time.Instant) IOException(java.io.IOException) URL(java.net.URL) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)6 Test (org.junit.Test)4 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)2 Blob (com.google.cloud.tools.jib.blob.Blob)2 Response (com.google.cloud.tools.jib.http.Response)2 TestWebServer (com.google.cloud.tools.jib.http.TestWebServer)2 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)2 ManifestDescriptorTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 Instant (java.time.Instant)2 DateTimeParseException (java.time.format.DateTimeParseException)2 Verifier (org.apache.maven.it.Verifier)2