forked from cloudify-cosmo/cloudify-bash-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
217 lines (166 loc) · 6.63 KB
/
tasks.py
File metadata and controls
217 lines (166 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. 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 collections
import subprocess
import fcntl
import select
import os
import errno
from os.path import dirname
from cloudify import utils
from cloudify.utils import get_manager_ip
from cloudify.decorators import operation
from bash_runner import resources
@operation
def run(ctx, script_path=None, log_all=False, **kwargs):
"""
Execute bash scripts.
Parameters:
scripts - A dictionary mapping lifecycle events to script paths.
script_path - The path to the script relative
to the blueprints root directory.
Will only be used if the 'scripts' argument is None.
Exceptions:
If both 'scripts' and 'script_path' is None.
A runtime exception will be raised since there is no
script to run.
"""
sh = get_script_to_run(ctx, script_path)
if sh is None:
return None
bash(sh, ctx, log_all)
return "[{0}] succeeded. return code 0".format(os.path.basename(sh))
def get_script_to_run(ctx, script_path=None):
if script_path:
return ctx.download_resource(script_path)
if 'scripts' in ctx.properties:
operation_simple_name = ctx.operation.split('.')[-1:].pop()
scripts = ctx.properties['scripts']
if operation_simple_name not in scripts:
ctx.logger.info("No script mapping found for operation {0}. "
"Nothing to do.".format(operation_simple_name))
return None
return ctx.download_resource(scripts[operation_simple_name])
raise RuntimeError('No script to run')
def run_and_return_output(ctx, script_path=None, log_all=False, **kwargs):
sh = get_script_to_run(ctx, script_path)
if sh is None:
return None
return bash(sh, ctx, log_all)
def strip_level(line, level):
return line.replace('[{0}] '.format(level), '', 1)
def is_info_log(line):
return line.startswith('[INFO]')
def is_error_log(line):
return line.startswith('[ERROR]')
def bash(path, ctx, log_all):
return execute("/bin/bash {0}".format(path), ctx, log_all)
def execute(command, ctx, log_all):
ctx.logger.info('Running command: %s' % command)
env = setup_environment(ctx)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env)
make_async(process.stdout)
make_async(process.stderr)
stdout = str()
stderr = str()
return_code = None
while True:
# Wait for data to become available
select.select([process.stdout, process.stderr], [], [])
return_code = process.poll()
# Try reading some data from each
stdout_piece = read_async(process.stdout)
stderr_piece = read_async(process.stderr)
if stdout_piece:
if is_info_log(stdout_piece) or log_all:
ctx.logger.info(strip_level(stdout_piece,
'INFO'))
if is_error_log(stdout_piece):
ctx.logger.error(strip_level(stdout_piece,
'ERROR'))
if stderr_piece:
ctx.logger.error(stderr_piece)
stdout += stdout_piece
stderr += stderr_piece
if return_code is not None and not stdout_piece and not stderr_piece:
break
ctx.logger.info('Done running command (return_code=%d): %s'
% (return_code, command))
if return_code == 0:
return stdout
else:
raise ProcessException(command, return_code, stdout, stderr)
# Helper function to add the O_NONBLOCK flag to a file descriptor
def make_async(fd):
fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) |
os.O_NONBLOCK)
# Helper function to read some data from a file descriptor,
# ignoring EAGAIN errors
def read_async(fd):
try:
return fd.readline()
except IOError, e:
if e.errno != errno.EAGAIN:
raise e
else:
return ''
def flatten(d, parent_key=''):
items = []
for k, v in d.items():
new_key = parent_key + '_' + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key).items())
else:
items.append((new_key, v))
return dict(items)
def setup_environment(ctx):
"""
Add some useful environment variables to the environment
"""
env = os.environ.copy()
# See in context.py
# https://github.com/CloudifySource
# /cosmo-celery-common/blob/develop/cloudify/context.py
env['CLOUDIFY_NODE_ID'] = ctx.node_id.encode('utf-8')
env['CLOUDIFY_BLUEPRINT_ID'] = ctx.blueprint_id.encode('utf-8')
env['CLOUDIFY_DEPLOYMENT_ID'] = ctx.deployment_id.encode('utf-8')
env['CLOUDIFY_MANAGER_IP'] = get_manager_ip().encode('utf-8')
env['CLOUDIFY_EXECUTION_ID'] = ctx.execution_id.encode('utf-8')
logging_script_path = os.path.join(dirname(resources.__file__),
"logging.sh")
file_server_script_path = os.path.join(dirname(resources.__file__),
"file_server.sh")
env['CLOUDIFY_LOGGING'] = logging_script_path
env['CLOUDIFY_FILE_SERVER'] = file_server_script_path
url = '{0}/{1}'.format(
utils.get_manager_file_server_blueprints_root_url(),
ctx.blueprint_id)
env['CLOUDIFY_FILE_SERVER_BLUEPRINT_ROOT'] = url.encode('utf-8')
# assuming properties are flat.
# inject each property as an environment variable.
for key, value in flatten(ctx.properties).iteritems():
env[key] = value.encode('utf-8') \
if isinstance(value, unicode) \
or isinstance(value, str) else repr(value)
return env
class ProcessException(Exception):
def __init__(self, command, exit_code, stdout, stderr):
Exception.__init__(self, stderr)
self.command = command
self.exit_code = exit_code
self.stdout = stdout
self.stderr = stderr