
    Vhe:                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlmZmZ d Zd Zd	 Zd
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: xfs_quota
short_description: Manage quotas on XFS filesystems
description:
  - Configure quotas on XFS filesystems.
  - Before using this module /etc/projects and /etc/projid need to be configured.
author:
  - William Leemans (@bushvin)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  type:
    description:
      - The XFS quota type.
    type: str
    required: true
    choices:
      - user
      - group
      - project
  name:
    description:
      - The name of the user, group or project to apply the quota to, if other than default.
    type: str
  mountpoint:
    description:
      - The mount point on which to apply the quotas.
    type: str
    required: true
  bhard:
    description:
      - Hard blocks quota limit.
      - This argument supports human readable sizes.
    type: str
  bsoft:
    description:
      - Soft blocks quota limit.
      - This argument supports human readable sizes.
    type: str
  ihard:
    description:
      - Hard inodes quota limit.
    type: int
  isoft:
    description:
      - Soft inodes quota limit.
    type: int
  rtbhard:
    description:
      - Hard realtime blocks quota limit.
      - This argument supports human readable sizes.
    type: str
  rtbsoft:
    description:
      - Soft realtime blocks quota limit.
      - This argument supports human readable sizes.
    type: str
  state:
    description:
      - Whether to apply the limits or remove them.
      - When removing limit, they are set to 0, and not quite removed.
    type: str
    default: present
    choices:
      - present
      - absent

requirements:
  - xfsprogs
a  
- name: Set default project soft and hard limit on /opt of 1g
  community.general.xfs_quota:
    type: project
    mountpoint: /opt
    bsoft: 1g
    bhard: 1g
    state: present

- name: Remove the default limits on /opt
  community.general.xfs_quota:
    type: project
    mountpoint: /opt
    state: absent

- name: Set default soft user inode limits on /home of 1024 inodes and hard of 2048
  community.general.xfs_quota:
    type: user
    mountpoint: /home
    isoft: 1024
    ihard: 2048
a  
bhard:
  description: The current C(bhard) setting in bytes.
  returned: always
  type: int
  sample: 1024
bsoft:
  description: The current C(bsoft) setting in bytes.
  returned: always
  type: int
  sample: 1024
ihard:
  description: The current C(ihard) setting in bytes.
  returned: always
  type: int
  sample: 100
isoft:
  description: The current C(isoft) setting in bytes.
  returned: always
  type: int
  sample: 100
rtbhard:
  description: The current C(rtbhard) setting in bytes.
  returned: always
  type: int
  sample: 1024
rtbsoft:
  description: The current C(rtbsoft) setting in bytes.
  returned: always
  type: int
  sample: 1024
N)AnsibleModulehuman_to_bytesc                  ,   t        t        t        d      t        d      t        d      t        d      t        dd      t        d      t        d      t        d      t        ddddg      t        ddg d	
      
      d      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }	| j                  d   }
| j                  dd      }|t	        |      }|t	        |      }|t	        |      }|	t	        |	      }	t        d      }t
        j                  j                  |      s | j                  dYdd|z  i| t        |      }| | j                  dYdd|z  i| |dk(  rZd}d}||}d |d!   vr3d"|d!   vr,d#|d!   vr%d$|d!   vrd%|d!   vr | j                  dYdd&|z  i| 	 t        j                  |       nW|d(k(  rSd)}d}||}d*|d!   vr,d+|d!   vr%d,|d!   vr | j                  dYdd-|d.|d!   d/i| 	 t        j                  |       n|d0k(  rd1}d2}||}d3|d!   vr%d4|d!   vrd5|d!   vr | j                  dYdd6|z  i| ||k7  r3t
        j                  j                  d7      s | j                  dYdd8i| ||k7  r3t
        j                  j                  d9      s | j                  dYdd:i| ||k7  r$|"t!        |       | j                  dYdd;|z  i| d}||k7  rgd<|z  }t#        | |||      \  }}}|d=k7  r)||d><   ||d?<   ||d@<   ||dA<    | j                  dYddBi| n"|j%                  dC      D ]  }dD|v sdE|v sd} n |
