Skip to content

Commit b447b0c

Browse files
authored
Specify config file path by env and fixing arm release (#557)
* 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
1 parent 6db82f5 commit b447b0c

File tree

19 files changed

+318
-89
lines changed

19 files changed

+318
-89
lines changed

RELEASE-NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.26.0
2+
* Option to set config file path from the ENV, see the docs.
3+
14
## 3.25.14
25
* Fixing SolverRemote NPE #533
36

docs/content/3-configuration/_index.en.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Default: `8.8.8.8`.
1313
### Web Server Port
1414
Web GUI port, Default: `5380`.
1515

16-
### DNS Server Port
16+
### DNS Server Port
1717
Default: `53`.
1818

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

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

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

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

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

8585
| Env | JSON | Terminal |
8686
|--------------------------------------|---------------------------------|------------|
8787
| `MG_RESOLVCONF_OVERRIDE_NAMESERVERS` | `resolvConfOverrideNameServers` | See --help |
8888

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

9393
| Env | JSON | Terminal |
@@ -129,6 +129,28 @@ Default: true
129129
|-----------------------------------------------|-----------------------------------------|-----------|
130130
| MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE | `dockerSolverHostMachineFallbackActive` | See -help |
131131

132+
## Config File Path
133+
134+
The location of the external config file (as the JSON file) can be changed by setting two variables:
135+
136+
### Config File Path
137+
138+
Relative or absolute path to the config file. Default: `conf/config.json`
139+
140+
| Env | JSON | Terminal |
141+
|---------------------|------|-----------|
142+
| MG_CONFIG_FILE_PATH | N/A | See -help |
143+
144+
### Working Dir (Optional)
145+
146+
Is the path which will be used when **ConfigFilePath** is set as a relative path.
147+
Default: `DPS executing path, aka Working Directory`.
148+
149+
| Env | JSON | Terminal |
150+
|-------------|------|----------|
151+
| MG_WORK_DIR | N/A | N/A |
152+
153+
132154
## Example JSON configuration
133155

134156
__Version 2__

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.25.14-snapshot
1+
version=3.26.0-snapshot

src/main/docker/qemu/Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
FROM multiarch/qemu-user-static:latest@sha256:fe60359c92e86a43cc87b3d906006245f77bfc0565676b80004cc666e4feb9f0
22

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

89
RUN mkdir releases &&\
910
dpkg-deb -X qemu-user-static_*.deb releases &&\
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mageddo.dnsproxyserver.config.application;
2+
3+
import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOCmdArgs;
4+
import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOEnv;
5+
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigEnv;
6+
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag;
7+
import com.mageddo.dnsproxyserver.utils.ObjectUtils;
8+
import lombok.RequiredArgsConstructor;
9+
import lombok.extern.slf4j.Slf4j;
10+
11+
import javax.inject.Inject;
12+
import javax.inject.Singleton;
13+
import java.nio.file.Path;
14+
15+
@Slf4j
16+
@Singleton
17+
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
18+
public class ConfigFileFinderService {
19+
20+
private final ConfigDAOEnv configDAOEnv;
21+
private final ConfigDAOCmdArgs configDAOCmdArgs;
22+
23+
public Path findPath(){
24+
final var envConfig = this.configDAOEnv.findRaw();
25+
final var argsConfig = this.configDAOCmdArgs.findRaw();
26+
27+
final var configFilePath = this.findConfigFilePath(envConfig, argsConfig);
28+
29+
final var workDir = envConfig.getWorkingDir();
30+
return ConfigPathMapper.build(workDir, configFilePath);
31+
}
32+
33+
private Path findConfigFilePath(ConfigEnv envConfig, ConfigFlag argsConfig) {
34+
return ObjectUtils.firstNonNullRequiring(
35+
envConfig.getConfigFilePath(),
36+
argsConfig.getConfigFileAsPath()
37+
);
38+
}
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mageddo.dnsproxyserver.config.application;
2+
3+
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag;
4+
import com.mageddo.utils.Files;
5+
import com.mageddo.utils.Runtime;
6+
import com.mageddo.utils.Tests;
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
import java.nio.file.Path;
10+
11+
@Slf4j
12+
class ConfigPathMapper {
13+
14+
public static Path build(Path workDir, Path configFilePath) {
15+
final var path = build0(workDir, configFilePath);
16+
log.debug("status=configPathBuilt, path={}", path);
17+
return path;
18+
}
19+
20+
private static Path build0(Path workDir, Path configPath) {
21+
if (runningInTestsAndNoCustomConfigPath(configPath)) {
22+
final var file = Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json");
23+
log.trace("status=runningInTests, usingEmptyFile={}", file);
24+
return file;
25+
}
26+
if (workDir != null) {
27+
return workDir
28+
.resolve(configPath)
29+
.toAbsolutePath()
30+
;
31+
}
32+
final var confRelativeToCurrDir = configPath.toAbsolutePath();
33+
if (Files.exists(confRelativeToCurrDir)) {
34+
return confRelativeToCurrDir;
35+
}
36+
return Runtime.getRunningDir()
37+
.resolve(configPath)
38+
.toAbsolutePath();
39+
}
40+
41+
private static boolean runningInTestsAndNoCustomConfigPath(Path configPath) {
42+
return isDefaultConfigFilePath(configPath) && Tests.inTest();
43+
}
44+
45+
private static boolean isDefaultConfigFilePath(Path configPath) {
46+
return ConfigFlag.DEFAULT_CONFIG_FILE_AS_PATH.equals(configPath);
47+
}
48+
}

src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
package com.mageddo.dnsproxyserver.config.dataprovider;
22

33
import com.mageddo.dnsproxyserver.config.Config;
4+
import com.mageddo.dnsproxyserver.config.application.ConfigFileFinderService;
45
import com.mageddo.dnsproxyserver.config.dataprovider.mapper.ConfigJsonV2Mapper;
5-
import com.mageddo.utils.Tests;
66
import lombok.RequiredArgsConstructor;
77
import lombok.extern.slf4j.Slf4j;
88

99
import javax.inject.Inject;
1010
import javax.inject.Singleton;
1111
import java.nio.file.Path;
12-
import java.util.Arrays;
1312

1413
@Slf4j
1514
@Singleton
1615
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
1716
public class ConfigDAOJson implements ConfigDAO {
1817

19-
private final ConfigDAOEnv configDAOEnv;
20-
private final ConfigDAOCmdArgs configDAOCmdArgs;
18+
private final ConfigFileFinderService configFileFinderService;
2119

2220
@Override
2321
public Config find() {
24-
final var workDir = this.configDAOEnv.findRaw().getCurrentPath();
25-
final var relativeConfigFilePath = this.configDAOCmdArgs.findRaw().getConfigPath();
26-
final var configFileAbsolutePath = ConfigPathBuilder.build(workDir, relativeConfigFilePath);
27-
return this.find(configFileAbsolutePath);
22+
return this.find(this.configFileFinderService.findPath());
2823
}
2924

3025
public Config find(Path configPath) {
@@ -33,10 +28,6 @@ public Config find(Path configPath) {
3328
return ConfigJsonV2Mapper.toConfig(jsonConfig, configPath);
3429
}
3530

36-
static boolean runningInTestsAndNoCustomConfigPath() {
37-
return !Arrays.toString(ConfigDAOCmdArgs.getArgs()).contains("--conf-path") && Tests.inTest();
38-
}
39-
4031
@Override
4132
public int priority() {
4233
return 2;

src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigPathBuilder.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/mapper/ConfigFlagMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class ConfigFlagMapper {
1010
public static Config toConfig(ConfigFlag config) {
1111
return Config.builder()
12-
.configPath(Files.pathOf(config.getConfigPath()))
12+
.configPath(Files.pathOf(config.getConfigFilePath()))
1313
.registerContainerNames(config.getRegisterContainerNames())
1414
.domain(config.getDomain())
1515
.logFile(config.getLogToFile())

src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigEnv.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public class ConfigEnv {
4747

4848
private static final String MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE = "MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE";
4949

50-
private Path currentPath;
50+
private static final String MG_CONFIG_FILE_PATH = "MG_CONFIG_FILE_PATH";
51+
52+
private Path workingDir;
5153
private String resolvConfPath;
5254
private String logFile;
5355
private String logLevel;
@@ -61,11 +63,12 @@ public class ConfigEnv {
6163
private Boolean noRemoteServers;
6264
private Integer noEntriesResponseCode;
6365
private Boolean dockerSolverHostMachineFallbackActive;
66+
private Path configFilePath;
6467

6568
public static ConfigEnv fromEnv() {
6669
return ConfigEnv
6770
.builder()
68-
.currentPath(Envs.getPathOrNull(MG_WORK_DIR))
71+
.workingDir(Envs.getPathOrNull(MG_WORK_DIR))
6972
.resolvConfPath(Envs.getStringOrDefault(MG_RESOLVCONF, DEFAULT_RESOLV_CONF_PATH))
7073
.logFile(findLogFilePath())
7174
.logLevel(Envs.getStringOrNull(MG_LOG_LEVEL))
@@ -79,6 +82,7 @@ public static ConfigEnv fromEnv() {
7982
.noRemoteServers(Envs.getBooleanOrNull(MG_NO_REMOTE_SERVERS))
8083
.noEntriesResponseCode(Envs.getIntegerOrNull(MG_NO_ENTRIES_RESPONSE_CODE))
8184
.dockerSolverHostMachineFallbackActive(Envs.getBooleanOrNull(MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE))
85+
.configFilePath(Envs.getPathOrNull(MG_CONFIG_FILE_PATH))
8286
.build();
8387
}
8488

0 commit comments

Comments
 (0)