You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datalab.apache.org by of...@apache.org on 2020/11/24 17:50:53 UTC
[incubator-datalab] 01/05: merge odahu (provisioning)
This is an automated email from the ASF dual-hosted git repository.
ofuks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-datalab.git
commit 43b7e1881e98774663243cee456f89d1630986ee
Author: Oleh Fuks <ol...@gmail.com>
AuthorDate: Fri Nov 20 13:05:42 2020 +0200
merge odahu (provisioning)
---
.../epam/datalab/dto/base/odahu/OdahuResult.java | 48 +++++++++
.../com/epam/datalab/dto/odahu/ActionOdahuDTO.java | 46 +++++++++
.../com/epam/datalab/dto/odahu/CreateOdahuDTO.java | 38 +++++++
.../backendapi/ProvisioningServiceApplication.java | 2 +
.../core/commands/CommandExecutorMockAsync.java | 2 +-
.../response/handlers/OdahuCallbackHandler.java | 94 +++++++++++++++++
.../backendapi/modules/ProductionModule.java | 3 +
.../backendapi/modules/ProvisioningDevModule.java | 3 +
.../backendapi/resources/OdahuResource.java | 70 +++++++++++++
.../datalab/backendapi/service/OdahuService.java | 34 ++++++
.../backendapi/service/impl/OdahuServiceImpl.java | 115 +++++++++++++++++++++
11 files changed, 454 insertions(+), 1 deletion(-)
diff --git a/services/datalab-model/src/main/java/com/epam/datalab/dto/base/odahu/OdahuResult.java b/services/datalab-model/src/main/java/com/epam/datalab/dto/base/odahu/OdahuResult.java
new file mode 100644
index 0000000..0241929
--- /dev/null
+++ b/services/datalab-model/src/main/java/com/epam/datalab/dto/base/odahu/OdahuResult.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.dto.base.odahu;
+
+import com.epam.datalab.dto.ResourceURL;
+import com.epam.datalab.dto.StatusBaseDTO;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class OdahuResult extends StatusBaseDTO<OdahuResult> {
+ private String name;
+ @JsonProperty("project_name")
+ private String projectName;
+ @JsonProperty("endpoint_name")
+ private String endpointName;
+ @JsonProperty("grafana_admin")
+ private String grafanaAdmin;
+ @JsonProperty("grafana_pass")
+ private String grafanaPassword;
+ @JsonProperty("oauth_cookie_secret")
+ private String oauthCookieSecret;
+ @JsonProperty("odahuflow_connection_decrypt_token")
+ private String decryptToken;
+ @JsonProperty("odahu_urls")
+ private List<ResourceURL> resourceUrls;
+}
diff --git a/services/datalab-model/src/main/java/com/epam/datalab/dto/odahu/ActionOdahuDTO.java b/services/datalab-model/src/main/java/com/epam/datalab/dto/odahu/ActionOdahuDTO.java
new file mode 100644
index 0000000..8ccdb55
--- /dev/null
+++ b/services/datalab-model/src/main/java/com/epam/datalab/dto/odahu/ActionOdahuDTO.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.dto.odahu;
+
+import com.epam.datalab.dto.ResourceBaseDTO;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class ActionOdahuDTO extends ResourceBaseDTO<ActionOdahuDTO> {
+ @JsonProperty("odahu_cluster_name")
+ private final String name;
+ @JsonProperty("project_name")
+ private final String project;
+ @JsonProperty("endpoint_name")
+ private final String endpoint;
+ @JsonProperty("ssh_key")
+ private final String key;
+ @JsonProperty("grafana_admin")
+ private String grafanaAdmin;
+ @JsonProperty("grafana_pass")
+ private String grafanaPassword;
+ @JsonProperty("oauth_cookie_secret")
+ private String oauthCookieSecret;
+ @JsonProperty("odahuflow_connection_decrypt_token")
+ private String decryptToken;
+}
diff --git a/services/datalab-model/src/main/java/com/epam/datalab/dto/odahu/CreateOdahuDTO.java b/services/datalab-model/src/main/java/com/epam/datalab/dto/odahu/CreateOdahuDTO.java
new file mode 100644
index 0000000..2fa47bf
--- /dev/null
+++ b/services/datalab-model/src/main/java/com/epam/datalab/dto/odahu/CreateOdahuDTO.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.dto.odahu;
+
+import com.epam.datalab.dto.ResourceBaseDTO;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class CreateOdahuDTO extends ResourceBaseDTO<CreateOdahuDTO> {
+ @JsonProperty("odahu_cluster_name")
+ private final String name;
+ @JsonProperty("project_name")
+ private final String project;
+ @JsonProperty("endpoint_name")
+ private final String endpoint;
+ @JsonProperty("ssh_key")
+ private final String key;
+}
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/ProvisioningServiceApplication.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/ProvisioningServiceApplication.java
index fa24201..76a1cdb 100644
--- a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/ProvisioningServiceApplication.java
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/ProvisioningServiceApplication.java
@@ -34,6 +34,7 @@ import com.epam.datalab.backendapi.resources.GitExploratoryResource;
import com.epam.datalab.backendapi.resources.ImageResource;
import com.epam.datalab.backendapi.resources.InfrastructureResource;
import com.epam.datalab.backendapi.resources.LibraryResource;
+import com.epam.datalab.backendapi.resources.OdahuResource;
import com.epam.datalab.backendapi.resources.ProjectResource;
import com.epam.datalab.backendapi.resources.ProvisioningHealthCheckResource;
import com.epam.datalab.backendapi.resources.base.KeyResource;
@@ -155,6 +156,7 @@ public class ProvisioningServiceApplication extends Application<ProvisioningServ
jersey.register(injector.getInstance(KeyResource.class));
jersey.register(injector.getInstance(CallbackHandlerResource.class));
jersey.register(injector.getInstance(ProjectResource.class));
+ jersey.register(injector.getInstance(OdahuResource.class));
jersey.register(injector.getInstance(ProvisioningHealthCheckResource.class));
environment.jersey().register(injector.getInstance(BucketResource.class));
}
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/commands/CommandExecutorMockAsync.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/commands/CommandExecutorMockAsync.java
index 38d6ca8..b65d891 100644
--- a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/commands/CommandExecutorMockAsync.java
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/commands/CommandExecutorMockAsync.java
@@ -302,7 +302,7 @@ public class CommandExecutorMockAsync implements Supplier<Boolean> {
private void action(String user, DockerAction action) {
String resourceType = parser.getResourceType();
- String prefixFileName = (Lists.newArrayList("project", "edge", "dataengine", "dataengine-service")
+ String prefixFileName = (Lists.newArrayList("project", "edge", "odahu", "dataengine", "dataengine-service")
.contains(resourceType) ? resourceType : "notebook") + "_";
String templateFileName = "mock_response/" + cloudProvider.getName() + '/' + prefixFileName +
action.toString() + JSON_FILE_ENDING;
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/response/handlers/OdahuCallbackHandler.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/response/handlers/OdahuCallbackHandler.java
new file mode 100644
index 0000000..494a93b
--- /dev/null
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/core/response/handlers/OdahuCallbackHandler.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.backendapi.core.response.handlers;
+
+import com.epam.datalab.backendapi.core.commands.DockerAction;
+import com.epam.datalab.dto.ResourceURL;
+import com.epam.datalab.dto.base.odahu.OdahuResult;
+import com.epam.datalab.rest.client.RESTService;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+
+@Slf4j
+public class OdahuCallbackHandler extends ResourceCallbackHandler<OdahuResult> {
+
+ private static final String ODAHU_URLS_FIELD = "odahu_urls";
+ private static final String GRAFANA_ADMIN = "grafana_admin";
+ private static final String GRAFANA_PASSWORD = "grafana_pass";
+ private static final String OAUTH_COOKIE_SECRET = "oauth_cookie_secret";
+ private static final String DECRYPT_TOKEN = "odahuflow_connection_decrypt_token";
+ private final String callbackUri;
+ private final String name;
+ private final String projectName;
+ private final String endpointName;
+
+ public OdahuCallbackHandler(RESTService selfService, String user, String uuid, DockerAction action,
+ String callbackUri, String name, String projectName, String endpointName) {
+ super(selfService, user, uuid, action);
+ this.callbackUri = callbackUri;
+ this.name = name;
+ this.projectName = projectName;
+ this.endpointName = endpointName;
+ }
+
+ @Override
+ protected String getCallbackURI() {
+ return callbackUri;
+ }
+
+ @Override
+ protected OdahuResult parseOutResponse(JsonNode resultNode, OdahuResult result) {
+ result.setName(name);
+ result.setProjectName(projectName);
+ result.setEndpointName(endpointName);
+
+ if (resultNode == null) {
+ return result;
+ }
+ result.setGrafanaAdmin(getTextValue(resultNode.get(GRAFANA_ADMIN)));
+ result.setGrafanaPassword(getTextValue(resultNode.get(GRAFANA_PASSWORD)));
+ result.setOauthCookieSecret(getTextValue(resultNode.get(OAUTH_COOKIE_SECRET)));
+ result.setDecryptToken(getTextValue(resultNode.get(DECRYPT_TOKEN)));
+
+ final JsonNode odahuUrls = resultNode.get(ODAHU_URLS_FIELD);
+ List<ResourceURL> urls = null;
+ if (odahuUrls != null) {
+ try {
+ urls = mapper.readValue(odahuUrls.toString(), new TypeReference<List<ResourceURL>>() {
+ });
+ result.setResourceUrls(urls);
+ } catch (IOException e) {
+ log.warn("Cannot parse field {} for UUID {} in JSON",
+ RESPONSE_NODE + "." + RESULT_NODE + "." + ODAHU_URLS_FIELD, getUUID(), e);
+ }
+ }
+
+ if (getAction() == DockerAction.CREATE && Objects.isNull(urls)) {
+ log.warn("There are no odahu urls in response file while creating {}", result);
+ }
+
+ return result;
+ }
+}
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProductionModule.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProductionModule.java
index f885338..18d38d8 100644
--- a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProductionModule.java
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProductionModule.java
@@ -29,9 +29,11 @@ import com.epam.datalab.backendapi.core.response.handlers.dao.CallbackHandlerDao
import com.epam.datalab.backendapi.core.response.handlers.dao.FileSystemCallbackHandlerDao;
import com.epam.datalab.backendapi.service.BucketService;
import com.epam.datalab.backendapi.service.CheckInactivityService;
+import com.epam.datalab.backendapi.service.OdahuService;
import com.epam.datalab.backendapi.service.ProjectService;
import com.epam.datalab.backendapi.service.RestoreCallbackHandlerService;
import com.epam.datalab.backendapi.service.impl.CheckInactivityServiceImpl;
+import com.epam.datalab.backendapi.service.impl.OdahuServiceImpl;
import com.epam.datalab.backendapi.service.impl.ProjectServiceImpl;
import com.epam.datalab.backendapi.service.impl.RestoreCallbackHandlerServiceImpl;
import com.epam.datalab.backendapi.service.impl.aws.BucketServiceAwsImpl;
@@ -78,6 +80,7 @@ public class ProductionModule extends ModuleBase<ProvisioningServiceApplicationC
bind(RestoreCallbackHandlerService.class).to(RestoreCallbackHandlerServiceImpl.class);
bind(CheckInactivityService.class).to(CheckInactivityServiceImpl.class);
bind(ProjectService.class).to(ProjectServiceImpl.class);
+ bind(OdahuService.class).to(OdahuServiceImpl.class);
if (configuration.getCloudProvider() == CloudProvider.GCP) {
bind(BucketService.class).to(BucketServiceGcpImpl.class);
} else if (configuration.getCloudProvider() == CloudProvider.AWS) {
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProvisioningDevModule.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProvisioningDevModule.java
index a94e706..f1b3319 100644
--- a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProvisioningDevModule.java
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/modules/ProvisioningDevModule.java
@@ -32,9 +32,11 @@ import com.epam.datalab.backendapi.core.response.handlers.dao.CallbackHandlerDao
import com.epam.datalab.backendapi.core.response.handlers.dao.FileSystemCallbackHandlerDao;
import com.epam.datalab.backendapi.service.BucketService;
import com.epam.datalab.backendapi.service.CheckInactivityService;
+import com.epam.datalab.backendapi.service.OdahuService;
import com.epam.datalab.backendapi.service.ProjectService;
import com.epam.datalab.backendapi.service.RestoreCallbackHandlerService;
import com.epam.datalab.backendapi.service.impl.CheckInactivityServiceImpl;
+import com.epam.datalab.backendapi.service.impl.OdahuServiceImpl;
import com.epam.datalab.backendapi.service.impl.ProjectServiceImpl;
import com.epam.datalab.backendapi.service.impl.RestoreCallbackHandlerServiceImpl;
import com.epam.datalab.backendapi.service.impl.aws.BucketServiceAwsImpl;
@@ -84,6 +86,7 @@ public class ProvisioningDevModule extends ModuleBase<ProvisioningServiceApplica
bind(RestoreCallbackHandlerService.class).to(RestoreCallbackHandlerServiceImpl.class);
bind(CheckInactivityService.class).to(CheckInactivityServiceImpl.class);
bind(ProjectService.class).to(ProjectServiceImpl.class);
+ bind(OdahuService.class).to(OdahuServiceImpl.class);
if (configuration.getCloudProvider() == CloudProvider.GCP) {
bind(BucketService.class).to(BucketServiceGcpImpl.class);
} else if (configuration.getCloudProvider() == CloudProvider.AWS) {
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/resources/OdahuResource.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/resources/OdahuResource.java
new file mode 100644
index 0000000..69969df
--- /dev/null
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/resources/OdahuResource.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.backendapi.resources;
+
+import com.epam.datalab.auth.UserInfo;
+import com.epam.datalab.backendapi.service.OdahuService;
+import com.epam.datalab.dto.odahu.ActionOdahuDTO;
+import com.epam.datalab.dto.odahu.CreateOdahuDTO;
+import com.google.inject.Inject;
+import io.dropwizard.auth.Auth;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+@Path("infrastructure/odahu")
+@Consumes(MediaType.APPLICATION_JSON)
+@Produces(MediaType.APPLICATION_JSON)
+public class OdahuResource {
+
+ private final OdahuService odahuService;
+
+ @Inject
+ public OdahuResource(OdahuService odahuService) {
+ this.odahuService = odahuService;
+ }
+
+ @POST
+ public Response createProject(@Auth UserInfo userInfo, CreateOdahuDTO dto) {
+ return Response.ok(odahuService.create(userInfo, dto)).build();
+ }
+
+ @Path("start")
+ @POST
+ public Response startProject(@Auth UserInfo userInfo, ActionOdahuDTO dto) {
+ return Response.ok(odahuService.start(userInfo, dto)).build();
+ }
+
+ @Path("stop")
+ @POST
+ public Response stopProject(@Auth UserInfo userInfo, ActionOdahuDTO dto) {
+ return Response.ok(odahuService.stop(userInfo, dto)).build();
+ }
+
+ @Path("terminate")
+ @POST
+ public Response terminateProject(@Auth UserInfo userInfo, ActionOdahuDTO dto) {
+ return Response.ok(odahuService.terminate(userInfo, dto)).build();
+ }
+}
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/service/OdahuService.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/service/OdahuService.java
new file mode 100644
index 0000000..46c23cf
--- /dev/null
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/service/OdahuService.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.backendapi.service;
+
+import com.epam.datalab.auth.UserInfo;
+import com.epam.datalab.dto.odahu.ActionOdahuDTO;
+import com.epam.datalab.dto.odahu.CreateOdahuDTO;
+
+public interface OdahuService {
+ String create(UserInfo userInfo, CreateOdahuDTO odahuCreateDTO);
+
+ String start(UserInfo userInfo, ActionOdahuDTO dto);
+
+ String stop(UserInfo userInfo, ActionOdahuDTO dto);
+
+ String terminate(UserInfo userInfo, ActionOdahuDTO dto);
+}
diff --git a/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/service/impl/OdahuServiceImpl.java b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/service/impl/OdahuServiceImpl.java
new file mode 100644
index 0000000..6be8ece
--- /dev/null
+++ b/services/provisioning-service/src/main/java/com/epam/datalab/backendapi/service/impl/OdahuServiceImpl.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.epam.datalab.backendapi.service.impl;
+
+import com.epam.datalab.auth.UserInfo;
+import com.epam.datalab.backendapi.ProvisioningServiceApplicationConfiguration;
+import com.epam.datalab.backendapi.core.commands.CommandBuilder;
+import com.epam.datalab.backendapi.core.commands.DockerAction;
+import com.epam.datalab.backendapi.core.commands.DockerCommands;
+import com.epam.datalab.backendapi.core.commands.ICommandExecutor;
+import com.epam.datalab.backendapi.core.commands.RunDockerCommand;
+import com.epam.datalab.backendapi.core.response.folderlistener.FolderListenerExecutor;
+import com.epam.datalab.backendapi.core.response.handlers.OdahuCallbackHandler;
+import com.epam.datalab.backendapi.service.OdahuService;
+import com.epam.datalab.dto.ResourceBaseDTO;
+import com.epam.datalab.dto.odahu.ActionOdahuDTO;
+import com.epam.datalab.dto.odahu.CreateOdahuDTO;
+import com.epam.datalab.rest.client.RESTService;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.inject.Inject;
+
+public class OdahuServiceImpl implements OdahuService {
+
+ private static final String CALLBACK_URI = "/api/odahu/status";
+ private static final String ODAHU_RESOURCE_TYPE = "odahu";
+ private static final String ODAHU_IMAGE = "docker.datalab-odahu";
+
+ private final ProvisioningServiceApplicationConfiguration configuration;
+ private final FolderListenerExecutor folderListenerExecutor;
+ private final CommandBuilder commandBuilder;
+ private final ICommandExecutor commandExecutor;
+ private final RESTService selfService;
+
+ @Inject
+ public OdahuServiceImpl(ProvisioningServiceApplicationConfiguration configuration,
+ FolderListenerExecutor folderListenerExecutor, CommandBuilder commandBuilder,
+ ICommandExecutor commandExecutor, RESTService selfService) {
+ this.configuration = configuration;
+ this.folderListenerExecutor = folderListenerExecutor;
+ this.commandBuilder = commandBuilder;
+ this.commandExecutor = commandExecutor;
+ this.selfService = selfService;
+ }
+
+ @Override
+ public String create(UserInfo userInfo, CreateOdahuDTO dto) {
+ return executeDocker(userInfo, dto, DockerAction.CREATE, ODAHU_RESOURCE_TYPE, ODAHU_IMAGE, dto.getName(),
+ dto.getProject(), dto.getEndpoint());
+ }
+
+ @Override
+ public String start(UserInfo userInfo, ActionOdahuDTO dto) {
+ return executeDocker(userInfo, dto, DockerAction.START, ODAHU_RESOURCE_TYPE, ODAHU_IMAGE, dto.getName(),
+ dto.getProject(), dto.getEndpoint());
+ }
+
+ @Override
+ public String stop(UserInfo userInfo, ActionOdahuDTO dto) {
+ return executeDocker(userInfo, dto, DockerAction.STOP, ODAHU_RESOURCE_TYPE, ODAHU_IMAGE, dto.getName(),
+ dto.getProject(), dto.getEndpoint());
+ }
+
+ @Override
+ public String terminate(UserInfo userInfo, ActionOdahuDTO dto) {
+ return executeDocker(userInfo, dto, DockerAction.TERMINATE, ODAHU_RESOURCE_TYPE, ODAHU_IMAGE, dto.getName(),
+ dto.getProject(), dto.getEndpoint());
+ }
+
+
+ private String executeDocker(UserInfo userInfo, ResourceBaseDTO dto, DockerAction action, String resourceType,
+ String image, String name, String project, String endpoint) {
+ String uuid = DockerCommands.generateUUID();
+
+ folderListenerExecutor.start(configuration.getKeyLoaderDirectory(),
+ configuration.getKeyLoaderPollTimeout(),
+ new OdahuCallbackHandler(selfService, userInfo.getName(), uuid, action, CALLBACK_URI, name, project, endpoint));
+
+ RunDockerCommand runDockerCommand = new RunDockerCommand()
+ .withInteractive()
+ .withName(String.join("_", userInfo.getSimpleName(), name, resourceType, action.toString(),
+ Long.toString(System.currentTimeMillis())))
+ .withVolumeForRootKeys(configuration.getKeyDirectory())
+ .withVolumeForResponse(configuration.getKeyLoaderDirectory())
+ .withVolumeForLog(configuration.getDockerLogDirectory(), resourceType)
+ .withResource(resourceType)
+ .withRequestId(uuid)
+ .withConfKeyName(configuration.getAdminKey())
+ .withImage(image)
+ .withAction(action);
+
+ try {
+ commandExecutor.executeAsync(userInfo.getName(), uuid, commandBuilder.buildCommand(runDockerCommand, dto));
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ }
+ return uuid;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datalab.apache.org
For additional commands, e-mail: commits-help@datalab.apache.org