diff --git a/.gitignore b/.gitignore index 409ee51..c1f8116 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,8 @@ build/ ### Mac OS ### .DS_Store -logs/ \ No newline at end of file +logs/ + +nginx/dhparam.pem +nginx/snakeoil* +authorized_keys \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e69de29..ea6046a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM eclipse-temurin:21-alpine AS build +WORKDIR /opt/app-build +ADD . /opt/app-build +RUN ./mvnw clean package + + +FROM eclipse-temurin:21-alpine +WORKDIR /opt/app +COPY --from=build /opt/app-build/target/dcdn-1.0-SNAPSHOT-shaded.jar /opt/app/app.jar +# Install sftp server +RUN apk add --no-cache openssh-sftp-server +RUN adduser -D site-deploy + +CMD ["java", "-jar", "/opt/app/app.jar"] \ No newline at end of file diff --git a/README.md b/README.md index 0c47827..e4e63dc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,13 @@ ### Setup + +First, to go `nginx/` and execute the following commands: +```bash +# Generate +openssl dhparam -out dhparam.pem 4096 + +openssl req -new -x509 -days 365 -noenc -out snakeoil.pem -keyout snakeoil.key -subj /CN=snakeoil +``` + 1. Replace IPs in `docker-compose.yml` 2. run like a compose project diff --git a/docker-compose.yml b/docker-compose.yml index 8d1f917..fc17812 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,13 +4,15 @@ services: context: . dockerfile: Dockerfile restart: unless-stopped - networks: - - app + ports: + - "[::1]:222:222" volumes: - /var/run/docker.sock:/var/run/docker.sock:ro - webroot:/var/www/html + - ./authorized_keys:/home/site-deploy/.ssh/authorized-keys:ro + - wr-deploy-temp:/tmp/deploy - nginx: + nginx: # name must not be changed image: nginx:1.27 restart: unless-stopped networks: @@ -29,6 +31,22 @@ services: depends_on: - app + sftp: + image: atmoz/sftp:alpine + volumes: + - ssh-data:/etc/ssh + - wr-deploy-temp:/home/site-deploy + - ./authorized_keys:/home/site-deploy/.ssh/keys/ssh-keys.pub:ro + ports: + - "[::1]:2222:22" + command: site-deploy:pass:1001 + volumes: webroot: - ssl-certs: \ No newline at end of file + ssl-certs: + wr-deploy-temp: + ssh-data: + +networks: + nginx: + external: false \ No newline at end of file diff --git a/src/main/java/eu/m724/Main.java b/src/main/java/eu/m724/Main.java index ce960cf..83bf047 100644 --- a/src/main/java/eu/m724/Main.java +++ b/src/main/java/eu/m724/Main.java @@ -8,8 +8,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.InetAddress; import java.net.StandardProtocolFamily; import java.net.UnixDomainSocketAddress; +import java.net.UnknownHostException; import java.util.concurrent.ExecutionException; public class Main { @@ -31,7 +33,30 @@ public class Main { LOGGER.info("- OS: {} version {}", info.getString("OperatingSystem"), info.getString("OSVersion")); LOGGER.info("- Hostname: {}", info.getString("Name")); - JSONObject containerInfo = null; + JSONObject appContainerInfo; + try { + appContainerInfo = engine.inspectContainer(getHostname()).get(); + LOGGER.debug("Raw container info: {}", appContainerInfo); + } catch (ExecutionException e) { + throw new RuntimeException("Exception getting container info", e); + } + + String appContainerId = appContainerInfo.getString("Id"); + String composeProject = appContainerInfo.getJSONObject("Config") + .getJSONObject("Labels") + .getString("com.docker.compose.project"); + + LOGGER.info("App info:"); + LOGGER.info("- Container ID: {} {}", appContainerId, appContainerInfo.getString("Name")); + LOGGER.info("- Compose project: {}", composeProject); + + LOGGER.info("Now waiting"); + synchronized (Main.class) { + Main.class.wait(); + } + + + /*JSONObject containerInfo = null; try { containerInfo = engine.inspectContainer("dcdn_nginx").get(); } catch (ExecutionException e) { @@ -44,7 +69,7 @@ public class Main { if (containerInfo != null) { - LOGGER.info("{}", containerInfo.toString()); + LOGGER.info("{}", containerInfo); var mounts = containerInfo.getJSONArray("Mounts"); LOGGER.debug("Detected {} mounts:", mounts.length()); @@ -62,7 +87,7 @@ public class Main { // TODO } catch (ExecutionException e) { throw new RuntimeException("Exception creating container", e); - }*/ + }/ var data = new JSONObject() .put("Image", "nginx:1.27"); @@ -75,6 +100,15 @@ public class Main { } } + */ proxy.close(); } + + public static String getHostname() { + try { + return InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + throw new RuntimeException(e); + } + } } \ No newline at end of file diff --git a/src/main/java/eu/m724/Nginx.java b/src/main/java/eu/m724/Nginx.java index 85bee5e..4038ff0 100644 --- a/src/main/java/eu/m724/Nginx.java +++ b/src/main/java/eu/m724/Nginx.java @@ -10,7 +10,7 @@ public class Nginx { public void prepareFiles() throws ProcessFailedException, IOException, InterruptedException { LOGGER.info("Generating dhparam, this will take several minutes"); - runCommand("openssl dhparam -out dhparam.pem 4096"); + runCommand("openssl dhparam -out dhparam.pem 4096"); // TODO make this generated with code and rotating and use 3072 until future proof LOGGER.info("Generating cert"); runCommand("openssl req -new -x509 -days 365 -noenc -out snakeoil.pem -keyout snakeoil.key -subj /CN=snakeoil");