diff --git a/simple_app/__init__.py b/examples/__init__.py similarity index 100% rename from simple_app/__init__.py rename to examples/__init__.py diff --git a/examples/vm_job/run.py b/examples/vm_job/run.py new file mode 100755 index 0000000..5ee65ec --- /dev/null +++ b/examples/vm_job/run.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright 2013 - Mirantis, Inc. +# +# 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. + +# TODO(rakhmerov): remove "mock" dependency later +import mock +from mistralclient.api import client as cl + + +MISTRAL_URL = "http://localhost:8989/v1" +WORKBOOK_NAME = "vm_job_workbook" +WORKBOOK_DEFINITION_FILE_NAME = "run_vm_job.yaml" + +cl.Client.authenticate = mock.MagicMock(return_value=(MISTRAL_URL, + "", "", "")) +CLIENT = cl.Client(mistral_url=MISTRAL_URL, project_name="mistral_demo") + + +def create_workbook(wb_name): + """Create a workbook with the given name if it doesn't exist.""" + wb = None + + for item in CLIENT.workbooks.list(): + if item.name == wb_name: + wb = item + break + + if not wb: + wb = CLIENT.workbooks.create(wb_name, + description="My test workbook", + tags=["test"]) + print "Created workbook: %s" % wb + else: + print "Workbook already exists: %s" % wb + + +def upload_workbook_definition(wb_name, file_name): + """Upload a workbook definition from the given file name.""" + with open(file_name) as definition_file: + definition = definition_file.read() + + CLIENT.workbooks.upload_definition(wb_name, definition) + + print "\nUploaded workbook:\n\"\n%s\"\n" % \ + CLIENT.workbooks.get_definition(wb_name) + + +def create_execution(wb_name, task_name): + execution = CLIENT.executions.create(wb_name, task_name) + + print "Created execution: %s" % execution + + +def main(): + """Main script.""" + create_workbook(WORKBOOK_NAME) + upload_workbook_definition(WORKBOOK_NAME, WORKBOOK_DEFINITION_FILE_NAME) + create_execution(WORKBOOK_NAME, "runJob") + + +if __name__ == '__main__': + main() diff --git a/examples/vm_job/run_vm_job.yaml b/examples/vm_job/run_vm_job.yaml new file mode 100644 index 0000000..070311b --- /dev/null +++ b/examples/vm_job/run_vm_job.yaml @@ -0,0 +1,97 @@ +Workflow: + tasks: + runJob: + requires: + createVM: $.vm_ip != null + action: DemoApp:runJob + on-success: deleteVM + on-error: sendJobError + + deleteVM: + action: Nova:deleteVM + on-finish: end + + sendJobError: + action: std:email + parameters: + address: admin@mycompany.com + subject: Workflow error + body: Error occurred while running a VM job in execution {$.execution.id} + on-finish: deleteVM + +# Creating a VM and waiting till it is up (an IP address has been assigned). + + createVM: + action: Nova:createVM + on-success: waitForIP + on-error: sendCreateVMError + + waitForIP: + action: std:repeat + parameters: + task: getIP + retries: 5 + delay: 3000 + break-on: $.vm_ip != null + on-finish: + sendCreateVMError: $.vm_ip = null + on-error: + sendCreateVMError + + getIP: + action: Nova:getIP + + sendCreateVMError: + action: std:email + parameters: + address: admin@mycompany.com + subject: Workflow error + body: Failed to create a VM in execution {$.execution.id} + on-finish: end + +# Terminal no-op task. + + end: + action: Util:no-op + +# events: +# runJob: +# type: periodic +# tasks: runJob +# parameters: +# cron-pattern: "*/1 * * * *" + +Services: + Nova: + type: REST_API + parameters: + baseUrl: {$.novaURL} + actions: + createVM: + parameters: + url: /servers/{$.vm_id} + method: POST + response: + select: $.server.id + store-as: vm_id + getIP: + parameters: + url: /servers/{$.vm_id} + method: GET + response: + select: $.server.accessIPv4 + store-as: vm_ip + deleteVM: + parameters: + url: /servers/{$.vm_id} + method: DELETE + + DemoAPP: + type: MISTRAL_REST_API + parameters: + baseUrl: http://{$.vm_ip}:8080/ + actions: + runJob: + parameters: + url: /runJob + method: GET diff --git a/examples/webhooks/README.md b/examples/webhooks/README.md new file mode 100644 index 0000000..dcb094a --- /dev/null +++ b/examples/webhooks/README.md @@ -0,0 +1,17 @@ +Webhooks scheduling +=================== + +### Prerequisites +For this Mistral example an OpenStack installation is optional. + +In case of running the example without OpenStack server side authentication +based on Keystone must be disabled by setting configuration option "auth_enable" +under group "pecan" to False like the following: + +[pecan]
+auth_enable = False
+ + +### Running the example +From the root folder ("mistral-extra" by default) run the following shell command:

+*tox -evenv -- python examples/webhooks/cmd/api.py --config-file path_to_config* diff --git a/simple_app/api/__init__.py b/examples/webhooks/__init__.py similarity index 100% rename from simple_app/api/__init__.py rename to examples/webhooks/__init__.py diff --git a/simple_app/api/controllers/__init__.py b/examples/webhooks/api/__init__.py similarity index 100% rename from simple_app/api/controllers/__init__.py rename to examples/webhooks/api/__init__.py diff --git a/simple_app/api/app.py b/examples/webhooks/api/app.py similarity index 87% rename from simple_app/api/app.py rename to examples/webhooks/api/app.py index 3db7468..4182d40 100644 --- a/simple_app/api/app.py +++ b/examples/webhooks/api/app.py @@ -18,14 +18,14 @@ import pecan app = { - 'root': 'simple_app.api.controllers.root.RootController', - 'modules': ['simple_app.api'], + 'root': 'examples.webhooks.api.controllers.root.RootController', + 'modules': ['examples.webhooks.api'], 'debug': True, } def get_pecan_config(): - # Set up the pecan configuration + # Set up the pecan configuration. return pecan.configuration.conf_from_dict(app) diff --git a/simple_app/api/client.py b/examples/webhooks/api/client.py similarity index 97% rename from simple_app/api/client.py rename to examples/webhooks/api/client.py index 4e5940e..95a05d0 100644 --- a/simple_app/api/client.py +++ b/examples/webhooks/api/client.py @@ -19,7 +19,7 @@ import pkg_resources as pkg from mistralclient.api import client -from simple_app import version +from examples.webhooks import version MISTRAL_URL = "http://localhost:8989/v1" client.Client.authenticate = mock.MagicMock(return_value=(MISTRAL_URL, @@ -40,8 +40,10 @@ def upload_workbook(): description="My test workbook", tags=["test"]) print("Uploading workbook definition...\n") + definition = get_workbook_definition() CLIENT.workbooks.upload_definition(WB_NAME, definition) + print definition print("\nUploaded.") diff --git a/simple_app/cmd/__init__.py b/examples/webhooks/api/controllers/__init__.py similarity index 100% rename from simple_app/cmd/__init__.py rename to examples/webhooks/api/controllers/__init__.py diff --git a/simple_app/api/controllers/resource.py b/examples/webhooks/api/controllers/resource.py similarity index 100% rename from simple_app/api/controllers/resource.py rename to examples/webhooks/api/controllers/resource.py diff --git a/simple_app/api/controllers/root.py b/examples/webhooks/api/controllers/root.py similarity index 92% rename from simple_app/api/controllers/root.py rename to examples/webhooks/api/controllers/root.py index 111f0af..150e229 100644 --- a/simple_app/api/controllers/root.py +++ b/examples/webhooks/api/controllers/root.py @@ -20,9 +20,9 @@ from pecan import rest from wsme import types as wtypes import wsmeext.pecan as wsme_pecan -from simple_app.api.controllers import resource -from simple_app.api.controllers import tasks -from simple_app.api import client +from examples.webhooks.api.controllers import resource +from examples.webhooks.api.controllers import tasks +from examples.webhooks.api import client LOG = logging.getLogger(__name__) diff --git a/simple_app/api/controllers/tasks.py b/examples/webhooks/api/controllers/tasks.py similarity index 98% rename from simple_app/api/controllers/tasks.py rename to examples/webhooks/api/controllers/tasks.py index c8a6a27..3b0fad0 100644 --- a/simple_app/api/controllers/tasks.py +++ b/examples/webhooks/api/controllers/tasks.py @@ -21,7 +21,7 @@ import pecan from wsme import types as wtypes import wsmeext.pecan as wsme_pecan -from simple_app import tasks +from examples.webhooks import tasks LOG = logging.getLogger(__name__) diff --git a/examples/webhooks/cmd/__init__.py b/examples/webhooks/cmd/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/simple_app/cmd/main.py b/examples/webhooks/cmd/run.py similarity index 94% rename from simple_app/cmd/main.py rename to examples/webhooks/cmd/run.py index 0908292..9412a9f 100644 --- a/simple_app/cmd/main.py +++ b/examples/webhooks/cmd/run.py @@ -29,9 +29,9 @@ from wsgiref import simple_server from oslo.config import cfg -from simple_app import config -from simple_app.api import app -from simple_app.api import client +from examples.webhooks import config +from examples.webhooks.api import app +from examples.webhooks.api import client eventlet.monkey_patch( @@ -39,7 +39,6 @@ eventlet.monkey_patch( logging.basicConfig(level=logging.WARN) LOG = logging.getLogger(__name__) -CLIENT = client.CLIENT def upload_wb_and_start(): diff --git a/simple_app/config.py b/examples/webhooks/config.py similarity index 96% rename from simple_app/config.py rename to examples/webhooks/config.py index 0a5f72a..eeb3e78 100644 --- a/simple_app/config.py +++ b/examples/webhooks/config.py @@ -16,7 +16,7 @@ from oslo.config import cfg -from simple_app import version +from examples.webhooks import version api_opts = [ diff --git a/simple_app/demo.yaml b/examples/webhooks/demo.yaml similarity index 100% rename from simple_app/demo.yaml rename to examples/webhooks/demo.yaml diff --git a/simple_app/tasks.py b/examples/webhooks/tasks.py similarity index 96% rename from simple_app/tasks.py rename to examples/webhooks/tasks.py index ee5201d..fbc54ac 100644 --- a/simple_app/tasks.py +++ b/examples/webhooks/tasks.py @@ -17,7 +17,7 @@ from time import sleep import threading -from simple_app.api import client +from examples.webhooks.api import client CLIENT = client.CLIENT diff --git a/simple_app/version.py b/examples/webhooks/version.py similarity index 92% rename from simple_app/version.py rename to examples/webhooks/version.py index 20d2f21..8cbae55 100644 --- a/simple_app/version.py +++ b/examples/webhooks/version.py @@ -16,5 +16,5 @@ from pbr import version -version_info = version.VersionInfo('simple_app') +version_info = version.VersionInfo('examples.webhooks') version_string = version_info.version_string diff --git a/setup.cfg b/setup.cfg index 93f3b0f..1b207c1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -18,10 +18,10 @@ author-email = openstack-dev@lists.openstack.org [files] packages = - mistralextra + examples [nosetests] -cover-package = mistralextra +cover-package = examples [extract_messages] keywords = _ gettext ngettext l_ lazy_gettext