Upload all
This commit is contained in:
40
src/main/java/tech/nevets/autoupdater/AuthFileHandler.java
Normal file
40
src/main/java/tech/nevets/autoupdater/AuthFileHandler.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package tech.nevets.autoupdater;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AuthFileHandler {
|
||||
private JsonObject file;
|
||||
|
||||
public void loadFile() {
|
||||
try {
|
||||
File jsonFile = new File("./auth-tokens.json");
|
||||
if (!jsonFile.exists()) {
|
||||
jsonFile.createNewFile();
|
||||
FileWriter writer = new FileWriter(jsonFile);
|
||||
writer.write("{ \"tokens\": [ \"token1\",\"token2\",\"token3\" ] }");
|
||||
writer.close();
|
||||
}
|
||||
file = new Gson().fromJson(new FileReader(jsonFile), JsonObject.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsToken(String authToken) {
|
||||
JsonArray jArray = file.get("tokens").getAsJsonArray();
|
||||
for (JsonElement element : jArray) {
|
||||
if (element.getAsString().equals(authToken)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
33
src/main/java/tech/nevets/autoupdater/Config.java
Normal file
33
src/main/java/tech/nevets/autoupdater/Config.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package tech.nevets.autoupdater;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Config {
|
||||
private JsonObject config;
|
||||
|
||||
public void loadFile() {
|
||||
try {
|
||||
File jsonFile = new File("./config.json");
|
||||
if (!jsonFile.exists()) {
|
||||
jsonFile.createNewFile();
|
||||
FileWriter writer = new FileWriter(jsonFile);
|
||||
writer.write("{ \"docker-socket\": \"unix:///var/run/docker.sock\" }");
|
||||
writer.close();
|
||||
}
|
||||
config = new Gson().fromJson(new FileReader(jsonFile), JsonObject.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public JsonObject getConfig() {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
48
src/main/java/tech/nevets/autoupdater/Main.java
Normal file
48
src/main/java/tech/nevets/autoupdater/Main.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package tech.nevets.autoupdater;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import spark.Request;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class Main {
|
||||
public static final Config CONFIG = new Config();
|
||||
private static final AuthFileHandler AUTH_TOKENS = new AuthFileHandler();
|
||||
|
||||
public static void main(String[] args) {
|
||||
CONFIG.loadFile();
|
||||
AUTH_TOKENS.loadFile();
|
||||
|
||||
post("/jenkins", (rq, rs) -> {
|
||||
if (rq.contentType().equals("application/json")) {
|
||||
if (!isAuthenticated(rq)) {
|
||||
halt(401, "{ \"error\":\"Not Authorized\" }");
|
||||
}
|
||||
Gson gson = new Gson();
|
||||
JsonObject json = gson.fromJson(rq.body(), JsonObject.class);
|
||||
JsonElement appNameJson = json.get("app-name");
|
||||
JsonElement jarFileNameJson = json.get("jarfile-name");
|
||||
JsonElement dockerContainerNameJson = json.get("docker-container-name");
|
||||
JsonElement workingDirectoryJson = json.get("working-directory");
|
||||
JsonElement downloadUrlJson = json.get("download-url");
|
||||
if (appNameJson == null || jarFileNameJson == null || dockerContainerNameJson == null || workingDirectoryJson == null || downloadUrlJson == null) {
|
||||
halt(405, "{ \"error\":\"Not all parameters passed\" }");
|
||||
}
|
||||
if (UpdateManager.update(appNameJson.getAsString(), jarFileNameJson.getAsString(), dockerContainerNameJson.getAsString(), workingDirectoryJson.getAsString(), downloadUrlJson.getAsString())) {
|
||||
rs.status(200);
|
||||
return "{ \"status\":\"success\" }";
|
||||
}
|
||||
} else {
|
||||
notFound("{ \"error\":404 }");
|
||||
}
|
||||
return "{ \"error\":\"500\" }";
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean isAuthenticated(Request rq) {
|
||||
String authHeader = rq.headers("Authorization");
|
||||
return AUTH_TOKENS.containsToken(authHeader);
|
||||
}
|
||||
}
|
||||
66
src/main/java/tech/nevets/autoupdater/UpdateManager.java
Normal file
66
src/main/java/tech/nevets/autoupdater/UpdateManager.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package tech.nevets.autoupdater;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.core.DockerClientBuilder;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class UpdateManager {
|
||||
public static boolean update(String appName, String jarFileName, String dockerContainerName, String workingDirectory, String downloadUrl) {
|
||||
File workingDir = new File(workingDirectory);
|
||||
if (!workingDir.isDirectory()) {
|
||||
System.out.println("is not dir");
|
||||
return false;
|
||||
}
|
||||
|
||||
DockerClient dockerClient = getDockerClient();
|
||||
|
||||
// Stop container here
|
||||
dockerClient.stopContainerCmd(getContainerByName(dockerClient, dockerContainerName)).exec();
|
||||
|
||||
// Delete old jar file
|
||||
File jarFile = new File(workingDirectory + "/" + jarFileName);
|
||||
if (jarFile.exists() && jarFile.isFile()) {
|
||||
System.out.println("deleted");
|
||||
return jarFile.delete();
|
||||
}
|
||||
|
||||
// Download new jar file
|
||||
try {
|
||||
FileUtils.copyURLToFile(new URL(downloadUrl), jarFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("error copying");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start container here
|
||||
dockerClient.startContainerCmd(getContainerByName(dockerClient, dockerContainerName));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static DockerClient getDockerClient() {
|
||||
DefaultDockerClientConfig config
|
||||
= DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
.withDockerTlsVerify("1")
|
||||
.withDockerHost(Main.CONFIG.getConfig().get("docker-socket").getAsString()).build();
|
||||
|
||||
return DockerClientBuilder.getInstance(config).build();
|
||||
}
|
||||
|
||||
private static String getContainerByName(DockerClient dockerClient, String containerName) {
|
||||
for (Container container : dockerClient.listContainersCmd().exec()) {
|
||||
if (Arrays.asList(container.getNames()).contains(containerName)) {
|
||||
return container.getId();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user