path-is-owned-by

Example:

# Set group and user attributes on an (existing) file.
- path-is-owned-by:
    path: /tmp/freckles.sh
    owner: freckles
    group: freckles

Description

Make sure a file/folder has a certain owner/group.

This will recursively apply the owner/group change in case the path is a directory. If the path does not exist, an empty file will be created.

Root/sudo permissions will be used to do the chown.

If the owner/group does not exist on the machine, this will create them before changing the target ownership.

Variables

Name Type Default Description

group

string --

the group of the file/folder

owner

string --

the owner of the file/folder

path

string --

the path in question

recursive

boolean False

Whether to apply the changes recursively (if folder).

system_user

boolean False

Whether the user and group should be of system user/group type.

Examples

Example 1

Set group and user attributes on an (existing) file.

Code
- path-is-owned-by:
    path: /tmp/freckles.sh
    owner: freckles
    group: freckles
Description

If the file /tmp/freckles.sh exists, this sets its group and owner to 'freckles'. If the file doesn't exist, nothing will be done.

Code

doc:
  short_help: Make sure a file/folder has a certain owner/group.
  help: |
    Make sure a file/folder has a certain owner/group.

    This will recursively apply the owner/group change in case the path is a directory.
    If the path does not exist, an empty file will be created.

    Root/sudo permissions will be used to do the chown.

    If the owner/group does not exist on the machine, this will create them before changing the target ownership.
  examples:
  - title: Set group and user attributes on an (existing) file.
    vars:
      path: /tmp/freckles.sh
      owner: freckles
      group: freckles
    desc: |
      If the file ``/tmp/freckles.sh`` exists, this sets its group and owner to 'freckles'. If the file doesn't exist, nothing will be done.

args:
  path:
    doc:
      short_help: the path in question
    type: string
    required: false
  owner:
    doc:
      short_help: the owner of the file/folder
    type: string
    required: false
    cli:
      metavar: USER
  group:
    doc:
      short_help: the group of the file/folder
    type: string
    required: false
    cli:
      metavar: GROUP
  system_user:
    doc:
      short_help: Whether the user and group should be of system user/group type.
    type: boolean
    required: false
    default: false
    cli:
      show_default: true
      is_flag: true
  recursive:
    doc:
      short_help: Whether to apply the changes recursively (if folder).
    required: false
    default: false
    type: boolean

meta:
  tags:
  - filesystem
  - file
  - chown
  - featured-frecklecutable

frecklets:
- group-exists:
    group: '{{:: group ::}}'
    system_group: '{{:: system_user ::}}'
    frecklet::skip: "{{:: group | true_if_empty_or('root') ::}}"
- user-exists:
    name: '{{:: owner ::}}'
    system_user: '{{:: system_user ::}}'
    frecklet::skip: "{{:: owner | true_if_empty_or('root') ::}}"
- frecklet:
    name: path-is-owned-by.at.yml
    type: ansible-tasklist
    desc:
      short: "ensure path '{{:: path ::}}' is owned by a user/group."
    properties:
      elevated: true
      internet: false
      idempotent: true
    resources:
      ansible-tasklist:
      - path-is-owned-by.at.yml
  vars:
    __path__: '{{:: path ::}}'
    __recurse__: '{{:: recursive ::}}'
    __owner__: '{{:: owner ::}}'
    __group__: '{{:: group ::}}'
frecklecute path-is-owned-by --help

Usage: frecklecute path-is-owned-by [OPTIONS]

  Make sure a file/folder has a certain owner/group.

  This will recursively apply the owner/group change in case the path is a
  directory. If the path does not exist, an empty file will be created.

  Root/sudo permissions will be used to do the chown.

  If the owner/group does not exist on the machine, this will create them
  before changing the target ownership.

Options:
  --group GROUP                   the group of the file/folder
  --owner USER                    the owner of the file/folder
  --path PATH                     the path in question
  --recursive / --no-recursive    Whether to apply the changes recursively (if
                                  folder).
  --system-user / --no-system-user
                                  Whether the user and group should be of
                                  system user/group type.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.path_is_owned_by.PathIsOwnedBy
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class PathIsOwnedBy(AutoPycklet):
    """Make sure a file/folder has a certain owner/group.

     This will recursively apply the owner/group change in case the path is a directory.
     If the path does not exist, an empty file will be created.

     Root/sudo permissions will be used to do the chown.

     If the owner/group does not exist on the machine, this will create them before changing the target ownership.

       Args:
         group: the group of the file/folder
         owner: the owner of the file/folder
         path: the path in question
         recursive: Whether to apply the changes recursively (if folder).
         system_user: Whether the user and group should be of system user/group type.

    """

    FRECKLET_ID = "path-is-owned-by"

    group: str = None
    owner: str = None
    path: str = None
    recursive: bool = None
    system_user: bool = None


    def __post_init__(self):
        super(PathIsOwnedBy, self).__init__(var_names=["group", "owner", "path", "recursive", "system_user"])


frecklet_class = PathIsOwnedBy
# -*- coding: utf-8 -*-


#
# module path: pycklets.path_is_owned_by.PathIsOwnedBy
#


from pyckles import AutoPycklet

class PathIsOwnedBy(AutoPycklet):
    """Make sure a file/folder has a certain owner/group.

     This will recursively apply the owner/group change in case the path is a directory.
     If the path does not exist, an empty file will be created.

     Root/sudo permissions will be used to do the chown.

     If the owner/group does not exist on the machine, this will create them before changing the target ownership.

       Args:
         group: the group of the file/folder
         owner: the owner of the file/folder
         path: the path in question
         recursive: Whether to apply the changes recursively (if folder).
         system_user: Whether the user and group should be of system user/group type.

    """

    FRECKLET_ID = "path-is-owned-by"

    def __init__(self, group=None, owner=None, path=None, recursive=None, system_user=None):

        super(PathIsOwnedBy, self).__init__(var_names=["group", "owner", "path", "recursive", "system_user"])
        self._group = group
        self._owner = owner
        self._path = path
        self._recursive = recursive
        self._system_user = system_user

    @property
    def group(self):
        return self._group

    @group.setter
    def group(self, group):
        self._group = group

    @property
    def owner(self):
        return self._owner

    @owner.setter
    def owner(self, owner):
        self._owner = owner

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        self._path = path

    @property
    def recursive(self):
        return self._recursive

    @recursive.setter
    def recursive(self, recursive):
        self._recursive = recursive

    @property
    def system_user(self):
        return self._system_user

    @system_user.setter
    def system_user(self, system_user):
        self._system_user = system_user



frecklet_class = PathIsOwnedBy