Skip to content

Commit

Permalink
feat(module): add acme_sh.install
Browse files Browse the repository at this point in the history
  • Loading branch information
genaumann committed Jan 6, 2024
1 parent ad30127 commit 6a72799
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions _modules/acme_sh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
acme.sh module
This module interacts with acme.sh https://github.com/acmesh-official/acme.sh
"""

import logging

from salt.exceptions import CommandExecutionError, SaltInvocationError


log = logging.getLogger(__name__)

def install(
email,
user='root',
system=False,
home=None,
):
"""
Install acme.sh
email
used to register an account to acme server, you will receive a renewal notice email here
user
user to install
default = root
system
system installation to /opt/acme.sh
default = False
home
customized dir to install acme.sh in.
When system is False, default: it installs into ~/.acme.sh
"""

# check input
if user != 'root' and system:
raise SaltInvocationError("Specify either user or sytem")

# clone https://github.com/acmesh-official/acme.sh

clone_name = f"acme.sh-{'global' if system else user}"
clone = __salt__["git.clone"]("/tmp", url="https://github.com/acmesh-official/acme.sh", name=clone_name)

if not clone:
CommandExecutionError("Failed to clone the acme.sh repository")

cmd = ["./acme.sh", "--install", "--accountemail", email, "--nocron"]

# if system
if system:
cmd.append("--home /opt/acme.sh")

# if user config
if user and home:
cmd.append(f"--home {home}")

install = __salt__["cmd.run_all"](" ".join(cmd), cwd=f"/tmp/{clone_name}", runas=user)
__salt__["file.remove"](f"/tmp/{clone_name}")

# check install process successfully
if install["retcode"] == 0:
log.debug("acme.sh successfully installed")
else:
return (False, "Installation failed, see logfiles for more information")

if user and not system and not home:
install_dir = __salt__["user.info"](user)["home"]
elif user and home:
install_dir = home
else:
install_dir = "/opt/acme.sh"

return(f"acme.sh successfully installed in {install_dir}")

0 comments on commit 6a72799

Please sign in to comment.