dk(  rX|sV| j&                  sDdF|z  }t#        | |||      \  }}}|d=k7  r(||d><   ||d?<   ||d@<   ||dA<    | j                  dYddGi| d|dH<   na|
dk(  r\|rZ||k7  rU| j&                  sDdI|z  }t#        | |||      \  }}}|d=k7  r(||d><   ||d?<   ||d@<   ||dA<    | j                  dYddJi| d|dH<   t)        | ||||dK      \  }}t)        | ||||dL      \  }}t)        | ||||dM      \  }}|
dk(  r0d=}d=}d=}d=}d=}d=}	||nd=}||nd=}||nd=}||nd=}||nd=}||nd=}t        ||||||N      |d<   g }|0t+        |      |k7  r"|j-                  dO|z         t+        |      |d<   |0t+        |      |k7  r"|j-                  dP|z         t+        |      |d<   |||k7  r|j-                  dQ|z         ||d<   |||k7  r|j-                  dR|z         ||d<   |	0t+        |	      |k7  r"|j-                  dS|	z         t+        |	      |d<   |0t+        |      |k7  r"|j-                  dT|z         t+        |      |d<   t/        |      d=kD  r| j&                  sv|k(  rdUdVdWj1                  |      }ndUdWdWj1                  |      dW|}t#        | |||      \  }}}|d=k7  r(||d><   ||d?<   ||d@<   ||dA<    | j                  dYddXi| d|dH<    | j2                  dYi | y # t        $ r"} | j                  dYdd'|z  i| Y d }~Zd }~ww xY w# t        $ r"} | j                  dYdd'|z  i| Y d }~d }~ww xY w)ZNstr)typeintT)r
   requiredpresentabsent)r
   defaultchoices)groupprojectuser)r
   r   r   )
bhardbsoftihardisoft
mountpointnamertbhardrtbsoftstater
   )argument_specsupports_check_moder
   r   r   r   r   r   r   r   r   r   	xfs_quotaF)changedmsgzPath '%s' is not a mount pointzDPath '%s' is not a mount point or not located on an xfs file system.r   -urootuquotamntoptsusrquotaquotauqnoenforce
qnoenforcezVPath '%s' is not mounted with the uquota/usrquota/quota/uqnoenforce/qnoenforce option.zUser '%s' does not exist.r   -ggquotagrpquotagqnoenforcezPath 'zP' is not mounted with the gquota/grpquota/gqnoenforce option. (current options: )r   -pz#0pquotaprjquotapqnoenforcezEPath '%s' is not mounted with the pquota/prjquota/pqnoenforce option.z/etc/projectsz$Path '/etc/projects' does not exist./etc/projidz"Path '/etc/projid' does not exist.z/Entry '%s' has not been defined in /etc/projid.z
project %sr   cmdrcstdoutstderrzCould not get project state.
zProject Id '%s' - is not set.zproject identifier is not setzproject -s %sz*Could not get quota realtime block report.r    zproject -C %sz8Failed to clear managed tree from project quota control.birtb)r   r   r   r   r   r   zbsoft=%szbhard=%szisoft=%szihard=%sz
rtbsoft=%sz
rtbhard=%szlimit z -d  zCould not set limits. )r   dictparamsget_bin_pathr   ospathismount	fail_jsonget_fs_by_mountpointpwdgetpwnamKeyErrorgrpgetgrnamisfileget_project_id
exec_quotasplit
check_modequota_reportr   appendlenjoin	exit_json)module
quota_typer   r   r   r   r   r   r   r   r   xfs_quota_binresultmptype_argquota_defaulteprj_setr4   r5   r6   r7   linecurrent_bsoftcurrent_bhardcurrent_isoftcurrent_ihardcurrent_rtbsoftcurrent_rtbhardlimits                                 o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/xfs_quota.pymainrg      s   E"E"E"E"65!e$e$E9x>ST549UV
 !F  v&J== D|,JMM'"EMM'"EMM'"EMM'"EmmI&GmmI&GMM'"E''T:Mu%u% ) )F 77??:&U=
JUfU	j	)B	z 	
V	
 	
 V< D ByM)"Y-/r)},R	]2ByM1F l 
	OLL 
w	< D ByM)"Y-/R	]2F r)}. 
	OLL 
