Skip to content

Commit

Permalink
#58: handle different state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante committed Jul 7, 2013
1 parent 0ec3d34 commit c9602c9
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class AgentCliPackager extends BasePackager
{
copyInputPackage(packagePath)
configure(packagePath, tokens)
if(metaModel.stateMachine)
generateStateMachineJarFile(metaModel.stateMachine,
packagePath.createRelative('lib'))
}

return new PackagedArtifact(location: packagePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public class AgentServerPackager extends BasePackager
copyInputPackage(packagedArtifacts.agentServer.location)
Resource serverRoot = configure(packagedArtifacts.agentServer.location, tokens)

if(metaModel.gluMetaModel.stateMachine)
generateStateMachineJarFile(metaModel.gluMetaModel.stateMachine,
serverRoot.createRelative('lib'))

shell.delete(packagedArtifacts.agentServerUpgrade.location)
shell.cp(serverRoot, packagedArtifacts.agentServerUpgrade.location)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package org.pongasoft.glu.packaging.setup

import org.linkedin.glu.groovy.utils.io.GluGroovyIOUtils
import org.linkedin.glu.groovy.utils.shell.Shell
import org.linkedin.groovy.util.ant.AntUtils
import org.linkedin.groovy.util.io.GroovyIOUtils
import org.linkedin.util.io.resource.Resource
import org.pongasoft.glu.provisioner.core.metamodel.StateMachineMetaModel
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -149,4 +151,42 @@ ${out.join('\n')}
}
}
}

