piscine
This commit is contained in:
parent
a82677b3bb
commit
1699603853
11
dockerfile/Dockerfile
Normal file
11
dockerfile/Dockerfile
Normal file
@ -0,0 +1,11 @@
|
||||
FROM linuxserver/openssh-server
|
||||
|
||||
RUN apk update
|
||||
RUN apk upgrade
|
||||
RUN apk add vim python3 py3-pip apache2 git
|
||||
RUN pip install discord-py --break-system-packages
|
||||
|
||||
RUN mkdir -p /run/apache2
|
||||
|
||||
|
||||
CMD exec /bin/sh -c "trap : TERM INT; sleep 9999999999d & wait"
|
||||
@ -7,10 +7,10 @@ import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.util.Map;
|
||||
|
||||
@Path("/containers")
|
||||
@Path("/docker")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class ContainerResource {
|
||||
public class DockerResource {
|
||||
|
||||
@Inject DockerService dockerService;
|
||||
|
||||
@ -46,8 +46,7 @@ public class ContainerResource {
|
||||
@Path("/all")
|
||||
public Response listContainers() {
|
||||
try {
|
||||
String id = dockerService.listAllContainers();
|
||||
return Response.ok(id).build();
|
||||
return Response.ok(dockerService.listAllContainers()).build();
|
||||
} catch (Exception e) {
|
||||
return Response.status(500)
|
||||
.entity(Map.of("error", e.getMessage()))
|
||||
@ -36,8 +36,8 @@ public class InstanceEndpoints {
|
||||
@POST
|
||||
@Path("/{id}/instance")
|
||||
public Response createInstance(@PathParam("id") Long jiId,
|
||||
@QueryParam("username") String username) {
|
||||
jiService.createInstance(jiId, username);
|
||||
@QueryParam("userId") Long userId) {
|
||||
jiService.createInstance(jiId, userId);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
|
||||
@ -1,12 +1,7 @@
|
||||
package fr.la_banquise.backend.rest;
|
||||
|
||||
import fr.la_banquise.backend.data.model.RolesAsso;
|
||||
import fr.la_banquise.backend.data.model.User;
|
||||
import fr.la_banquise.backend.rest.request.BulkUserDelRequest;
|
||||
import fr.la_banquise.backend.rest.request.BulkUserPostRequest;
|
||||
import fr.la_banquise.backend.rest.request.UserRequest;
|
||||
import fr.la_banquise.backend.rest.response.BulkUserDelResponse;
|
||||
import fr.la_banquise.backend.rest.response.BulkUserPostResponse;
|
||||
import fr.la_banquise.backend.rest.response.LoggedUserResponse;
|
||||
import fr.la_banquise.backend.services.UserService;
|
||||
import io.quarkus.security.Authenticated;
|
||||
@ -22,7 +17,6 @@ import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -72,9 +66,8 @@ public class UserEndpoints {
|
||||
public Response addRole(@PathParam("id") Long userId,
|
||||
@QueryParam("role") String role) {
|
||||
try {
|
||||
User user = userService.getUser(userId);
|
||||
user.role.add(userService.fromString(role));
|
||||
return Response.ok(user.role).build();
|
||||
userService.updateRole(role, userId);
|
||||
return Response.ok().build();
|
||||
} catch (Exception e) {
|
||||
return Response.status(404)
|
||||
.entity(Map.of("User or Role not found", e))
|
||||
@ -88,9 +81,8 @@ public class UserEndpoints {
|
||||
public Response removeRole(@PathParam("id") Long userId,
|
||||
@QueryParam("role") String role) {
|
||||
try {
|
||||
User user = userService.getUser(userId);
|
||||
user.role.remove(userService.fromString(role));
|
||||
return Response.ok(user.role).build();
|
||||
userService.removeRole(role, userId);
|
||||
return Response.ok().build();
|
||||
} catch (Exception e) {
|
||||
return Response.status(404)
|
||||
.entity(Map.of("User or Role not found", e))
|
||||
|
||||
@ -49,11 +49,11 @@ public class UsersEndpoints {
|
||||
public Response createUsersBulk(BulkUserPostRequest users) {
|
||||
BulkUserPostResponse response = userService.createUsers(
|
||||
users);
|
||||
if (response.success_names.size() == users.users.size())
|
||||
if (response.successNames.size() == users.users.size())
|
||||
return Response.ok().build();
|
||||
return Response.status(202)
|
||||
.entity(Map.of("These users were already created : ",
|
||||
response.already_created))
|
||||
response.alreadyCreated))
|
||||
.build();
|
||||
}
|
||||
|
||||
@ -61,13 +61,13 @@ public class UsersEndpoints {
|
||||
@RolesAllowed("ROOT")
|
||||
public Response deleteUserBulk(BulkUserDelRequest users) {
|
||||
BulkUserDelResponse response = userService.deleteUsers(users);
|
||||
if (response.success_names.size() == users.usernames.size())
|
||||
if (response.successNames.size() == users.usernames.size())
|
||||
return Response.ok().build();
|
||||
|
||||
Map<String, String> retour = new HashMap<String, String>();
|
||||
for (int id = 0; id < response.failed_names.size(); id++) {
|
||||
retour.put(response.failed_names.get(id),
|
||||
response.failed_reasons.get(id));
|
||||
for (int id = 0; id < response.failedNames.size(); id++) {
|
||||
retour.put(response.failedNames.get(id),
|
||||
response.failedReasons.get(id));
|
||||
}
|
||||
return Response.status(202).entity(retour).build();
|
||||
}
|
||||
|
||||
@ -7,13 +7,13 @@ import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
public class BulkUserDelResponse {
|
||||
public List<String> success_names;
|
||||
public List<String> failed_names;
|
||||
public List<String> failed_reasons;
|
||||
public List<String> successNames;
|
||||
public List<String> failedNames;
|
||||
public List<String> failedReasons;
|
||||
|
||||
public BulkUserDelResponse() {
|
||||
this.success_names = new ArrayList<String>();
|
||||
this.failed_names = new ArrayList<String>();
|
||||
this.failed_reasons = new ArrayList<String>();
|
||||
this.successNames = new ArrayList<String>();
|
||||
this.failedNames = new ArrayList<String>();
|
||||
this.failedReasons = new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,11 +7,11 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class BulkUserPostResponse {
|
||||
public List<String> success_names;
|
||||
public List<String> already_created;
|
||||
public List<String> successNames;
|
||||
public List<String> alreadyCreated;
|
||||
|
||||
public BulkUserPostResponse() {
|
||||
this.success_names = new ArrayList<String>();
|
||||
this.already_created = new ArrayList<String>();
|
||||
this.successNames = new ArrayList<String>();
|
||||
this.alreadyCreated = new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package fr.la_banquise.backend.services;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.BuildImageResultCallback;
|
||||
import com.github.dockerjava.api.command.CreateContainerResponse;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
@ -9,11 +10,15 @@ import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.PortBinding;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class DockerService {
|
||||
@ -46,26 +51,7 @@ public class DockerService {
|
||||
return container.getId();
|
||||
}
|
||||
|
||||
public String createContainer(String containerName, int port) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
ExposedPort tcpSsh = ExposedPort.tcp(80);
|
||||
|
||||
Ports portBindings = new Ports();
|
||||
portBindings.bind(tcpSsh, Ports.Binding.bindPort(port));
|
||||
|
||||
HostConfig hostConfig =
|
||||
HostConfig.newHostConfig().withPortBindings(portBindings);
|
||||
|
||||
CreateContainerResponse container =
|
||||
dockerClient.createContainerCmd("nginx:latest")
|
||||
.withName(containerName)
|
||||
.withExposedPorts(tcpSsh)
|
||||
.withHostConfig(hostConfig)
|
||||
.exec();
|
||||
|
||||
// dockerClient.startContainerCmd(container.getId()).exec();
|
||||
return container.getId();
|
||||
}
|
||||
/*
|
||||
public String listAllContainers() {
|
||||
StringBuilder json = new StringBuilder();
|
||||
@ -176,7 +162,70 @@ public class DockerService {
|
||||
json.append("]");
|
||||
return json.toString();
|
||||
}*/
|
||||
public String listAllContainers() { return ""; }
|
||||
|
||||
|
||||
////////////////////
|
||||
public String createContainer(String name, int port) {
|
||||
ExposedPort tcpSsh = ExposedPort.tcp(2222);
|
||||
|
||||
Ports portBindings = new Ports();
|
||||
portBindings.bind(tcpSsh, Ports.Binding.bindPort(port));
|
||||
|
||||
HostConfig hostConfig =
|
||||
HostConfig.newHostConfig().withPortBindings(portBindings);
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
labels.put("test", "banquise");
|
||||
labels.put("environment", "development");
|
||||
labels.put("version", "1.0");
|
||||
labels.put("created-by", "java-docker-api");
|
||||
|
||||
dockerClient.buildImageCmd()
|
||||
.withDockerfile(new File("./dockerfile/Dockerfile"))
|
||||
.withBaseDirectory(new File("./dockerfile"))
|
||||
.withTags(Set.of("ji-python:latest"))
|
||||
.exec(new BuildImageResultCallback())
|
||||
.awaitImageId();
|
||||
|
||||
CreateContainerResponse container =
|
||||
dockerClient.createContainerCmd("ji-python:latest")
|
||||
.withName(name)
|
||||
.withLabels(labels)
|
||||
.withExposedPorts(tcpSsh)
|
||||
.withHostConfig(hostConfig)
|
||||
.withEnv(
|
||||
"SUDO_ACCESS=false",
|
||||
"PASSWORD_ACCESS=true",
|
||||
"USER_NAME=test", // TODO: CHANGE THIS
|
||||
"USER_PASSWORD=test" // AND this also oc
|
||||
)
|
||||
.exec();
|
||||
|
||||
return container.getId();
|
||||
}
|
||||
|
||||
public InspectContainerResponse.ContainerState getStatusContainer(String name) {
|
||||
InspectContainerResponse container =
|
||||
dockerClient.inspectContainerCmd(name).exec();
|
||||
return container.getState();
|
||||
}
|
||||
|
||||
public boolean remove(String name) {
|
||||
try {
|
||||
dockerClient.removeContainerCmd(name).exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containerExists(String name) {
|
||||
try {
|
||||
dockerClient.inspectContainerCmd(name).exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void start(String containerId) {
|
||||
dockerClient.startContainerCmd(containerId).exec();
|
||||
@ -186,7 +235,11 @@ public class DockerService {
|
||||
dockerClient.stopContainerCmd(containerId).exec();
|
||||
}
|
||||
|
||||
public void remove(String containerId) {
|
||||
dockerClient.removeContainerCmd(containerId).exec();
|
||||
public List<Container> listAllContainers() {
|
||||
return dockerClient.listContainersCmd()
|
||||
.withShowAll(true)
|
||||
.exec();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -35,14 +35,14 @@ public class InstanceService {
|
||||
|
||||
@Inject JiRepository jiRepository;
|
||||
|
||||
@Inject DockerClient dockerClient;
|
||||
@Inject DockerService dockerService;
|
||||
|
||||
public List<Instance> getAllInstances() {
|
||||
return instanceRepository.findAll().list();
|
||||
}
|
||||
|
||||
public List<Instance> getInstancesByOwner(String username) {
|
||||
User user = userRepository.findByName(username);
|
||||
public List<Instance> getInstancesByOwner(Long id) {
|
||||
User user = userRepository.findById(id);
|
||||
return instanceRepository.find("owner", user).list();
|
||||
}
|
||||
|
||||
@ -52,15 +52,13 @@ public class InstanceService {
|
||||
|
||||
public InspectContainerResponse.ContainerState getStatusContainer(Long id) {
|
||||
Instance instance = instanceRepository.findById(id);
|
||||
InspectContainerResponse container =
|
||||
dockerClient.inspectContainerCmd(instance.name).exec();
|
||||
return container.getState();
|
||||
return dockerService.getStatusContainer(instance.name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Instance createInstance(String username, Ji ji) {
|
||||
User user = userRepository.findByName(username);
|
||||
String name = username + "-" + ji.id;
|
||||
public Instance createInstance(Long id, Ji ji) {
|
||||
User user = userRepository.findById(id);
|
||||
String name = user.name + "-" + ji.id;
|
||||
int port = getFreePort(1).iterator().next();
|
||||
Instance instance = new Instance(name, port, user);
|
||||
instanceRepository.persist(instance);
|
||||
@ -70,56 +68,23 @@ public class InstanceService {
|
||||
public String createContainer(Long instanceId) {
|
||||
Instance instance = instanceRepository.findById(instanceId);
|
||||
|
||||
ExposedPort tcpSsh = ExposedPort.tcp(22);
|
||||
|
||||
Ports portBindings = new Ports();
|
||||
portBindings.bind(tcpSsh, Ports.Binding.bindPort(instance.port));
|
||||
|
||||
HostConfig hostConfig =
|
||||
HostConfig.newHostConfig().withPortBindings(portBindings);
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
labels.put("test", "banquise");
|
||||
labels.put("environment", "development");
|
||||
labels.put("version", "1.0");
|
||||
labels.put("created-by", "java-docker-api");
|
||||
|
||||
CreateContainerResponse container =
|
||||
dockerClient.createContainerCmd("nginx:latest")
|
||||
.withName(instance.name)
|
||||
.withLabels(labels)
|
||||
.withExposedPorts(tcpSsh)
|
||||
.withHostConfig(hostConfig)
|
||||
.exec();
|
||||
|
||||
return container.getId();
|
||||
return dockerService.createContainer(instance.name, instance.port);
|
||||
}
|
||||
|
||||
public boolean deleteContainer(String containerName) {
|
||||
try {
|
||||
dockerClient.removeContainerCmd(containerName).exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
public boolean deleteContainer(Long id) {
|
||||
Instance instance = instanceRepository.findById(id);
|
||||
|
||||
return dockerService.remove(instance.name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean deleteInstance(Long id) {
|
||||
Instance instance = instanceRepository.findById(id);
|
||||
if (!containerExists(instance.name))
|
||||
if (!dockerService.containerExists(instance.name))
|
||||
return instanceRepository.deleteById(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean containerExists(String containerName) {
|
||||
try {
|
||||
dockerClient.inspectContainerCmd(containerName).exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Integer> getUsedPorts() {
|
||||
Set<Integer> retour = new HashSet<>();
|
||||
List<Instance> allInstances = getAllInstances();
|
||||
@ -146,4 +111,5 @@ public class InstanceService {
|
||||
|
||||
return retour;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -61,9 +61,9 @@ public class JiService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Instance createInstance(Long id, String username) {
|
||||
Ji ji = jiRepository.findById(id);
|
||||
Instance instance = instanceService.createInstance(username, ji);
|
||||
public Instance createInstance(Long jiId, Long userId) {
|
||||
Ji ji = jiRepository.findById(jiId);
|
||||
Instance instance = instanceService.createInstance(userId, ji);
|
||||
ji.instances.add(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
@ -90,6 +90,8 @@ public class SujetService {
|
||||
}
|
||||
/*
|
||||
* TODO: Seuls les respos du sujet et root doivent pouvoir avoir acces
|
||||
* TODO: move ce todo et refactor poru que les check d acces soient dans
|
||||
* rest et pas dans services
|
||||
* @Transactional
|
||||
public Sujet updateSujet(Long id) {
|
||||
Sujet sujet = sujetRepository.findById(id);
|
||||
|
||||
@ -68,10 +68,10 @@ public class UserService {
|
||||
userToDel =
|
||||
userRepository.find("name", requestName.name).firstResult();
|
||||
userRepository.delete(userToDel);
|
||||
response.success_names.add(requestName.name);
|
||||
response.successNames.add(requestName.name);
|
||||
} catch (Exception e) {
|
||||
response.failed_names.add(requestName.name);
|
||||
response.failed_reasons.add(e.toString());
|
||||
response.failedNames.add(requestName.name);
|
||||
response.failedReasons.add(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,9 +86,9 @@ public class UserService {
|
||||
for (UserRequest user : usersRequest.users) {
|
||||
try {
|
||||
createUser(user);
|
||||
response.success_names.add(user.name);
|
||||
response.successNames.add(user.name);
|
||||
} catch (Exception e) {
|
||||
response.already_created.add(user.name);
|
||||
response.alreadyCreated.add(user.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -106,4 +106,17 @@ public class UserService {
|
||||
default -> throw new Error("Wrong role str");
|
||||
};
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateRole(String role, Long userId) {
|
||||
User user = userRepository.findById(userId);
|
||||
user.role.add(fromString(role));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeRole(String role, Long userId) {
|
||||
User user = userRepository.findById(userId);
|
||||
user.role.remove(fromString(role));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user