Skip to content

Commit

Permalink
#16: implement tool commandlet docker (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
KGoroll1 authored Mar 19, 2024
1 parent 970e1fc commit 9c29a10
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.devonfw.tools.ide.tool.aws.Aws;
import com.devonfw.tools.ide.tool.az.Azure;
import com.devonfw.tools.ide.tool.cobigen.Cobigen;
import com.devonfw.tools.ide.tool.docker.Docker;
import com.devonfw.tools.ide.tool.eclipse.Eclipse;
import com.devonfw.tools.ide.tool.gcviewer.GcViewer;
import com.devonfw.tools.ide.tool.gh.Gh;
Expand Down Expand Up @@ -84,6 +85,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new Aws(context));
add(new Cobigen(context));
add(new Jmc(context));
add(new Docker(context));
add(new Sonar(context));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.devonfw.tools.ide.process;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.common.SystemPath;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.VariableLine;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.os.SystemInfoImpl;
import com.devonfw.tools.ide.util.FilenameUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -22,6 +14,14 @@
import java.util.Objects;
import java.util.stream.Collectors;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.common.SystemPath;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.VariableLine;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.os.SystemInfoImpl;
import com.devonfw.tools.ide.util.FilenameUtil;

/**
* Implementation of {@link ProcessContext}.
*/
Expand Down Expand Up @@ -116,7 +116,7 @@ public ProcessResult run(ProcessMode processMode) {
if (this.executable == null) {
throw new IllegalStateException("Missing executable to run process!");
}
List<String> args = new ArrayList<>(this.arguments.size() + 2);
List<String> args = new ArrayList<>(this.arguments.size() + 4);
String interpreter = addExecutable(this.executable.toString(), args);
args.addAll(this.arguments);
if (this.context.debug().isEnabled()) {
Expand All @@ -140,7 +140,7 @@ public ProcessResult run(ProcessMode processMode) {
List<String> err = null;

if (processMode == ProcessMode.DEFAULT_CAPTURE) {
try (BufferedReader outReader = new BufferedReader(new InputStreamReader(process.getInputStream()));) {
try (BufferedReader outReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
out = outReader.lines().collect(Collectors.toList());
}
try (BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
Expand Down Expand Up @@ -192,8 +192,7 @@ private String createCommandMessage(String interpreter, String suffix) {
}
}
sb.append(suffix);
String message = sb.toString();
return message;
return sb.toString();
}

private String getSheBang(Path file) {
Expand Down Expand Up @@ -314,6 +313,10 @@ private String addExecutable(String executable, List<String> args) {
}
args.add(bash);
}
if ("msi".equalsIgnoreCase(fileExtension)) {
args.add(0, "/i");
args.add(0, "msiexec");
}
args.add(executable);
return interpreter;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.devonfw.tools.ide.tool;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.io.FileAccess;
Expand All @@ -9,10 +15,6 @@
import com.devonfw.tools.ide.repo.ToolRepository;
import com.devonfw.tools.ide.version.VersionIdentifier;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

/**
* {@link ToolCommandlet} that is installed globally.
*/
Expand All @@ -24,13 +26,70 @@ public abstract class GlobalToolCommandlet extends ToolCommandlet {
* @param context the {@link IdeContext}.
* @param tool the {@link #getName() tool name}.
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of}
* method.
* method.
*/
public GlobalToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {

super(context, tool, tags);
}

/**
* Performs the installation of the {@link #getName() tool} via a package manager.
*
* @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
* @param commands - A {@link Map} containing the commands used to perform the installation for each package manager.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and
* nothing has changed.
*/
protected boolean installWithPackageManger(Map<PackageManager, List<String>> commands, boolean silent) {

Path binaryPath = this.context.getPath().findBinary(Path.of(getBinaryName()));

if (binaryPath != null && Files.exists(binaryPath) && !this.context.isForceMode()) {
IdeLogLevel level = silent ? IdeLogLevel.DEBUG : IdeLogLevel.INFO;
this.context.level(level).log("{} is already installed at {}", this.tool, binaryPath);
return false;
}

Path bashPath = this.context.getPath().findBinary(Path.of("bash"));
if (bashPath == null || !Files.exists(bashPath)) {
context.warning("Bash was not found on this machine. Not Proceeding with installation of tool " + this.tool);
return false;
}

PackageManager foundPackageManager = null;
for (PackageManager pm : commands.keySet()) {
if (Files.exists(this.context.getPath().findBinary(Path.of(pm.toString().toLowerCase())))) {
foundPackageManager = pm;
break;
}
}

int finalExitCode = 0;
if (foundPackageManager == null) {
context.warning("No supported Package Manager found for installation");
return false;
} else {
List<String> commandList = commands.get(foundPackageManager);
if (commandList != null) {
for (String command : commandList) {
ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.WARNING).executable(bashPath)
.addArgs("-c", command);
finalExitCode = pc.run();
}
}
}

if (finalExitCode == 0) {
this.context.success("Successfully installed {}", this.tool);
} else {
this.context.warning("{} was not successfully installed", this.tool);
return false;
}
postInstall();
return true;
}

@Override
protected boolean isExtract() {

Expand Down Expand Up @@ -79,5 +138,4 @@ protected boolean doInstall(boolean silent) {
postInstall();
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devonfw.tools.ide.tool;

public enum PackageManager {
APT, ZYPPER, YUM, DNF
}
79 changes: 79 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/docker/Docker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.devonfw.tools.ide.tool.docker;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.os.SystemArchitecture;
import com.devonfw.tools.ide.repo.ToolRepository;
import com.devonfw.tools.ide.tool.GlobalToolCommandlet;
import com.devonfw.tools.ide.tool.PackageManager;
import com.devonfw.tools.ide.version.VersionIdentifier;

public class Docker extends GlobalToolCommandlet {
/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public Docker(IdeContext context) {

super(context, "docker", Set.of(Tag.DOCKER));
}

@Override
public boolean isExtract() {

return switch (context.getSystemInfo().getOs()) {
case WINDOWS -> false;
case MAC -> context.getSystemInfo().getArchitecture().equals(SystemArchitecture.ARM64);
case LINUX -> true;
};
}

@Override
protected boolean doInstall(boolean silent) {

if (context.getSystemInfo().isLinux()) {
return installWithPackageManger(getPackageMangerCommands(), silent);
} else {
return super.doInstall(silent);
}
}

private Map<PackageManager, List<String>> getPackageMangerCommands() {

Map<PackageManager, List<String>> commands = new HashMap<>();

String edition = getEdition();
ToolRepository toolRepository = this.context.getDefaultToolRepository();
VersionIdentifier configuredVersion = getConfiguredVersion();
String resolvedVersion = toolRepository.resolveVersion(this.tool, edition, configuredVersion).toString();

commands.put(PackageManager.ZYPPER, Arrays.asList(
"sudo zypper addrepo https://download.opensuse.org/repositories/isv:/Rancher:/stable/rpm/isv:Rancher:stable.repo",
String.format("sudo zypper --no-gpg-checks install rancher-desktop=%s*", resolvedVersion)));

commands.put(PackageManager.APT, Arrays.asList(
"curl -s https://download.opensuse.org/repositories/isv:/Rancher:/stable/deb/Release.key | gpg --dearmor | sudo dd status=none of=/usr/share/keyrings/isv-rancher-stable-archive-keyring.gpg",
"echo 'deb [signed-by=/usr/share/keyrings/isv-rancher-stable-archive-keyring.gpg] https://download.opensuse.org/repositories/isv:/Rancher:/stable/deb/ ./' | sudo dd status=none of=/etc/apt/sources.list.d/isv-rancher-stable.list",
"sudo apt update",
String.format("sudo apt install -y --allow-downgrades rancher-desktop=%s*", resolvedVersion)));

return commands;
}

@Override
protected String getBinaryName() {

if (context.getSystemInfo().isLinux()) {
return "rancher-desktop";
} else {
return super.getBinaryName();
}
}
}

0 comments on commit 9c29a10

Please sign in to comment.