protected Resource generateStateMachineJarFile(StateMachineMetaModel stateMachineMetaModel,
Resource toFolder)
{
shell.withTempFile { Resource r ->

def lines = []

stateMachineMetaModel.defaultTransitions.transitions.each { state, transitions ->
transitions = transitions
.collect { transition -> "[to: '${transition.to}', action: '${transition.action}']"}
.join(', ')
lines << " '${state}': [${transitions}]"
}

def content = """
defaultTransitions =
[
${lines.join(',\n')}
]
defaultEntryState = '${stateMachineMetaModel.defaultEntryState}'
"""

shell.saveContent(r.createRelative('glu/DefaultStateMachine.groovy'), content)

shell.mkdirs(toFolder)

Resource jarFile = toFolder.createRelative('glu-state-machine.jar')

shell.ant { ant ->
ant.jar(destfile: jarFile.file,
basedir: r.file)
}

return jarFile
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ public class ConsoleServerPackager extends BasePackager

parts << metaModel.version

Resource packagePath =outputFolder.createRelative(parts.join('-'))
Resource packagePath = outputFolder.createRelative(parts.join('-'))
if(!dryMode)
{
copyInputPackage(packagePath)
configure(packagePath, tokens)
if(metaModel.gluMetaModel.stateMachine)
generateStateMachineJarFile(metaModel.gluMetaModel.stateMachine,
packagePath.createRelative('glu/repository/plugins'))
}
return new PackagedArtifact(location: packagePath,
host: metaModel.host.resolveHostAddress(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.nio.file.Files
public abstract class BasePackagerTest extends GroovyTestCase
{
public static final Object DIRECTORY = new Object()
public static final Object FILE = new Object()

public static class BinaryResource
{
Expand Down Expand Up @@ -182,7 +183,17 @@ public abstract class BasePackagerTest extends GroovyTestCase
assertEquals("binary content differ for ${r}",
rootShell.sha1(expectedValue.resource), rootShell.sha1(r))
else
assertEquals("mismatch content for ${r}", expectedValue, r.file.text)
if(expectedValue instanceof Closure)
{
expectedValue(r)
}
else
{
if(expectedValue.is(FILE))
assertTrue("${r} is file", !r.isDirectory())
else
assertEquals("mismatch content for ${r}", expectedValue, r.file.text)
}

if(r.path.endsWith('.sh'))
assertTrue("${r} is executable", Files.isExecutable(r.file.toPath()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package test.setup

import org.linkedin.glu.groovy.utils.shell.Shell
import org.linkedin.glu.groovy.utils.shell.ShellImpl
import org.linkedin.util.io.resource.Resource
import org.pongasoft.glu.packaging.setup.AgentCliPackager
import org.pongasoft.glu.packaging.setup.PackagedArtifact

Expand Down Expand Up @@ -102,6 +103,16 @@ truststorePassword=nacEn92x8-1
def metaModel = """
fabrics['f1'] = [ : ]
stateMachine = [
defaultTransitions: [
NONE: [[to: 's1', action: 'noneTOs1']],
s1: [[to: 'NONE', action: 's1TOnone'], [to: 's2', action: 's1TOs2']],
s2: [[to: 's1', action: 's2TOs1']]
],
defaultEntryState: 's2'
]
"""

def inputPackage = shell.mkdirs("/dist/org.linkedin.glu.agent-cli-${GLU_VERSION}")
Expand All @@ -126,6 +137,27 @@ fabrics['f1'] = [ : ]
"/README.md": 'this is the readme',
"/lib": DIRECTORY,
"/lib/acme.jar": 'this is the jar',
"/lib/glu-state-machine.jar": { Resource r ->
shell.withTempFile { Resource t ->
shell.unzip(r, t)
def er2 = [
'/META-INF': DIRECTORY,
'/META-INF/MANIFEST.MF': FILE,
'/glu': DIRECTORY,
'/glu/DefaultStateMachine.groovy': """
defaultTransitions =
[
'NONE': [[to: 's1', action: 'noneTOs1']],
's1': [[to: 'NONE', action: 's1TOnone'], [to: 's2', action: 's1TOs2']],
's2': [[to: 's1', action: 's2TOs1']]
]
defaultEntryState = 's2'
"""
]
checkPackageContent(er2, t)
}
},
"/conf": DIRECTORY,
"/conf/clientConfig.properties": """#
# Copyright (c) 2010-2010 LinkedIn, Inc
Expand Down Expand Up @@ -159,4 +191,6 @@ sslEnabled=false
checkPackageContent(expectedResources, artifact.location)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.pongasoft.glu.provisioner.core.metamodel.AgentMetaModel
import org.pongasoft.glu.provisioner.core.metamodel.ConsoleMetaModel
import org.pongasoft.glu.provisioner.core.metamodel.FabricMetaModel
import org.pongasoft.glu.provisioner.core.metamodel.GluMetaModel
import org.pongasoft.glu.provisioner.core.metamodel.StateMachineMetaModel
import org.pongasoft.glu.provisioner.core.metamodel.ZooKeeperClusterMetaModel

/**
Expand All @@ -30,6 +31,7 @@ public class GluMetaModelImpl implements GluMetaModel
public static final String META_MODEL_VERSION = '1.0.0'

Map<String, FabricMetaModel> fabrics
StateMachineMetaModel stateMachine
String gluVersion
String metaModelVersion = META_MODEL_VERSION
String zooKeeperRoot = DEFAULT_ZOOKEEPER_ROOT
Expand Down Expand Up @@ -102,6 +104,9 @@ public class GluMetaModelImpl implements GluMetaModel
gluVersion: gluVersion
]

if(stateMachine)
res.stateMachine = stateMachine.toExternalRepresentation()

if(fabrics)
res.fabrics = fabrics.collectEntries { k, v ->
[k, GluGroovyCollectionUtils.xorMap(v.toExternalRepresentation(), ['name'])] }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2013 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.pongasoft.glu.provisioner.core.metamodel.impl

import org.linkedin.groovy.util.state.StateMachine
import org.pongasoft.glu.provisioner.core.metamodel.StateMachineMetaModel

/**
* @author yan@pongasoft.com */
public class StateMachineMetaModelImpl implements StateMachineMetaModel
{
StateMachine defaultTransitions
String defaultEntryState

@Override
def toExternalRepresentation()
{
[
defaultTransitions: defaultTransitions.transitions,
defaultEntryState: defaultEntryState,
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.JsonParseException
import org.codehaus.groovy.control.CompilationFailedException
import org.linkedin.groovy.util.io.GroovyIOUtils
import org.linkedin.groovy.util.json.JsonUtils
import org.linkedin.groovy.util.state.StateMachineImpl
import org.linkedin.util.io.resource.Resource
import org.pongasoft.glu.provisioner.core.metamodel.GluMetaModel
import org.pongasoft.glu.provisioner.core.metamodel.KeysMetaModel
Expand All @@ -32,6 +33,7 @@ import org.pongasoft.glu.provisioner.core.metamodel.impl.KeyStoreMetaModelImpl
import org.pongasoft.glu.provisioner.core.metamodel.impl.KeysMetaModelImpl
import org.pongasoft.glu.provisioner.core.metamodel.impl.PhysicalHostMetaModelImpl
import org.pongasoft.glu.provisioner.core.metamodel.impl.ServerMetaModelImpl
import org.pongasoft.glu.provisioner.core.metamodel.impl.StateMachineMetaModelImpl
import org.pongasoft.glu.provisioner.core.metamodel.impl.ZooKeeperClusterMetaModelImpl
import org.pongasoft.glu.provisioner.core.metamodel.impl.ZooKeeperMetaModelImpl

Expand Down Expand Up @@ -98,10 +100,28 @@ public class GluMetaModelBuilder
if(jsonModel.zooKeeperRoot)
gluMetaModel.zooKeeperRoot = jsonModel.zooKeeperRoot

// state machine?
gluMetaModel.stateMachine = deserializeStateMachine(jsonModel.stateMachine)

// fabrics
jsonModel.fabrics?.each { name, fabricModel -> deserializeFabric(name, fabricModel)}
}

StateMachineMetaModelImpl deserializeStateMachine(Map stateMachineModel)
{
if(stateMachineModel)
{
StateMachineImpl stateMachine =
new StateMachineImpl(transitions: stateMachineModel.defaultTransitions)

return new StateMachineMetaModelImpl(defaultTransitions: stateMachine,
defaultEntryState: stateMachineModel.defaultEntryState)

}
else
return null
}

/**
* Deserialize a fabric
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class GluMetaModelJsonGroovyDsl
{
def binding = new GluMetaModelBinding(metaModelVersion: GluMetaModelImpl.META_MODEL_VERSION,
gluVersion: null,
stateMachine: null,
zooKeeperRoot: null,
fabrics: [:],
agents: [],
consoles: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public interface GluMetaModel extends Externable

String getZooKeeperRoot();

/**
* If you need to redefine the standard state machine (not a typical use case!)
*/
StateMachineMetaModel getStateMachine();

/**
* This is the version of glu
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2013 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.pongasoft.glu.provisioner.core.metamodel;

import org.linkedin.glu.utils.core.Externable;
import org.linkedin.groovy.util.state.StateMachine;

/**
* @author yan@pongasoft.com
*/
public interface StateMachineMetaModel extends Externable
{
StateMachine getDefaultTransitions();
String getDefaultEntryState();
}
Loading

0 comments on commit c9602c9

Please sign in to comment.