Skip to content

Commit

Permalink
#166: wrap command execution in glu script
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante committed Nov 18, 2012
1 parent da0254d commit 2e2c267
Show file tree
Hide file tree
Showing 15 changed files with 496 additions and 135 deletions.
Expand Up @@ -45,6 +45,7 @@ import org.linkedin.glu.agent.impl.command.CommandManagerImpl
import org.linkedin.glu.agent.api.NoSuchCommandException
import org.linkedin.glu.commands.impl.MemoryCommandExecutionIOStorage
import org.linkedin.glu.commands.impl.CommandExecution
import org.linkedin.glu.agent.impl.command.CommandGluScriptFactoryFactory

/**
* The main implementation of the agent
Expand Down Expand Up @@ -89,9 +90,24 @@ def class AgentImpl implements Agent, AgentContext, Shutdownable
_sigar = args.sigar
_mop = args.mop ?: new MOPImpl()
_scriptManager = args.scriptManager ?: new ScriptManagerImpl(agentContext: this)
if(args.commandManager)
_commandManager = args.commandManager
else
{
_commandManager = new CommandManagerImpl(agentContext: this,
ioStorage: new MemoryCommandExecutionIOStorage(clock: clock),
scriptManager: _scriptManager)
if(!args.scriptManager)
{
def f = new CommandGluScriptFactoryFactory(ioStorage: _commandManager.ioStorage)
_scriptManager.scriptFactoryFactory.chain(f)
}
}

_commandManager = args.commandManager ?:
new CommandManagerImpl(agentContext: this,
storage: new MemoryCommandExecutionIOStorage(clock: clock))
ioStorage: new MemoryCommandExecutionIOStorage(clock: clock),
scriptManager: _scriptManager)
def storage = args.storage
if(storage != null)
_scriptManager = new StateKeeperScriptManager(scriptManager: _scriptManager,
Expand Down
@@ -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()]
}
}
}
}
}
}
@@ -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)
}
}
@@ -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
}
}

0 comments on commit 2e2c267

Please sign in to comment.