Skip to content

Instantly share code, notes, and snippets.

@maxott
Forked from jackhong/test01.rb
Created October 9, 2012 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxott/3856722 to your computer and use it in GitHub Desktop.
Save maxott/3856722 to your computer and use it in GitHub Desktop.
DSL in OMF 6
# Original test01.rb
defProperty('res1', "unconfigured-node-1", "ID of a node")
defProperty('res2', "unconfigured-node-2", "ID of a node")
defGroup('Actor', property.res1)
onEvent(:ALL_UP) do |event|
wait 3
info "TEST - allGroups"
allGroups.net.w0.ip = '10.0.0.1'
allGroups.exec("/bin/date")
wait 3
info "TEST - group"
group("Actor").exec("/bin/hostname -f")
wait 3
Experiment.done
end
# Moving to OMF 6
# * We could favour underscored ruby style method name, and alias camelcase method names in omf 5
# OMF_VERSIONS = 6.0
# (maybe think about a different name than 'property')
#
def_property('res1', { :default => "unconfigured-node-1",
:description => "ID of a node",
:type => "virtual_machine" } )
def_property('packet_size', { :default => 128,
:description => "Size of pkts in Byte",
:type => "fixnum" } )
# def_group creates a new resource of type 'Group'
#
def_group('myGroup') do |g|
g.add_resource('res1','res2', 'groupX', ...)
end
## This...
#
def_group('group1') do |g|
g.memberships << 'group2'
end
#
## Equals to that...
#
def_group('group2') do |g|
g.add_resource('group1')
end
# Event is now more or less a pure condition checking.
# :prop? is a binding property
# in english: event triggers when
# resource_name1 has its property_nameA equals to valueX
# AND any resource have at least one of its property equals to valueY
# AND resource_name2 has that same property equals to valueY
#
# there is no order in the list of conditions
#
def_event :all_up?, [
['resource_name1', 'property_nameA', 'valueX'],
[ nil, :prop?, 'valueY'],
[ 'resource_name2', :prop?, 'valueY']
]
#
# optional: matched_properties = {:prop => 'whatever property matched :prop?'}
#
on_event :all_up? do |matched_properties|
all_groups.state = "ON"
group('myGroup') do |g|
# for all members of g, get the value of their 'interfaces' property
# that value might be a set of interface resources for a single member of g
# for each member of that set which matches the condition name = 'wlan0'
# configure its essid property to 'foo'
#
# so this results in a configure message sent to the 'wlan0' resource interface of
# all resources, setting their essid to 'foo'
#
g.interfaces[:name => 'wlan0'].essid = 'foo'
# %index% will be substituted by the resource receiving the configure
# it will replace it by the value of its 'index' property
#
g.interfaces[:name => 'wlan0'].ip = '10.0.0.%index%'
g.create_resource('app_foo', :type => "generic_application" , :path, .....)
my_app = { :type => "application", :path => etc... }
g.create_resource('app_foo', my_app)
g.applications[:name => 'app_foo'].target_ip = g[:name => 'node1'].interfaces[:name => 'wlan0'].ip
after 10.second do
g.applications[:name => 'app_foo'].packet_size = property.packet_size
end
# Internally, we implement this so that the following is executed 10s after the previous
after 10.second do
g.applications[:name => 'app_foo'].packet_size = 1024
end
end
done! # Experiment.done, simply shutting down main event loop.
end
#
# Experiment with an application
#
defProperty('res1', "unconfigured-node-1", "ID of a node")
defProperty('res2', "unconfigured-node-2", "ID of a node")
defGroup('Actor', property.res1) do |n|
n.addApplication('foo') do |a|
a.property('pktsize', 512)
a.measure('udp', :interval => 5)
a.measure('tcp', :samples => 10) do |m|
m.addMetric('size',:average)
end
end
end
onEvent(:ALL_UP) do |event|
wait 3
info "TEST - allGroups"
allGroups.startApplications
wait 10
allGroups.stopApplication('foo')
Experiment.done
end
# def_group creates a new resource of type 'Group'
#
def_group('Actor') do |g|
g.add_resource('res1')
# This results in a new group being implicitly created e.g. 'actor_app_foo'
# and all the application resources 'foo' have to be members of that new group
#
# NOTE: we need to have some sanity checks to ensure that properties which are Ruby identifiers
# do raise some error for the user, e.g. we cannot have a 'to_s' property
#
g.add_application('foo') do |app|
app.pkt_size = 512
app.measure('tcp', :samples => 10) do |m|
m.metric('size',:average)
end
end
end
all_groups.applications.state = :run
g.applications[:name => 'foo'].state = stop
g.each_member do |n|
n.request_applications do |apps|
apps.configure_state =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment