File size: 7,081 Bytes
4de31ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# WordPress to Hugging Face Spaces Deployment Script
# This script helps you deploy WordPress to Hugging Face Spaces

set -e

echo "πŸš€ WordPress to Hugging Face Spaces Deployment Helper"
echo "================================================="

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# Check if required files exist
check_files() {
    print_step "Checking required files..."
    
    local files=("Dockerfile.hf-spaces" "wp-config-hf.php" "db.php" "start-hf.sh" "README-HF-SPACES.md")
    local missing_files=()
    
    for file in "${files[@]}"; do
        if [ ! -f "$file" ]; then
            missing_files+=("$file")
        fi
    done
    
    if [ ${#missing_files[@]} -ne 0 ]; then
        print_error "Missing required files:"
        for file in "${missing_files[@]}"; do
            echo "  - $file"
        done
        exit 1
    fi
    
    print_status "All required files found βœ“"
}

# Get user input
get_user_input() {
    print_step "Getting deployment information..."
    
    echo -n "Enter your Hugging Face username: "
    read -r HF_USERNAME
    
    echo -n "Enter your Space name: "
    read -r SPACE_NAME
    
    echo -n "Do you want to create a new directory for deployment? (y/n): "
    read -r CREATE_DIR
    
    if [ "$CREATE_DIR" = "y" ] || [ "$CREATE_DIR" = "Y" ]; then
        DEPLOY_DIR="./hf-spaces-deployment"
        echo "Deployment directory: $DEPLOY_DIR"
    else
        echo -n "Enter deployment directory path: "
        read -r DEPLOY_DIR
    fi
}

# Create deployment directory
setup_deployment_dir() {
    print_step "Setting up deployment directory..."
    
    if [ -d "$DEPLOY_DIR" ]; then
        print_warning "Directory $DEPLOY_DIR already exists"
        echo -n "Do you want to overwrite it? (y/n): "
        read -r OVERWRITE
        
        if [ "$OVERWRITE" = "y" ] || [ "$OVERWRITE" = "Y" ]; then
            rm -rf "$DEPLOY_DIR"
        else
            print_error "Deployment cancelled"
            exit 1
        fi
    fi
    
    mkdir -p "$DEPLOY_DIR"
    print_status "Created deployment directory: $DEPLOY_DIR"
}

# Copy files
copy_files() {
    print_step "Copying files to deployment directory..."
    
    # Copy and rename Dockerfile
    cp "Dockerfile.hf-spaces" "$DEPLOY_DIR/Dockerfile"
    print_status "Copied Dockerfile.hf-spaces β†’ Dockerfile"
    
    # Copy other files
    cp "wp-config-hf.php" "$DEPLOY_DIR/"
    cp "db.php" "$DEPLOY_DIR/"
    cp "start-hf.sh" "$DEPLOY_DIR/"
    cp "app.py" "$DEPLOY_DIR/"
    cp "README-HF-SPACES.md" "$DEPLOY_DIR/README.md"
    
    print_status "Copied all required files"
}

# Create .gitignore
create_gitignore() {
    print_step "Creating .gitignore..."
    
    cat > "$DEPLOY_DIR/.gitignore" << 'EOF'
# WordPress
wp-config.php
wp-content/uploads/
wp-content/cache/
wp-content/database/
*.log

# System files
.DS_Store
Thumbs.db

# IDE
.vscode/
.idea/
*.swp
*.swo

# Temporary files
*.tmp
*.temp
EOF
    
    print_status "Created .gitignore"
}

# Create app.py for HF Spaces compatibility
create_app_py() {
    print_step "Creating app.py for HF Spaces..."
    
    cat > "$DEPLOY_DIR/app.py" << 'EOF'
#!/usr/bin/env python3
"""
Hugging Face Spaces compatibility file for WordPress
This file is required by HF Spaces but the actual application runs in Docker
"""

import subprocess
import time
import sys

def main():
    print("πŸš€ Starting WordPress on Hugging Face Spaces...")
    print("This is a Docker-based WordPress deployment.")
    print("The actual WordPress application is running in the Docker container.")
    
    # Keep the process alive
    try:
        while True:
            time.sleep(60)
            print("WordPress container is running...")
    except KeyboardInterrupt:
        print("Shutting down...")
        sys.exit(0)

if __name__ == "__main__":
    main()
EOF
    
    print_status "Created app.py"
}

# Initialize git repository
init_git() {
    print_step "Initializing git repository..."
    
    cd "$DEPLOY_DIR"
    
    if [ ! -d ".git" ]; then
        git init
        print_status "Initialized git repository"
    else
        print_warning "Git repository already exists"
    fi
    
    # Add HF Spaces remote if provided
    if [ -n "$HF_USERNAME" ] && [ -n "$SPACE_NAME" ]; then
        HF_REMOTE="https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
        
        if git remote get-url origin >/dev/null 2>&1; then
            print_warning "Remote 'origin' already exists"
        else
            git remote add origin "$HF_REMOTE"
            print_status "Added remote: $HF_REMOTE"
        fi
    fi
    
    cd - > /dev/null
}

# Create deployment instructions
create_instructions() {
    print_step "Creating deployment instructions..."
    
    cat > "$DEPLOY_DIR/DEPLOYMENT.md" << EOF
# Deployment Instructions

## Quick Deploy to Hugging Face Spaces

### Method 1: Git Push (Recommended)

1. Commit and push the files:
   \`\`\`bash
   cd $DEPLOY_DIR
   git add .
   git commit -m "Initial WordPress deployment"
   git push origin main
   \`\`\`

### Method 2: Web Upload

1. Go to your HF Space: https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME
2. Upload these files:
   - Dockerfile
   - wp-config-hf.php
   - db.php
   - start-hf.sh
   - README.md
   - app.py

## After Deployment

1. Wait for the build to complete (5-10 minutes)
2. Access your WordPress site:
   - **Main site**: https://$HF_USERNAME-$SPACE_NAME.hf.space/
   - **Admin panel**: https://$HF_USERNAME-$SPACE_NAME.hf.space/wp-admin/
   - **Health check**: https://$HF_USERNAME-$SPACE_NAME.hf.space/health.html

## Default Login

- **Username**: demo
- **Password**: demo123

⚠️ **Important**: Change the default password after first login!

## Troubleshooting

If the deployment fails:
1. Check the build logs in your HF Space
2. Ensure all files are uploaded correctly
3. Verify the Dockerfile syntax
4. Check that port 7860 is properly configured

EOF
    
    print_status "Created deployment instructions"
}

# Main deployment process
main() {
    echo
    print_step "Starting deployment process..."
    
    check_files
    get_user_input
    setup_deployment_dir
    copy_files
    create_gitignore
    create_app_py
    init_git
    create_instructions
    
    echo
    print_status "πŸŽ‰ Deployment preparation completed!"
    echo
    echo -e "${GREEN}Next steps:${NC}"
    echo "1. cd $DEPLOY_DIR"
    echo "2. Review the files"
    echo "3. Follow instructions in DEPLOYMENT.md"
    echo
    echo -e "${BLUE}Your WordPress will be available at:${NC}"
    echo "https://$HF_USERNAME-$SPACE_NAME.hf.space/"
    echo
    print_warning "Remember to change the default password (demo/demo123) after deployment!"
}

# Run main function
main "$@"