Skip to content

Commit

Permalink
issue #47
Browse files Browse the repository at this point in the history
  • Loading branch information
th-schwarz committed Oct 27, 2024
1 parent 24e2ac2 commit 22c320c
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 201 deletions.
11 changes: 5 additions & 6 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,16 @@ logging:
dyndrest:
provider: domainrobot
zones:
- name: dynhost.info
ns: ns.domain.info
hosts:
- myhost:1234567890abcdef
domainrobot:
autodns:
password: pwd_t
user: user_t
zones:
- name: dynhost.info
ns: ns.domain.info
hosts:
- myhost:1234567890abcdef
----

The `zones` section should be used for importing the hosts and zones configuration to the database initially. Existing data entries won't be updated. The example defines a host `myhost.dynhost.info` with the api-token `1234567890abcdef`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,6 @@ public record AppConfig(
@Nullable String healthCheckUserPassword,
@Nullable String adminUserName,
@Nullable String adminUserPassword,
@Nullable String adminApiToken,
Database database) {
@Nullable String adminApiToken) {

/** Represents a configuration class for a database connection. */
public record Database(
String password,
String dumpFile,
@Nullable Backup backup,
@Nullable Restore restore) {

/**
* Represents a backup configuration.
*
* @param enabled Specifies if the backup is enabled or not.
* @param path The path where the backup files will be stored, e.g {@code ./backup}. This should
* be a valid file system path.
* @param cron The cron expression that defines the backup schedule.
*/
public record Backup(boolean enabled, String path, String cron) {}

/**
* Represents a restore configuration.
*
* @param enabled Specifies if the restore functionality is enabled or not.
* @param path The path where the backup files for restore are stored, e.g. {@code ./restore}.
* This should be a valid file system path.
*/
public record Restore(boolean enabled, String path) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* The ZoneImport class represents the base configuration of a zone import. It contains a list of
* Zone objects, which represent individual zones with their name, name server, and hosts.
*/
@ConfigurationProperties
@ConfigurationProperties(prefix = "dyndrest")
public record ZoneImportConfig(@Nullable List<ZoneImportConfig.Zone> zones) {

/**
Expand All @@ -26,16 +26,10 @@ public List<HostEnriched> getHosts() {
return enrichedHosts;
}
for (ZoneImportConfig.Zone zone : zones) {
for (String hostRaw : zone.hosts()) {
String[] parts = hostRaw.split(":");
if (parts.length != 2) {
throw new IllegalArgumentException(
"The host entry must be in the following format: [sld|:[apiToken], but it was: "
+ hostRaw);
}
for (Host host : zone.hosts()) {
HostEnriched hostEnriched = new HostEnriched();
hostEnriched.setName(parts[0]);
hostEnriched.setApiToken(parts[1]);
hostEnriched.setName(host.sld());
hostEnriched.setApiToken(host.apiToken());
hostEnriched.setZone(zone.name);
hostEnriched.setNs(zone.ns);
enrichedHosts.add(hostEnriched);
Expand All @@ -51,5 +45,14 @@ public List<HostEnriched> getHosts() {
* @param ns name server
* @param hosts host / subdomains
*/
public record Zone(String name, String ns, List<String> hosts) {}
public record Zone(String name, String ns, List<Host> hosts) {}


/**
* Represents a Host entity with an sld (second-level domain) and an apiToken.
*
* @param sld The second-level domain associated with the host.
* @param apiToken The API token for authentication or other purposes.
*/
public record Host(String sld, String apiToken) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package codes.thischwa.dyndrest.model.config.database;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Represents a backup configuration.
*
* @param enabled Enabled backup.
* @param path The path where the backup files will be stored, e.g {@code ./backup}. This should be
* a valid file system path.
* @param cron The cron expression that defines the backup schedule.
*/
@ConfigurationProperties(prefix = "dyndrest.database.backup")
public record DatabaseBackupConfig(boolean enabled, String path, String cron) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package codes.thischwa.dyndrest.model.config.database;


import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Represents a restore configuration.
*
* @param enabled Enables the restore.
* @param path The path where the backup files for restore are stored, e.g. {@code ./restore}. This
* should be a valid file system path.
*/
@ConfigurationProperties(prefix = "dyndrest.database.restore")
public record DatabaseRestoreConfig(boolean enabled, String path) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package codes.thischwa.dyndrest.model.config.database;


import org.springframework.boot.context.properties.ConfigurationProperties;

/** Represents a configuration class for a database connection. */
@ConfigurationProperties(prefix = "dyndrest.database")
public record DatabaseServiceConfig(
String dumpFile) {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package codes.thischwa.dyndrest.server.config;

import codes.thischwa.dyndrest.model.IpSetting;
import codes.thischwa.dyndrest.model.UpdateLog;
import codes.thischwa.dyndrest.model.config.AppConfig;
import codes.thischwa.dyndrest.repository.UpdateLogRepo;
import codes.thischwa.dyndrest.service.HostZoneService;
import java.time.LocalDateTime;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
Expand All @@ -30,25 +26,18 @@ public class ApplicationStartup implements ApplicationListener<ApplicationReadyE
private final Environment env;

private final HostZoneService hostZoneService;
private final UpdateLogRepo updateLogRepo;

/**
* Initializes the application on startup.
*
* @param config the application configuration
* @param env the application environment
* @param hostZoneService the host zone service
* @param updateLogRepo the update log repository
*/
public ApplicationStartup(
AppConfig config,
Environment env,
HostZoneService hostZoneService,
UpdateLogRepo updateLogRepo) {
public ApplicationStartup(AppConfig config, Environment env, HostZoneService hostZoneService) {
this.config = config;
this.env = env;
this.hostZoneService = hostZoneService;
this.updateLogRepo = updateLogRepo;
}

@Override
Expand All @@ -64,11 +53,8 @@ public void onApplicationEvent(final ApplicationReadyEvent event) {
log.info(" - spring.h2.console.enabled: {}", env.getProperty("spring.h2.console.enabled"));
log.info(" - spring.h2.console.path: {}", env.getProperty("spring.h2.console.path"));
log.info(" - dyndrest.database.dump-file: {}", env.getProperty("dyndrest.database.dump-file"));
log.info(" - backup enabled: {}",
config.database().backup() != null && config.database().backup().enabled());
log.info(
" - restore enabled: {}",
config.database().restore() != null && config.database().restore().enabled());
log.info(" - backup enabled: {}", env.getProperty("dyndrest.database.backup.enabled"));
log.info(" - restore enabled: {}", env.getProperty("dyndrest.database.restore.enabled"));
log.info("*** Endpoints:");

ApplicationContext applicationContext = event.getApplicationContext();
Expand All @@ -78,29 +64,5 @@ public void onApplicationEvent(final ApplicationReadyEvent event) {
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
map.forEach((key, value) -> log.info(" * {}", key));
hostZoneService.importOnStart();
// buildDummyUpdateLogs();
}

private void buildDummyUpdateLogs() {
try {
LocalDateTime dateTime = LocalDateTime.now();
updateLogRepo.save(
UpdateLog.getInstance(
1,
new IpSetting("198.0.1.0", "2a03:4000:41:32:0:0:1:0"),
UpdateLog.Status.success,
dateTime,
dateTime));
for (int i = 1; i <= 150; i++) {
dateTime = dateTime.plusMinutes(10);
updateLogRepo.save(
UpdateLog.getInstance(
1, new IpSetting("198.0.1." + i), UpdateLog.Status.success, dateTime, dateTime));

log.info("Dummy logs created");
}
} catch (Exception e) {
log.error("Error creating dummy logs", e);
}
}
}
28 changes: 7 additions & 21 deletions src/main/java/codes/thischwa/dyndrest/service/BackupService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codes.thischwa.dyndrest.service;

import codes.thischwa.dyndrest.model.config.AppConfig;
import codes.thischwa.dyndrest.model.config.database.DatabaseBackupConfig;
import codes.thischwa.dyndrest.model.config.database.DatabaseServiceConfig;
import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -29,33 +30,22 @@
public class BackupService {

private final JdbcTemplate jdbcTemplate;
private final DatabaseBackupConfig backup;

private final boolean enabled;
private final AppConfig.Database database;
private final AppConfig.Database.Backup backup;

@Nullable private String dumpPath;
@Nullable private final String dumpPath;

/**
* The BackupService class is responsible for performing database backups based on the provided
* configuration.
*/
public BackupService(AppConfig appConfig, JdbcTemplate jdbcTemplate) {
assert appConfig.database().backup() != null;
database = appConfig.database();
this.backup = appConfig.database().backup();
public BackupService(DatabaseServiceConfig databaseServiceConfig, DatabaseBackupConfig backupConfig, JdbcTemplate jdbcTemplate) {
dumpPath = Paths.get(backupConfig.path(), databaseServiceConfig.dumpFile()).toString();
this.jdbcTemplate = jdbcTemplate;
enabled = backup.enabled();
this.backup = backupConfig;
}

@PostConstruct
void init() {
if (enabled) {
log.info("Enabled with cron: {}, at {}", backup.cron(), backup.path());
} else {
log.info("Disabled");
return;
}
Path backupPath = Paths.get(backup.path()).normalize();
if (!Files.exists(backupPath)) {
try {
Expand All @@ -65,7 +55,6 @@ void init() {
throw new RuntimeException("couldn't create backup path: " + backupPath.toAbsolutePath());
}
}
dumpPath = Paths.get(backupPath.toString(), database.dumpFile()).toString();
}

/**
Expand All @@ -75,9 +64,6 @@ void init() {
@Transactional
@Scheduled(cron = "${dyndrest.database.backup.cron}")
public void process() {
if (!enabled) {
return;
}
String sql = String.format("SCRIPT DROP TO '%s';", dumpPath);
jdbcTemplate.execute(sql);
log.info("Database backup successful processed: {}", dumpPath);
Expand Down
Loading

0 comments on commit 22c320c

Please sign in to comment.