1
0
mirror of https://github.com/openshift/openshift-ansible.git synced 2026-02-05 06:46:04 +01:00
Files
openshift-ansible/roles/openshift_node/library/swapoff.py
2022-09-29 23:15:14 -04:00

180 lines
5.0 KiB
Python

#!/usr/bin/python
# pylint: disable=missing-docstring
#
# Copyright 2017 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# 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.
import subprocess
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = '''
---
module: swapoff
short_description: Disable swap and comment from /etc/fstab
version_added: "2.4"
description:
- This module disables swap and comments entries from /etc/fstab
author:
- "Michael Gugino <mgugino@redhat.com>"
'''
EXAMPLES = '''
# Pass in a message
- name: Disable Swap
swapoff: {}
'''
def check_swap_in_fstab(module):
'''Check for uncommented swap entries in fstab'''
res = subprocess.call(['grep', '^[^#].*swap', '/etc/fstab'])
if res == 2:
# rc 2 == cannot open file.
result = {'failed': True,
'changed': False,
'msg': 'unable to read /etc/fstab',
'state': 'unknown'}
module.fail_json(**result)
elif res == 1:
# No grep match, fstab looks good.
check_result = False
elif res == 0:
# There is an uncommented entry for fstab.
check_result = True
else:
# Some other grep error code, we shouldn't get here.
result = {'failed': True,
'changed': False,
'msg': 'unknown problem with grep "^[^#].*swap" /etc/fstab ',
'state': 'unknown'}
module.fail_json(**result)
return check_result
def check_swap_in_systemd(module):
'''Check for active .swap systemd units'''
res = subprocess.run(['systemctl', '--all', '--type=swap', '--no-legend'], check=False, stdout=subprocess.PIPE)
if res.returncode != 0:
# There was an error executing systemctl
result = {'failed': True,
'changed': False,
'msg': 'unknown problem with systemctl --all --type=swap --no-legend',
'state': 'unknown'}
module.fail_json(**result)
units = []
for line in res.stdout.decode().splitlines():
unit = line.split()[0]
if unit.endswith(".swap"):
units.append(unit)
return units
def check_swapon_status(module):
'''Check if swap is actually in use.'''
try:
res = subprocess.check_output(['swapon', '--show'])
except subprocess.CalledProcessError:
# Some other grep error code, we shouldn't get here.
result = {'failed': True,
'changed': False,
'msg': 'unable to execute swapon --show',
'state': 'unknown'}
module.fail_json(**result)
return 'NAME' in str(res)
def comment_swap_fstab(module):
'''Comment out swap lines in /etc/fstab'''
res = subprocess.call(['sed', '-i.bak', 's/^[^#].*swap.*/#&/', '/etc/fstab'])
if res:
result = {'failed': True,
'changed': False,
'msg': 'sed failed to comment swap in /etc/fstab',
'state': 'unknown'}
module.fail_json(**result)
def disable_systemd_units(module, units):
'''Disable units in systemd'''
for unit in units:
res = subprocess.call(['systemctl', 'disable', unit])
if res:
result = {'failed': True,
'changed': False,
'msg': 'systemct failed to disable ' + unit,
'state': 'unknown'}
module.fail_json(**result)
def run_swapoff(module, changed):
'''Run swapoff command'''
res = subprocess.call(['swapoff', '--all'])
if res:
result = {'failed': True,
'changed': changed,
'msg': 'swapoff --all returned {}'.format(str(res)),
'state': 'unknown'}
module.fail_json(**result)
def run_module():
'''Run this module'''
module = AnsibleModule(
supports_check_mode=False,
argument_spec={}
)
changed = False
swap_fstab_res = check_swap_in_fstab(module)
if swap_fstab_res:
comment_swap_fstab(module)
changed = True
swap_is_inuse_res = check_swapon_status(module)
if swap_is_inuse_res:
run_swapoff(module, changed)
changed = True
swap_systemd_units = check_swap_in_systemd(module)
if swap_systemd_units:
disable_systemd_units(module, swap_systemd_units)
changed = True
result = {'changed': changed}
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()