Ejemplo n.º 1
0
def main():
    """
    Azure Li/Vli config file lookup

    Lookup config file as provided by the Azure Li/VLi storage backend
    and make it locally available at the location described by
    Defaults.get_config_file_name()
    """
    Logger.setup()
    status = StatusReport('config_lookup')
    azure_config = Defaults.mount_config_source()

    try:
        azure_config_lookup_paths = [azure_config.location]
        azure_config_file = Path.which(
            azure_config.name, azure_config_lookup_paths
        )
        if not azure_config_file:
            raise AzureHostedConfigFileNotFoundException(
                'Config file not found at: {0}/{1}'.format(
                    azure_config.location, azure_config.name
                )
            )
        Path.create(
            os.path.dirname(Defaults.get_config_file_name())
        )
        Command.run(
            ['cp', azure_config_file, Defaults.get_config_file_name()]
        )
        os.chmod(Defaults.get_config_file_name(), 0o600)
        status.set_success()
    finally:
        Defaults.umount_config_source(azure_config)
def main():
    """
    Azure Li/Vli machine constraints

    Validation of machine requirements in the scope of an Azure Li/Vli instance
    """
    Logger.setup()
    status = StatusReport('machine_constraints')
    config = RuntimeConfig(Defaults.get_config_file())
    machine_constraints = config.get_machine_constraints()

    constraint_errors = []

    if machine_constraints:
        try:
            check_cpu_count_validates_constraint(machine_constraints)
        except Exception as issue:
            constraint_errors.append(issue)
        try:
            check_main_memory_validates_constraint(machine_constraints)
        except Exception as issue:
            constraint_errors.append(issue)

    if constraint_errors:
        raise AzureHostedException(constraint_errors)

    status.set_success()
 def setup(self, mock_Path_create, mock_exists):
     mock_exists.return_value = False
     with patch('builtins.open', create=True) as mock_open:
         mock_open.return_value = MagicMock(spec=io.IOBase)
         self.report = StatusReport('some_service')
         file_handle = mock_open.return_value.__enter__.return_value
         mock_open.assert_called_once_with(
             '/var/lib/azure_li_services/some_service.report.yaml', 'w')
         assert file_handle.write.call_args_list == [
             call('some_service'),
             call(':'),
             call('\n'),
             call('  '),
             call('reboot'),
             call(':'),
             call(' '),
             call('false'),
             call('\n'),
             call('  '),
             call('success'),
             call(':'),
             call(' '),
             call('false'),
             call('\n')
         ]
Ejemplo n.º 4
0
def main():
    """
    Azure Li/Vli storage mount setup

    Updates fstab with new storage mount entries and activates
    them in the scope of an Azure Li/Vli instance
    """
    Logger.setup()
    status = StatusReport('storage')
    config = RuntimeConfig(Defaults.get_config_file())
    storage_config = config.get_storage_config()

    storage_errors = []

    if storage_config:
        fstab_entries = []
        for storage in storage_config:
            try:
                if 'device' not in storage or 'mount' not in storage:
                    raise AzureHostedStorageMountException(
                        'At least one of {0} missing in {1}'.format(
                            ('device', 'mount'), storage))
                Path.create(storage['mount'])
                fstab_entries.append(
                    '{device} {mount} {fstype} {options} 0 0'.format(
                        device=storage['device'],
                        mount=storage['mount'],
                        fstype=storage.get('file_system') or 'auto',
                        options=','.join(
                            storage.get('mount_options', ['defaults']))))
            except Exception as issue:
                storage_errors.append(issue)

        if fstab_entries:
            with open('/etc/fstab', 'a') as fstab:
                fstab.write(os.linesep)
                for entry in fstab_entries:
                    fstab.write(entry)
                    fstab.write(os.linesep)

            Command.run(['mount', '-a'])

            for storage in storage_config:
                min_size = storage.get('min_size')
                if min_size:
                    try:
                        check_storage_size_validates_constraint(
                            min_size, storage['mount'])
                    except Exception as issue:
                        storage_errors.append(issue)

    if storage_errors:
        raise AzureHostedException(storage_errors)

    status.set_success()
Ejemplo n.º 5
0
def main():
    """
    Azure Li/Vli system setup

    Runs machine setup tasks in the scope of an Azure Li/Vli instance
    """
    Logger.setup()
    status = StatusReport('system_setup')
    config = RuntimeConfig(Defaults.get_config_file())
    hostname = config.get_hostname()
    stonith_config = config.get_stonith_config()

    system_setup_errors = []

    if hostname:
        try:
            set_hostname(hostname)
        except Exception as issue:
            system_setup_errors.append(issue)

    if stonith_config:
        try:
            set_stonith_service(stonith_config)
            enable_extra_kernel_modules()
        except Exception as issue:
            system_setup_errors.append(issue)

    try:
        set_kdump_service(config.get_crash_dump_config(), status)
    except Exception as issue:
        system_setup_errors.append(issue)

    try:
        set_kernel_samepage_merging_mode()
    except Exception as issue:
        system_setup_errors.append(issue)

    try:
        set_energy_performance_settings()
    except Exception as issue:
        system_setup_errors.append(issue)

    try:
        set_saptune_service()
    except Exception as issue:
        system_setup_errors.append(issue)

    if system_setup_errors:
        raise AzureHostedException(system_setup_errors)

    status.set_success()
