use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.
the class InstallPluginCommand method execute.
// pkg private for testing
void execute(Terminal terminal, String pluginId, boolean isBatch, Environment env) throws Exception {
if (pluginId == null) {
throw new UserException(ExitCodes.USAGE, "plugin id is required");
}
// TODO: remove this leniency!! is it needed anymore?
if (Files.exists(env.pluginsFile()) == false) {
terminal.println("Plugins directory [" + env.pluginsFile() + "] does not exist. Creating...");
Files.createDirectory(env.pluginsFile());
}
Path pluginZip = download(terminal, pluginId, env.tmpFile());
Path extractedZip = unzip(pluginZip, env.pluginsFile());
install(terminal, isBatch, extractedZip, env);
}
use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.
the class InstallPluginCommand method unzip.
private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException {
// unzip plugin to a staging temp dir
final Path target = stagingDirectory(pluginsDir);
pathsToDeleteOnShutdown.add(target);
boolean hasEsDir = false;
try (ZipInputStream zipInput = new ZipInputStream(Files.newInputStream(zip))) {
ZipEntry entry;
byte[] buffer = new byte[8192];
while ((entry = zipInput.getNextEntry()) != null) {
if (entry.getName().startsWith("elasticsearch/") == false) {
// only extract the elasticsearch directory
continue;
}
hasEsDir = true;
Path targetFile = target.resolve(entry.getName().substring("elasticsearch/".length()));
// and ensuring the normalized entry is still rooted with the target plugin directory.
if (targetFile.normalize().startsWith(target) == false) {
throw new IOException("Zip contains entry name '" + entry.getName() + "' resolving outside of plugin directory");
}
// before their children (although this makes sense, but is it guaranteed?)
if (!Files.isSymbolicLink(targetFile.getParent())) {
Files.createDirectories(targetFile.getParent());
}
if (entry.isDirectory() == false) {
try (OutputStream out = Files.newOutputStream(targetFile)) {
int len;
while ((len = zipInput.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
}
}
zipInput.closeEntry();
}
}
Files.delete(zip);
if (hasEsDir == false) {
IOUtils.rm(target);
throw new UserException(ExitCodes.DATA_ERROR, "`elasticsearch` directory is missing in the plugin zip");
}
return target;
}
use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.
the class RemovePluginCommand method execute.
// pkg private for testing
void execute(Terminal terminal, String pluginName, Environment env) throws Exception {
if (pluginName == null) {
throw new UserException(ExitCodes.USAGE, "plugin name is required");
}
terminal.println("-> Removing " + Strings.coalesceToEmpty(pluginName) + "...");
final Path pluginDir = env.pluginsFile().resolve(pluginName);
if (Files.exists(pluginDir) == false) {
throw new UserException(ExitCodes.CONFIG, "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
}
final List<Path> pluginPaths = new ArrayList<>();
final Path pluginBinDir = env.binFile().resolve(pluginName);
if (Files.exists(pluginBinDir)) {
if (Files.isDirectory(pluginBinDir) == false) {
throw new UserException(ExitCodes.IO_ERROR, "Bin dir for " + pluginName + " is not a directory");
}
pluginPaths.add(pluginBinDir);
terminal.println(VERBOSE, "Removing: " + pluginBinDir);
}
terminal.println(VERBOSE, "Removing: " + pluginDir);
final Path tmpPluginDir = env.pluginsFile().resolve(".removing-" + pluginName);
try {
Files.move(pluginDir, tmpPluginDir, StandardCopyOption.ATOMIC_MOVE);
} catch (final AtomicMoveNotSupportedException e) {
// this can happen on a union filesystem when a plugin is not installed on the top layer; we fall back to a non-atomic move
Files.move(pluginDir, tmpPluginDir);
}
pluginPaths.add(tmpPluginDir);
IOUtils.rm(pluginPaths.toArray(new Path[pluginPaths.size()]));
// we preserve the config files in case the user is upgrading the plugin, but we print
// a message so the user knows in case they want to remove manually
final Path pluginConfigDir = env.configFile().resolve(pluginName);
if (Files.exists(pluginConfigDir)) {
terminal.println("-> Preserving plugin config files [" + pluginConfigDir + "] in case of upgrade, delete manually if not needed");
}
}
use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.
the class RemoveSettingKeyStoreCommandTests method testNoSettings.
public void testNoSettings() throws Exception {
createKeystore("");
UserException e = expectThrows(UserException.class, this::execute);
assertEquals(ExitCodes.USAGE, e.exitCode);
assertThat(e.getMessage(), containsString("Must supply at least one setting"));
}
use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.
the class RemoveSettingKeyStoreCommandTests method testMissing.
public void testMissing() throws Exception {
UserException e = expectThrows(UserException.class, () -> execute("foo"));
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
assertThat(e.getMessage(), containsString("keystore not found"));
}
Aggregations