Jun Hu | 9e45abc | 2018-01-17 17:07:36 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # ============LICENSE_START========================================== |
| 4 | # =================================================================== |
Jun (Nicolas) Hu | 8cbcb30 | 2018-02-21 14:18:44 -0500 | [diff] [blame] | 5 | # Copyright (c) 2017 AT&T |
Jun Hu | 9e45abc | 2018-01-17 17:07:36 -0500 | [diff] [blame] | 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 | # Afther K8s master up and running. This script will be triggered in each worker nodes. It will join the nodes, and mount the NFS directory. |
| 21 | |
| 22 | import subprocess |
| 23 | from cloudify import ctx |
| 24 | from cloudify.exceptions import NonRecoverableError |
| 25 | |
| 26 | START_COMMAND = 'sudo kubeadm join --token {0} {1}:{2}' |
| 27 | |
| 28 | |
| 29 | def execute_command(_command): |
| 30 | |
| 31 | ctx.logger.debug('_command {0}.'.format(_command)) |
| 32 | |
| 33 | subprocess_args = { |
| 34 | 'args': _command.split(), |
| 35 | 'stdout': subprocess.PIPE, |
| 36 | 'stderr': subprocess.PIPE |
| 37 | } |
| 38 | |
| 39 | ctx.logger.debug('subprocess_args {0}.'.format(subprocess_args)) |
| 40 | |
| 41 | process = subprocess.Popen(**subprocess_args) |
| 42 | output, error = process.communicate() |
| 43 | |
| 44 | ctx.logger.debug('command: {0} '.format(_command)) |
| 45 | ctx.logger.debug('output: {0} '.format(output)) |
| 46 | ctx.logger.debug('error: {0} '.format(error)) |
| 47 | ctx.logger.debug('process.returncode: {0} '.format(process.returncode)) |
| 48 | |
| 49 | if process.returncode: |
| 50 | ctx.logger.error('Running `{0}` returns error.'.format(_command)) |
| 51 | return False |
| 52 | |
| 53 | return output |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | |
| 58 | hostname = execute_command('hostname') |
| 59 | ctx.instance.runtime_properties['hostname'] = hostname.rstrip('\n') |
| 60 | |
| 61 | # Get the master cluster info. |
| 62 | masters = \ |
| 63 | [x for x in ctx.instance.relationships if |
| 64 | x.target.instance.runtime_properties.get( |
| 65 | 'KUBERNETES_MASTER', False)] |
| 66 | if len(masters) != 1: |
| 67 | raise NonRecoverableError( |
| 68 | 'Currently, a Kubernetes node must have a ' |
| 69 | 'dependency on one Kubernetes master.') |
| 70 | master = masters[0] |
| 71 | bootstrap_token = \ |
| 72 | master.target.instance.runtime_properties['bootstrap_token'] |
| 73 | master_ip = \ |
| 74 | master.target.instance.runtime_properties['master_ip'] |
| 75 | master_port = \ |
| 76 | master.target.instance.runtime_properties['master_port'] |
| 77 | |
| 78 | # Join the cluster. |
| 79 | cniCommand1=subprocess.Popen(["sudo", "sysctl", 'net.bridge.bridge-nf-call-iptables=1'], stdout=subprocess.PIPE) |
| 80 | join_command = \ |
| 81 | 'sudo kubeadm join --token {0} {1}:{2}'.format( |
| 82 | bootstrap_token, master_ip, master_port) |
| 83 | execute_command(join_command) |
| 84 | |
| 85 | #mount |
| 86 | mount_command=\ |
| 87 | 'sudo mount -t nfs -o proto=tcp,port=2049 {0}:/dockerdata-nfs /dockerdata-nfs'.format(master_ip) |
| 88 | execute_command(mount_command) |