Ejemplo n.º 6
0
def main():
    """
    Azure Li/Vli script call

    Calls a custom script in the scope of an Azure Li/Vli instance
    """
    Logger.setup()
    status = StatusReport('call')
    config = RuntimeConfig(Defaults.get_config_file())
    call_script = config.get_call_script()

    if call_script:
        call_source = Defaults.mount_config_source()
        Command.run([
            'bash', '-c', '{0}/{1}'.format(call_source.location, call_script)
        ])

    status.set_success()
Ejemplo n.º 7
0
def main():
    """
    Azure Li/Vli user setup

    Creates the configured user and its access setup for ssh
    and sudo services in the scope of an Azure Li/Vli instance
    """
    Logger.setup()
    status = StatusReport('user')
    config = RuntimeConfig(Defaults.get_config_file())

    user_config = config.get_user_config()

    if not user_config:
        raise AzureHostedUserConfigDataException(
            'credentials section missing in config file')

    user_setup_errors = []

    for user in user_config:
        try:
            create_or_modify_user(user)
        except Exception as issue:
            user_setup_errors.append(issue)
        else:
            try:
                setup_ssh_authorization(user)
            except Exception as issue:
                user_setup_errors.append(issue)
            try:
                setup_sudo_authorization(user)
            except Exception as issue:
                user_setup_errors.append(issue)
            try:
                setup_user_attributes(user)
            except Exception as issue:
                user_setup_errors.append(issue)

    setup_sudo_config()

    if user_setup_errors:
        raise AzureHostedException(user_setup_errors)

    status.set_success()
Ejemplo n.º 8
0
def main():
    """
    Azure Li/Vli package installation

    Creates a local rpm-md repository and registers it with zypper.
    Installs all packages configured in the scope of an Azure
    Li/Vli instance
    """
    Logger.setup()
    status = StatusReport('install')
    config = RuntimeConfig(Defaults.get_config_file())
    packages_config = config.get_packages_config()

    if packages_config:
        install_source = Defaults.mount_config_source()

        local_repos = {}
        local_repos.update(import_raw_sources(packages_config, install_source))
        local_repos.update(
            import_repository_sources(packages_config, install_source))
        for repository_name, repository_metadata in local_repos.items():
            repository_location = repository_metadata[0]
            Command.run(['zypper', 'removerepo', repository_name],
                        raise_on_error=False)
            Command.run([
                'zypper', 'addrepo', '--no-gpgcheck', repository_location,
                repository_name
            ])

        packages_to_install = []
        for repository_metadata in local_repos.values():
            packages_to_install += repository_metadata[1]
        if packages_to_install:
            Command.run([
                'zypper', '--non-interactive', 'install',
                '--auto-agree-with-licenses'
            ] + list(filter(None, packages_to_install)))

    status.set_success()
Ejemplo n.º 9
0
 def get_service_reports():
     from azure_li_services.status_report import StatusReport
     service_reports = []
     unit_to_service_map = {
         'config_lookup': 'azure-li-config-lookup',
         'user': '******',
         'install': 'azure-li-install',
         'network': 'azure-li-network',
         'call': 'azure-li-call',
         'machine_constraints': 'azure-li-machine-constraints',
         'system_setup': 'azure-li-system-setup',
         'storage': 'azure-li-storage'
     }
     unit_to_service_map_ordered = OrderedDict(
         sorted(unit_to_service_map.items()))
     for unit, service in unit_to_service_map_ordered.items():
         report = StatusReport(unit,
                               init_state=False,
                               systemd_service_name=service)
         report.load()
         service_reports.append(report)
     return service_reports
class TestStatusReport(object):
    @patch('os.path.exists')
    @patch('azure_li_services.status_report.Path.create')
    def setup(self, mock_Path_create, mock_exists):
        mock_exists.return_value = False
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            self.report = StatusReport('some_service')
            file_handle = mock_open.return_value.__enter__.return_value
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert file_handle.write.call_args_list == [
                call('some_service'),
                call(':'),
                call('\n'),
                call('  '),
                call('reboot'),
                call(':'),
                call(' '),
                call('false'),
                call('\n'),
                call('  '),
                call('success'),
                call(':'),
                call(' '),
                call('false'),
                call('\n')
            ]

    def test_set_success(self):
        with patch('builtins.open', create=True) as mock_open:
            self.report.set_success()
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert self.report.status['some_service']['success'] is True

    def test_set_failed(self):
        with patch('builtins.open', create=True) as mock_open:
            self.report.set_failed()
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert self.report.status['some_service']['success'] is False

    def test_set_reboot_required(self):
        with patch('builtins.open', create=True) as mock_open:
            self.report.set_reboot_required()
            mock_open.assert_called_once_with(
                '/var/lib/azure_li_services/some_service.report.yaml', 'w')
            assert self.report.status['some_service']['reboot'] is True

    def test_get_systemd_service(self):
        assert self.report.get_systemd_service() is None

    def test_load(self):
        self.report.status_file = '../data/some_service.report.yaml'
        self.report.load()
        assert self.report.get_state() is True
        assert self.report.get_reboot() is True