diff options
Diffstat (limited to 'snaps/provisioning')
11 files changed, 128 insertions, 138 deletions
diff --git a/snaps/provisioning/ansible_pb/__init__.py b/snaps/provisioning/ansible_pb/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/snaps/provisioning/ansible_pb/__init__.py +++ /dev/null diff --git a/snaps/provisioning/ansible_pb/centos-network-setup/__init__.py b/snaps/provisioning/ansible_pb/centos-network-setup/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/snaps/provisioning/ansible_pb/centos-network-setup/__init__.py +++ /dev/null diff --git a/snaps/provisioning/ansible_pb/centos-network-setup/playbooks/__init__.py b/snaps/provisioning/ansible_pb/centos-network-setup/playbooks/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/snaps/provisioning/ansible_pb/centos-network-setup/playbooks/__init__.py +++ /dev/null diff --git a/snaps/provisioning/ansible_pb/centos-network-setup/playbooks/configure_host.yml b/snaps/provisioning/ansible_pb/centos-network-setup/playbooks/configure_host.yml deleted file mode 100644 index 8df03cb..0000000 --- a/snaps/provisioning/ansible_pb/centos-network-setup/playbooks/configure_host.yml +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs") -# and others. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at: -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. ---- -- name: Configure NIC - hosts: all - become: yes - become_method: sudo - become_user: root - - tasks: - - name: Setup /etc/sysconfig/network-scripts/ifcfg-eth1 file - action: template owner=root group=root mode=644 src=../templates/ifcfg-interface dest=/etc/sysconfig/network-scripts/ifcfg-{{nic_name}} - - name : Restart Network - command: systemctl restart network
\ No newline at end of file diff --git a/snaps/provisioning/ansible_pb/centos-network-setup/templates/ifcfg-interface b/snaps/provisioning/ansible_pb/centos-network-setup/templates/ifcfg-interface deleted file mode 100644 index 47aa3fa..0000000 --- a/snaps/provisioning/ansible_pb/centos-network-setup/templates/ifcfg-interface +++ /dev/null @@ -1,14 +0,0 @@ -DEVICE={{ nic_name }} -NAME={{ nic_name }} -IPADDR={{ nic_ip }} - -DEFROUTE=no -NETMASK=255.255.255.0 -NM_CONTROLLED=no -IPV6INIT=yes -IPV6_AUTOCONF=yes -IPV6_DEFROUTE=yes -IPV6_PEERDNS=yes -IPV6_PEERROUTES=yes -IPV6_FAILURE_FATAL=no -ONBOOT=yes
\ No newline at end of file diff --git a/snaps/provisioning/ansible_pb/ubuntu-network-setup/__init__.py b/snaps/provisioning/ansible_pb/ubuntu-network-setup/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/snaps/provisioning/ansible_pb/ubuntu-network-setup/__init__.py +++ /dev/null diff --git a/snaps/provisioning/ansible_pb/ubuntu-network-setup/playbooks/__init__.py b/snaps/provisioning/ansible_pb/ubuntu-network-setup/playbooks/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/snaps/provisioning/ansible_pb/ubuntu-network-setup/playbooks/__init__.py +++ /dev/null diff --git a/snaps/provisioning/ansible_pb/ubuntu-network-setup/playbooks/configure_host.yml b/snaps/provisioning/ansible_pb/ubuntu-network-setup/playbooks/configure_host.yml deleted file mode 100644 index 5d43f96..0000000 --- a/snaps/provisioning/ansible_pb/ubuntu-network-setup/playbooks/configure_host.yml +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs") -# and others. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at: -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. ---- -- name: Configure NIC - hosts: all - become: yes - become_method: sudo - become_user: root - - tasks: - - name: Setup /etc/network/interfaces.d/{{nic_name}}.cfg file - action: template owner=root group=root mode=644 src=../templates/ethN.cfg dest=/etc/network/interfaces.d/{{nic_name}}.cfg - - name : Restart Network - command: service networking restart
\ No newline at end of file diff --git a/snaps/provisioning/ansible_pb/ubuntu-network-setup/templates/ethN.cfg b/snaps/provisioning/ansible_pb/ubuntu-network-setup/templates/ethN.cfg deleted file mode 100644 index 3fa7708..0000000 --- a/snaps/provisioning/ansible_pb/ubuntu-network-setup/templates/ethN.cfg +++ /dev/null @@ -1,2 +0,0 @@ -auto {{ nic_name }} -iface {{ nic_name }} inet dhcp diff --git a/snaps/provisioning/ansible_utils.py b/snaps/provisioning/ansible_utils.py index 63f26e1..019a8e7 100644 --- a/snaps/provisioning/ansible_utils.py +++ b/snaps/provisioning/ansible_utils.py @@ -21,52 +21,80 @@ import paramiko try: from ansible.parsing.dataloader import DataLoader - from ansible.vars import VariableManager - from ansible.inventory import Inventory + from ansible.vars.manager import VariableManager + from ansible.inventory.manager import InventoryManager from ansible.executor.playbook_executor import PlaybookExecutor except: pass __author__ = 'spisarski' +from warnings import warn +warn('This utility will be removed in a subsequent release', + DeprecationWarning) + logger = logging.getLogger('ansible_utils') -def apply_playbook(playbook_path, hosts_inv, host_user, ssh_priv_key_file_path, - variables=None, proxy_setting=None): +def apply_playbook(playbook_path, hosts_inv=None, host_user=None, + ssh_priv_key_file_path=None, password=None, variables=None, + proxy_setting=None): """ Executes an Ansible playbook to the given host :param playbook_path: the (relative) path to the Ansible playbook :param hosts_inv: a list of hostnames/ip addresses to which to apply the - Ansible playbook + Ansible playbook (not required when PB is configured for + localhost) :param host_user: A user for the host instances (must be a password-less - sudo user if playbook has "sudo: yes" - :param ssh_priv_key_file_path: the file location of the ssh key + sudo user if playbook has "sudo: yes") (not required when + PB is configured for localhost) + :param ssh_priv_key_file_path: the file location of the ssh key. Required + if password is None (not required when PB is + configured for localhost) + :param password: the file location of the ssh key. Required if + ssh_priv_key_file_path is None (not required when PB is + configured for localhost) :param variables: a dictionary containing any substitution variables needed by the Jinga 2 templates :param proxy_setting: instance of os_credentials.ProxySettings class - :return: the results + :raises AnsibleException when the return code from the Ansible library is + not 0 + :return: the return code from the Ansible library only when 0. + Implementation now raises an exception otherwise """ if not os.path.isfile(playbook_path): - raise AnsibleException('Requested playbook not found - ' + playbook_path) + raise AnsibleException( + 'Requested playbook not found - ' + playbook_path) + + pk_file_path = None + if ssh_priv_key_file_path: + pk_file_path = os.path.expanduser(ssh_priv_key_file_path) + if not password: + if not os.path.isfile(pk_file_path): + raise AnsibleException( + 'Requested private SSH key not found - ' + pk_file_path) - pk_file_path = os.path.expanduser(ssh_priv_key_file_path) - if not os.path.isfile(pk_file_path): - raise AnsibleException('Requested private SSH key not found - ' + - pk_file_path) + passwords = None + if password: + passwords = {'conn_pass': password, 'become_pass': password} import ansible.constants ansible.constants.HOST_KEY_CHECKING = False - variable_manager = VariableManager() + loader = DataLoader() + inventory = InventoryManager(loader=loader) + if hosts_inv: + for host in hosts_inv: + inventory.add_host(host=host, group='ungrouped') + connection = 'ssh' + else: + connection = 'local' + + variable_manager = VariableManager(loader=loader, inventory=inventory) + if variables: variable_manager.extra_vars = variables - loader = DataLoader() - inventory = Inventory(loader=loader, variable_manager=variable_manager, - host_list=hosts_inv) - variable_manager.set_inventory(inventory) - ssh_extra_args = None if proxy_setting and proxy_setting.ssh_proxy_cmd: ssh_extra_args = '-o ProxyCommand=\'%s\'' % proxy_setting.ssh_proxy_cmd @@ -76,14 +104,15 @@ def apply_playbook(playbook_path, hosts_inv, host_user, ssh_priv_key_file_path, 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'become', 'become_method', 'become_user', 'verbosity', - 'check', 'timeout']) + 'check', 'timeout', 'diff']) ansible_opts = options( listtags=False, listtasks=False, listhosts=False, syntax=False, - connection='ssh', module_path=None, forks=100, remote_user=host_user, - private_key_file=pk_file_path, ssh_common_args=None, - ssh_extra_args=ssh_extra_args, become=None, become_method=None, - become_user=None, verbosity=11111, check=False, timeout=30) + connection=connection, module_path=None, forks=100, + remote_user=host_user, private_key_file=pk_file_path, + ssh_common_args=None, ssh_extra_args=ssh_extra_args, become=None, + become_method=None, become_user=None, verbosity=11111, check=False, + timeout=30, diff=None) logger.debug('Setting up Ansible Playbook Executor for playbook - ' + playbook_path) @@ -93,18 +122,28 @@ def apply_playbook(playbook_path, hosts_inv, host_user, ssh_priv_key_file_path, variable_manager=variable_manager, loader=loader, options=ansible_opts, - passwords=None) + passwords=passwords) logger.debug('Executing Ansible Playbook - ' + playbook_path) - return executor.run() + ret_val = executor.run() + + if ret_val != 0: + raise AnsibleException( + 'Error applying playbook [{}] with value [{}] using the connection' + ' type of [{}]'.format( + playbook_path, ret_val, connection)) + return ret_val -def ssh_client(ip, user, private_key_filepath, proxy_settings=None): + +def ssh_client(ip, user, private_key_filepath=None, password=None, + proxy_settings=None): """ Retrieves and attemts an SSH connection :param ip: the IP of the host to connect :param user: the user with which to connect - :param private_key_filepath: the path to the private key file + :param private_key_filepath: when None, password is required + :param password: when None, private_key_filepath is required :param proxy_settings: instance of os_credentials.ProxySettings class (optional) :return: the SSH client if can connect else false @@ -120,12 +159,17 @@ def ssh_client(ip, user, private_key_filepath, proxy_settings=None): proxy_cmd_str = proxy_cmd_str.replace("%p", '22') proxy_cmd = paramiko.ProxyCommand(proxy_cmd_str) - pk_abs_path = os.path.expanduser(private_key_filepath) - ssh.connect(ip, username=user, key_filename=pk_abs_path, - sock=proxy_cmd) + pk_abs_path = None + if not password and private_key_filepath: + pk_abs_path = os.path.expanduser(private_key_filepath) + + ssh.connect( + ip, username=user, key_filename=pk_abs_path, password=password, + sock=proxy_cmd) + logger.info('Obtained SSH connection to %s', ip) return ssh except Exception as e: - logger.warning('Unable to connect via SSH with message - ' + str(e)) + logger.debug('Unable to connect via SSH with message - ' + str(e)) class AnsibleException(Exception): diff --git a/snaps/provisioning/tests/ansible_utils_tests.py b/snaps/provisioning/tests/ansible_utils_tests.py index da056b2..b6ace31 100644 --- a/snaps/provisioning/tests/ansible_utils_tests.py +++ b/snaps/provisioning/tests/ansible_utils_tests.py @@ -18,15 +18,20 @@ import uuid import os import pkg_resources from scp import SCPClient + +from snaps.config.keypair import KeypairConfig +from snaps.config.network import PortConfig +from snaps.config.security_group import ( + Direction, Protocol, SecurityGroupConfig, SecurityGroupRuleConfig) +from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig + from snaps.openstack import create_flavor from snaps.openstack import create_image from snaps.openstack import create_instance from snaps.openstack import create_keypairs from snaps.openstack import create_network from snaps.openstack import create_router -from snaps.openstack.create_security_group import ( - SecurityGroupRuleSettings, Direction, Protocol, OpenStackSecurityGroup, - SecurityGroupSettings) +from snaps.openstack.create_security_group import OpenStackSecurityGroup from snaps.openstack.tests import openstack_tests from snaps.openstack.tests.create_instance_tests import check_dhcp_lease from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase @@ -52,7 +57,7 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): """ super(self.__class__, self).__start__() - self.nova = nova_utils.nova_client(self.os_creds) + self.nova = nova_utils.nova_client(self.os_creds, self.os_session) guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.keypair_priv_filepath = 'tmp/' + guid @@ -78,13 +83,15 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): os_image_settings = openstack_tests.ubuntu_image_settings( name=guid + '-' + '-image', image_metadata=self.image_metadata) - self.image_creator = create_image.OpenStackImage(self.os_creds, - os_image_settings) + self.image_creator = create_image.OpenStackImage( + self.os_creds, os_image_settings) self.image_creator.create() # First network is public self.pub_net_config = openstack_tests.get_pub_net_config( - net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet', + project_name=self.os_creds.project_name, + net_name=guid + '-pub-net', + mtu=1442, subnet_name=guid + '-pub-subnet', router_name=guid + '-pub-router', external_net=self.ext_net_name) @@ -98,16 +105,17 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): self.router_creator.create() # Create Flavor + flavor_config = openstack_tests.get_flavor_config( + name=guid + '-flavor-name', ram=2048, disk=10, + vcpus=2, metadata=self.flavor_metadata) + self.flavor_creator = create_flavor.OpenStackFlavor( - self.admin_os_creds, - create_flavor.FlavorSettings(name=guid + '-flavor-name', - ram=2048, disk=10, vcpus=2, - metadata=self.flavor_metadata)) + self.admin_os_creds, flavor_config) self.flavor_creator.create() # Create Key/Pair self.keypair_creator = create_keypairs.OpenStackKeypair( - self.os_creds, create_keypairs.KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) @@ -115,32 +123,30 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): # Create Security Group sec_grp_name = guid + '-sec-grp' - rule1 = SecurityGroupRuleSettings(sec_grp_name=sec_grp_name, - direction=Direction.ingress, - protocol=Protocol.icmp) - rule2 = SecurityGroupRuleSettings(sec_grp_name=sec_grp_name, - direction=Direction.ingress, - protocol=Protocol.tcp, - port_range_min=22, - port_range_max=22) + rule1 = SecurityGroupRuleConfig( + sec_grp_name=sec_grp_name, direction=Direction.ingress, + protocol=Protocol.icmp) + rule2 = SecurityGroupRuleConfig( + sec_grp_name=sec_grp_name, direction=Direction.ingress, + protocol=Protocol.tcp, port_range_min=22, port_range_max=22) self.sec_grp_creator = OpenStackSecurityGroup( self.os_creds, - SecurityGroupSettings(name=sec_grp_name, - rule_settings=[rule1, rule2])) + SecurityGroupConfig( + name=sec_grp_name, rule_settings=[rule1, rule2])) self.sec_grp_creator.create() # Create instance ports_settings = list() ports_settings.append( - create_network.PortSettings( + PortConfig( name=self.port_1_name, network_name=self.pub_net_config.network_settings.name)) - instance_settings = create_instance.VmInstanceSettings( + instance_settings = VmInstanceConfig( name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=ports_settings, - floating_ip_settings=[create_instance.FloatingIpSettings( + floating_ip_settings=[FloatingIpConfig( name=self.floating_ip_name, port_name=self.port_1_name, router_name=self.pub_net_config.router_settings.name)]) @@ -237,6 +243,9 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): # Block until VM's ssh port has been opened self.assertTrue(self.inst_creator.vm_ssh_active(block=True)) + # Block until cloud-init has completed + self.assertTrue(self.inst_creator.cloud_init_complete(block=True)) + ssh_client = self.inst_creator.ssh_client() self.assertIsNotNone(ssh_client) @@ -256,18 +265,19 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): relative_pb_path = pkg_resources.resource_filename( 'snaps.provisioning.tests.playbooks', 'simple_playbook.yml') - retval = self.inst_creator.apply_ansible_playbook(relative_pb_path) - self.assertEqual(0, retval) + self.inst_creator.apply_ansible_playbook(relative_pb_path) - ssh = ansible_utils.ssh_client(ip, user, priv_key, - self.os_creds.proxy_settings) + ssh = ansible_utils.ssh_client( + ip, user, private_key_filepath=priv_key, + proxy_settings=self.os_creds.proxy_settings) self.assertIsNotNone(ssh) - + scp = None try: scp = SCPClient(ssh.get_transport()) scp.get('~/hello.txt', self.test_file_local_path) finally: - scp.close() + if scp: + scp.close() ssh.close() self.assertTrue(os.path.isfile(self.test_file_local_path)) @@ -305,6 +315,9 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): # Block until VM's ssh port has been opened self.assertTrue(self.inst_creator.vm_ssh_active(block=True)) + # Block until cloud-init has completed + self.assertTrue(self.inst_creator.cloud_init_complete(block=True)) + # Apply Security Group self.inst_creator.add_security_group( self.sec_grp_creator.get_security_group()) @@ -318,20 +331,21 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): relative_pb_path = pkg_resources.resource_filename( 'snaps.provisioning.tests.playbooks', 'template_playbook.yml') - retval = self.inst_creator.apply_ansible_playbook(relative_pb_path, - variables={ - 'name': 'Foo'}) - self.assertEqual(0, retval) + self.inst_creator.apply_ansible_playbook( + relative_pb_path, variables={'name': 'Foo'}) - ssh = ansible_utils.ssh_client(ip, user, priv_key, - self.os_creds.proxy_settings) + ssh = ansible_utils.ssh_client( + ip, user, private_key_filepath=priv_key, + proxy_settings=self.os_creds.proxy_settings) self.assertIsNotNone(ssh) + scp = None try: scp = SCPClient(ssh.get_transport()) scp.get('/tmp/hello.txt', self.test_file_local_path) finally: - scp.close() + if scp: + scp.close() ssh.close() self.assertTrue(os.path.isfile(self.test_file_local_path)) |