|
| 1 | +/* |
| 2 | + * Copyright 2021-2023, Seqera. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package io.seqera.tower.cli.commands.datastudios; |
| 19 | + |
| 20 | +import io.seqera.tower.ApiException; |
| 21 | +import io.seqera.tower.cli.commands.global.WorkspaceOptionalOptions; |
| 22 | +import io.seqera.tower.cli.exceptions.TowerException; |
| 23 | +import io.seqera.tower.cli.responses.Response; |
| 24 | +import io.seqera.tower.cli.responses.datastudios.DataStudiosCreated; |
| 25 | +import io.seqera.tower.cli.utils.FilesHelper; |
| 26 | +import io.seqera.tower.model.DataStudioConfiguration; |
| 27 | +import io.seqera.tower.model.DataStudioCreateRequest; |
| 28 | +import io.seqera.tower.model.DataStudioCreateResponse; |
| 29 | +import io.seqera.tower.model.DataStudioDto; |
| 30 | +import io.seqera.tower.model.DataStudioStatus; |
| 31 | +import picocli.CommandLine; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.file.Path; |
| 35 | + |
| 36 | +import static io.seqera.tower.cli.utils.ResponseHelper.waitStatus; |
| 37 | + |
| 38 | +@CommandLine.Command( |
| 39 | + name = "add", |
| 40 | + description = "Add new data studio." |
| 41 | +) |
| 42 | +public class AddCmd extends AbstractStudiosCmd{ |
| 43 | + |
| 44 | + @CommandLine.Option(names = {"-n", "--name"}, description = "Data Studio name.", required = true) |
| 45 | + public String name; |
| 46 | + |
| 47 | + @CommandLine.Option(names = {"-d", "--description"}, description = "Data studio description.") |
| 48 | + public String description; |
| 49 | + |
| 50 | + @CommandLine.Mixin |
| 51 | + public WorkspaceOptionalOptions workspace; |
| 52 | + |
| 53 | + @CommandLine.Option(names = {"-t", "--template"}, description = "Data studio template container image. Available templates can be listed with 'studios templates' command", required = true) |
| 54 | + public String template; |
| 55 | + |
| 56 | + @CommandLine.Option(names = {"-c", "--compute-env"}, description = "Compute environment name.", required = true) |
| 57 | + public String computeEnv; |
| 58 | + |
| 59 | + @CommandLine.Mixin |
| 60 | + public DataStudioConfigurationOptions dataStudioConfigOptions; |
| 61 | + |
| 62 | + @CommandLine.Option(names = {"--conda-env-yml", "--conda-env-yaml"}, description = "Path to a YAML env file with Conda packages to be installed in the Data Studio environment.") |
| 63 | + public Path condaEnv; |
| 64 | + |
| 65 | + @CommandLine.Option(names = {"-a", "--autoStart"}, description = "Create Data Studio and start it immediately, defaults to false", defaultValue = "false") |
| 66 | + public Boolean autoStart; |
| 67 | + |
| 68 | + @CommandLine.Option(names = {"--wait"}, description = "Wait until DataStudio is in RUNNING status. Valid options: ${COMPLETION-CANDIDATES}.") |
| 69 | + public DataStudioStatus wait; |
| 70 | + |
| 71 | + @Override |
| 72 | + protected Response exec() throws ApiException { |
| 73 | + Long wspId = workspaceId(workspace.workspace); |
| 74 | + |
| 75 | + try { |
| 76 | + DataStudioCreateRequest request = prepareRequest(); |
| 77 | + DataStudioCreateResponse response = api().createDataStudio(request, wspId, autoStart); |
| 78 | + DataStudioDto dataStudioDto = response.getStudio(); |
| 79 | + assert dataStudioDto != null; |
| 80 | + return new DataStudiosCreated(dataStudioDto.getSessionId(), wspId, workspaceRef(wspId), autoStart); |
| 81 | + } catch (ApiException e) { |
| 82 | + if (e.getCode() == 403) { |
| 83 | + throw new TowerException(String.format("User not entitled to create studio at %s workspace", wspId)); |
| 84 | + } |
| 85 | + throw e; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + DataStudioCreateRequest prepareRequest() throws TowerException { |
| 90 | + DataStudioCreateRequest request = new DataStudioCreateRequest(); |
| 91 | + request.setName(name); |
| 92 | + if (description != null && !description.isEmpty()) {request.description(description);} |
| 93 | + request.setDataStudioToolUrl(template); |
| 94 | + request.setComputeEnvId(computeEnv); |
| 95 | + |
| 96 | + String condaEnvString = null; |
| 97 | + if (condaEnv != null) { |
| 98 | + try { |
| 99 | + condaEnvString = FilesHelper.readString(condaEnv); |
| 100 | + } catch (IOException e) { |
| 101 | + throw new TowerException(String.format("Cannot read conda environment file: %s. %s", condaEnv, e)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + DataStudioConfiguration newConfig = dataStudioConfigurationFrom(dataStudioConfigOptions, condaEnvString); |
| 107 | + request.setConfiguration(setDefaults(newConfig)); |
| 108 | + return request; |
| 109 | + } |
| 110 | + |
| 111 | + DataStudioConfiguration setDefaults(DataStudioConfiguration config) { |
| 112 | + if (config.getGpu() == null) { |
| 113 | + config.setGpu(0); |
| 114 | + } |
| 115 | + if (config.getCpu() == null) { |
| 116 | + config.setCpu(2); |
| 117 | + } |
| 118 | + if (config.getMemory() == null) { |
| 119 | + config.setMemory(8192); |
| 120 | + } |
| 121 | + return config; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected Integer onBeforeExit(int exitCode, Response response) { |
| 126 | + |
| 127 | + if (!autoStart) { |
| 128 | + return exitCode; |
| 129 | + } |
| 130 | + |
| 131 | + if (exitCode != 0 || wait == null || response == null) { |
| 132 | + return exitCode; |
| 133 | + } |
| 134 | + |
| 135 | + DataStudiosCreated createdResponse = ((DataStudiosCreated) response); |
| 136 | + return onBeforeExit(exitCode, createdResponse.sessionId, createdResponse.workspaceId, wait); |
| 137 | + } |
| 138 | +} |
0 commit comments