Skip to content

Commit

Permalink
Specify config file path by env and fixing arm release (#557)
Browse files Browse the repository at this point in the history
* Implementing env config file path option

* fixing compiling errors

* fixing test

* fixing test

* testing

* updating the docs

* [Gradle Release Plugin] - new version commit:  '3.26.0-snapshot'.

* release notes

* fixing arm build mirror and ajusting test

* new mirrors

* formatting

* refactoring

* clean code
  • Loading branch information
mageddo authored Sep 4, 2024
1 parent 6db82f5 commit b447b0c
Show file tree
Hide file tree
Showing 19 changed files with 318 additions and 89 deletions.
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.26.0
* Option to set config file path from the ENV, see the docs.

## 3.25.14
* Fixing SolverRemote NPE #533

Expand Down
34 changes: 28 additions & 6 deletions docs/content/3-configuration/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Default: `8.8.8.8`.
### Web Server Port
Web GUI port, Default: `5380`.

### DNS Server Port
### DNS Server Port
Default: `53`.

### Log Level
Expand All @@ -40,7 +40,7 @@ If should register container name / service name as a hostname. Default: false.
### Domain
The container names domain used on the registered container, services. Default: `docker`.

Ex:
Ex:
```bash
docker run --rm --name nginx nginx
```
Expand All @@ -61,8 +61,8 @@ Default: false.
If DPS must be set as the default DNS automatically, commonly requires DPS be run as sudo/administrator permissions,
this options also won't work in some cases when running inside a docker container, [see the feature details][1].

### Host Machine Hostname
Hostname to solve machine IP, domain can be changed by Domain option. Default: `host.docker`.
### Host Machine Hostname
Hostname to solve machine IP, domain can be changed by Domain option. Default: `host.docker`.

| Env | JSON | Terminal |
|----------------------------|-----------------------|------------|
Expand All @@ -79,15 +79,15 @@ Docker host address. Default value is SO dependent.
| `MG_DOCKER_HOST` | `dockerHost` | See --help |

### Resolvconf Override Name Servers
If must comment all existing nameservers at `resolv.conf` file (Linux, MacOS) or just put DPS at the first place.
If must comment all existing nameservers at `resolv.conf` file (Linux, MacOS) or just put DPS at the first place.
Default: true.

| Env | JSON | Terminal |
|--------------------------------------|---------------------------------|------------|
| `MG_RESOLVCONF_OVERRIDE_NAMESERVERS` | `resolvConfOverrideNameServers` | See --help |

### Resolvconf
Linux/Mac resolvconf or systemd-resolved path to set DPS as default DNS.
Linux/Mac resolvconf or systemd-resolved path to set DPS as default DNS.
Default: `/host/etc/systemd/resolved.conf,/host/etc/resolv.conf,/etc/systemd/resolved.conf,/etc/resolv.conf`.

| Env | JSON | Terminal |
Expand Down Expand Up @@ -129,6 +129,28 @@ Default: true
|-----------------------------------------------|-----------------------------------------|-----------|
| MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE | `dockerSolverHostMachineFallbackActive` | See -help |

## Config File Path

The location of the external config file (as the JSON file) can be changed by setting two variables:

### Config File Path

Relative or absolute path to the config file. Default: `conf/config.json`

| Env | JSON | Terminal |
|---------------------|------|-----------|
| MG_CONFIG_FILE_PATH | N/A | See -help |

### Working Dir (Optional)

Is the path which will be used when **ConfigFilePath** is set as a relative path.
Default: `DPS executing path, aka Working Directory`.

| Env | JSON | Terminal |
|-------------|------|----------|
| MG_WORK_DIR | N/A | N/A |


## Example JSON configuration

__Version 2__
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=3.25.14-snapshot
version=3.26.0-snapshot
9 changes: 5 additions & 4 deletions src/main/docker/qemu/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
FROM multiarch/qemu-user-static:latest@sha256:fe60359c92e86a43cc87b3d906006245f77bfc0565676b80004cc666e4feb9f0

ARG DEB_MIRROR1="http://ftp.de.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.5+ds-2_amd64.deb"
ARG DEB_MIRROR2="http://ftp.de.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.5+ds-1_amd64.deb"
ARG DEB_MIRROR3="https://ftp.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.4+ds-1_amd64.deb"
RUN wget ${DEB_MIRROR1} || wget ${DEB_MIRROR2} || wget ${DEB_MIRROR3}
ARG DEB_MIRROR1="http://archive.ubuntu.com/ubuntu/pool/universe/q/qemu/qemu-user-static_8.2.2+ds-0ubuntu1.2_amd64.deb"
ARG DEB_MIRROR2="http://ftp.de.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.5+ds-2_amd64.deb"
ARG DEB_MIRROR3="http://download.unesp.br/ubuntu/pool/universe/q/qemu/qemu-user-static_8.2.2+ds-0ubuntu1_amd64.deb"
ARG DEB_MIRROR4="https://deb.sipwise.com/debian/pool/main/q/qemu/qemu-user-static_8.2.1+ds-1~bpo12+1_amd64.deb"
RUN wget ${DEB_MIRROR1} || wget ${DEB_MIRROR2} || wget ${DEB_MIRROR3} || wget ${DEB_MIRROR4}

RUN mkdir releases &&\
dpkg-deb -X qemu-user-static_*.deb releases &&\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mageddo.dnsproxyserver.config.application;

import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOCmdArgs;
import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOEnv;
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigEnv;
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag;
import com.mageddo.dnsproxyserver.utils.ObjectUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.nio.file.Path;

@Slf4j
@Singleton
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
public class ConfigFileFinderService {

private final ConfigDAOEnv configDAOEnv;
private final ConfigDAOCmdArgs configDAOCmdArgs;

public Path findPath(){
final var envConfig = this.configDAOEnv.findRaw();
final var argsConfig = this.configDAOCmdArgs.findRaw();

final var configFilePath = this.findConfigFilePath(envConfig, argsConfig);

final var workDir = envConfig.getWorkingDir();
return ConfigPathMapper.build(workDir, configFilePath);
}

private Path findConfigFilePath(ConfigEnv envConfig, ConfigFlag argsConfig) {
return ObjectUtils.firstNonNullRequiring(
envConfig.getConfigFilePath(),
argsConfig.getConfigFileAsPath()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mageddo.dnsproxyserver.config.application;

import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag;
import com.mageddo.utils.Files;
import com.mageddo.utils.Runtime;
import com.mageddo.utils.Tests;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Path;

@Slf4j
class ConfigPathMapper {

public static Path build(Path workDir, Path configFilePath) {
final var path = build0(workDir, configFilePath);
log.debug("status=configPathBuilt, path={}", path);
return path;
}

private static Path build0(Path workDir, Path configPath) {
if (runningInTestsAndNoCustomConfigPath(configPath)) {
final var file = Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json");
log.trace("status=runningInTests, usingEmptyFile={}", file);
return file;
}
if (workDir != null) {
return workDir
.resolve(configPath)
.toAbsolutePath()
;
}
final var confRelativeToCurrDir = configPath.toAbsolutePath();
if (Files.exists(confRelativeToCurrDir)) {
return confRelativeToCurrDir;
}
return Runtime.getRunningDir()
.resolve(configPath)
.toAbsolutePath();
}

private static boolean runningInTestsAndNoCustomConfigPath(Path configPath) {
return isDefaultConfigFilePath(configPath) && Tests.inTest();
}

private static boolean isDefaultConfigFilePath(Path configPath) {
return ConfigFlag.DEFAULT_CONFIG_FILE_AS_PATH.equals(configPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package com.mageddo.dnsproxyserver.config.dataprovider;

import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.config.application.ConfigFileFinderService;
import com.mageddo.dnsproxyserver.config.dataprovider.mapper.ConfigJsonV2Mapper;
import com.mageddo.utils.Tests;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.nio.file.Path;
import java.util.Arrays;

@Slf4j
@Singleton
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
public class ConfigDAOJson implements ConfigDAO {

private final ConfigDAOEnv configDAOEnv;
private final ConfigDAOCmdArgs configDAOCmdArgs;
private final ConfigFileFinderService configFileFinderService;

@Override
public Config find() {
final var workDir = this.configDAOEnv.findRaw().getCurrentPath();
final var relativeConfigFilePath = this.configDAOCmdArgs.findRaw().getConfigPath();
final var configFileAbsolutePath = ConfigPathBuilder.build(workDir, relativeConfigFilePath);
return this.find(configFileAbsolutePath);
return this.find(this.configFileFinderService.findPath());
}

public Config find(Path configPath) {
Expand All @@ -33,10 +28,6 @@ public Config find(Path configPath) {
return ConfigJsonV2Mapper.toConfig(jsonConfig, configPath);
}

static boolean runningInTestsAndNoCustomConfigPath() {
return !Arrays.toString(ConfigDAOCmdArgs.getArgs()).contains("--conf-path") && Tests.inTest();
}

@Override
public int priority() {
return 2;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ConfigFlagMapper {
public static Config toConfig(ConfigFlag config) {
return Config.builder()
.configPath(Files.pathOf(config.getConfigPath()))
.configPath(Files.pathOf(config.getConfigFilePath()))
.registerContainerNames(config.getRegisterContainerNames())
.domain(config.getDomain())
.logFile(config.getLogToFile())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public class ConfigEnv {

private static final String MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE = "MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE";

private Path currentPath;
private static final String MG_CONFIG_FILE_PATH = "MG_CONFIG_FILE_PATH";

private Path workingDir;
private String resolvConfPath;
private String logFile;
private String logLevel;
Expand All @@ -61,11 +63,12 @@ public class ConfigEnv {
private Boolean noRemoteServers;
private Integer noEntriesResponseCode;
private Boolean dockerSolverHostMachineFallbackActive;
private Path configFilePath;

public static ConfigEnv fromEnv() {
return ConfigEnv
.builder()
.currentPath(Envs.getPathOrNull(MG_WORK_DIR))
.workingDir(Envs.getPathOrNull(MG_WORK_DIR))
.resolvConfPath(Envs.getStringOrDefault(MG_RESOLVCONF, DEFAULT_RESOLV_CONF_PATH))
.logFile(findLogFilePath())
.logLevel(Envs.getStringOrNull(MG_LOG_LEVEL))
Expand All @@ -79,6 +82,7 @@ public static ConfigEnv fromEnv() {
.noRemoteServers(Envs.getBooleanOrNull(MG_NO_REMOTE_SERVERS))
.noEntriesResponseCode(Envs.getIntegerOrNull(MG_NO_ENTRIES_RESPONSE_CODE))
.dockerSolverHostMachineFallbackActive(Envs.getBooleanOrNull(MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE))
.configFilePath(Envs.getPathOrNull(MG_CONFIG_FILE_PATH))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
import com.mageddo.dnsproxyserver.config.dataprovider.ConfigPropDAO;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import picocli.CommandLine;
import picocli.CommandLine.Option;

import java.io.PrintWriter;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Callable;


@Getter
@NoArgsConstructor
public class ConfigFlag implements Callable<Boolean> {

public static final String DEFAULT_CONFIG_FILE_PATH = "conf/config.json";
public static final Path DEFAULT_CONFIG_FILE_AS_PATH = Paths.get(DEFAULT_CONFIG_FILE_PATH);

@Option(
names = {"-version", "--version"}, description = "Shows the current version (default false)"
)
Expand Down Expand Up @@ -46,9 +52,9 @@ public class ConfigFlag implements Callable<Boolean> {
@Option(
names = {"-conf-path", "--conf-path"},
description = "The config file path (default conf/config.json)",
defaultValue = "conf/config.json"
defaultValue = DEFAULT_CONFIG_FILE_PATH
)
private String configPath;
private String configFilePath;

@Option(
names = {"-service", "--service"},
Expand Down Expand Up @@ -236,4 +242,11 @@ public Boolean call() {
return false;
}

@JsonIgnore
public Path getConfigFileAsPath() {
if (StringUtils.isNotBlank(this.configFilePath)) {
return Paths.get(this.configFilePath);
}
return null;
}
}
Loading

0 comments on commit b447b0c

Please sign in to comment.