Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
fix(AppStore): deployment of DataApps via Portainer (#886)
Browse files Browse the repository at this point in the history
* fix(AppStore): reset ActionsType enum, name Portainer container, fail-safe image-url

* test: fix ActionTypeTest toString_nothing_returnValidOutput

* feat(AppController): add app.props var for connector network

* fix(AppController): use internal docker port and not exposed

* docs: add fixes to changelog
  • Loading branch information
tmberthold authored Feb 7, 2022
1 parent 1cb7011 commit 52ebae4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 48 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ All notable changes to this project will be documented in this file.

## [X.X.X] - XXXX-XX-XX

### Added
- Add `portainer.application.connector.network` to `application.properties.`. Default is set to `local`.

### Changed
- Increase equalsverifier version from 3.8.3 to 3.9.
- Increase postgresql version from 42.3.1 to 42.3.2.
- Increase flyway-core version from 8.4.3 to 8.4.4.
- Increase pitest-maven version from 1.7.3 to 1.7.4.

###Fixed
### Fixed
- Deployment of DataApps via Portainer
- Escape data source URL before creating data source bean.

## [7.0.0] - 2022-01-31
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import okhttp3.Response;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -90,6 +91,12 @@ public class AppController extends BaseResourceController<App, AppDesc, AppView,
*/
private static final int DEFAULT_HTTPS_PORT = 443;

/**
* The network of the connector to join apps in.
*/
@Value("${portainer.application.connector.network:local}")
private String connectorNetwork;

@Hidden
@ApiResponse(responseCode = ResponseCode.METHOD_NOT_ALLOWED,
description = ResponseDescription.METHOD_NOT_ALLOWED)
Expand Down Expand Up @@ -276,17 +283,19 @@ private String deployApp(final App app) throws IOException, AppNotDeployedExcept
final var containerId = portainerSvc.createContainer(template, volumeMap,
app.getEndpoints());

// 5. Get container description from portainer (e.g. randomly created container-name).
// 5. Get container description from portainer.
final var containerDesc = portainerSvc.getDescriptionByContainerId(containerId);
persistContainerData(app, containerId, containerDesc);

// 6. Get "bride" network-id in Portainer
final var networkId = portainerSvc.getNetworkId("bridge");
// 6. Get "bride" network-id in Portainer and join app in network
final var networkIdBridge = portainerSvc.getNetworkId("bridge");
portainerSvc.joinNetwork(containerId, networkIdBridge);

// 7. Join container into the new created network.
portainerSvc.joinNetwork(containerId, networkId);
// 7. Get setting for connector network and join app in network
final var networkIdConnector = portainerSvc.getNetworkId(connectorNetwork);
portainerSvc.joinNetwork(containerId, networkIdConnector);

// 8. Delete registry (credentials are one-time-usage)
// 8. Delete registry (credentials should be one-time-usage)
portainerSvc.deleteRegistry(registryId);

return containerId;
Expand Down Expand Up @@ -316,17 +325,16 @@ private void persistContainerData(final App app, final String containerId,

// Generate endpoint accessURLs depending on deployment information.
for (final var endpoint : app.getEndpoints()) {
final var port = endpoint.getEndpointPort();

// Uses IDS endpoint description info and not template (/api/apps/{id}/endpoints).
final var protocol =
endpoint.getEndpointPort() == DEFAULT_HTTPS_PORT ? "https://" : "http://";
final var protocol = port == DEFAULT_HTTPS_PORT ? "https://" : "http://";

// Uses IDS endpoint description info and not template (/api/apps/{id}/endpoints).
final var suffix =
endpoint.getPath() != null ? endpoint.getPath() : "";

final var exposedPort = endpoint.getExposedPort();

final var location = protocol + containerName + ":" + exposedPort + suffix;
final var location = protocol + containerName + ":" + port + suffix;
appEndpointSvc.setLocation(endpoint, location);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,20 @@ public enum ActionType {
/**
* Start an app.
*/
START("Start"),
START,

/**
* Stop an app.
*/
STOP("Stop"),
STOP,

/**
* Delete an app.
*/
DELETE("Delete"),
DELETE,

/**
* Describes an app.
*/
DESCRIBE("Describe");

/**
* Holds the enums string.
*/
private final String value;

/**
* Constructor.
*
* @param name The name of the action type.
*/
ActionType(final String name) {
this.value = name;
}

@Override
public String toString() {
return value;
}

DESCRIBE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,6 @@ public String createContainer(
final List<AppEndpointImpl> appEndpoints
) throws IOException {
final var templateObject = toJsonObject(appStoreTemplate);
final var image = templateObject.getString("image");

//get all ports from the appTemplate (with label)
var portLabelMap = new HashMap<String, String>();
Expand All @@ -818,12 +817,15 @@ public String createContainer(
}
}

final var containerName = "app-" + System.currentTimeMillis();

final var builder = getRequestBuilder();
final var urlBuilder = new HttpUrl.Builder()
.scheme(portainerConfig.getScheme())
.host(portainerConfig.getHost())
.port(portainerConfig.getPort())
.addPathSegments(API_ENDPOINT + endpointId + "/docker/containers/create");
.addPathSegments(API_ENDPOINT + endpointId + "/docker/containers/create")
.addQueryParameter("name", containerName);

final var url = urlBuilder.build();
builder.addHeader("Authorization", "Bearer " + getJwtToken());
Expand All @@ -835,8 +837,7 @@ public String createContainer(
templateObject,
new ArrayList<>(portLabelMap.keySet()),
volumes,
image
);
containerName);

//add json payload to request
builder.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,31 @@ private PortainerUtil() {
* @param templateObject The template object.
* @param ports The list of ports.
* @param volumes Map of volumes.
* @param image The image name.
* @param containerName The container name.
* @return payload for the creation of a container.
*/
public static JSONObject createContainerJSONPayload(final JSONObject templateObject,
final List<String> ports,
final Map<String, String> volumes,
final String image) {

final String containerName) {
// Build json payload and fill single fields.
final var jsonPayload = new JSONObject() {{
put("Env", new JSONArray());
put("OpenStdin", false);
put("Tty", false);
put("Labels", new JSONObject());
put("name", "");
put("Cmd", new JSONArray());
put("Image", templateObject.getString("registry")
+ "/" + templateObject.getString("image"));
put("name", containerName);
}};

// Build image url and add it to payload.
var imageUrl = templateObject.getString("registry");
if (!imageUrl.endsWith("/")) {
imageUrl += "/";
}
imageUrl += templateObject.getString("image");
jsonPayload.put("Image", imageUrl);

// Build exposed ports part of json payload.
final var exposedPorts = new JSONObject();
for (final var port : ports) {
Expand Down Expand Up @@ -92,7 +97,6 @@ public static JSONObject createContainerJSONPayload(final JSONObject templateObj
hostConfig.put("Binds", binds);

jsonPayload.put("HostConfig", hostConfig);
jsonPayload.put("name", image);

// Build volumes part of json payload.
final var volumesJSON = new JSONObject();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ portainer.application.host=localhost
portainer.application.port=9000
portainer.application.username=admin
portainer.application.password=password
portainer.application.connector.network=local

####################################################################################################
## Spring Security settings ##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void toString_nothing_returnValidOutput() {
final var inputAsString = input.toString();

/* ASSERT */
assertEquals("Start", inputAsString);
assertEquals("START", inputAsString);
}

}

0 comments on commit 52ebae4

Please sign in to comment.