Coverage for .tox/coverage/lib/python3.11/site-packages/wuttamess/util.py: 100%
23 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-25 07:00 -0600
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-25 07:00 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttaMess -- Fabric Automation Helpers
5# Copyright © 2024 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24Misc. Utilities
25"""
27from pathlib import Path
28from typing_extensions import Any, Mapping
30from mako.template import Template
33def exists(c, path):
34 """
35 Returns ``True`` if given path exists on the host, otherwise ``False``.
36 """
37 return not c.run(f'test -e {path}', warn=True).failed
40def get_home_path(c, user=None):
41 """
42 Get the path to user's home folder on target machine.
44 :param c: Fabric connection.
46 :param user: Username whose home folder you want. If not
47 specified, the username for the current connection is assumed.
49 :returns: Home folder path as string.
50 """
51 user = user or c.user
52 home = c.run(f'getent passwd {user} | cut -d: -f6').stdout.strip()
53 home = home.rstrip('/')
54 return home
57def is_symlink(c, path):
58 """
59 Check if the given path is a symlink.
61 :param c: Fabric connection.
63 :param path: Path to check, on target machine.
65 :returns: ``True`` if path is a symlink, else ``False``.
66 """
67 # nb. this function is derived from one copied from fabric v1
68 cmd = 'test -L "$(echo %s)"' % path
69 result = c.run(cmd, warn=True)
70 return False if result.failed else True
73def mako_renderer(c, env={}):
74 """
75 This returns a *function* suitable for use as a ``fabsync`` file
76 renderer. The function assumes the file is a Mako template.
78 :param c: Fabric connection.
80 :param env: Environment dictionary to be used as Mako template
81 context.
83 Typical usage is something like::
85 from fabric import task
86 from wuttamess import sync, util
88 root = sync.make_root('files')
89 env = {}
91 @task
92 def foo(c):
94 # define possible renderers for fabsync
95 renderers = {'mako': util.mako_renderer(c, env)}
97 sync.check_isync(c, root, 'etc/postfix', renderers=renderers)
98 """
99 def render(path: Path, vars: Mapping[str, Any], **kwargs) -> bytes:
100 return Template(filename=str(path)).render(**env)
102 return render
105def set_timezone(c, timezone):
106 """
107 Set the system timezone.
109 :param c: Fabric connection.
111 :param timezone: Standard timezone name,
112 e.g. ``'America/Chicago'``.
113 """
114 c.run(f"bash -c 'echo {timezone} > /etc/timezone'")
116 if is_symlink(c, '/etc/localtime'):
117 c.run(f'ln -sf /usr/share/zoneinfo/{timezone} /etc/localtime')
118 else:
119 c.run(f'cp /usr/share/zoneinfo/{timezone} /etc/localtime')