def gen_openstack_playbook(self): data = self.options data.pop('func') openstack_credential_args = ('auth_url', 'username', 'password', 'project_name') openstack_auth = {} for cred_arg in openstack_credential_args: openstack_auth.update({cred_arg: self.options['os_%s' % cred_arg]}) if 'os_domain_name' in self.options: openstack_auth.update({'domain_name': self.options['os_domain_name']}) if self.options['floating_ip']: ip_type = 'public' else: ip_type = 'private' # Define instance names cluster_name = 'k8s-' + get_cluster_name() if 'cluster_name' in self.options.keys(): cluster_name = 'k8s-' + self.options['cluster_name'] os_security_group_name = cluster_name + '-%s' % id_generator() self.pbook_content[0]['tasks'].append( {'name': 'Create security group', 'os_security_group': { 'auth': openstack_auth, 'name': os_security_group_name, 'description': 'Contains security rules for the Kubernetes cluster', 'region_name': self.options['os_region_name'], 'state': 'present'}} ) self.pbook_content[0]['tasks'].append( {'name': 'Add security rules', 'os_security_group_rule': { 'auth': openstack_auth, 'security_group': os_security_group_name, 'protocol': '{{item}}', 'region_name': self.options['os_region_name'], 'state': 'present'}, 'with_items': ['tcp', 'udp', 'icmp']} ) for role in ('masters', 'nodes', 'etcds'): os_instance_names = list() if '%s_count' % role in self.options.keys(): for x in range(self.options['%s_count' % role]): if self.options['add_node']: current_inventory = self.Cfg.read_inventory() cluster_name = '-'.join( current_inventory['all']['hosts'][0]['hostname'].split('-')[:-1] ) os_instance_names.append( cluster_name + '-%s' % id_generator() ) else: os_instance_names.append( cluster_name + '-%s' % id_generator() ) self.pbook_content[0]['tasks'].append( {'name': 'Create %s network ports' % role, 'os_port': { 'auth': openstack_auth, 'name': '{{item}}', 'region_name': self.options['os_region_name'], 'network': self.options['network'], 'allowed_address_pairs': [{'ip_address': self.options['kube_network']}], 'security_groups': [os_security_group_name], 'state': 'present'}, 'with_items': os_instance_names} ) self.pbook_content[0]['tasks'].append( {'name': 'Provision OS %s instances' % role, 'os_server': { 'auth': openstack_auth, 'name': '{{item}}', 'state': 'present', 'flavor': self.options['%s_flavor' % role], 'key_name': self.options['sshkey'], 'region_name': self.options['os_region_name'], 'auto_ip': self.options['floating_ip'], 'security_groups': [os_security_group_name], 'nics': 'port-name={{ item }}', 'image': self.options['image']}, 'register': 'os_%s' % role, 'with_items': os_instance_names} ) # Write os instances json self.pbook_content[0]['tasks'].append( {'name': 'Generate a file with OS %s instances list' % role, 'copy': {'dest': '%s' % self.instances[role]['file'], 'content': '{{os_%s.results}}' % role}} ) # Wait for ssh task self.pbook_content[0]['tasks'].append( {'name': 'Wait until SSH is available', 'wait_for': { 'host': '{{item.openstack.%s_v4}}' % ip_type, 'port': 22, 'search_regex': 'SSH', 'state': 'started', 'delay': 10}, 'with_items': '{{os_%s.results}}' % role} ) self.write_local_inventory() self.write_playbook()
def gen_gce_playbook(self): data = self.options data.pop('func') if 'tags' in self.options: self.options['tags'] = ','.join(self.options['tags']) # Options list of ansible GCE module gce_options = [ 'machine_type', 'image', 'zone', 'service_account_email', 'pem_file', 'credentials_file', 'project_id', 'tags', 'network', 'subnetwork' ] # Define instance names cluster_name = 'k8s-' + get_cluster_name() for role in ['masters', 'nodes', 'etcds']: gce_instance_names = list() if '%s_count' % role in self.options.keys(): for x in range(self.options['%s_count' % role]): if self.options['add_node']: current_inventory = self.Cfg.read_inventory() cluster_name = '-'.join( current_inventory['all']['hosts'][0]['hostname'].split('-')[:-2] ) gce_instance_names.append( cluster_name + '-%s' % id_generator() ) elif 'cluster_name' in self.options.keys(): gce_instance_names.append( self.options['cluster_name'] + '-%s' % id_generator() ) else: gce_instance_names.append( cluster_name + '-%s' % id_generator() ) gce_instance_names = ','.join(gce_instance_names) # Define GCE task gce_task = {'gce': {}, 'name': 'Provision GCE %s instances' % role, 'register': 'gce_%s' % role} for opt in gce_options: if opt in self.options.keys(): d = {opt: self.options[opt]} gce_task['gce'].update(d) gce_task['gce'].update({'machine_type': self.options['%s_machine_type' % role]}) gce_task['gce'].update({'instance_names': '%s' % gce_instance_names}) self.pbook_content[0]['tasks'].append(gce_task) # Write gce instances json self.pbook_content[0]['tasks'].append( {'name': 'Generate a file with %s list' % role, 'copy': {'dest': '%s' % self.instances['%s' % role]['file'], 'content': '{{gce_%s.instance_data}}' % role}} ) # Wait for ssh task if self.options['use_private_ip']: instance_ip = '{{ item.private_ip }}' else: instance_ip = '{{ item.public_ip }}' self.pbook_content[0]['tasks'].append( {'local_action': {'host': '%s' % instance_ip, 'module': 'wait_for', 'port': 22, 'state': 'started', 'timeout': 600}, 'name': 'Wait until SSH is available', 'with_items': '{{gce_%s.instance_data}}' % role} ) self.write_local_inventory() self.write_playbook()
def gen_openstack_playbook(self): data = self.options data.pop('func') openstack_credential_args = ('auth_url', 'username', 'password', 'project_name') openstack_auth = {} for cred_arg in openstack_credential_args: openstack_auth.update({cred_arg: self.options['os_%s' % cred_arg]}) if self.options['floating_ip']: ip_type = 'public' else: ip_type = 'private' # Define instance names cluster_name = 'k8s-' + get_cluster_name() os_security_group_name = cluster_name + '-%s' % id_generator() self.pbook_content[0]['tasks'].append({ 'name': 'Create security group', 'os_security_group': { 'auth': openstack_auth, 'name': os_security_group_name, 'description': 'Contains security rules for the Kubernetes cluster', 'region_name': self.options['os_region_name'], 'state': 'present' } }) self.pbook_content[0]['tasks'].append({ 'name': 'Add security rules', 'os_security_group_rule': { 'auth': openstack_auth, 'security_group': os_security_group_name, 'protocol': '{{item}}', 'region_name': self.options['os_region_name'], 'state': 'present' }, 'with_items': ['tcp', 'udp', 'icmp'] }) for role in ('masters', 'nodes', 'etcds'): os_instance_names = list() if '%s_count' % role in self.options.keys(): for x in range(self.options['%s_count' % role]): if self.options['add_node']: current_inventory = self.Cfg.read_inventory() cluster_name = '-'.join( current_inventory['all']['hosts'][0] ['hostname'].split('-')[:-1]) os_instance_names.append(cluster_name + '-%s' % id_generator()) elif 'cluster_name' in self.options.keys(): os_instance_names.append(self.options['cluster_name'] + '-%s' % id_generator()) os_security_group_name = self.options[ 'cluster_name'] + '-%s' % id_generator() else: os_instance_names.append(cluster_name + '-%s' % id_generator()) self.pbook_content[0]['tasks'].append({ 'name': 'Create %s network ports' % role, 'os_port': { 'auth': openstack_auth, 'name': '{{item}}', 'region_name': self.options['os_region_name'], 'network': self.options['network'], 'allowed_address_pairs': [{ 'ip_address': self.options['kube_network'] }], 'security_groups': (os_security_group_name, ), 'state': 'present' }, 'with_items': os_instance_names }) self.pbook_content[0]['tasks'].append({ 'name': 'Provision OS %s instances' % role, 'os_server': { 'auth': openstack_auth, 'name': '{{item}}', 'state': 'present', 'flavor': self.options['%s_flavor' % role], 'key_name': self.options['sshkey'], 'region_name': self.options['os_region_name'], 'auto_ip': self.options['floating_ip'], 'security_groups': (os_security_group_name, ), 'nics': 'port-name={{ item }}', 'image': self.options['image'] }, 'register': 'os_%s' % role, 'with_items': os_instance_names }) # Write os instances json self.pbook_content[0]['tasks'].append({ 'name': 'Generate a file with OS %s instances list' % role, 'copy': { 'dest': '%s' % self.instances[role]['file'], 'content': '{{os_%s.results}}' % role } }) # Wait for ssh task self.pbook_content[0]['tasks'].append({ 'name': 'Wait until SSH is available', 'wait_for': { 'host': '{{item.openstack.%s_v4}}' % ip_type, 'port': 22, 'search_regex': 'SSH', 'state': 'started', 'delay': 10 }, 'with_items': '{{os_%s.results}}' % role }) self.write_local_inventory() self.write_playbook()
def gen_gce_playbook(self): data = self.options data.pop('func') if 'tags' in self.options: self.options['tags'] = ','.join(self.options['tags']) # Options list of ansible GCE module gce_options = [ 'machine_type', 'image', 'zone', 'service_account_email', 'pem_file', 'credentials_file', 'project_id', 'tags' ] # Define instance names cluster_name = 'k8s-' + get_cluster_name() for role in ['masters', 'nodes', 'etcds']: gce_instance_names = list() if '%s_count' % role in self.options.keys(): for x in range(self.options['%s_count' % role]): if self.options['add_node']: current_inventory = self.Cfg.read_inventory() cluster_name = '-'.join( current_inventory['all']['hosts'][0] ['hostname'].split('-')[:-2]) gce_instance_names.append(cluster_name + '-%s' % id_generator()) elif 'cluster_name' in self.options.keys(): gce_instance_names.append( self.options['cluster_name'] + '-%s' % id_generator()) else: gce_instance_names.append(cluster_name + '-%s' % id_generator()) gce_instance_names = ','.join(gce_instance_names) # Define GCE task gce_task = { 'gce': {}, 'name': 'Provision GCE %s instances' % role, 'register': 'gce_%s' % role } for opt in gce_options: if opt in self.options.keys(): d = {opt: self.options[opt]} gce_task['gce'].update(d) gce_task['gce'].update( {'machine_type': self.options['%s_machine_type' % role]}) gce_task['gce'].update( {'instance_names': '%s' % gce_instance_names}) self.pbook_content[0]['tasks'].append(gce_task) # Write gce instances json self.pbook_content[0]['tasks'].append({ 'name': 'Generate a file with %s list' % role, 'copy': { 'dest': '%s' % self.instances['%s' % role]['file'], 'content': '{{gce_%s.instance_data}}' % role } }) # Wait for ssh task self.pbook_content[0]['tasks'].append({ 'local_action': { 'host': '{{ item.public_ip }}', 'module': 'wait_for', 'port': 22, 'state': 'started', 'timeout': 600 }, 'name': 'Wait until SSH is available', 'with_items': '{{gce_%s.instance_data}}' % role }) self.write_local_inventory() self.write_playbook()
def format_inventory(self, masters, nodes, etcds): new_inventory = { 'all': { 'hosts': [] }, 'kube-master': { 'hosts': [] }, 'etcd': { 'hosts': [] }, 'kube-node': { 'hosts': [] }, 'k8s-cluster:children': { 'hosts': [{ 'hostname': 'kube-node', 'hostvars': [] }, { 'hostname': 'kube-master', 'hostvars': [] }] }, } if self.platform == 'openstack': if self.options['floating_ip']: ip_type = 'public_v4' else: ip_type = 'private_v4' # handle masters new_instances = [] for master in masters: new_instances.append({ 'public_ip': master['openstack'][ip_type], 'name': master['item'] }) masters = new_instances # handle nodes new_instances = [] for node in nodes: new_instances.append({ 'public_ip': node['openstack'][ip_type], 'name': node['item'] }) nodes = new_instances # handle etcds new_instances = [] for etcd in etcds: new_instances.append({ 'public_ip': etcd['openstack'][ip_type], 'name': etcd['item'] }) etcds = new_instances if not self.options['add_node']: if not masters and len(nodes) == 1: masters = [nodes[0]] elif not masters: masters = nodes[0:2] if not etcds and len(nodes) >= 3: etcds = nodes[0:3] elif not etcds and len(nodes) < 3: etcds = [nodes[0]] elif etcds and len(etcds) < 3: etcds = [etcds[0]] if self.platform in ['aws', 'gce', 'openstack']: if self.options['add_node']: current_inventory = self.read_inventory() cluster_name = '-'.join(current_inventory['all']['hosts'][0] ['hostname'].split('-')[:-1]) new_inventory = current_inventory else: cluster_name = 'k8s-' + get_cluster_name() if self.options['use_private_ip']: instance_ip = 'private_ip' else: instance_ip = 'public_ip' for host in nodes + masters + etcds: if self.platform == 'aws': host['name'] = "%s-%s" % (cluster_name, id_generator(5)) new_inventory['all']['hosts'].append({ 'hostname': '%s' % host['name'], 'hostvars': [{ 'name': 'ansible_ssh_host', 'value': host[instance_ip] }] }) if not self.options['add_node']: for host in nodes: new_inventory['kube-node']['hosts'].append({ 'hostname': '%s' % host['name'], 'hostvars': [] }) for host in masters: new_inventory['kube-master']['hosts'].append({ 'hostname': '%s' % host['name'], 'hostvars': [] }) for host in etcds: new_inventory['etcd']['hosts'].append({ 'hostname': '%s' % host['name'], 'hostvars': [] }) elif self.platform == 'metal': for host in nodes + masters + etcds: if '[' in host: r = re.search('(^.*)\[(.*)\]', host) inventory_hostname = r.group(1) var_str = r.group(2) hostvars = list() for var in var_str.split(','): hostvars.append({ 'name': var.split('=')[0], 'value': var.split('=')[1] }) else: inventory_hostname = host hostvars = [] new_inventory['all']['hosts'].append({ 'hostname': inventory_hostname, 'hostvars': hostvars }) for host in nodes: new_inventory['kube-node']['hosts'].append({ 'hostname': host.split('[')[0], 'hostvars': [] }) for host in masters: new_inventory['kube-master']['hosts'].append({ 'hostname': host.split('[')[0], 'hostvars': [] }) for host in etcds: new_inventory['etcd']['hosts'].append({ 'hostname': host.split('[')[0], 'hostvars': [] }) return (new_inventory)
def gen_openstack_playbook(self): openstack_credential_args = ('auth_url', 'username', 'password', 'project_name') openstack_auth = {} for cred_arg in openstack_credential_args: openstack_auth.update({ cred_arg: os.environ.get( "OS_%s" % cred_arg.upper(), self.options.get('os_%s' % cred_arg), ) }) _diff = set(openstack_credential_args) - set(openstack_auth.keys()) if _diff: print("%s not found in the configuration or environment" % _diff) sys.exit(2) if 'os_domain_name' in self.options: openstack_auth.update( {'domain_name': self.options['os_domain_name']}) if self.options['floating_ip']: ip_type = 'public' else: ip_type = 'private' # Define instance names cluster_name = 'k8s-' + self.options['cluster_name'] os_security_group_name = cluster_name + '-%s' % id_generator() self.pbook_content[0]['tasks'].append({ 'name': 'Create security group', 'os_security_group': { 'auth': openstack_auth, 'name': os_security_group_name, 'description': ('Contains security rules for the Kubernetes' ' cluster'), 'region_name': os.environ.get("OS_REGION_NAME", self.options['os_region_name']), 'state': 'present', }, }) self.pbook_content[0]['tasks'].append({ 'name': 'Add security rules', 'os_security_group_rule': { 'auth': openstack_auth, 'security_group': os_security_group_name, 'protocol': '{{item}}', 'region_name': os.environ.get("OS_REGION_NAME", self.options['os_region_name']), 'state': 'present', }, 'with_items': ['tcp', 'udp', 'icmp'], }) for role in ('masters', 'nodes', 'etcds'): os_instance_names = list() if '%s_count' % role in list(self.options.keys()): for x in range(self.options['%s_count' % role]): if self.options['add_node']: current_inventory = self.Cfg.read_inventory() cluster_name = '-'.join( current_inventory['all']['hosts'][0] ['hostname'].split('-')[:-1]) os_instance_names.append(cluster_name + '-%s' % id_generator()) else: os_instance_names.append(cluster_name + '-%s' % id_generator()) self.pbook_content[0]['tasks'].append({ 'name': 'Create %s network ports' % role, 'os_port': { 'auth': openstack_auth, 'name': '{{item}}', 'region_name': os.environ.get( "OS_REGION_NAME", self.options['os_region_name'], ), 'network': self.options['network'], 'allowed_address_pairs': [{ 'ip_address': self.options['kube_network'] }], 'security_groups': [os_security_group_name], 'state': 'present', }, 'with_items': os_instance_names, }) host_zones = None if self.options.get("os_availability_zones"): # brain f**k warning # this divides the lists of hosts into zones # >>> hosts # >>> ['host1', 'host2', 'host3', 'host4', 'host5'] # >>> zones # >>> ['A', 'B'] # >>> list(zip([hosts[i:i + n] for i in range(0, len(hosts), n)], zones)) # noqa # >>> [(['host1', 'host2', 'host3'], 'A'), (['host4', 'host5'], 'B')] # noqa hosts, zones = os_instance_names, self.options[ 'os_availability_zones'] if len(zones) == len(hosts): host_zones = [{ "name": k, "zone": v } for k, v in zip(hosts, zones)] else: end = len(zones) + 1 if len(zones) % 2 else len(zones) host_zones = list( zip([ hosts[i:i + end] for i in range(0, len(hosts), end) ], zones)) host_zones = list(distribute_hosts(host_zones)) provision_item = { "name": 'Provision OS %s instances' % role, "register": 'os_%s' % role, } net_id = self.options.get('os_network_id') net_id = ",net-id=%s" % net_id if net_id else "" meta = "k8s-role=%s" % role if '%s_meta' % role in self.options: meta = meta + "," + self.options.get("%s_meta" % role) os_server_item = { 'auth': openstack_auth, 'name': '{{item.name}}', 'state': 'present', 'flavor': self.options['%s_flavor' % role], 'key_name': self.options['sshkey'], 'region_name': self.options['os_region_name'], 'auto_ip': self.options['floating_ip'], 'security_groups': [os_security_group_name], 'nics': 'port-name={{ item.name }}' + net_id, 'image': self.options['image'], 'boot_from_volume': self.options.get('%s_boot_from_volume' % role, True), 'volume_size': self.options['%s_volume_size' % role], 'meta': meta, 'userdata': self.options.get("userdata", "") } if host_zones: os_server_item["availability_zone"] = "{{item.zone}}" provision_item["os_server"] = os_server_item provision_item["loop"] = host_zones else: provision_item["loop"] = [{ "name": name } for name in os_instance_names] self.pbook_content[0]['tasks'].append(provision_item) # Write os instances json self.pbook_content[0]['tasks'].append({ 'name': 'Generate a file with OS %s instances list' % role, 'copy': { 'dest': '%s' % self.instances[role]['file'], 'content': '{{os_%s.results}}' % role, }, }) # Wait for ssh task self.pbook_content[0]['tasks'].append({ 'name': 'Wait until SSH is available', 'wait_for': { 'host': '{{item.openstack.%s_v4}}' % ip_type, 'port': 22, 'search_regex': 'SSH', 'state': 'started', 'delay': 10, }, 'with_items': '{{os_%s.results}}' % role, }) self.write_local_inventory() self.write_playbook()