Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#166: wrap command execution in glu script
- Loading branch information
Showing
15 changed files
with
496 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...lu.agent-impl/src/main/groovy/org/linkedin/glu/agent/impl/command/CommandGluScript.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2012 Yan Pujante | ||
* | ||
* Licensed 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.linkedin.glu.agent.impl.command | ||
|
||
import org.linkedin.glu.commands.impl.CommandExecution | ||
import org.linkedin.glu.commands.impl.CommandStreamStorage | ||
import org.linkedin.glu.commands.impl.StreamType | ||
import org.linkedin.glu.groovy.utils.concurrent.CallExecution | ||
import org.linkedin.glu.commands.impl.CommandExecutionIOStorage | ||
import org.linkedin.util.annotations.Initializable | ||
|
||
/** | ||
* The glu script that wraps the execution of a glu command | ||
* | ||
* @author yan@pongasoft.com */ | ||
public class CommandGluScript | ||
{ | ||
@Initializable(required = true) | ||
String commandId | ||
|
||
@Initializable(required = true) | ||
transient CommandExecutionIOStorage ioStorage | ||
|
||
// will be set | ||
transient volatile CommandExecution commandExecution | ||
|
||
def install = { args -> | ||
commandExecution = args.commandExecution | ||
} | ||
|
||
def uninstall = { | ||
commandExecution = null | ||
} | ||
|
||
/** | ||
* Used in case | ||
*/ | ||
private CommandExecution findCommandExecution() | ||
{ | ||
if(!commandExecution) | ||
commandId = ioStorage.findCommandExecution(commandId) | ||
commandExecution | ||
} | ||
|
||
/** | ||
* Execute the command execution | ||
*/ | ||
def start = { args -> | ||
|
||
CommandExecution commandExecution = findCommandExecution() | ||
|
||
commandExecution.syncCaptureIO { CommandStreamStorage storage -> | ||
|
||
def actionArgs = [*:commandExecution.args] | ||
|
||
// handle stdin... | ||
storage.withOrWithoutStorageInput(StreamType.stdin) { stdin -> | ||
if(stdin) | ||
actionArgs.stdin = stdin | ||
|
||
// handle stdout... | ||
storage.withOrWithoutStorageOutput(StreamType.stdout) { stdout -> | ||
if(stdout) | ||
actionArgs.stdout = stdout | ||
|
||
// handle stderr | ||
storage.withOrWithoutStorageOutput(StreamType.stderr) { stderr -> | ||
if(stderr) | ||
actionArgs.stderr = stderr | ||
|
||
// finally run the commandExecution | ||
def callExecution = new CallExecution(action: 'run', | ||
actionArgs: actionArgs, | ||
clock: shell.clock, | ||
source: [invocable: commandExecution.command]) | ||
|
||
return [exitValue: callExecution.runSync()] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...t-impl/src/main/groovy/org/linkedin/glu/agent/impl/command/CommandGluScriptFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2012 Yan Pujante | ||
* | ||
* Licensed 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.linkedin.glu.agent.impl.command | ||
|
||
import org.linkedin.glu.agent.impl.script.ScriptFactory | ||
import org.linkedin.glu.agent.impl.script.ScriptConfig | ||
import org.linkedin.util.annotations.Initializable | ||
import org.linkedin.glu.commands.impl.CommandExecutionIOStorage | ||
|
||
/** | ||
* @author yan@pongasoft.com */ | ||
public class CommandGluScriptFactory implements ScriptFactory | ||
{ | ||
@Initializable(required = true) | ||
CommandExecutionIOStorage ioStorage | ||
|
||
@Initializable(required = true) | ||
String commandId | ||
|
||
@Override | ||
def createScript(ScriptConfig scriptConfig) | ||
{ | ||
new CommandGluScript(ioStorage: ioStorage, commandId: commandId) | ||
} | ||
|
||
@Override | ||
def toExternalRepresentation() | ||
{ | ||
return [commandGluScriptFactory: commandId] | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "CommandGluScriptFactory[${commandId}]" | ||
} | ||
|
||
boolean equals(o) | ||
{ | ||
if(this.is(o)) return true | ||
if(getClass() != o.class) return false | ||
|
||
CommandGluScriptFactory that = (CommandGluScriptFactory) o | ||
|
||
if(commandId != that.commandId) return false | ||
|
||
return true | ||
} | ||
|
||
int hashCode() | ||
{ | ||
return (commandId != null ? commandId.hashCode() : 0) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...src/main/groovy/org/linkedin/glu/agent/impl/command/CommandGluScriptFactoryFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2012 Yan Pujante | ||
* | ||
* Licensed 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.linkedin.glu.agent.impl.command | ||
|
||
import org.linkedin.glu.agent.impl.script.AbstractScriptFactoryFactory | ||
import org.linkedin.glu.agent.impl.script.ScriptFactory | ||
import org.linkedin.util.annotations.Initializable | ||
import org.linkedin.glu.commands.impl.CommandExecutionIOStorage | ||
|
||
/** | ||
* @author yan@pongasoft.com */ | ||
public class CommandGluScriptFactoryFactory extends AbstractScriptFactoryFactory | ||
{ | ||
@Initializable(required = true) | ||
CommandExecutionIOStorage ioStorage | ||
|
||
@Override | ||
protected ScriptFactory doCreateScriptFactory(def args) | ||
{ | ||
if(args.commandGluScriptFactory) | ||
return new CommandGluScriptFactory(ioStorage: ioStorage, | ||
commandId: args.commandGluScriptFactory) | ||
|
||
return null | ||
} | ||
} |
Oops, something went wrong.