y	 < D ByM)"Y-/R	]2F [  = )HFR!GR6R= )FFP!EPP= T%5.:N:VF ELPV = %C!+FM3
!SBQw #u!t#)x #)x    N%CNvN"LL. D74?:dB"' Ig$$%,%/sJ%W"FF7$'F5M#%F4L'-F8$'-F8$$F$$ HLR !%F9h7t}/D$$%,%/sJ%W"FF7$'F5M#%F4L'-F8$'-F8$$F$$ VZ` !%F9#/z4S$ M= $0z4S$ M= (4z4U($O_
  *7)B)6)B)6)B)6)B-<-H/a-<-H/aF; ESZ=8Z%'(e*wSZ=8Z%'(e*wUm3Z%'(wUm3Z%'(ws7|>\G+,Lys7|>\G+,Ly
5zA~  }$*2CHHUOD % +3CHHUOTJ!+FM3
!SBQw #u!t#)x #)x    G%<GG yFvg  	OFN!<t!CNvNN	O*  	OFN!<t!CNvNN	Os0   Z: 0[( :	[%[  [%(	\1\\c                    d }d }|dk(  rd}n|dk(  rd}n|dk(  rd}|dk(  rd}	d	}
d
}n|dk(  rd}	d}
d}n|dk(  rd}	d}
d
}t        | |dd	|      \  }}}|dk7  r&t        d|||      } | j                  ddd
z  i| |j                  d      D ]^  }|j	                         j                         }t        |      dkD  s0|d   |k(  s9t        |d         z  }t        |d         |z  } ||fS  ||fS )Nr   r/   r   r"   r   r*   r9   z-bblocksi   r:   z-iinodes   r;   z-rzrealtime blockszreport r<   r   F)r    r5   r6   r7   r!   z"Could not get quota report for %s.r8         r=   )rM   r>   rD   rN   striprR   r   )rU   rW   r   r   rV   	used_typesofthardrZ   used_arg	used_namefactorr5   r6   r7   rX   r^   s                    rf   rP   rP     sU   DDY	v		w	C		c			e	%	#8DjB 
Qw	
 	XAIMXQWXT" zz|!!#t9q=T!W_tAw<&(DtAw<&(D: :    c                     |ddg||gz   }| j                  |d      \  }}}d|j                  d      v s|dk(  r%d|j                  d      v r| j                  d	
       |||fS )Nz-xz-cT)use_unsafe_shellz%XFS_GETQUOTA: Operation not permittedr8   rk   z5xfs_quota: cannot set limits: Operation not permittedzNYou need to be root or have CAP_SYS_ADMIN capability to perform this operation)r!   )run_commandrN   rD   )rU   rW   r4   r   r5   r6   r7   s          rf   rM   rM     s    $
%j(9
9C!--cD-IR/6<<3EE7C<< 	` 	 	
 vvru   c                 T   d }t        dd      5 }|j                         D ]p  }|j                         j                         }t	        |      dk(  s0|d   | k(  s9|d   dk(  sBt        t        g d|            }|d   j                  d	      |d<    n d d d        |S # 1 sw Y   |S xY w)
Nz/proc/mountsr   rk   rm   xfs)specfilevfstyper%   freqpassnor%   ,)open	readlinesrn   rN   rR   r>   zip)r   mprsr^   rY   s        rf   rE   rE     s    
C	nc	" aKKM 	D##%B2w!|1 31PRTU "%Y!5!5c!:I	 J Js   A BBB"1BB'c                     d }t        dd      5 }|j                         D ]0  }|j                         j                  d      }|d   | k(  s+|d   } n d d d        |S # 1 sw Y   |S xY w)Nr3   rz   :r   rm   )r   r   rn   	partition)r   prjidr   r^   s       rf   rL   rL     ss    E	mS	! QKKM 	D::<))#.DAw$Q		 L Ls   ;AAA'__main__)
__future__r   r   r   r
   __metaclass__DOCUMENTATIONEXAMPLESRETURNrI   rA   rF   ansible.module_utils.basicr   r   rg   rP   rM   rE   rL   __name__r=   ru   rf   <module>r      si    A @JX.
B  	 
 D@F,^ 	 zF ru   