Jun Hu | 9e45abc | 2018-01-17 17:07:36 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # ============LICENSE_START========================================== |
| 4 | # =================================================================== |
| 5 | # Copyright © 2017 AT&T |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | #============LICENSE_END============================================ |
| 19 | |
| 20 | # This tack will be triggered after VM created. It will check whether docker is up and running. |
| 21 | |
| 22 | import subprocess |
| 23 | from cloudify import ctx |
| 24 | from cloudify.exceptions import OperationRetry |
| 25 | |
| 26 | |
| 27 | def check_command(command): |
| 28 | |
| 29 | try: |
| 30 | process = subprocess.Popen( |
| 31 | command.split() |
| 32 | ) |
| 33 | except OSError: |
| 34 | return False |
| 35 | |
| 36 | output, error = process.communicate() |
| 37 | |
| 38 | ctx.logger.debug('command: {0} '.format(command)) |
| 39 | ctx.logger.debug('output: {0} '.format(output)) |
| 40 | ctx.logger.debug('error: {0} '.format(error)) |
| 41 | ctx.logger.debug('process.returncode: {0} '.format(process.returncode)) |
| 42 | |
| 43 | if process.returncode: |
| 44 | ctx.logger.error('Running `{0}` returns error.'.format(command)) |
| 45 | return False |
| 46 | |
| 47 | return True |
| 48 | |
| 49 | |
| 50 | def execute_command(_command): |
| 51 | |
| 52 | ctx.logger.debug('_command {0}.'.format(_command)) |
| 53 | |
| 54 | subprocess_args = { |
| 55 | 'args': _command.split(), |
| 56 | 'stdout': subprocess.PIPE, |
| 57 | 'stderr': subprocess.PIPE |
| 58 | } |
| 59 | |
| 60 | ctx.logger.debug('subprocess_args {0}.'.format(subprocess_args)) |
| 61 | |
| 62 | process = subprocess.Popen(**subprocess_args) |
| 63 | output, error = process.communicate() |
| 64 | |
| 65 | ctx.logger.debug('command: {0} '.format(_command)) |
| 66 | ctx.logger.debug('error: {0} '.format(error)) |
| 67 | ctx.logger.debug('process.returncode: {0} '.format(process.returncode)) |
| 68 | |
| 69 | if process.returncode: |
| 70 | ctx.logger.error('Running `{0}` returns error.'.format(_command)) |
| 71 | return False |
| 72 | |
| 73 | return output |
| 74 | |
| 75 | |
| 76 | if __name__ == '__main__': |
| 77 | |
| 78 | # Check if Docker PS works |
| 79 | docker = check_command('docker ps') |
| 80 | if not docker: |
| 81 | raise OperationRetry( |
| 82 | 'Docker is not present on the system.') |
| 83 | ctx.logger.info('Docker is present on the system.') |
| 84 | |
| 85 | # Next check if Cloud Init is running. |
| 86 | finished = False |
| 87 | ps = execute_command('ps -ef') |
| 88 | for line in ps.split('\n'): |
| 89 | if '/usr/bin/python /usr/bin/cloud-init modules' in line: |
| 90 | raise OperationRetry( |
| 91 | 'You provided a Cloud-init Cloud Config to configure instances. ' |
| 92 | 'Waiting for Cloud-init to complete.') |
| 93 | ctx.logger.info('Cloud-init finished.') |