earthmant | 5e1853a | 2017-08-04 09:02:48 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import subprocess |
| 4 | from cloudify import ctx |
| 5 | from cloudify.exceptions import OperationRetry |
| 6 | |
| 7 | |
| 8 | def check_command(command): |
| 9 | |
| 10 | try: |
| 11 | process = subprocess.Popen( |
| 12 | command.split() |
| 13 | ) |
| 14 | except OSError: |
| 15 | return False |
| 16 | |
| 17 | output, error = process.communicate() |
| 18 | |
| 19 | ctx.logger.debug('command: {0} '.format(command)) |
| 20 | ctx.logger.debug('output: {0} '.format(output)) |
| 21 | ctx.logger.debug('error: {0} '.format(error)) |
| 22 | ctx.logger.debug('process.returncode: {0} '.format(process.returncode)) |
| 23 | |
| 24 | if process.returncode: |
| 25 | ctx.logger.error('Running `{0}` returns error.'.format(command)) |
| 26 | return False |
| 27 | |
| 28 | return True |
| 29 | |
| 30 | |
| 31 | def execute_command(_command): |
| 32 | |
| 33 | ctx.logger.debug('_command {0}.'.format(_command)) |
| 34 | |
| 35 | subprocess_args = { |
| 36 | 'args': _command.split(), |
| 37 | 'stdout': subprocess.PIPE, |
| 38 | 'stderr': subprocess.PIPE |
| 39 | } |
| 40 | |
| 41 | ctx.logger.debug('subprocess_args {0}.'.format(subprocess_args)) |
| 42 | |
| 43 | process = subprocess.Popen(**subprocess_args) |
| 44 | output, error = process.communicate() |
| 45 | |
| 46 | ctx.logger.debug('command: {0} '.format(_command)) |
| 47 | ctx.logger.debug('output: {0} '.format(output)) |
| 48 | ctx.logger.debug('error: {0} '.format(error)) |
| 49 | ctx.logger.debug('process.returncode: {0} '.format(process.returncode)) |
| 50 | |
| 51 | if process.returncode: |
| 52 | ctx.logger.error('Running `{0}` returns error.'.format(_command)) |
| 53 | return False |
| 54 | |
| 55 | return output |
| 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | |
| 60 | docker_command = 'docker ps' |
| 61 | |
| 62 | if not check_command(docker_command): |
| 63 | raise OperationRetry('Waiting for docker to be installed.') |
| 64 | |
| 65 | finished = False |
| 66 | ps = execute_command('ps -ef') |
| 67 | for line in ps.split('\n'): |
| 68 | if '/usr/bin/python /usr/bin/cloud-init modules' in line: |
| 69 | ctx.logger.error('in line') |
| 70 | raise OperationRetry('Waiting for Cloud Init to finish.') |
| 71 | |
| 72 | ctx.logger.info('Docker is ready and Cloud Init finished.') |