Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Solve #1848, Add action to execute general command #1867

Merged
merged 3 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class AbstractActionFactory implements ActionFactory {
addAction(EchoAction.class);
addAction(SleepAction.class);
addAction(SyncAction.class);
addAction(ExecAction.class);
}

protected static void addAction(Class<? extends SmartAction> actionClass) {
Expand Down
130 changes: 130 additions & 0 deletions smart-action/src/main/java/org/smartdata/action/ExecAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* 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 org.smartdata.action;

import org.smartdata.action.annotation.ActionSignature;
import org.smartdata.utils.StringUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* An action to execute general command.
*
*/
// TODO: Add security restrictions
@ActionSignature(
actionId = "exec",
displayName = "exec",
usage = ExecAction.CMD + " $cmdString"
+ " [" + ExecAction.EXECDIR + " $executionDirectory" + "]"
+ " [" + ExecAction.ENV + " $envKVs" + "]"
)
public class ExecAction extends SmartAction {
public static final String CMD = "-cmd";
public static final String EXECDIR = "-execdir";
public static final String ENV = "-env"; // multi-KVs separated with '|'
public static final String SSM_ENV_PREFIX = "SSMENV";
private Map<String, String> env = new HashMap<>();
private String cmdStr = "";
private String execDir = "";

@Override
public void init(Map<String, String> args) {
super.init(args);
String key;
String value;
for (String arg : args.keySet()) {
switch (arg) {
case CMD:
cmdStr = args.get(arg);
break;
case EXECDIR:
execDir = args.get(arg);
break;
case ENV:
value = args.get(ENV);
if (value == null || value.length() == 0) {
break;
}
env.putAll(parseEnvString(value));
break;
default:
key = SSM_ENV_PREFIX + (arg.startsWith("-") ? arg.replaceFirst("-", "_") : arg);
env.put(key, args.get(arg));
}
}
}

private Map<String, String> parseEnvString(String envStr) {
String[] items = envStr.split("\\|");
String key;
String temp;
Map<String, String> ret = new HashMap<>();
for (String it : items) {
int idx = it.indexOf("=");
if (idx != -1) {
key = it.substring(0, idx).trim();
if (it.length() == idx + 1) {
temp = "";
} else {
temp = it.substring(idx + 1, it.length());
temp = temp.replaceAll("\\s+$", "");
}
if (key.length() > 0) {
ret.put(key, temp);
}
}
}
return ret;
}

@Override
protected void execute() throws Exception {
List<String> cmdItems = StringUtil.parseCmdletString(cmdStr);
if (cmdItems.size() == 0) {
return;
}

ProcessBuilder builder = new ProcessBuilder(cmdItems);
if (execDir != null && execDir.length() > 0) {
builder.directory(new File(execDir));
}

Map<String, String> envVars = builder.environment();
envVars.putAll(env);

builder.redirectErrorStream(true);
Process p = builder.start();

BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
appendLog(line);
}
int eCode = p.waitFor();
if (eCode != 0) {
throw new IOException("Exit code = " + eCode);
}
}
}
6 changes: 6 additions & 0 deletions smart-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<version>${kerby.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
90 changes: 90 additions & 0 deletions smart-common/src/main/java/org/smartdata/utils/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
*/
package org.smartdata.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -127,4 +130,91 @@ public static long pharseTimeString(String str) {
}
return intval;
}

public static List<String> parseCmdletString(String cmdlet)
throws ParseException {
if (cmdlet == null || cmdlet.length() == 0) {
return new ArrayList<>();
}

char[] chars = (cmdlet + " ").toCharArray();
List<String> blocks = new ArrayList<String>();
char c;
char[] token = new char[chars.length];
int tokenlen = 0;
boolean sucing = false;
boolean string = false;
for (int idx = 0; idx < chars.length; idx++) {
c = chars[idx];
if (c == ' ' || c == '\t') {
if (string) {
token[tokenlen++] = c;
}
if (sucing) {
blocks.add(String.valueOf(token, 0, tokenlen));
tokenlen = 0;
sucing = false;
}
} else if (c == ';') {
if (string) {
throw new ParseException("Unexpected break of string", idx);
}

if (sucing) {
blocks.add(String.valueOf(token, 0, tokenlen));
tokenlen = 0;
sucing = false;
}
} else if (c == '\\') {
boolean tempAdded = false;
if (sucing || string) {
token[tokenlen++] = chars[++idx];
tempAdded = true;
}

if (!tempAdded && !sucing) {
sucing = true;
token[tokenlen++] = chars[++idx];
}
} else if (c == '"') {
if (sucing) {
throw new ParseException("Unexpected \"", idx);
}

if (string) {
if (chars[idx + 1] != '"') {
string = false;
blocks.add(String.valueOf(token, 0, tokenlen));
tokenlen = 0;
} else {
idx++;
}
} else {
string = true;
}
} else if (c == '\r' || c == '\n') {
if (sucing) {
sucing = false;
blocks.add(String.valueOf(token, 0, tokenlen));
tokenlen = 0;
}

if (string) {
throw new ParseException("String cannot in more than one line", idx);
}
} else {
if (string) {
token[tokenlen++] = chars[idx];
} else {
sucing = true;
token[tokenlen++] = chars[idx];
}
}
}

if (string) {
throw new ParseException("Unexpected tail of string", chars.length);
}
return blocks;
}
}
45 changes: 45 additions & 0 deletions smart-common/src/test/java/org/smartdata/utils/TestStringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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 org.smartdata.utils;

import org.junit.Assert;
import org.junit.Test;

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

/**
* Tests for StringUtil.
*/
public class TestStringUtil {

@Test
public void testCmdletString() throws Exception {
Map<String, Integer> strs = new HashMap<>();
strs.put("int a b -c d -e f \"gg ' kk ' ff\" \" mn \"", 9);
strs.put("cat /dir/file ", 2);

List<String> items;
for (String str : strs.keySet()) {
items = StringUtil.parseCmdletString(str);
Assert.assertTrue(strs.get(str) == items.size());
System.out.println(items.size() + " -> " + str);
}
}
}