Skip to content

Commit 59ed5c2

Browse files
committed
Se entadarizar documentación se utiliza reStructuredText
1 parent a32f4c1 commit 59ed5c2

8 files changed

Lines changed: 349 additions & 146 deletions

File tree

fabfile.py

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,26 @@ def get_connection(
2626
config: dict | None = None
2727
) -> Connection | DockerRunner | Context:
2828
"""
29-
Return a connection object based on environment variables or config.
29+
Resolve the appropriate connection object for the site.
3030
31-
Priority:
32-
1. Environment variables: DEPLOYER_HOST, USER, PORT
33-
2. Docker runner if configured.
34-
3. Host/port from config.
35-
4. Fallback connection or local.
31+
Selects the correct runner based on environment variables,
32+
Docker settings, or host definitions. Priority order:
3633
37-
:param fallback_connection: Local context or default connection
38-
:param config: Optional site configuration dictionary
39-
:return: Fabric Connection, DockerRunner, or Context
34+
1. Environment variables: ``DEPLOYER_HOST``, ``DEPLOYER_USER``,
35+
``DEPLOYER_PORT``
36+
2. Docker runner (if ``runner: docker`` is set in config)
37+
3. SSH host/port defined in config
38+
4. Fallback connection
39+
5. Default to local context
40+
41+
:param fallback_connection: Default local or remote runner.
42+
:type fallback_connection: Union[Connection, DockerRunner, Context]
43+
44+
:param config: Optional site config loaded from YAML.
45+
:type config: dict or None
46+
47+
:return: A connection object suitable for deployment.
48+
:rtype: Union[Connection, DockerRunner, Context]
4049
"""
4150
# 1. If environment variable is set, override all
4251
host = os.getenv("DEPLOYER_HOST")
@@ -82,8 +91,14 @@ def deploy(c: Connection | DockerRunner | Context, site: str) -> None:
8291
"""
8392
Deploy a single site defined in the configuration file.
8493
85-
:param c: Fabric connection object.
86-
:param site: Name of the site defined in sites.yml.
94+
Loads site-specific configuration, resolves the proper connection
95+
(local, Docker, or SSH), and runs the full deployment process.
96+
97+
:param c: Fabric connection or context object.
98+
:type c: Union[Connection, DockerRunner, Context]
99+
100+
:param site: Name of the site as defined in ``sites.yml``.
101+
:type site: str
87102
"""
88103
# Load all site definitions from the YAML config
89104
sites = load_sites()
@@ -108,9 +123,13 @@ def deploy(c: Connection | DockerRunner | Context, site: str) -> None:
108123
@task
109124
def deploy_all(c: Connection | DockerRunner | Context) -> None:
110125
"""
111-
Deploy all sites listed in sites.yml.
126+
Deploy all sites listed in the configuration file.
127+
128+
Iterates through all entries in ``sites.yml`` and runs the
129+
deployment pipeline for each one sequentially.
112130
113-
:param c: Fabric connection object.
131+
:param c: Fabric connection or context object.
132+
:type c: Union[Connection, DockerRunner, Context]
114133
"""
115134
# Load all site definitions
116135
sites = load_sites()
@@ -127,20 +146,29 @@ def deploy_all(c: Connection | DockerRunner | Context) -> None:
127146
@task
128147
def list_sites(c: Connection | DockerRunner | Context) -> None:
129148
"""
130-
Display all available site configurations.
149+
Display all configured sites and their repository URLs.
150+
151+
Loads site data from ``sites.yml`` and prints the site name
152+
alongside its associated Git repository.
131153
132-
:param c: Fabric connection object.
154+
:param c: Fabric connection or context object.
155+
:type c: Union[Connection, DockerRunner, Context]
133156
"""
134157
# Print table of available site names
135158
print_site_list()
136159

137160
@task(help={"site": "Name of the site to rollback"})
138161
def rollback(c: Connection | DockerRunner | Context, site: str) -> None:
139162
"""
140-
Rollback a site to its previous release.
163+
Rollback a site to the previous release.
164+
165+
Identifies the most recent backup release and reverts to it.
166+
167+
:param c: Fabric connection or context object.
168+
:type c: Union[Connection, DockerRunner, Context]
141169
142-
:param c: Fabric connection object.
143170
:param site: Name of the site to rollback.
171+
:type site: str
144172
"""
145173
# Initialize logger
146174
logger = get_logger(site)
@@ -166,9 +194,13 @@ def rollback(c: Connection | DockerRunner | Context, site: str) -> None:
166194
@task
167195
def rollback_all(c: Connection | DockerRunner | Context) -> None:
168196
"""
169-
Rollback all sites to their previous release.
197+
Rollback all sites to their previous releases.
198+
199+
Iterates through all configured sites and performs a rollback
200+
to their last known good deployment.
170201
171-
:param c: Fabric connection object.
202+
:param c: Fabric connection or context object.
203+
:type c: Union[Connection, DockerRunner, Context]
172204
"""
173205
# Load all site configs
174206
sites = load_sites()
@@ -185,10 +217,16 @@ def rollback_all(c: Connection | DockerRunner | Context) -> None:
185217
@task(help={"site": "Name of the site to unlock"})
186218
def unlock(c: Connection | DockerRunner | Context, site: str) -> None:
187219
"""
188-
Remove the lock file for a specific site.
220+
Force-remove the deployment lock for a specific site.
221+
222+
Useful when a previous deployment was interrupted and the
223+
lock wasn't released.
224+
225+
:param c: Fabric connection or context object.
226+
:type c: Union[Connection, DockerRunner, Context]
189227
190-
:param c: Fabric connection object.
191228
:param site: Name of the site to unlock.
229+
:type site: str
192230
"""
193231
# Initialize logger
194232
logger = get_logger(site)
@@ -214,9 +252,12 @@ def unlock(c: Connection | DockerRunner | Context, site: str) -> None:
214252
@task
215253
def unlock_all(c: Connection | DockerRunner | Context) -> None:
216254
"""
217-
Unlock all sites by removing their lock files.
255+
Remove lock files for all sites defined in ``sites.yml``.
256+
257+
Useful when recovering from interrupted deploys or rollbacks.
218258
219-
:param c: Fabric connection object.
259+
:param c: Fabric connection or context object.
260+
:type c: Union[Connection, DockerRunner, Context]
220261
"""
221262
# Load site configurations
222263
sites = load_sites()

