Issue 126: added kill

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2355 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-01 01:04:58 +00:00
parent 4b24b990be
commit 3c8a34b674
10 changed files with 155 additions and 8 deletions

View File

@ -133,7 +133,7 @@ public class ScriptBuilder {
new Function<Statement, Iterable<String>>() { new Function<Statement, Iterable<String>>() {
@Override @Override
public Iterable<String> apply(Statement from) { public Iterable<String> apply(Statement from) {
return from.functionDependecies(); return from.functionDependecies(osFamily);
} }
})); }));
List<String> unresolvedFunctions = Lists.newArrayList(dependentFunctions); List<String> unresolvedFunctions = Lists.newArrayList(dependentFunctions);

View File

@ -92,7 +92,7 @@ public class Call implements Statement {
} }
@Override @Override
public Iterable<String> functionDependecies() { public Iterable<String> functionDependecies(OsFamily family) {
return ImmutableList.of(function); return ImmutableList.of(function);
} }
} }

View File

@ -47,7 +47,7 @@ public class InterpretableStatement implements Statement {
} }
@Override @Override
public Iterable<String> functionDependecies() { public Iterable<String> functionDependecies(OsFamily family) {
return ImmutableList.of(); return ImmutableList.of();
} }

View File

@ -0,0 +1,56 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.jclouds.scriptbuilder.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
/**
* Statement used in a shell script
*
* @author Adrian Cole
*/
public class Kill implements Statement {
public static final Map<OsFamily, String> OS_TO_KILL = ImmutableMap.of(OsFamily.UNIX,
"[ -n \"$FOUND_PID\" ] && {\n echo stopping $FOUND_PID\n kill -9 $FOUND_PID\n}\n",
OsFamily.WINDOWS,
"if defined FOUND_PID (\r\n TASKKILL /F /T /PID %FOUND_PID% >NUL\r\n)\r\n");
public Kill() {
}
public String render(OsFamily family) {
return OS_TO_KILL.get(checkNotNull(family, "family"));
}
@Override
public Iterable<String> functionDependecies(OsFamily family) {
return ImmutableList.of();
}
}

View File

@ -29,7 +29,7 @@ package org.jclouds.scriptbuilder.domain;
* @author Adrian Cole * @author Adrian Cole
*/ */
public interface Statement { public interface Statement {
Iterable<String> functionDependecies(); Iterable<String> functionDependecies(OsFamily family);
String render(OsFamily family); String render(OsFamily family);
} }

View File

@ -31,6 +31,7 @@ import java.util.Map;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class Statements { public class Statements {
private static final Kill KILL = new Kill();
public static Statement switchOn(String variable, Map<String, Statement> valueToActions) { public static Statement switchOn(String variable, Map<String, Statement> valueToActions) {
return new Switch(variable, valueToActions); return new Switch(variable, valueToActions);
@ -41,7 +42,7 @@ public class Statements {
} }
/** /**
* Stores the pid into the variable foundPid if successful. * Stores the pid into the variable {@code FOUND_PID} if successful.
* *
* @param args * @param args
* - what to search for in the process tree. * - what to search for in the process tree.
@ -50,6 +51,15 @@ public class Statements {
return new Call("findPid", args); return new Call("findPid", args);
} }
/**
* Kills the pid and subprocesses related to the variable {@code FOUND_PID} if set.
*
* @see findPid
*/
public static Statement kill() {
return KILL;
}
public static Statement interpret(String portableStatement) { public static Statement interpret(String portableStatement) {
return new InterpretableStatement(portableStatement); return new InterpretableStatement(portableStatement);
} }

View File

@ -127,10 +127,10 @@ public class Switch implements Statement {
} }
@Override @Override
public Iterable<String> functionDependecies() { public Iterable<String> functionDependecies(OsFamily family) {
List<String> functions = Lists.newArrayList(); List<String> functions = Lists.newArrayList();
for (Statement statement : valueToActions.values()) { for (Statement statement : valueToActions.values()) {
Iterables.addAll(functions, statement.functionDependecies()); Iterables.addAll(functions, statement.functionDependecies(family));
} }
return functions; return functions;
} }

View File

@ -1,6 +1,9 @@
package org.jclouds.scriptbuilder; package org.jclouds.scriptbuilder;
import static org.jclouds.scriptbuilder.domain.Statements.*; import static org.jclouds.scriptbuilder.domain.Statements.findPid;
import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import static org.jclouds.scriptbuilder.domain.Statements.kill;
import static org.jclouds.scriptbuilder.domain.Statements.switchOn;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
@ -60,6 +63,23 @@ public class ScriptBuilderTest {
+ ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
} }
ScriptBuilder seekAndDestroyBuilder = new ScriptBuilder().addStatement(findPid("{args}"))
.addStatement(kill());
@Test
public void testSeekAndDestroyWindows() throws MalformedURLException, IOException {
assertEquals(seekAndDestroyBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources
.newReaderSupplier(Resources.getResource("test_seek_and_destroy."
+ ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
@Test
public void testSeekAndDestroyUNIX() throws MalformedURLException, IOException {
assertEquals(seekAndDestroyBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources
.newReaderSupplier(Resources.getResource("test_seek_and_destroy."
+ ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
@Test @Test
public void testSwitchOn() { public void testSwitchOn() {
ScriptBuilder builder = new ScriptBuilder(); ScriptBuilder builder = new ScriptBuilder();

View File

@ -0,0 +1,31 @@
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
unset PATH JAVA_HOME LD_LIBRARY_PATH
function abort {
echo "aborting: $@" 1>&2
exit 1
}
function findPid {
unset FOUND_PID;
[ $# -eq 1 ] || {
abort "findPid requires a parameter of pattern to match"
return 1
}
local PATTERN="$1"; shift
local _FOUND=`ps auxwww|grep "$PATTERN"|grep -v " $0"|grep -v grep|awk '{print $2}'`
[ -n "$_FOUND" ] && {
export FOUND_PID=$_FOUND
return 0
} || {
return 1
}
}
export PATH=/usr/ucb/bin:/bin:/usr/bin:/usr/sbin
findPid $@ || exit 1
[ -n "$FOUND_PID" ] && {
echo stopping $FOUND_PID
kill -9 $FOUND_PID
}
exit 0

View File

@ -0,0 +1,30 @@
@echo off
set PATH=
set JAVA_HOME=
set PATH=
GOTO FUNCTION_END
:abort
echo aborting: %EXCEPTION%
exit /b 1
:findPid
set FOUND_PID=
set _expression=%1
shift
set FIND_PROCESS=TASKLIST /FI "WINDOWTITLE eq %_expression%" /NH
FOR /F "usebackq tokens=2 delims= " %%A IN (`cmd /c "%FIND_PROCESS% 2>NUL"`) DO (
SET FOUND_PID=%%A
)
if defined FOUND_PID (
exit /b 0
) else (
set EXCEPTION=%_expression% not found
exit /b 1
)
:FUNCTION_END
set PATH=c:\windows\;C:\windows\system32;c:\windows\system32\wbem
call :findPid %*
if errorlevel 1 goto abort
if defined FOUND_PID (
TASKKILL /F /T /PID %FOUND_PID% >NUL
)
exit /b 0