You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stratos.apache.org by Udara Liyanage <ud...@wso2.com> on 2015/04/22 06:08:58 UTC
Python agent - Utility method to read payload parameters
Hi,
Is there a util method to read payload parameters in Python Cartridge
agent? I am writing a plugin/extension which needs to read payload
parameters to function.
Currently I am reading the file and access the properties
launch_params_file =
os.path.abspath(os.path.dirname(__file__)).split("extensions")[0] +
"payload/launch-params"
metadata_file = open(launch_params_file, "r")
properties = {}
for param in metadata_payload_content.split(","):
if param.strip() != "":
param_value = param.strip().split("=")
properties[param_value[0]] = param_value[1]
token = properties['TOKEN']
Udara Liyanage
Software Engineer
WSO2, Inc.: http://wso2.com
lean. enterprise. middleware
web: http://udaraliyanage.wordpress.com
phone: +94 71 443 6897
Re: Python agent - Utility method to read payload parameters
Posted by Udara Liyanage <ud...@wso2.com>.
Hi Chamila,
Thanks Chamila. Better to rename so someone looking know that they are
payload parameters
On Wed, Apr 22, 2015 at 11:30 PM, Imesh Gunaratne <im...@apache.org> wrote:
> A good design Chamila! May be we could rename the variable "values" to
> something meaningful.
>
> Thanks
>
> On Wed, Apr 22, 2015 at 11:37 AM, Chamila De Alwis <ch...@wso2.com>
> wrote:
>
>> Hi Udara,
>>
>> You don't have to read the payload parameter file from the plugins. When
>> subclassing from the plugin contract class ICartridgeAgentPlugin in
>> plugins/contracts.py and overriding run_plugin() method, you receive the
>> payload parameters as a dictionary in the argument "values".
>>
>> Following is a brief example of a plugin, CustomPlugin, which reads a
>> value from the meta data service and the payload parameters. I hope the
>> code is self-describable.
>>
>>
>> import mdsclient
>> from plugins.contracts import ICartridgeAgentPlugin
>> import time
>> from modules.util.log import LogFactory
>> import os
>>
>>
>> class CustomPlugin(ICartridgeAgentPlugin):
>>
>> def run_plugin(self, values):
>> log = LogFactory().get_log(__name__)
>> # wait till SOME_VALUE becomes available
>> mds_response = None
>> while mds_response is None:
>> time.sleep(5)
>> mds_response = mdsclient.get(app=True)
>> if mds_response is not None and mds_response.properties.get("SOME_VALUE") is None:
>> mds_response =
>>
>> env_var["STRATOS_HOST_NAME"] = values["HOST_NAME"]
>> payload_ports = values["PORT_MAPPINGS"].split("|")
>>
>> On Apr 22, 2015 9:39 AM, "Udara Liyanage" <ud...@wso2.com> wrote:
>>
>>> Hi,
>>>
>>> Is there a util method to read payload parameters in Python Cartridge agent? I am writing a plugin/extension which needs to read payload parameters to function.
>>>
>>> Currently I am reading the file and access the properties
>>>
>>>
>>> launch_params_file = os.path.abspath(os.path.dirname(__file__)).split("extensions")[0] + "payload/launch-params"
>>> metadata_file = open(launch_params_file, "r")
>>> properties = {}
>>> for param in metadata_payload_content.split(","):
>>> if param.strip() != "":
>>> param_value = param.strip().split("=")
>>> properties[param_value[0]] = param_value[1]
>>>
>>> token = properties['TOKEN']
>>>
>>> Udara Liyanage
>>> Software Engineer
>>> WSO2, Inc.: http://wso2.com
>>> lean. enterprise. middleware
>>>
>>> web: http://udaraliyanage.wordpress.com
>>> phone: +94 71 443 6897
>>>
>>
>
>
> --
> Imesh Gunaratne
>
> Technical Lead, WSO2
> Committer & PMC Member, Apache Stratos
>
--
Udara Liyanage
Software Engineer
WSO2, Inc.: http://wso2.com
lean. enterprise. middleware
web: http://udaraliyanage.wordpress.com
phone: +94 71 443 6897
Re: Python agent - Utility method to read payload parameters
Posted by Imesh Gunaratne <im...@apache.org>.
A good design Chamila! May be we could rename the variable "values" to
something meaningful.
Thanks
On Wed, Apr 22, 2015 at 11:37 AM, Chamila De Alwis <ch...@wso2.com>
wrote:
> Hi Udara,
>
> You don't have to read the payload parameter file from the plugins. When
> subclassing from the plugin contract class ICartridgeAgentPlugin in
> plugins/contracts.py and overriding run_plugin() method, you receive the
> payload parameters as a dictionary in the argument "values".
>
> Following is a brief example of a plugin, CustomPlugin, which reads a
> value from the meta data service and the payload parameters. I hope the
> code is self-describable.
>
>
> import mdsclient
> from plugins.contracts import ICartridgeAgentPlugin
> import time
> from modules.util.log import LogFactory
> import os
>
>
> class CustomPlugin(ICartridgeAgentPlugin):
>
> def run_plugin(self, values):
> log = LogFactory().get_log(__name__)
> # wait till SOME_VALUE becomes available
> mds_response = None
> while mds_response is None:
> time.sleep(5)
> mds_response = mdsclient.get(app=True)
> if mds_response is not None and mds_response.properties.get("SOME_VALUE") is None:
> mds_response =
>
> env_var["STRATOS_HOST_NAME"] = values["HOST_NAME"]
> payload_ports = values["PORT_MAPPINGS"].split("|")
>
> On Apr 22, 2015 9:39 AM, "Udara Liyanage" <ud...@wso2.com> wrote:
>
>> Hi,
>>
>> Is there a util method to read payload parameters in Python Cartridge agent? I am writing a plugin/extension which needs to read payload parameters to function.
>>
>> Currently I am reading the file and access the properties
>>
>>
>> launch_params_file = os.path.abspath(os.path.dirname(__file__)).split("extensions")[0] + "payload/launch-params"
>> metadata_file = open(launch_params_file, "r")
>> properties = {}
>> for param in metadata_payload_content.split(","):
>> if param.strip() != "":
>> param_value = param.strip().split("=")
>> properties[param_value[0]] = param_value[1]
>>
>> token = properties['TOKEN']
>>
>> Udara Liyanage
>> Software Engineer
>> WSO2, Inc.: http://wso2.com
>> lean. enterprise. middleware
>>
>> web: http://udaraliyanage.wordpress.com
>> phone: +94 71 443 6897
>>
>
--
Imesh Gunaratne
Technical Lead, WSO2
Committer & PMC Member, Apache Stratos
Re: Python agent - Utility method to read payload parameters
Posted by Chamila De Alwis <ch...@wso2.com>.
Hi Udara,
You don't have to read the payload parameter file from the plugins. When
subclassing from the plugin contract class ICartridgeAgentPlugin in
plugins/contracts.py and overriding run_plugin() method, you receive the
payload parameters as a dictionary in the argument "values".
Following is a brief example of a plugin, CustomPlugin, which reads a value
from the meta data service and the payload parameters. I hope the code is
self-describable.
import mdsclient
from plugins.contracts import ICartridgeAgentPlugin
import time
from modules.util.log import LogFactory
import os
class CustomPlugin(ICartridgeAgentPlugin):
def run_plugin(self, values):
log = LogFactory().get_log(__name__)
# wait till SOME_VALUE becomes available
mds_response = None
while mds_response is None:
time.sleep(5)
mds_response = mdsclient.get(app=True)
if mds_response is not None and
mds_response.properties.get("SOME_VALUE") is None:
mds_response =
env_var["STRATOS_HOST_NAME"] = values["HOST_NAME"]
payload_ports = values["PORT_MAPPINGS"].split("|")
On Apr 22, 2015 9:39 AM, "Udara Liyanage" <ud...@wso2.com> wrote:
> Hi,
>
> Is there a util method to read payload parameters in Python Cartridge agent? I am writing a plugin/extension which needs to read payload parameters to function.
>
> Currently I am reading the file and access the properties
>
>
> launch_params_file = os.path.abspath(os.path.dirname(__file__)).split("extensions")[0] + "payload/launch-params"
> metadata_file = open(launch_params_file, "r")
> properties = {}
> for param in metadata_payload_content.split(","):
> if param.strip() != "":
> param_value = param.strip().split("=")
> properties[param_value[0]] = param_value[1]
>
> token = properties['TOKEN']
>
> Udara Liyanage
> Software Engineer
> WSO2, Inc.: http://wso2.com
> lean. enterprise. middleware
>
> web: http://udaraliyanage.wordpress.com
> phone: +94 71 443 6897
>