File size: 10,231 Bytes
32e4bbf |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# Cloudflare Manager - Complete Usage Guide
## Table of Contents
1. [Installation](#installation)
2. [Quick Start](#quick-start)
3. [Interactive CLI](#interactive-cli)
4. [Python API Examples](#python-api-examples)
5. [Complete Workflows](#complete-workflows)
6. [Equivalent curl Commands](#equivalent-curl-commands)
## Installation
```bash
# Install dependencies
pip install -r requirements.txt
# Or install manually
pip install requests
```
## Quick Start
### Option 1: Quickstart Script (Recommended for Beginners)
```bash
python3 quickstart.py
```
This will guide you through:
- Creating a Pages project
- Deploying from a directory
- Binding a domain
- Getting nameservers
### Option 2: Interactive CLI
```bash
python3 cloudflare_manager.py
```
This provides a full-featured menu with all operations.
### Option 3: Example Script
```bash
python3 example_usage.py
```
This runs pre-configured examples.
## Interactive CLI
The interactive CLI provides these operations:
```
Operations Menu:
1. List Pages Projects
2. Create Pages Project
3. Deploy Pages Project (from directory)
4. Add Domain to Pages Project
5. List Domains for Pages Project
6. Create Zone and Get Nameservers
7. Get Nameservers for Existing Domain
8. List Zones
9. Create Worker Route
10. List Worker Routes
11. Add Worker Custom Domain
0. Exit
```
## Python API Examples
### 1. Basic Setup
```python
from cloudflare_manager import CloudflareManager, CloudflareAccount
# Initialize with your credentials
account = CloudflareAccount(
email="your-email@example.com",
token="your-api-token"
)
cf = CloudflareManager(account)
```
### 2. Deploy Pages Project
```python
# Create project
project = cf.create_pages_project("my-site", "main")
# Deploy from directory
deployment = cf.deploy_pages_project(
project_name="my-site",
directory="./public", # Your static files directory
branch="main",
commit_message="Deploy via API"
)
print(f"Deployed to: {deployment['url']}")
```
### 3. Bind Domain and Get Nameservers
```python
# Create zone (adds domain to Cloudflare)
zone = cf.create_zone("example.com")
nameservers = zone["name_servers"]
print("Add these nameservers to your domain registrar:")
for ns in nameservers:
print(f" {ns}")
# Bind domain to Pages project
result = cf.add_pages_domain("my-site", "example.com")
print(f"Domain status: {result['status']}")
# If DNS validation is needed
if result.get("validation_data"):
val = result["validation_data"]
print(f"Add DNS record: {val['type']} {val['name']} = {val['value']}")
```
### 4. Configure Worker Routes
```python
# Get zone ID
zone = cf.get_zone_by_name("example.com")
zone_id = zone["id"]
# Create worker route
cf.create_worker_route(
zone_id=zone_id,
pattern="example.com/api/*",
script_name="my-api-worker"
)
# Add custom domain to worker
cf.add_worker_domain(
hostname="api.example.com",
service="my-api-worker",
zone_id=zone_id,
environment="production"
)
```
## Complete Workflows
### Workflow 1: Deploy Static Website
```python
from cloudflare_manager import CloudflareManager, CloudflareAccount
# 1. Setup
account = CloudflareAccount(
email="exslym@closedbyme.com",
token="21f3fb278a15b732a4f52c95d5042d78d1a21"
)
cf = CloudflareManager(account)
# 2. Create and deploy
cf.create_pages_project("my-blog", "main")
cf.deploy_pages_project("my-blog", "./public", "main")
# 3. Add custom domain
zone = cf.create_zone("myblog.com")
cf.add_pages_domain("my-blog", "myblog.com")
cf.add_pages_domain("my-blog", "www.myblog.com")
print("✓ Website deployed!")
print(f"Nameservers: {zone['name_servers']}")
```
### Workflow 2: API with Custom Domain
```python
# 1. Setup (same as above)
# ...
# 2. Get zone
zone = cf.get_zone_by_name("example.com")
zone_id = zone["id"]
# 3. Configure routes
cf.create_worker_route(
zone_id=zone_id,
pattern="example.com/api/*",
script_name="api-worker"
)
# 4. Add subdomain
cf.add_worker_domain(
hostname="api.example.com",
service="api-worker",
zone_id=zone_id
)
print("✓ API configured at api.example.com")
```
### Workflow 3: Multiple Domains on One Project
```python
# 1. Deploy project
cf.create_pages_project("multi-domain-site", "main")
cf.deploy_pages_project("multi-domain-site", "./dist", "main")
# 2. Add multiple domains
domains = ["example.com", "www.example.com", "example.org", "www.example.org"]
for domain in domains:
# Create zone for each root domain
if not domain.startswith("www."):
cf.create_zone(domain)
# Bind to Pages
cf.add_pages_domain("multi-domain-site", domain)
print("✓ All domains configured")
```
## Equivalent curl Commands
For reference, here are the equivalent curl commands for key operations:
### 1. List Accounts
```bash
curl https://api.cloudflare.com/client/v4/accounts \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
```
### 2. Create Pages Project
```bash
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-d '{
"name": "my-pages-app",
"production_branch": "main"
}'
```
### 3. Deploy Pages Project
```bash
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments \
-H 'Content-Type: multipart/form-data' \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-F branch=main \
-F commit_message='Deploy via curl' \
-F manifest='{"index.html": "abc123"}' \
-F index.html=@./index.html
```
### 4. Add Domain to Pages
```bash
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/domains \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-d '{"name": "example.com"}'
```
### 5. Create Zone (Get Nameservers)
```bash
curl https://api.cloudflare.com/client/v4/zones \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-d '{
"account": {"id": "'$ACCOUNT_ID'"},
"name": "example.com",
"type": "full"
}'
```
### 6. Get Zone Details (Including Nameservers)
```bash
curl https://api.cloudflare.com/client/v4/zones/$ZONE_ID \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
```
### 7. Create Worker Route
```bash
curl https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-d '{
"pattern": "example.com/*",
"script": "my-workers-script"
}'
```
### 8. Add Worker Custom Domain
```bash
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/domains \
-X PUT \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-d '{
"hostname": "api.example.com",
"service": "my-worker",
"zone_id": "'$ZONE_ID'",
"environment": "production"
}'
```
## Testing with Provided Credentials
Using the provided test account:
```python
from cloudflare_manager import CloudflareManager, CloudflareAccount
account = CloudflareAccount(
email="exslym@closedbyme.com",
token="21f3fb278a15b732a4f52c95d5042d78d1a21"
)
cf = CloudflareManager(account)
# Test 1: List existing projects
projects = cf.list_pages_projects()
print(f"Found {len(projects)} project(s)")
# Test 2: List zones
zones = cf.list_zones()
for zone in zones:
print(f"Domain: {zone['name']}")
print(f"Nameservers: {zone['name_servers']}")
# Test 3: Deploy current directory
cf.create_pages_project("test-deploy", "main")
cf.deploy_pages_project("test-deploy", ".", "main")
```
## Environment Variables (Optional)
You can also use environment variables:
```bash
export CLOUDFLARE_EMAIL="your-email@example.com"
export CLOUDFLARE_API_TOKEN="your-token"
```
Then in Python:
```python
import os
from cloudflare_manager import CloudflareManager, CloudflareAccount
account = CloudflareAccount(
email=os.getenv("CLOUDFLARE_EMAIL"),
token=os.getenv("CLOUDFLARE_API_TOKEN")
)
cf = CloudflareManager(account)
```
## Troubleshooting
### Issue: "Invalid request headers" or "Invalid format for Authorization header"
**Solution**: Check that your API token is correct and has the required permissions.
### Issue: "Zone not found"
**Solution**: The domain must first be added to Cloudflare using `create_zone()`.
### Issue: "Failed to deploy"
**Solution**: Ensure:
1. The directory exists and contains files
2. There's an `index.html` file
3. Files are not too large (25MB limit per file)
### Issue: Domain validation required
**Solution**: Add the DNS validation record shown in the response to your DNS settings.
## Advanced Usage
### Using Multiple Accounts
```python
from cloudflare_manager import MultiAccountManager
manager = MultiAccountManager()
# Add multiple accounts
manager.add_account("personal", "personal@example.com", "token1")
manager.add_account("work", "work@example.com", "token2")
# Use specific account
personal_cf = manager.get_account("personal")
work_cf = manager.get_account("work")
# Deploy to both accounts
personal_cf.deploy_pages_project("my-site", "./public", "main")
work_cf.deploy_pages_project("company-site", "./dist", "main")
```
### Batch Operations
```python
# Deploy to multiple projects
projects = ["site1", "site2", "site3"]
for project in projects:
cf.create_pages_project(project, "main")
cf.deploy_pages_project(project, f"./projects/{project}", "main")
print(f"✓ Deployed {project}")
```
## API Rate Limits
Cloudflare has rate limits:
- **Standard**: 1,200 requests per 5 minutes
- **Per endpoint**: Varies
The manager handles errors gracefully, but for bulk operations, consider adding delays:
```python
import time
for project in large_project_list:
cf.deploy_pages_project(project, f"./{project}", "main")
time.sleep(1) # Add 1 second delay
```
## Support
For issues or questions:
1. Check the [Cloudflare API docs](https://developers.cloudflare.com/api/)
2. Review error messages carefully
3. Ensure API token has correct permissions
4. Verify account limits (zones, projects, etc.)
## License
MIT License - Use freely in your projects!
|