3 # ============LICENSE_START==========================================
4 # ===================================================================
5 # Copyright (c) 2017 AT&T
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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============================================
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.
23 from cloudify import ctx
24 from cloudify.exceptions import NonRecoverableError
26 START_COMMAND = 'sudo kubeadm join --token {0} {1}:{2}'
29 def execute_command(_command):
31 ctx.logger.debug('_command {0}.'.format(_command))
34 'args': _command.split(),
35 'stdout': subprocess.PIPE,
36 'stderr': subprocess.PIPE
39 ctx.logger.debug('subprocess_args {0}.'.format(subprocess_args))
41 process = subprocess.Popen(**subprocess_args)
42 output, error = process.communicate()
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))
49 if process.returncode:
50 ctx.logger.error('Running `{0}` returns error.'.format(_command))
56 if __name__ == '__main__':
58 hostname = execute_command('hostname')
59 ctx.instance.runtime_properties['hostname'] = hostname.rstrip('\n')
61 # Get the master cluster info.
63 [x for x in ctx.instance.relationships if
64 x.target.instance.runtime_properties.get(
65 'KUBERNETES_MASTER', False)]
67 raise NonRecoverableError(
68 'Currently, a Kubernetes node must have a '
69 'dependency on one Kubernetes master.')
72 master.target.instance.runtime_properties['bootstrap_token']
74 master.target.instance.runtime_properties['master_ip']
76 master.target.instance.runtime_properties['master_port']
79 cniCommand1=subprocess.Popen(["sudo", "sysctl", 'net.bridge.bridge-nf-call-iptables=1'], stdout=subprocess.PIPE)
81 'sudo kubeadm join --token {0} {1}:{2}'.format(
82 bootstrap_token, master_ip, master_port)
83 execute_command(join_command)
87 'sudo mount -t nfs -o proto=tcp,port=2049 {0}:/dockerdata-nfs /dockerdata-nfs'.format(master_ip)
88 execute_command(mount_command)