Added verbosity

This commit is contained in:
Steven Tracey 2022-11-21 21:56:58 -05:00
parent 7c8583ffd8
commit da86458782
3 changed files with 34 additions and 9 deletions

View File

@ -18,7 +18,11 @@ public class Config {
if (!jsonFile.exists()) {
jsonFile.createNewFile();
FileWriter writer = new FileWriter(jsonFile);
writer.write("{ \"docker-socket\": \"unix:///var/run/docker.sock\" }");
writer.write("{ " +
"\"docker-socket\":\"unix:///var/run/docker.sock\", " +
"\"port\":8080" +
"\"verbose\":false" +
" }");
writer.close();
}
config = new Gson().fromJson(new FileReader(jsonFile), JsonObject.class);

View File

@ -3,6 +3,7 @@ package tech.nevets.autoupdater;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.http.auth.AUTH;
import spark.Request;
import static spark.Spark.*;
@ -10,31 +11,40 @@ import static spark.Spark.*;
public class Main {
public static final Config CONFIG = new Config();
private static final AuthFileHandler AUTH_TOKENS = new AuthFileHandler();
public static boolean isVerbose = false;
public static void main(String[] args) {
CONFIG.loadFile();
AUTH_TOKENS.loadFile();
isVerbose = CONFIG.getConfig().get("verbose").getAsBoolean();
port(CONFIG.getConfig().get("port").getAsInt());
post("/jenkins", (rq, rs) -> {
if (rq.contentType() == null) halt(404, "Invalid content Type!");
if (rq.contentType().equals("application/json")) {
if (!isAuthenticated(rq)) {
if (isVerbose) System.out.println("Is not authenticated");
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) {
if (jarFileNameJson == null || dockerContainerNameJson == null || workingDirectoryJson == null || downloadUrlJson == null) {
if (isVerbose) System.out.println("Not all params passed");
halt(405, "{ \"error\":\"Not all parameters passed\" }");
}
if (UpdateManager.update(appNameJson.getAsString(), jarFileNameJson.getAsString(), dockerContainerNameJson.getAsString(), workingDirectoryJson.getAsString(), downloadUrlJson.getAsString())) {
if (UpdateManager.update(jarFileNameJson.getAsString(), dockerContainerNameJson.getAsString(), workingDirectoryJson.getAsString(), downloadUrlJson.getAsString())) {
rs.status(200);
if (isVerbose) System.out.println("Success");
return "{ \"status\":\"success\" }";
}
} else {
if (isVerbose) System.out.println("Not found");
notFound("{ \"error\":404 }");
}
return "{ \"error\":\"500\" }";
@ -43,6 +53,15 @@ public class Main {
private static boolean isAuthenticated(Request rq) {
String authHeader = rq.headers("Authorization");
if (isVerbose) {
System.out.println("Auth Header: " + authHeader);
System.out.println("All headers: ");
for (String header : rq.headers()) {
System.out.print(header + ", ");
}
System.out.print("\n");
System.out.println("Is authenticated: " + AUTH_TOKENS.containsToken(authHeader));
}
return AUTH_TOKENS.containsToken(authHeader);
}
}

View File

@ -12,10 +12,10 @@ 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) {
public static boolean update(String jarFileName, String dockerContainerName, String workingDirectory, String downloadUrl) {
File workingDir = new File(workingDirectory);
if (!workingDir.isDirectory()) {
System.out.println("is not dir");
if (Main.isVerbose) System.out.println("Is not a directory");
return false;
}
@ -27,16 +27,17 @@ public class UpdateManager {
// Delete old jar file
File jarFile = new File(workingDirectory + "/" + jarFileName);
if (jarFile.exists() && jarFile.isFile()) {
System.out.println("deleted");
if (Main.isVerbose) System.out.println("Jarfile deleted");
return jarFile.delete();
}
// Download new jar file
try {
FileUtils.copyURLToFile(new URL(downloadUrl), jarFile);
FileUtils.copyURLToFile(new URL(downloadUrl), jarFile, 30000, 30000);
if (Main.isVerbose) System.out.println("Downloaded updated jarfile");
} catch (IOException e) {
e.printStackTrace();
System.out.println("error copying");
if (Main.isVerbose) System.out.println("error copying");
return false;
}
@ -58,6 +59,7 @@ public class UpdateManager {
private static String getContainerByName(DockerClient dockerClient, String containerName) {
for (Container container : dockerClient.listContainersCmd().exec()) {
if (Arrays.asList(container.getNames()).contains(containerName)) {
if (Main.isVerbose) System.out.println("Container id: " + container.getId());
return container.getId();
}
}