dfilppi | 9981f55 | 2017-08-07 20:10:53 +0000 | [diff] [blame] | 1 | ######### |
| 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 | |
| 16 | import requests |
| 17 | |
| 18 | from cloudify import compute |
| 19 | from cloudify import exceptions |
| 20 | from cloudify import ctx |
| 21 | |
| 22 | |
| 23 | def 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 | |
| 48 | userdata_handlers = { |
| 49 | 'http': lambda params: requests.get(params['url']).text |
| 50 | } |