def disable(service, runlevels='345'): if has_binary('update-rc.d', runner=sudo): return sudo('update-rc.d disable %s %s' % (service, runlevels)) elif has_binary('chkconfig', runner=sudo): return sudo('chkconfig %s off' % service) else: raise NotImplementedError('update-rc.d nor chkconfig not found!')
def add_repo(url, name=None, source_packages=False, remove=False): repo_type = 'deb' if not source_packages else 'deb-src' sources_file = '/etc/apt/sources.list' if not name: sources_file = '/etc/apt/sources.list.d/%s.list' % name if ' ' not in url and has_binary('which add-apt-repository', runner=sudo): add_args = ' '.join(['-s' if source_packages else '', '-r' if remove else '']) sudo('%s add-apt-repository -y %s "%s"' % (APT_ENV_VARS, add_args, url)) elif not remove: sources_line = '%s %s' % (repo_type, url) files.append(sources_file, sources_line) if remove: sources_line = '%s %s' % (repo_type, url) with settings(warn_only=True): grep_result = sudo("grep -rn '%s' " "/etc/apt/sources.list " "/etc/apt/sources.list.d/*.list" % sources_line) if grep_result.return_code == 0: for grep_output_line in grep_result.split('\n'): grep_params = grep_output_line.split(':', 3)[0:2] file_name = grep_params[0] line_num = grep_params[1] sudo("sed -i '%sd' %s" % (line_num, file_name))
def distro_name(): """ Return simple Linux distribution name identifier, e.g. ``"fedora"``. Uses files like ``/etc/os-release`` (or ``/etc/*-release``) and tools like ``/etc/issue``, and ``lsb_release``, trying to identify short id of the system. Examples: * ``fedora`` * ``rhel`` * ``centos`` * ``ubuntu`` * ``debian`` * ``other`` """ with hide('running', 'stdout'): if has_binary('lsb_release'): distro_id = run('lsb_release -s -i').strip().lower() if distro_id: return distro_id if exists('/etc/lsb-release'): distro_id = run('''awk -F '=' '$1 == "DISTRIB_ID" \ {print $2; exit }' \ /etc/lsb-release ''', shell=False).strip().lower() if distro_id: return distro_id if exists('/etc/os-release'): distro_id = run('''awk -F '=' '$1 == "ID" \ {print $2; exit }' \ /etc/os-release''', shell=False).strip().lower() if distro_id: return distro_id # and now the fallback method (guess by existing files) sentinel_files = ( ('fedora', ('fedora-release',)), ('centos', ('centos-release',)), ('debian', ('debian_version',)), ('gentoo', ('gentoo-release',)), ) for name, sentinels in sentinel_files: for sentinel in sentinels: if exists('/etc/%s' % sentinel): return name return "other"
def distro_name(): """ Return simple Linux distribution name identifier, e.g. ``"fedora"``. Uses files like ``/etc/os-release`` (or ``/etc/*-release``) and tools like ``/etc/issue``, and ``lsb_release``, trying to identify short id of the system. Examples: * ``fedora`` * ``rhel`` * ``centos`` * ``ubuntu`` * ``debian`` * ``other`` """ with hide('running', 'stdout'): if has_binary('lsb_release'): distro_id = run('lsb_release -s -i').strip().lower() if distro_id: return distro_id if exists('/etc/lsb-release'): distro_id = run('''awk -F '=' '$1 == "DISTRIB_ID" \ {print $2; exit }' \ /etc/lsb-release ''', shell=False).strip().lower() if distro_id: return distro_id if exists('/etc/os-release'): distro_id = run('''awk -F '=' '$1 == "ID" \ {print $2; exit }' \ /etc/os-release''', shell=False).strip().lower() if distro_id: return distro_id # and now the fallback method (guess by existing files) sentinel_files = ( ('fedora', ('fedora-release', )), ('centos', ('centos-release', )), ('debian', ('debian_version', )), ('gentoo', ('gentoo-release', )), ) for name, sentinels in sentinel_files: for sentinel in sentinels: if exists('/etc/%s' % sentinel): return name return "other"
def _service_command(service_name): command = '/etc/init.d/%s' if has_binary('service', runner=sudo): command = 'service %s' return command % service_name