|
| 1 | +// Copyright 2025 The Nomulus Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package google.registry.tools.javascrap; |
| 16 | + |
| 17 | +import com.google.common.collect.ImmutableSet; |
| 18 | +import com.google.common.collect.Sets; |
| 19 | +import com.google.common.flogger.FluentLogger; |
| 20 | +import google.registry.dns.DnsUtils; |
| 21 | +import google.registry.model.EppResource; |
| 22 | +import google.registry.model.EppResourceUtils; |
| 23 | +import google.registry.model.ForeignKeyUtils; |
| 24 | +import google.registry.model.domain.Domain; |
| 25 | +import google.registry.model.domain.DomainHistory; |
| 26 | +import google.registry.model.host.Host; |
| 27 | +import google.registry.model.reporting.HistoryEntry; |
| 28 | +import google.registry.persistence.VKey; |
| 29 | +import google.registry.util.Clock; |
| 30 | +import google.registry.util.RegistryEnvironment; |
| 31 | +import google.registry.util.SystemClock; |
| 32 | +import org.joda.time.DateTime; |
| 33 | + |
| 34 | +import java.util.Locale; |
| 35 | + |
| 36 | +import static com.google.common.base.Preconditions.checkArgument; |
| 37 | +import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 38 | +import static google.registry.flows.ResourceFlowUtils.loadAndVerifyExistence; |
| 39 | +import static google.registry.persistence.transaction.TransactionManagerFactory.tm; |
| 40 | + |
| 41 | +public class DomainCleanupHosts { |
| 42 | + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| 43 | + @SuppressWarnings("unused") |
| 44 | + public static void main(String[] args) throws Exception { |
| 45 | + checkArgument(args.length == 2, "Expecting two args: `env` and `domainName`"); |
| 46 | + String envName = args[0]; |
| 47 | + String domainName = args[1]; |
| 48 | + RegistryEnvironment.valueOf(envName.toUpperCase(Locale.ROOT)).setup(); |
| 49 | + |
| 50 | + Clock clock = new SystemClock(); |
| 51 | + DateTime now = clock.nowUtc(); |
| 52 | + |
| 53 | + Domain domain = loadAndVerifyExistence(Domain.class, domainName, now); |
| 54 | + tm().transact(() -> { |
| 55 | + ImmutableSet<VKey<Host>> validHosts = |
| 56 | + domain.getNsHosts().stream() |
| 57 | + .map(tm()::loadByKey) |
| 58 | + .filter(e -> e.getDeletionTime().isAfterNow()) |
| 59 | + .map(Host::createVKey) |
| 60 | + .collect(toImmutableSet()); |
| 61 | + |
| 62 | + logger.atInfo().log("NsHosts: %s\nInvalid Hosts: %s", domain.getNsHosts(), Sets.difference(domain.getNsHosts(), validHosts)); |
| 63 | + |
| 64 | + long revision_id = (Long) tm().getEntityManager().createNativeQuery( |
| 65 | + "SELECT MAX(history_revision_id) from \"DomainHistory\" " + |
| 66 | + "WHERE domain_repo_id = '" + domain.getRepoId() + "'") |
| 67 | + .getSingleResultOrNull(); |
| 68 | + logger.atInfo().log("Latest history id is %s", revision_id); |
| 69 | + |
| 70 | + DomainHistory domainHistory = tm().loadByKey(VKey.create(DomainHistory.class, new HistoryEntry.HistoryEntryId(domain.getRepoId(), revision_id))); |
| 71 | + logger.atInfo().log("DomainHistory nsHosts: %s", domainHistory.getNsHosts()); |
| 72 | + |
| 73 | + // DnsUtils.requestDomainDnsRefresh(domainName); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments