Skip to content

Instantly share code, notes, and snippets.

@fultonj
Created June 25, 2017 03:01
Show Gist options
  • Save fultonj/d002e98900ec4da82f2ef4d2540408f0 to your computer and use it in GitHub Desktop.
Save fultonj/d002e98900ec4da82f2ef4d2540408f0 to your computer and use it in GitHub Desktop.
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
import os
import shutil
import six
import tempfile
import re
from mistral.actions import base
from mistral.workflow import utils as mistral_workflow_utils
class FileExists(base.Action):
"""Fails unless path exists on localhost"""
def __init__(self, path):
self.path = path
def run(self):
return_value = {'stderr': ''}
if (isinstance(self.path, six.string_types) and
os.path.exists(self.path)):
return_value['stdout'] = "Found file %s" % self.path
mistral_result = {"data": return_value}
else:
return_value['stdout'] = "File %s not found" % self.path
mistral_result = {"error": return_value}
return mistral_workflow_utils.Result(**mistral_result)
class MakeTempDir(base.Action):
"""Creates temporary directory under /tmp/ on localhost"""
def __init__(self):
pass
def run(self):
return_value = {'stderr': ''}
_path = tempfile.mkdtemp(dir='/tmp/', prefix='file-mistral-action')
if (isinstance(_path, six.string_types) and
os.path.exists(_path)):
return_value['path'] = _path
return_value['stdout'] = "Created directory %s" % _path
mistral_result = {"data": return_value}
else:
return_value['stdout'] = "Unable to create directory %s" % _path
mistral_result = {"error": return_value}
return mistral_workflow_utils.Result(**mistral_result)
class RemoveTempDir(base.Action):
"""Removes path on localhost; path must start with /tmp"""
def __init__(self, path):
self.path = path
def run(self):
if (not isinstance(self.path, six.string_types) or
not re.match('^/tmp', self.path)):
msg = "path must be a string beginning with '/tmp'"
return mistral_workflow_utils.Result(error=msg)
return_value = {'stderr': ''}
try:
shutil.rmtree(self.path)
return_value['stdout'] = "Deleted directory %s" % self.path
mistral_result = {"data": return_value}
except Exception as err:
return mistral_workflow_utils.Result(error=six.text_type(err))
return mistral_workflow_utils.Result(**mistral_result)
pushd /home/stack/tripleo-common/
sudo rm -Rf /usr/lib/python2.7/site-packages/tripleo_common*
sudo python setup.py install
sudo cp /usr/share/tripleo-common/sudoers /etc/sudoers.d/tripleo-common
sudo systemctl restart openstack-mistral-executor
sudo systemctl restart openstack-mistral-engine
sudo mistral-db-manage populate
popd
if [[ ! -e /usr/lib/python2.7/site-packages/tripleo_common/actions/files.pyc ]]; then
echo "WARNING: files.py did not compile"
exit 1
fi
action=tripleo.files
grep $action /home/stack/tripleo-common/setup.cfg
mistral action-list | grep $action
if [[ ! $? -eq 0 ]]; then
echo "WARNING: $action action not found"
fi
if [[ $RUN -eq 1 ]]; then
WORKFLOW=files
EXISTS=$(mistral workflow-list | grep $WORKFLOW | wc -l)
if [[ $EXISTS -gt 0 ]]; then
mistral workflow-update files.yaml
else
mistral workflow-create files.yaml
fi
mistral execution-create $WORKFLOW
UUID=$(mistral execution-list | grep $WORKFLOW | awk {'print $2'} | tail -1)
if [ -z $UUID ]; then
echo "Error: unable to find UUID. Exixting."
exit 1
fi
for TASK_ID in $(mistral task-list $UUID | awk {'print $2'} | egrep -v 'ID|^$'); do
echo $TASK_ID
mistral task-get $TASK_ID
mistral task-get-result $TASK_ID | jq . | sed -e 's/\\n/\n/g' -e 's/\\"/"/g'
done
export UUID
export TASK_ID
fi
+---------------+-------+---------------+--------+-------+---------------+------------------+
| ID | Name | Project ID | Tags | Input | Created at | Updated at |
+---------------+-------+---------------+--------+-------+---------------+------------------+
| 1a3a8c0d- | files | b31c2039ca394 | <none> | | 2017-06-23 | 2017-06-25 |
| a81c-41e9-89b | | 32da6942f1615 | | | 20:27:42 | 02:29:55 |
| 0-50d6806eea9 | | 551ca8 | | | | |
| 1 | | | | | | |
+---------------+-------+---------------+--------+-------+---------------+------------------+
+-------------------+--------------------------------------+
| Field | Value |
+-------------------+--------------------------------------+
| ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| Workflow ID | 1a3a8c0d-a81c-41e9-89b0-50d6806eea91 |
| Workflow name | files |
| Description | |
| Task Execution ID | <none> |
| State | RUNNING |
| State info | None |
| Created at | 2017-06-25 02:32:28 |
| Updated at | 2017-06-25 02:32:28 |
+-------------------+--------------------------------------+
7c01fdca-86e2-443b-9191-f8e68dc761c8
+---------------+--------------------------------------+
| Field | Value |
+---------------+--------------------------------------+
| ID | 7c01fdca-86e2-443b-9191-f8e68dc761c8 |
| Name | create_temp_file |
| Workflow name | files |
| Execution ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| State | SUCCESS |
| State info | None |
| Created at | 2017-06-25 02:32:28 |
| Updated at | 2017-06-25 02:32:29 |
+---------------+--------------------------------------+
{
"stdout": "Created directory /tmp/file-mistral-actionA5vXZ7",
"stderr": "",
"path": "/tmp/file-mistral-actionA5vXZ7"
}
99762343-9735-4625-9712-c3df166f5940
+---------------+--------------------------------------+
| Field | Value |
+---------------+--------------------------------------+
| ID | 99762343-9735-4625-9712-c3df166f5940 |
| Name | the_file_exists |
| Workflow name | files |
| Execution ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| State | SUCCESS |
| State info | None |
| Created at | 2017-06-25 02:32:28 |
| Updated at | 2017-06-25 02:32:28 |
+---------------+--------------------------------------+
{
"stdout": "Found file /etc/issue",
"stderr": ""
}
62a58a38-14e4-408b-bf79-ea1f9195259d
+---------------+---------------------------------------------+
| Field | Value |
+---------------+---------------------------------------------+
| ID | 62a58a38-14e4-408b-bf79-ea1f9195259d |
| Name | deletion_should_fail |
| Workflow name | files |
| Execution ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| State | ERROR |
| State info | path must be a string beginning with '/tmp' |
| Created at | 2017-06-25 02:32:29 |
| Updated at | 2017-06-25 02:32:29 |
+---------------+---------------------------------------------+
"path must be a string beginning with '/tmp'"
63483065-d689-4de3-a8e9-cfb8e41b5e84
+---------------+--------------------------------------+
| Field | Value |
+---------------+--------------------------------------+
| ID | 63483065-d689-4de3-a8e9-cfb8e41b5e84 |
| Name | print_tmp |
| Workflow name | files |
| Execution ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| State | SUCCESS |
| State info | None |
| Created at | 2017-06-25 02:32:29 |
| Updated at | 2017-06-25 02:32:29 |
+---------------+--------------------------------------+
"/tmp/file-mistral-actionA5vXZ7"
8a857217-c229-4059-9dc0-671140aaa2df
+---------------+--------------------------------------+
| Field | Value |
+---------------+--------------------------------------+
| ID | 8a857217-c229-4059-9dc0-671140aaa2df |
| Name | remove_tmp |
| Workflow name | files |
| Execution ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| State | SUCCESS |
| State info | None |
| Created at | 2017-06-25 02:32:29 |
| Updated at | 2017-06-25 02:32:29 |
+---------------+--------------------------------------+
{
"stdout": "Deleted directory /tmp/file-mistral-actionA5vXZ7",
"stderr": ""
}
9d854dfb-7429-4caf-9174-af109505127f
+---------------+--------------------------------------+
| Field | Value |
+---------------+--------------------------------------+
| ID | 9d854dfb-7429-4caf-9174-af109505127f |
| Name | deletion_success |
| Workflow name | files |
| Execution ID | 545bc24c-4635-4a17-b396-c6e63012abdb |
| State | SUCCESS |
| State info | None |
| Created at | 2017-06-25 02:32:29 |
| Updated at | 2017-06-25 02:32:29 |
+---------------+--------------------------------------+
"Delted temporary file"
---
version: "2.0"
files:
tasks:
the_file_exists:
action: tripleo.files.file_exists path='/etc/issue'
on-success: create_temp_file
on-error: fail_missing_file
fail_missing_file:
action: std.echo output="File in missing, you need to put it there"
create_temp_file:
action: tripleo.files.make_temp_dir
publish:
tmp: <% task(create_temp_file).result.path %>
on-success: print_tmp
on-error: fail_no_tmp
fail_no_tmp:
action: std.echo output="Unable to create tmp file"
print_tmp:
action: std.echo output=<% $.tmp %>
on-success: remove_tmp
remove_tmp:
action: tripleo.files.remove_temp_dir path=<% $.tmp %>
on-success: deletion_success
on-error: deletion_failure
deletion_failure:
action: std.echo output="Unable to delete tmp file"
deletion_success:
action: std.echo output="Delted temporary file"
on-success: deletion_should_fail
deletion_should_fail:
action: tripleo.files.remove_temp_dir path='/home/foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment