Commit 82044df5 authored by Christoph Holter's avatar Christoph Holter
Browse files
parents 1d1fec96 9adf0974
package cc.catalysts.api;
import cc.catalysts.config.NavbarConfig;
import cc.catalysts.exception.DockerException;
import cc.catalysts.exception.UamResourceException;
import cc.catalysts.repository.DockerContainerRepository;
import cc.catalysts.service.DockerService;
import cc.catalysts.service.UamService;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
......@@ -22,6 +25,8 @@ public class InteractiveApplicationServiceApiController implements InteractiveAp
private final UamService uamService;
private final NavbarConfig navbarConfig;
private final static Logger LOGGER = LoggerFactory.getLogger(InteractiveApplicationServiceApiController.class);
@Autowired
public InteractiveApplicationServiceApiController(DockerService dockerService,
DockerContainerRepository repository,
......@@ -38,23 +43,28 @@ public class InteractiveApplicationServiceApiController implements InteractiveAp
}
@Override
public DockerContainer startContainer(@RequestBody StartContainerParameters parameters, Principal principal) throws UamResourceException {
public DockerContainer startContainer(@RequestBody StartContainerParameters parameters, Principal principal) throws ApiException {
if (this.uamService.getAvailableContainers(userFromPrincipal(principal)) <= 0) {
throw new UamResourceException("No containers available");
throw new UamResourceException("No more apps available");
}
if (!this.uamService.consumeContainer(userFromPrincipal(principal))) {
if (!this.uamService.consumeContainer(userFromPrincipal(principal), parameters.getImage().getName())) {
throw new UamResourceException("Could not consume resource");
}
DockerContainer ctn = dockerService.startContainer(parameters.getImage(),
userFromPrincipal(principal),
parameters.getName(),
parameters.getDescription());
if (parameters.isShared()) {
dockerService.shareContainer(ctn, userFromPrincipal(principal));
try {
DockerContainer ctn = dockerService.startContainer(parameters.getImage(),
userFromPrincipal(principal),
parameters.getName(),
parameters.getDescription());
if (parameters.isShared()) {
dockerService.shareContainer(ctn, userFromPrincipal(principal));
}
return ctn;
} catch (com.github.dockerjava.api.exception.DockerException e) {
LOGGER.error("app start failed, exception was: " + e.getMessage());
throw new DockerException("Start of app failed");
}
return ctn;
}
@Override
......@@ -62,30 +72,62 @@ public class InteractiveApplicationServiceApiController implements InteractiveAp
return dockerContainerRepository.getRunningContainers(userFromPrincipal(principal));
}
@Override
public void stopContainer(@RequestBody DockerContainer ctn,
Principal principal) {
dockerService.stopContainer(ctn, userFromPrincipal(principal));
Principal principal) throws ApiException {
try {
dockerService.stopContainer(ctn, userFromPrincipal(principal));
} catch (com.github.dockerjava.api.exception.DockerException e) {
LOGGER.error("app stop failed, exception was: " + e.getMessage());
throw new DockerException("Stop of app failed");
}
}
@Override
public void resumeContainer(@RequestBody DockerContainer ctn, Principal principal) {
dockerService.resumeContainer(ctn, userFromPrincipal(principal));
public void resumeContainer(@RequestBody DockerContainer ctn, Principal principal) throws ApiException {
try {
dockerService.resumeContainer(ctn, userFromPrincipal(principal));
} catch (com.github.dockerjava.api.exception.DockerException e) {
LOGGER.error("app resume failed, exception was: " + e.getMessage());
throw new DockerException("Resume of app failed");
}
}
@Override
public void removeContainer(@RequestBody DockerContainer ctn, Principal principal) {
this.dockerService.removeContainer(ctn, userFromPrincipal(principal));
this.uamService.allocateContainer(userFromPrincipal(principal));
public void removeContainer(@RequestBody DockerContainer ctn, Principal principal) throws ApiException {
try {
this.dockerService.removeContainer(ctn, userFromPrincipal(principal));
} catch (com.github.dockerjava.api.exception.DockerException e) {
LOGGER.error("app removal failed, exception was: " + e.getMessage());
throw new DockerException("Removal of app failed");
}
if (!this.uamService.allocateContainer(userFromPrincipal(principal), ctn.getImageName())) {
throw new UamResourceException("Could not allocate resource");
};
}
@Override
public void shareContainer(@ApiParam(value = "The container to share") @RequestBody DockerContainer ctn, Principal principal) {
this.dockerService.shareContainer(ctn, userFromPrincipal(principal));
public void shareContainer(
@ApiParam(value = "The app to share") @RequestBody DockerContainer ctn,
Principal principal) throws ApiException {
try {
this.dockerService.shareContainer(ctn, userFromPrincipal(principal));
} catch (com.github.dockerjava.api.exception.DockerException e) {
LOGGER.error("sharing failed, exception was: " + e.getMessage());
throw new DockerException("Sharing of app failed");
}
}
@Override
public void unshareContainer(@ApiParam(value = "The container to unshare") @RequestBody DockerContainer ctn, Principal principal) {
public void unshareContainer(
@ApiParam(value = "The app to unshare") @RequestBody DockerContainer ctn,
Principal principal) throws ApiException {
try {
this.dockerService.unshareContainer(ctn, userFromPrincipal(principal));
} catch (com.github.dockerjava.api.exception.DockerException e) {
LOGGER.error("unsharing failed, exception was: " + e.getMessage());
throw new DockerException("Unsharing of app failed");
}
}
@Override
......
package cc.catalysts.exception;
import cc.catalysts.api.ApiException;
public class DockerException extends ApiException {
public DockerException(String message) {
super("Docker exception: ", message);
}
}
......@@ -21,7 +21,6 @@ import org.apache.commons.lang.text.StrSubstitutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.MessageFormat;
import java.text.Normalizer;
import java.util.*;
import java.util.stream.Collectors;
......
......@@ -74,8 +74,8 @@ public class UamService {
return (int) response.getBody().getCount();
}
public boolean consumeContainer(User user) {
ConsumptionDto consumptionDto = new ConsumptionDto(this.resourceConcurrentContainers, 1, "started Container");
public boolean consumeContainer(User user, String appName) {
ConsumptionDto consumptionDto = new ConsumptionDto(this.resourceConcurrentContainers, 1, "started app: " + appName);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<ConsumptionResponseDto> response = restTemplate.exchange(
......@@ -92,8 +92,8 @@ public class UamService {
return true;
}
public void allocateContainer(User user) {
AllocationDto allocationDto = new AllocationDto(this.resourceConcurrentContainers, 1, "removed Container", null);
public boolean allocateContainer(User user, String appName) {
AllocationDto allocationDto = new AllocationDto(this.resourceConcurrentContainers, 1, "removed app: " + appName, null);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<String> response = new RestTemplate().exchange(
......@@ -103,5 +103,10 @@ public class UamService {
String.class,
user.getId()
);
if (response == null ||
!response.getStatusCode().equals(HttpStatus.OK)) {
return false;
}
return true;
}
}
......@@ -2,8 +2,6 @@ package cc.catalysts.uam.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.validation.constraints.Size;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResourceType {
......
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
......@@ -27,19 +27,15 @@ export class ContainerComponent {
public constructor(private apiService: InteractiveApplicationServiceApi) {
}
public getStateName(): string {
return DockerContainer.StateEnum[this.container.state];
}
public isPausedContainer(): boolean {
return this.container.state === DockerContainer.StateEnum.STOPPED;
}
public getShareToggleButtonInfo(): string {
if (this.container.shared) {
return 'Make private';
return 'Make app private';
} else {
return 'Share container';
return 'Share app';
}
}
......
......@@ -14,7 +14,7 @@
<template #startContainerModal let-start="close" let-d="dismiss">
<div class="modal-header">
<h5 class="modal-title" id="startContainerModalLabel">Start container {{ imageInfo.image.name }}</h5>
<h5 class="modal-title" id="startContainerModalLabel">Start app {{ imageInfo.image.name }}</h5>
<button type="button" class="close" aria-label="Close" (click)="d()">
<span aria-hidden="true">&times;</span>
</button>
......@@ -56,7 +56,8 @@
<template #errorMessageModal let-d="dismiss">
<div class="modal-header">
<h5 class="modal-title">Something went wrong</h5>
<h5 class="modal-title" *ngIf="!errorTitle">Something went wrong</h5>
<h5 class="modal-title" *ngIf="errorTitle">{{ errorTitle }}</h5>
<button type="button" class="close" aria-label="Close" (click)="d()">
<span aria-hidden="true">&times;</span>
</button>
......
......@@ -42,6 +42,7 @@ export class ImageItemComponent {
public selectedContainer?: DockerContainer = null;
public errorTitle: string;
public errorMessage: string;
constructor(private dockerService: DockerImageService,
......@@ -78,9 +79,10 @@ export class ImageItemComponent {
},
(err) => {
try {
this.showError((<ApiException>err.json()).whatsWrong);
const exception: ApiException = <ApiException>err.json();
this.showError(exception.message, exception.whatsWrong);
} catch (_) {
this.showError('Could not start container.');
this.showError('Could not start app.', null);
}
console.error(err);
}
......@@ -103,9 +105,10 @@ export class ImageItemComponent {
},
(err) => {
try {
this.showError((<ApiException>err.json()).whatsWrong);
const exception: ApiException = <ApiException>err.json();
this.showError(exception.message, exception.whatsWrong);
} catch (_) {
this.showError('Could not remove container.');
this.showError('Could not remove app.', null);
}
console.error(err);
}
......@@ -119,9 +122,10 @@ export class ImageItemComponent {
() => container.state = DockerContainer.StateEnum.RUNNING,
(err) => {
try {
this.showError((<ApiException>err.json()).whatsWrong);
const exception: ApiException = <ApiException>err.json();
this.showError(exception.message, exception.whatsWrong);
} catch (_) {
this.showError('Could not resume container.');
this.showError('Could not resume app.', null);
}
console.error(err);
}
......@@ -135,9 +139,10 @@ export class ImageItemComponent {
() => container.state = DockerContainer.StateEnum.STOPPED,
(err) => {
try {
this.showError((<ApiException>err.json()).whatsWrong);
const exception: ApiException = <ApiException>err.json();
this.showError(exception.message, exception.whatsWrong);
} catch (_) {
this.showError('Could not stop container.');
this.showError('Could not stop app.', null);
}
console.error(err);
}
......@@ -145,7 +150,8 @@ export class ImageItemComponent {
}
}
private showError(message: string) {
private showError(message: string, title: string) {
this.errorTitle = title;
this.errorMessage = message;
this.modalService.open(this.errorMessageModalRef);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment