added pyinfra method to deploy wordpress
This commit is contained in:
parent
fd02d6f704
commit
d99841843c
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
.env
|
.env
|
||||||
|
pyinfra_wordpress.egg-info/
|
||||||
|
|
14
podman.service.j2
Normal file
14
podman.service.j2
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
[Unit]
|
||||||
|
Description={{description}}
|
||||||
|
After=network.target
|
||||||
|
StartLimitIntervalSec=0
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/usr/bin/podman run --rm --name={{name}} {{environment}} {{mount}} {{port}} {{container}}:{{version}}
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
|
109
pyinfra.py
Normal file
109
pyinfra.py
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
from io import StringIO
|
||||||
|
import importlib.resources
|
||||||
|
|
||||||
|
from pyinfra.operations import files, systemd, server
|
||||||
|
|
||||||
|
|
||||||
|
def deploy_wordpress(
|
||||||
|
unix_user: str,
|
||||||
|
domain: str,
|
||||||
|
wordpress_version: str,
|
||||||
|
wordpress_port: int,
|
||||||
|
mysql_ip: str,
|
||||||
|
mysql_port: int,
|
||||||
|
mysql_password: str = None,
|
||||||
|
mysql_root_password: str = None,
|
||||||
|
_su: dict = {},
|
||||||
|
):
|
||||||
|
# check that podman is installed
|
||||||
|
secrets = [
|
||||||
|
f"MYSQL_PASSWORD={mysql_password}",
|
||||||
|
f"MYSQL_ROOT_PASSWORD={mysql_root_password}",
|
||||||
|
]
|
||||||
|
env = "\n".join(secrets)
|
||||||
|
files.put(
|
||||||
|
name="upload secrets",
|
||||||
|
src=StringIO(env),
|
||||||
|
dest=f"/home/{unix_user}/.env",
|
||||||
|
mode="0600",
|
||||||
|
user=unix_user,
|
||||||
|
)
|
||||||
|
|
||||||
|
# is this also possible with a config file and file.line()?
|
||||||
|
server.shell(
|
||||||
|
name="set firewall rules",
|
||||||
|
commands=[
|
||||||
|
"ufw default deny incoming",
|
||||||
|
"ufw default allow outgoing",
|
||||||
|
"ufw allow 443",
|
||||||
|
"ufw allow 80",
|
||||||
|
"ufw allow 22",
|
||||||
|
"ufw status",
|
||||||
|
# "ufw reload?",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
print(importlib.resources.files(__package__))
|
||||||
|
service_path = f"/home/{unix_user}/.config/systemd/user/"
|
||||||
|
environment = [
|
||||||
|
f"WORDPRESS_DB_HOST={mysql_ip}:{mysql_port}",
|
||||||
|
f"WORDPRESS_DB_PASSWORD={mysql_password}",
|
||||||
|
"WORDPRESS_DB_USER=wordpress",
|
||||||
|
"WORDPRESS_DB_NAME=wordpress",
|
||||||
|
]
|
||||||
|
mount = [
|
||||||
|
f"type=bind,source=/home/{unix_user}/wp-content,destination=/var/www/html/wp-content",
|
||||||
|
f"type=bind,source=/home/{unix_user}/uploads.ini,destination=/usr/local/etc/php/conf.d/uploads.ini",
|
||||||
|
]
|
||||||
|
files.template(
|
||||||
|
name="upload wordpress systemd service",
|
||||||
|
src=importlib.resources.files(__package__) / "podman.service.j2",
|
||||||
|
dest=f"{service_path}wordpress.service",
|
||||||
|
description="run wordpress podman container",
|
||||||
|
name=f"{domain}_wp",
|
||||||
|
environment="-e " + "-e ".join(environment),
|
||||||
|
mount="--mount " + "--mount ".join(mount),
|
||||||
|
port=f"-p 127.0.0.1:{nginx_port}:80"
|
||||||
|
container="wordpress",
|
||||||
|
version=wordpress_version,
|
||||||
|
**_su,
|
||||||
|
)
|
||||||
|
|
||||||
|
environment = [
|
||||||
|
f"MYSQL_ROOT_PASSWORD={mysql_password}",
|
||||||
|
f"MYSQL_PASSWORD={mysql_password}",
|
||||||
|
"MYSQL_DATABASE=wordpress",
|
||||||
|
"MYSQL_USER=wordpress",
|
||||||
|
]
|
||||||
|
files.template(
|
||||||
|
name="upload mysql systemd service",
|
||||||
|
src=importlib.resources.files(__package__) / "podman.service.j2",
|
||||||
|
dest=f"{service_path}mysql.service",
|
||||||
|
description="run mysql podman container",
|
||||||
|
name=f"{domain}_db",
|
||||||
|
environment="-e " + "-e ".join(environment),
|
||||||
|
mount=f"--mount type=bind,source=/home/{unix_user}/db_data/_data/,destination=/var/lib/mysql",
|
||||||
|
port=f"-p {mysql_port}:3306"
|
||||||
|
container="mysql",
|
||||||
|
version="5.7",
|
||||||
|
**_su,
|
||||||
|
)
|
||||||
|
|
||||||
|
files.put(
|
||||||
|
name="upload uploads.ini",
|
||||||
|
src=importlib.resources.files(__package__) / "uploads.ini",
|
||||||
|
dest=f"/home/{unix_user}/uploads.ini",
|
||||||
|
**_su,
|
||||||
|
)
|
||||||
|
|
||||||
|
for container in ["db", "wp"]:
|
||||||
|
systemd.service(
|
||||||
|
name=f"restart {domain}_{container} service",
|
||||||
|
service=f"{domain}_{container}.service",
|
||||||
|
enabled=True,
|
||||||
|
running=True,
|
||||||
|
restarted=True,
|
||||||
|
daemon_reload=True,
|
||||||
|
user_name=unix_user,
|
||||||
|
user_mode=True,
|
||||||
|
)
|
39
pyproject.toml
Normal file
39
pyproject.toml
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
[build-system]
|
||||||
|
requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "pyinfra_wordpress"
|
||||||
|
description = "pyinfra module to deploy wordpress in a podman container"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.10"
|
||||||
|
keywords = ["pyinfra", "wordpress", "podman"]
|
||||||
|
classifiers = [
|
||||||
|
"Development Status :: 4 - Beta",
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"Intended Audience :: Developers",
|
||||||
|
]
|
||||||
|
dynamic = [
|
||||||
|
"version"
|
||||||
|
]
|
||||||
|
dependencies = [
|
||||||
|
"pyinfra",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
"black",
|
||||||
|
"mypy",
|
||||||
|
"isort",
|
||||||
|
"pylint",
|
||||||
|
"pylama",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 120
|
||||||
|
|
||||||
|
[tool.isort]
|
||||||
|
profile = "black"
|
||||||
|
|
||||||
|
[tool.mypy]
|
||||||
|
ignore_missing_imports = "True"
|
Loading…
Reference in a new issue