Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyleiger committed Apr 29, 2020
0 parents commit ce58bfe
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 0 deletions.
135 changes: 135 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea

.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### NotepadPP template
# Notepad++ backups #
*.bak
### Linux template
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### LibreOffice template
# LibreOffice locks
.~lock.*#
### macOS template
# General
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Windows template
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# IntelliJ Project File
*.iml

#Maven
target/
*.versionsBackup
dependency-reduced-pom.xml

#JavaDocs
javadoc/

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# DockerProcessWrapper

A simple process wrapper which runs the process as it self but sends a command to it, if the TERM signal will be received. This is useful for applications in docker containers. It is written with Java 8.

## Table of Contents

* [Authors](#authors--contributors)
* [License](#license)

## Authors & Contributors

* **Dominic Wienzek** - *Development* - [Skyleiger](https://github.com/skyleiger)

See also the list of [contributors](https://github.com/Skyleiger/RadioBots-GameCloud/contributors) who participated in this project.

## License

This project is is the property of the author(s) and may only be used with the consent of the author(s). Other uses are not permitted.
121 changes: 121 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.dwienzek</groupId>
<artifactId>docker-process-wrapper</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.targetEncoding>UTF-8</project.build.targetEncoding>
</properties>

<developers>
<developer>
<name>Dominic Wienzek</name>
<email>dominic.wienzek@gmail.com</email>
<url>https://dominicwienzek.de</url>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>net.sf.jopt-simple</groupId>
<artifactId>jopt-simple</artifactId>
<version>5.0.4</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<finalName>DockerProcessWrapper</finalName>
<defaultGoal>clean install</defaultGoal>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>*</directory>
<includes>
<include>*.versionsBackup</include>
</includes>
</fileset>
<fileset>
<directory>javadoc</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Main-Class>de.dwienzek.dockerprocesswrapper.DockerProcessWrapper</Main-Class>
<Implementation-Title>DockerProcessWrapper</Implementation-Title>
<Specification-Title>DockerProcessWrapper</Specification-Title>
<Version>${project.version}</Version>
<Implementation-Version>${project.version}</Implementation-Version>
<Specification-Version>${project.version}</Specification-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package de.dwienzek.dockerprocesswrapper;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import sun.misc.Signal;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DockerProcessWrapper {

public static void main(String[] args) throws IOException {
OptionParser optionParser = new OptionParser();
optionParser.accepts("help", "Show the help");
optionParser.accepts("execute", "The command to execute").withRequiredArg();
optionParser.accepts("directory", "The execute directory").withOptionalArg();
optionParser.accepts("shutdown-command", "The shutdown command sent to the process").withRequiredArg();

OptionSet optionSet = optionParser.parse(args);

if (optionSet.has("help")) {
optionParser.printHelpOn(System.out);
return;
}

if (!optionSet.has("execute") || !optionSet.has("shutdown-command")) {
System.out.println("Please enter the correct arguments.");
System.out.println("Example: DockerProcessWrapper.jar --execute <command> --shutdown-command <shutdown-command> (--directory <directory>)");
System.out.println("Use --help for help.");
}

String commandString = (String) optionSet.valueOf("execute");
List<String> command = new ArrayList<>();
if (commandString.contains(" ")) {
command.addAll(Arrays.asList(commandString.split(" ")));
} else {
command.add(commandString);
}

Process process;
ProcessBuilder processBuilder = new ProcessBuilder().redirectOutput(ProcessBuilder.Redirect.INHERIT).redirectError(ProcessBuilder.Redirect.INHERIT).command(command);
if (optionSet.has("directory")) {
processBuilder.directory(new File((String) optionSet.valueOf("directory")));
}
process = processBuilder.start();

Thread thread = new Thread() {
@Override
public void run() {
Console console = System.console();
String line;
while (!isInterrupted() && (line = console.readLine()) != null) {
try {
process.getOutputStream().write((line + "\n").getBytes());
process.getOutputStream().flush();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
};
thread.setDaemon(true);
thread.start();

Signal.handle(new Signal("TERM"), signal -> {
try {
process.getOutputStream().write((optionSet.valueOf("shutdown-command") + "\n").getBytes());
process.getOutputStream().flush();
} catch (IOException exception) {
exception.printStackTrace();
}
});

try {
process.waitFor();
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}

}

0 comments on commit ce58bfe

Please sign in to comment.