File size: 7,922 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 |
#!/usr/bin/env python3
"""
Example usage of Cloudflare Manager
Demonstrates key features:
1. Deploy Pages project
2. Bind domain
3. Get nameservers
4. Configure worker routes
"""
from cloudflare_manager import CloudflareManager, CloudflareAccount
def example_pages_deployment():
"""Example: Deploy a Pages project and bind a domain"""
print("\n" + "="*60)
print("Example 1: Deploy Pages Project with Domain")
print("="*60)
# Initialize manager
account = CloudflareAccount(
email="exslym@closedbyme.com",
token="21f3fb278a15b732a4f52c95d5042d78d1a21"
)
cf = CloudflareManager(account)
# Step 1: Create Pages project
print("\nπ Step 1: Creating Pages project...")
project_name = "my-test-site"
project = cf.create_pages_project(project_name, "main")
if project:
print(f"β Project URL: https://{project.get('subdomain')}")
# Step 2: Deploy from current directory (with index.html)
print("\nπ¦ Step 2: Deploying from directory...")
deployment = cf.deploy_pages_project(
project_name=project_name,
directory=".", # Current directory with index.html
branch="main",
commit_message="Initial deployment via API"
)
if deployment:
print(f"β Deployment URL: {deployment.get('url')}")
# Step 3: List all projects
print("\nπ Step 3: Listing all Pages projects...")
projects = cf.list_pages_projects()
for proj in projects:
print(f" - {proj['name']}: https://{proj.get('subdomain')}")
return cf, project_name
def example_domain_and_nameservers(cf, project_name=None):
"""Example: Add domain and get nameservers"""
print("\n" + "="*60)
print("Example 2: Domain Binding and Nameservers")
print("="*60)
domain_name = input("\nπ Enter a domain name to add (e.g., example.com): ").strip()
if not domain_name:
print("β οΈ Skipping domain example (no domain provided)")
return
# Step 1: Create zone to get nameservers
print(f"\nπ Step 1: Creating zone for {domain_name}...")
zone = cf.create_zone(domain_name)
if zone:
nameservers = zone.get("name_servers", [])
zone_id = zone.get("id")
print(f"\nπ Nameservers to add to your domain registrar:")
print("="*60)
for ns in nameservers:
print(f" {ns}")
print("="*60)
# Step 2: Add domain to Pages project (if project exists)
if project_name:
print(f"\nπ Step 2: Adding domain to Pages project '{project_name}'...")
result = cf.add_pages_domain(project_name, domain_name)
if result:
print(f"β Domain added: {result.get('name')}")
print(f" Status: {result.get('status')}")
# Show validation info if needed
if result.get('validation_data'):
val = result['validation_data']
print(f"\nπ DNS Validation Required:")
print(f" Type: {val.get('type')}")
print(f" Name: {val.get('name')}")
print(f" Value: {val.get('value')}")
return zone_id
return None
def example_worker_upload(cf):
"""Example: Upload a Worker script"""
print("\n" + "="*60)
print("Example 3: Upload Worker Script")
print("="*60)
configure = input("\nπ Do you want to upload a worker? (y/n): ").strip().lower()
if configure != 'y':
print("β οΈ Skipping worker upload")
return None
# Upload example worker
print("\nπ€ Uploading example worker...")
result = cf.upload_worker(
script_name="example-worker",
worker_file="example_worker.js"
)
if result:
print(f"β Worker uploaded successfully!")
print(f" Worker ID: {result.get('id')}")
return "example-worker"
else:
print("β Failed to upload worker")
return None
def example_worker_routes(cf, zone_id=None):
"""Example: Configure worker routes"""
print("\n" + "="*60)
print("Example 4: Worker Routes Configuration (Optional)")
print("="*60)
if not zone_id:
print("β οΈ No zone_id available. Skipping worker routes example.")
print(" (You need a zone/domain first)")
return
configure = input("\nπ Do you want to configure worker routes? (y/n): ").strip().lower()
if configure != 'y':
print("β οΈ Skipping worker routes configuration")
return
# Step 1: Get route details
pattern = input("Enter route pattern (e.g., example.com/api/*): ").strip()
script_name = input("Enter worker script name: ").strip()
if pattern and script_name:
# Create worker route
print(f"\nπ Creating worker route...")
route = cf.create_worker_route(zone_id, pattern, script_name)
if route:
print(f"β Route created: {route.get('id')}")
# Step 2: List all routes
print(f"\nπ Listing all worker routes...")
routes = cf.list_worker_routes(zone_id)
if routes:
for route in routes:
print(f" - {route.get('pattern')} -> {route.get('script', 'N/A')}")
else:
print(" No routes found")
def example_list_zones(cf):
"""Example: List all zones"""
print("\n" + "="*60)
print("Example 5: List All Zones")
print("="*60)
zones = cf.list_zones()
if zones:
print(f"\nπ Found {len(zones)} zone(s):")
for zone in zones:
print(f"\n Domain: {zone['name']}")
print(f" Zone ID: {zone['id']}")
print(f" Status: {zone.get('status', 'unknown')}")
nameservers = zone.get("name_servers", [])
if nameservers:
print(f" Nameservers:")
for ns in nameservers:
print(f" - {ns}")
else:
print(" No zones found")
def main():
"""Run all examples"""
print("""
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cloudflare Manager - Example Usage β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
""")
print("This script demonstrates the key features:")
print(" 1. Deploy Pages project")
print(" 2. Bind domain and get nameservers")
print(" 3. Upload Worker script")
print(" 4. Configure worker routes (optional)")
print(" 5. List all zones")
proceed = input("\nDo you want to proceed? (y/n): ").strip().lower()
if proceed != 'y':
print("\nπ Exiting...")
return
# Example 1: Pages deployment
cf, project_name = example_pages_deployment()
# Example 2: Domain and nameservers
zone_id = example_domain_and_nameservers(cf, project_name)
# Example 3: Upload Worker
worker_name = example_worker_upload(cf)
# Example 4: Worker routes (optional)
example_worker_routes(cf, zone_id)
# Example 5: List zones
example_list_zones(cf)
print("\n" + "="*60)
print("β All examples completed!")
print("="*60)
print("\nNext steps:")
print(" 1. Update nameservers at your domain registrar")
print(" 2. Wait for DNS propagation (5-30 minutes)")
print(" 3. Your site will be live!")
if worker_name:
print(f" 4. Your worker is accessible at: https://{worker_name}.<account>.workers.dev")
if __name__ == "__main__":
main()
|