-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteadd.py
More file actions
executable file
·106 lines (83 loc) · 2.89 KB
/
siteadd.py
File metadata and controls
executable file
·106 lines (83 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""
Add a new site to the sites configuration file.
This script allows you to add a new site to the sites configuration file.
"""
import sys
from pathlib import Path
import yaml
# Define the path to the main sites configuration file
SITES_FILE = Path(__file__).resolve().parent / "sites.yml"
def load_sites():
"""
Load all site configurations from the YAML file.
If the configuration file does not exist, an empty dictionary
is returned. Otherwise, parses and returns the YAML contents.
:return: Dictionary mapping site domains to their configurations.
:rtype: dict
"""
# Return an empty dictionary if the file doesn't exist
if not SITES_FILE.exists():
return {}
# Open and load the YAML file
with open(SITES_FILE) as f:
return yaml.safe_load(f) or {}
def save_sites(sites):
"""
Save all site configurations to the YAML file.
Overwrites the existing file content with the provided dictionary.
Preserves key order and disables flow-style formatting for readability.
:param sites: Dictionary containing all site configurations.
:type sites: dict
"""
# Open the YAML file in write mode and dump contents
with open(SITES_FILE, "w") as f:
yaml.dump(
sites,
f,
default_flow_style=False,
sort_keys=False
)
def add_site(domain, repo_url):
"""
Add a new site configuration entry to the YAML file.
Creates a new site entry based on the domain and repository URL.
Sets sensible defaults for deployment path, branch, runner, and
virtual environment folder. Prevents overwriting existing sites.
:param domain: Full domain name of the new site
(e.g., ``app.example.com``).
:type domain: str
:param repo_url: Git repository URL for the new site.
:type repo_url: str
"""
# Load existing configurations
sites = load_sites()
# Prevent overwriting an existing entry
if domain in sites:
print(f"The configuration for {domain} already exists.")
return
# Build the basic config dictionary
sites[domain] = {
"repository": repo_url,
"deploy_path": f"/var/www/sites/{domain}",
"branch": "main",
"venv": "venv",
"runner": "local"
}
# Save the updated configuration
save_sites(sites)
# Confirm success to user
print(f"The configuration for {domain} has been added.")
# Handle command-line execution
if __name__ == "__main__":
# Ensure exactly two arguments are passed (domain and repo)
LIMIT_ARGS = 2
if len(sys.argv) != LIMIT_ARGS:
print("Usage: ./siteadd.py \"app.example.com\" "
"\"git@github.com:example/www.example.com.git\"")
sys.exit(1)
# Parse arguments
domain = sys.argv[1]
repo_url = sys.argv[2]
# Call the site add function
add_site(domain, repo_url)