fabricator/deploy.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,42 @@
3333

3434
def deploy_site(c: Connection | DockerRunner | Context, config: dict) -> None:
3535
"""
36-
Deploy a Python project using runner (local, docker, or ssh).
37-
38-
This function orchestrates the full deployment pipeline:
39-
- Acquires a deployment lock to prevent concurrent deploys.
40-
- Verifies remote directory structure.
41-
- Creates a compressed backup of the current version.
42-
- Clones the repository and creates a timestamped release folder.
43-
- Updates the `current` symlink to the new release.
44-
- Symlinks shared files and writable folders.
45-
- Installs dependencies, runs migrations, collectstatic, restarts services.
46-
- Performs automatic rollback in case of failure.
47-
- Releases the lock regardless of success or failure.
48-
49-
:param c: Fabric connection or context object (local, ssh, or Docker).
50-
:param config: Site configuration dictionary loaded from sites.yml.
36+
Deploy a Python web project using local, Docker, or SSH context.
37+
38+
This function executes the full deployment pipeline for a Django or
39+
Python-based project. It handles preparation, code updates, shared
40+
resources, virtualenv management, database migrations, static files,
41+
and service restarts. It also ensures safety through locking and
42+
rollback mechanisms.
43+
44+
Deployment steps include:
45+
46+
1. Acquiring a lock to prevent concurrent deployments.
47+
2. Verifying and preparing the remote directory structure.
48+
3. Creating a compressed backup of the current version.
49+
4. Cloning the repository into a new timestamped release folder.
50+
5. Cleaning up old releases beyond `max_releases`.
51+
6. Linking shared files and directories to the new release.
52+
7. Setting permissions on writable directories.
53+
8. Installing dependencies in a virtual environment.
54+
9. Running Django database migrations.
55+
10. Running Django `collectstatic`.
56+
11. Updating the `current` symlink to point to the new release.
57+
12. Restarting services like Gunicorn to apply changes.
58+
13. Rolling back to the previous release if a failure occurs.
59+
14. Releasing the deployment lock at the end.
60+
61+
:param c: The Fabric context or connection object. Can be a local
62+
`Context`, remote `Connection`, or `DockerRunner`.
63+
:type c: Union[Connection, DockerRunner, Context]
64+
65+
:param config: Dictionary with all deployment-related settings loaded
66+
from `sites.yml`, including project name, paths, shared
67+
files, and service settings.
68+
:type config: dict
69+
70+
:raises DeployerException: If any step in the process fails.
71+
:rtype: None
5172
"""
5273
# Initialize logger for this site
5374
logger = get_logger(config['name'])

fabricator/exceptions/deployer_exceptions.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ def __init__(self, message, code = None, params = None):
88
"""
99
Initialize the DeployerException.
1010
11-
:param message: The error message.
12-
:param code: The error code.
13-
:param params: The error parameters.
11+
Stores the error message, optional code, and any additional
12+
parameters passed for debugging or context.
13+
14+
:param message: Description of the error.
15+
:type message: str
16+
17+
:param code: Optional error code.
18+
:type code: int or str, optional
19+
20+
:param params: Optional error context or data.
21+
:type params: dict or any, optional
1422
"""
1523
self.message = message
1624
self.code = code
@@ -19,9 +27,12 @@ def __init__(self, message, code = None, params = None):
1927

2028
def __str__(self):
2129
"""
22-
Return a string representation of the error.
30+
Return a formatted string representation of the error.
31+
32+
If a code is provided, it is included in the output. Otherwise,
33+
only the message is shown.
2334
24-
:return: A string representation of the error.
35+
:return: A string representing the error.
2536
:rtype: str
2637
"""
2738
if self.code is not None:

fabricator/logger.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
1111

1212
def get_logger(name: str = "deploy") -> logging.Logger:
1313
"""
14-
Return a reusable logger configured to output to the console.
14+
Get or create a reusable console-based logger by name.
1515
16-
Ensures that handlers are added only once to avoid duplicate logs.
16+
This function returns a configured `logging.Logger` instance with
17+
an attached console handler. It prevents duplication of handlers
18+
if the logger is requested multiple times.
1719
18-
:param name: Name to identify the logger (e.g., the site name).
19-
:return: Configured Logger instance.
20+
The logger outputs messages in the format:
21+
`[timestamp] [LEVEL] message`.
22+
23+
:param name: Name to identify the logger instance. Typically used
24+
to separate logs by context or project.
25+
:type name: str
26+
27+
:return: A configured `Logger` object ready for use.
28+
:rtype: logging.Logger
2029
"""
2130
logger = logging.getLogger(name)
2231

0 commit comments

Comments
 (0)