storage yaratildi
This commit is contained in:
5
resources/scripts/backup.sh
Normal file
5
resources/scripts/backup.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
file=/tmp/db-$(/usr/bin/date +\%Y-%m-%d-%H:%M:%S).sql
|
||||
container=postgres
|
||||
/usr/bin/docker container exec $container pg_dump -U postgres django > $file
|
||||
mc cp $file b2/buket-name
|
||||
rm $file
|
||||
23
resources/scripts/create_buckets.py
Normal file
23
resources/scripts/create_buckets.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import boto3
|
||||
import os
|
||||
from botocore.client import Config
|
||||
|
||||
s3 = boto3.resource('s3',
|
||||
endpoint_url='http://minio:9000',
|
||||
aws_access_key_id='minioadmin',
|
||||
aws_secret_access_key='minioadmin123',
|
||||
config=Config(signature_version='s3v4'),
|
||||
region_name='us-east-1')
|
||||
|
||||
buckets = ['media', 'static']
|
||||
|
||||
for bucket_name in buckets:
|
||||
bucket = s3.Bucket(bucket_name)
|
||||
try:
|
||||
bucket.create()
|
||||
print(f"Bucket '{bucket_name}' yaratildi.")
|
||||
except Exception as e:
|
||||
if "BucketAlreadyOwnedByYou" in str(e) or "BucketAlreadyExists" in str(e):
|
||||
print(f"Bucket '{bucket_name}' allaqachon mavjud.")
|
||||
else:
|
||||
print(f"Xato: {e}")
|
||||
17
resources/scripts/entrypoint-server.sh
Normal file
17
resources/scripts/entrypoint-server.sh
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
while ! nc -z $DB_HOST $DB_PORT; do
|
||||
sleep 2
|
||||
echo "Waiting postgress...."
|
||||
done
|
||||
|
||||
python3 manage.py collectstatic --noinput
|
||||
python3 manage.py migrate --noinput
|
||||
|
||||
gunicorn config.wsgi:application -b 0.0.0.0:8000 --workers $(($(nproc) * 2 + 1))
|
||||
|
||||
|
||||
|
||||
exit $?
|
||||
|
||||
|
||||
18
resources/scripts/entrypoint.sh
Normal file
18
resources/scripts/entrypoint.sh
Normal file
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
while ! nc -z $DB_HOST $DB_PORT; do
|
||||
sleep 2
|
||||
echo "Waiting postgress...."
|
||||
done
|
||||
|
||||
|
||||
python3 manage.py collectstatic --noinput
|
||||
python3 manage.py migrate --noinput
|
||||
|
||||
uvicorn config.asgi:application --host 0.0.0.0 --port 8000 --reload --reload-dir core --reload-dir config
|
||||
|
||||
|
||||
|
||||
exit $?
|
||||
|
||||
|
||||
167
resources/scripts/migrate_firestore.py
Normal file
167
resources/scripts/migrate_firestore.py
Normal file
@@ -0,0 +1,167 @@
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from django.core.files.base import ContentFile
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, firestore
|
||||
|
||||
# 1. Django muhitini yuklash
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
|
||||
django.setup()
|
||||
|
||||
from core.apps.vendors.models import SectionModel, CategoryModel, VendorModel, VendorproductModel
|
||||
|
||||
# 2. Firebase ulanish
|
||||
cert_path = "fondexuzb-firebase-adminsdk-fbsvc-7b0e2d6200.json"
|
||||
if not firebase_admin._apps:
|
||||
cred = credentials.Certificate(cert_path)
|
||||
firebase_admin.initialize_app(cred)
|
||||
|
||||
db = firestore.client()
|
||||
|
||||
def download_image(url):
|
||||
"""Rasmni URL orqali yuklab olish"""
|
||||
if not url or not url.startswith('http'):
|
||||
return None
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
if response.status_code == 200:
|
||||
return ContentFile(response.content)
|
||||
except Exception as e:
|
||||
print(f"Rasm yuklashda xato: {url} -> {e}")
|
||||
return None
|
||||
|
||||
def migrate_sections():
|
||||
print("--- Sections migratsiyasi boshlandi ---")
|
||||
docs = db.collection('sections').stream()
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
section, created = SectionModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'name': data.get('name', 'Nomsiz Section'),
|
||||
'is_active': data.get('isActive', True),
|
||||
'image_url': data.get('sectionImage', ''),
|
||||
'color': data.get('color', ''),
|
||||
'service_type': data.get('serviceType', ''),
|
||||
}
|
||||
)
|
||||
if created and section.image_url:
|
||||
img_file = download_image(section.image_url)
|
||||
if img_file:
|
||||
section.image.save(f"section_{doc.id}.png", img_file, save=True)
|
||||
print(f"Section {section.name} {'yaratildi' if created else 'mavjud'}")
|
||||
|
||||
def migrate_categories():
|
||||
print("\n--- Categories migratsiyasi boshlandi ---")
|
||||
docs = db.collection('vendor_categories').stream()
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
section_id = data.get('section_id')
|
||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||
|
||||
category, created = CategoryModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'section': section,
|
||||
'title': data.get('title', 'Nomsiz Kategoriya'),
|
||||
'description': data.get('description', ''),
|
||||
'photo_url': data.get('photo', ''),
|
||||
'is_publish': data.get('publish', True),
|
||||
'order': data.get('order', 0),
|
||||
}
|
||||
)
|
||||
if created and category.photo_url:
|
||||
img_file = download_image(category.photo_url)
|
||||
if img_file:
|
||||
category.photo.save(f"cat_{doc.id}.png", img_file, save=True)
|
||||
print(f"Category {category.title} {'yaratildi' if created else 'mavjud'}")
|
||||
|
||||
def migrate_vendors():
|
||||
print("\n--- Vendors migratsiyasi boshlandi ---")
|
||||
docs = db.collection('vendors').stream()
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
section_id = data.get('section_id')
|
||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||
|
||||
vendor, created = VendorModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'section': section,
|
||||
'title': data.get('title', 'Nomsiz Vendor'),
|
||||
'description': data.get('description', ''),
|
||||
'phone': data.get('phonenumber', ''),
|
||||
'location': {
|
||||
'lat': data.get('latitude', 0),
|
||||
'lng': data.get('longitude', 0),
|
||||
},
|
||||
'photo_url': data.get('photo', ''),
|
||||
'is_active': data.get('enabled_listing_can_stay', True),
|
||||
}
|
||||
)
|
||||
if created and vendor.photo_url:
|
||||
img_file = download_image(vendor.photo_url)
|
||||
if img_file:
|
||||
vendor.photo.save(f"vendor_{doc.id}.png", img_file, save=True)
|
||||
print(f"Vendor {vendor.title} {'yaratildi' if created else 'mavjud'}")
|
||||
|
||||
def migrate_products():
|
||||
print("\n--- Products migratsiyasi boshlandi ---")
|
||||
|
||||
batch_size = 100
|
||||
last_doc = None
|
||||
|
||||
while True:
|
||||
query = db.collection('vendor_products').limit(batch_size)
|
||||
if last_doc:
|
||||
query = query.start_after(last_doc)
|
||||
|
||||
docs = list(query.stream())
|
||||
if not docs:
|
||||
break
|
||||
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
vendor_id = data.get('vendorID')
|
||||
category_id = data.get('categoryID')
|
||||
section_id = data.get('section_id')
|
||||
|
||||
vendor = VendorModel.objects.filter(firestore_id=vendor_id).first()
|
||||
category = CategoryModel.objects.filter(firestore_id=category_id).first()
|
||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||
|
||||
product, created = VendorproductModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'vendor': vendor,
|
||||
'category': category,
|
||||
'section': section,
|
||||
'name': data.get('name', 'Nomsiz Product'),
|
||||
'description': data.get('description', ''),
|
||||
'price': float(data.get('price', 0)),
|
||||
'discount_price': float(data.get('disPrice', 0)),
|
||||
'quantity': data.get('quantity', -1),
|
||||
'is_publish': data.get('publish', True),
|
||||
'photos_json': data.get('photos', []),
|
||||
}
|
||||
)
|
||||
if created and product.photos_json:
|
||||
main_img_url = product.photos_json[0]
|
||||
img_file = download_image(main_img_url)
|
||||
if img_file:
|
||||
product.image.save(f"prod_{doc.id}.png", img_file, save=True)
|
||||
print(f"Product {product.name} {'yaratildi' if created else 'mavjud'}")
|
||||
|
||||
last_doc = docs[-1]
|
||||
print(f"--- {batch_size} ta mahsulot qayta ishlandi ---")
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate_sections()
|
||||
migrate_categories()
|
||||
migrate_vendors()
|
||||
migrate_products()
|
||||
print("\n✅ Migratsiya muvaffaqiyatli yakunlandi!")
|
||||
35
resources/scripts/set_minio_policy.py
Normal file
35
resources/scripts/set_minio_policy.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import boto3
|
||||
import json
|
||||
from botocore.client import Config
|
||||
|
||||
s3 = boto3.client('s3',
|
||||
endpoint_url='http://minio:9000',
|
||||
aws_access_key_id='minioadmin',
|
||||
aws_secret_access_key='minioadmin123',
|
||||
config=Config(signature_version='s3v4'),
|
||||
region_name='us-east-1')
|
||||
|
||||
def set_public_policy(bucket_name):
|
||||
policy = {
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Principal": {"AWS": ["*"]},
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": [f"arn:aws:s3:::{bucket_name}/*"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
policy_string = json.dumps(policy)
|
||||
|
||||
try:
|
||||
s3.put_bucket_policy(Bucket=bucket_name, Policy=policy_string)
|
||||
print(f"Bucket '{bucket_name}' siyosati 'Public Read' qilib belgilandi.")
|
||||
except Exception as e:
|
||||
print(f"Xato: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
set_public_policy('media')
|
||||
set_public_policy('static')
|
||||
Reference in New Issue
Block a user