blob: ba63bb532806c9bc4d495f785160eae1cd7ed272 [file] [log] [blame]
dfilppi9981f552017-08-07 20:10:53 +00001#########
2# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# * See the License for the specific language governing permissions and
14# * limitations under the License.
15
16import requests
17
18from cloudify import compute
19from cloudify import exceptions
20from cloudify import ctx
21
22
23def handle_userdata(server):
24
25 existing_userdata = server.get('userdata')
26 install_agent_userdata = ctx.agent.init_script()
27
28 if not (existing_userdata or install_agent_userdata):
29 return
30
31 if isinstance(existing_userdata, dict):
32 ud_type = existing_userdata['type']
33 if ud_type not in userdata_handlers:
34 raise exceptions.NonRecoverableError(
35 "Invalid type '{0}' for server userdata)".format(ud_type))
36 existing_userdata = userdata_handlers[ud_type](existing_userdata)
37
38 if not existing_userdata:
39 final_userdata = install_agent_userdata
40 elif not install_agent_userdata:
41 final_userdata = existing_userdata
42 else:
43 final_userdata = compute.create_multi_mimetype_userdata(
44 [existing_userdata, install_agent_userdata])
45 server['userdata'] = final_userdata
46
47
48userdata_handlers = {
49 'http': lambda params: requests.get(params['url']).text
50}