استيراد مشروع Compose
يستورد هذا الشرح العملي مشروع Compose صغيرًا — API، وقاعدة بيانات PostgreSQL التي يحفظ فيها ملاحظاته، وذاكرة تخزين مؤقت Redis — ويبني النتيجة من أجل Docker Compose. ويصوغ الاستيراد مسودة كل ملف يقرؤه البناء، مع نقل ما يذكره ملف Compose: اتصالات API بقاعدة البيانات وذاكرة التخزين المؤقت، ومستخدم قاعدة البيانات واسمها، وكلمة المرور على هيئة إشارة إلى متغير، و tag كل صورة. كل ملف وكل كتلة من الإخراج أدناه مأخوذ من تشغيل فعلي؛ والمشروع وكل مرحلة من الشرح العملي موجودان في المثال import-compose، حيث يشغّل نص فحص برمجي الاستيراد مرة أخرى، ويقارن ما يكتبه وما يطبعه والكتل التي تقتبسها هذه الصفحة بالملفات المحفوظة هناك، ويتحقق مما كتبه الاستيراد ويبنيه.
صُغ مسودة المثال
يمكنك تشغيل هذا الاستيراد بالأوامر الواردة في هذه الصفحة، أو اجعل مساعد الذكاء الاصطناعي يشغّله نيابةً عنك.
اكتب المثال في مجلد جديد يحمل اسمه:
aggrega draft --example import-composeثم يطبع الأوامر التي تُشغَّل بعد ذلك. يدخل الأول إلى المجلد الجديد؛ ويدخل الذي يليه، أدناه، إلى مجلد source الخاص به، الذي يحوي ما تستورده هذه الصفحة وهو المكان الذي يجري فيه الاستيراد:
cd import-compose
المشروع
compose.yaml:
# A small notes service: its API, the PostgreSQL database it keeps the notes in,# and the Redis cache in front of it.name: notes
services: api: image: ghcr.io/example/notes-api:2.3.0 ports: - "8080:8080" environment: DATABASE_HOST: db DATABASE_USER: notes DATABASE_NAME: notes DATABASE_PASSWORD: ${POSTGRES_PASSWORD} CACHE_HOST: cache depends_on: - db - cache db: image: postgres:16 environment: POSTGRES_USER: notes POSTGRES_DB: notes POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} volumes: - db-data:/var/lib/postgresql/data cache: image: redis:7
volumes: db-data:وبجواره، يجعل .aggrega-config.yaml المجلد مساحة عمل، فيكتب الاستيراد مسوداته هناك:
# The workspace key marks this directory as the workspace root, so an import run# here writes its drafts beside compose.yaml.workspace: {}# The registry the importer is fetched from the first time an import needs it.default_registry: hub.aggrega.aiيوازن الاستيراد كل خدمة مقابل Components كتالوج مساحة العمل، ويصوغ --target مسودة Manifest من Recipes الخاصة به. ولا تسمّي مساحة العمل أي مجلد كتالوج، لذا يقرأ الاستيراد كتالوج المكتبة القياسية من registry الذي يجلب منه مستورِده: لقطة موقَّعة من الكتالوج المطروح، يجلبها أول استيراد ويتحقق منها ويحفظها في ذاكرة التخزين المؤقت للمحتوى في مساحة العمل، ويسمّيها بملخّصها في ملف السجل الذي يكتبه الاستيراد بجوار مسوداته. وتقرأ مساحة العمل التي تسمّي مجلدات كتالوج خاصة بها — في AGGREGA_CATALOG_DIRS، أو تحت workspace.catalog_dirs — تلك المجلدات أولًا، واللقطة المطروحة خلفها.
ملف الإجابات
لا يحتاج بناء Docker Compose إلى شيء لا يذكره ملف Compose، لذا لا يطلب هذا الاستيراد شيئًا. أما بناء المشروع نفسه من أجل Kubernetes، مع --target helm، فيحتاج إلى قيمة واحدة لا يحملها أي ملف Compose: اسم Kubernetes Secret الذي تُقرأ منه كلمة مرور قاعدة البيانات، لأن بناء Kubernetes يقرأ كلمة المرور من Secret لا من shell لديك. ويحفظ المثال تلك الإجابة في import-answers.yaml، بجوار compose.yaml، حيث يبحث عنها الاستيراد:
# Values the import could not draft from the source, keyed by the drafted file and the key path inside# it. Write each one beside its key; an import of the same source, and a sync, read them from here and# ask nothing. A secret is never written here: its key asks where the secret comes from — a variable# name, ${NAME}, or the name of a Kubernetes Secret.release.yaml: # The origin of the secret value, written by the operator in the Release. Read exactly as # values.origin is. Write it here or in values.origin, never in both. A secret: name where it comes # from, never the value. spec.components.db.features.password.infra.origin: "notes-db-password"كتب الاستيراد الملف بنفسه، في أول مرة شُغِّل فيها مع --target helm: سرد القيمة التي تعذّر عليه صوغها، ومفتاحها فارغ، وتوقف. والإجابة هي اسم Secret، لا كلمة المرور أبدًا. أما الاستيراد من أجل Docker Compose فلا تُطلب منه قيمة كهذه، ويبقي الإجابة في الملف، ولا يستخدمها. يصف ملف إجابات الاستيراد الملف، والأسئلة التي يطرحها الاستيراد في الطرفية بدلًا منه، وما يفعله حين لا يتوفر أي منهما.
يسرد aggrega import كل علامة يقبلها الأمر، ومنها --answers التي تسمّي ملف إجابات محفوظًا في مكان آخر.
انتقل إلى source:
cd sourceشغّل الاستيراد
aggrega import compose.yaml --target composeيسرد على الإخراج القياسي ما كتبه:
wrote aggregate.yamlwrote release.yamlwrote manifest.yamlwrote .aggrega-import.jsonعلى الخطأ القياسي، حيث يمثّل <workspace> المجلد الذي شغّلته فيه:
[… omitted: a warning that the importer runs unverified, printed where its module is read from beside its Plugin entity in a catalog folder rather than fetched]read as the compose format, with drafted paths relative to <workspace>warn: the value of environment.POSTGRES_USER of node db is carried as written in the drafts: its format marks the field as no credential, though it can carry one, and setting empty_credentials to all writes it emptywarn: the value of environment.POSTGRES_DB of node db is carried as written in the drafts: its format marks the field as no credential, though it can carry one, and setting empty_credentials to all writes it emptymapped api (ghcr.io/example/notes-api) to core/compute/server as a reference, NONEmapped db (postgres) to core/datastore/postgres as a reference, HIGH at 0.9mapped cache (redis) to core/datastore/redis as a reference, HIGH at 0.9Pull complete: <workspace>/release.lock (entities=10, source_artifacts=0, external_inputs=0)
- الأسطر
warnتسمّي كل قيمة تحملها المسودات كما هي، بخدمتها ومتغيرها لا بقيمتها أبدًا: فلا يعلّم Compose أي قيمة بوصفها من بيانات الاعتماد، ويمكن أن تحمل أي منها واحدة. وهنا لا يحمل أي منها شيئًا كهذا. فالمتغيرات التي يقرؤها API من خدمة أخرى لا تُنقل بوصفها قيمًا على الإطلاق — إذ تسلّمها الاتصالات — والمتغيران اللذان يقرآن${POSTGRES_PASSWORD}إشارة إلى متغير لا غير، لذا لا يسمّيهما أي تحذير. - الأسطر
mappedتقول ما صارت إليه كل خدمة. تشغّلapiصورة حاوية خاصة بها، لذا هي مثيل من الخادم العام في المكتبة القياسية،core/compute/server. وتشغّلdbوcacheصورتي الحاوية اللتين تشغّلهما Recipes منصة Compose في المكتبة القياسية من أجلcore/datastore/postgresوcore/datastore/redis؛ وقد طابق كل منهما ذلك Component في الطبقة العليا، عند 0.9، وتشير المسودات إليه. - السطر
Pull completeهو الاستيراد وهو يجلب ما يعتمد عليه Release المسوَّد، كما يفعلaggrega pull، إلىrelease.lock، فتجتاز المسودات التحقق وتُبنى فورًا. وتحت--offlineلا يجلب الاستيراد شيئًا ويسمّي الأمرaggrega pullالذي ينبغي تشغيله.
ما يكتبه
تحمل ملفات Aggregate و Release و Manifest الاسم imported/notes، نسبةً إلى name: الخاص بمشروع Compose — فملف Compose الذي لا يحوي name: يُسمّى باسم ملفه، compose، والدليل الذي يحويه، حين يُستورد بوصفه دليلًا، باسم ذلك الدليل، كما يسمّي Compose مشروعًا — تحت مساحة الأسماء التي يحددها إعداد target_namespace للمستورِد، وهي imported ما لم تحدد غيرها (الإعدادات).
aggregate.yaml — إدخال واحد لكل خدمة، والاتصالات التي تستخلصها بيئة API، والقيم وكلمات المرور التي يضبطها ملف Compose:
# Not carried, since the draft reads the services and the models alone: volumes.apiVersion: aggrega.ai/v1beta1kind: Aggregatemetadata: name: imported/notes version: 0.1.0 description: Draft Aggregate imported from a source in the compose format.spec: components: api: type: core/compute/server features: # For the compose target, the lines below declare database_password on api: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none. database_password: type: core/secret-value values: key: DATABASE_PASSWORD uses: cache: cache: target: cache # The variables the source sets to what cache publishes, delivered from it under the names the source gives them. inject: env: - in: "CACHE_HOST" from: host db: db: target: db # The variables the source sets to what db publishes, delivered from it under the names the source gives them. inject: env: - in: "DATABASE_HOST" from: host - in: "DATABASE_USER" from: user - in: "DATABASE_NAME" from: database db: type: core/datastore/postgres # The values of core/datastore/postgres the source sets as POSTGRES_USER, POSTGRES_DB, the variables its Compose Recipes set from them. values: user: "notes" database: "notes" features: # For the compose target, the lines below declare password on db: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none. password: type: core/secret-value values: key: POSTGRES_PASSWORD cache: type: core/datastore/redisيصبح كل متغيّر من متغيرات API يسمّي خدمة أخرى كتلة inject لذلك الاتصال: ضبط ملف Compose المتغيّر DATABASE_HOST على db، ويسلّم الاتصال host المطروح لقاعدة البيانات تحت ذلك الاسم، فيتبع قاعدة البيانات على أي هدف بدلًا من نسخه. وقد طابق DATABASE_USER و DATABASE_NAME المستخدم وقاعدة البيانات اللذين تُنشأ بهما خدمة PostgreSQL، لذا يُسلَّمان مما تطرحه هي أيضًا، وتصبح هاتان القيمتان values الخاصة بقاعدة البيانات نفسها. وتصبح كل كلمة مرور إرفاق core/secret-value، معلنًا بالمفتاح الذي يُقرأ تحته متغيّرها.
release.yaml — البيئة، وصورة حاوية API ومنفذه، وإصدار محرك كل مخزن بيانات، ومصدر كل كلمة مرور:
apiVersion: aggrega.ai/v1beta1kind: Releasemetadata: name: imported/notes # environment is a drafted default, since no source names the environment this Release deploys to; change it to name the one it deploys to. environment: development version: 0.1.0 description: Draft Release imported from a source in the compose format.spec: aggregate: imported/notes manifest: imported/notes components: # The source also states, for the Release to carry where core/compute/server takes it: # ports: # - "8080:8080" api: infra: image: "ghcr.io/example/notes-api:2.3.0" port: 8080 # Published on this machine's loopback address, 127.0.0.1: reachable from this machine only, by any process running on it. Delete this line to keep the service internal. published_port: 8080 features: database_password: type: core/secret-value infra: # Attached by reference, never as a value: the value api reads as DATABASE_PASSWORD, read from the variable POSTGRES_PASSWORD, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value. origin: "${POSTGRES_PASSWORD}" # The source states, for the Release to carry where core/datastore/postgres takes it: # volumes: # - "db-data:/var/lib/postgresql/data" db: infra: # The tag of the image the source runs, postgres:16, the engine_version of core/datastore/postgres its Compose Recipes run. engine_version: "16" features: password: type: core/secret-value infra: # Attached by reference, never as a value: the password of db, which the source sets as POSTGRES_PASSWORD, read from the variable POSTGRES_PASSWORD, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value. origin: "${POSTGRES_PASSWORD}" cache: infra: # The tag of the image the source runs, redis:7, the engine_version of core/datastore/redis its Compose Recipes run. engine_version: "7"البيئة، development، قيمة افتراضية يكتبها الاستيراد لأن أي ملف Compose لا يسمّي واحدة؛ غيّرها حيث ينشر Release في مكان آخر. وتصبح tags الصور إصدارات المحرك التي تأخذها Components المكتبة القياسية. وقيمة port في API هي المنفذ الذي تستمع إليه حاويته، و published_port هي منفذ المضيف الذي يطرحه إدخال ports فيه، وكلاهما 8080 كما يذكرهما ملف Compose؛ وكما يقول تعليقه، تصل أي عملية على هذا الجهاز إلى API على http://127.0.0.1:8080 ولا يصل إليه شيء آخر، وحذف السطر يبقيه على شبكة المكدس وحدها. وما لا يأخذه أي مفتاح يبقى تعليقًا فوق إدخاله: إدخال ports في المصدر، الذي يحمله port و published_port بالفعل، ووحدة التخزين المسمّاة لقاعدة البيانات.
manifest.yaml — Recipe منصة Compose لكل نوع مكوِّن، والتحقيق الذي يكتب الإشارة إلى متغير كلمة المرور في ملف Compose كما هي مكتوبة:
apiVersion: aggrega.ai/v1beta1kind: Manifestmetadata: name: imported/notes version: 0.1.0spec: mappings: components: type: core/compute/server: compose/server core/datastore/postgres: compose/postgres core/datastore/redis: compose/redis features: type: core/secret-value: compose/dotenv-secretيسجّل release.lock كيانات الكتالوج التي انتهى إليها Release، بحسب الملخّص، أما .aggrega-import.json فهو سجل ما كتبه الاستيراد، الذي يقرؤه aggrega import --sync لاحقًا.
التحقق والبناء
aggrega validate --release release.yamlaggrega build --release release.yaml

يطبع التحقق:
notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)release dependency validation passed: release.yaml (schema-pillar + dependency pillars)ويطبع البناء، حيث يمثّل <workspace> المجلد الذي شغّلته فيه:
notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)[… omitted: a total build time, which differs per run]Build completed: 1 file written to <workspace>/distالسطر notice استشاري: يقبل Feature كلمة المرور قيمة origin من values في Aggregate أو من infra في Release، ويبلّغ الإشعار بأن ورقة واحدة معلنة في كليهما. ولا يغيّر أي حكم.
تقرأ خدمة api في dist/compose/docker-compose.yaml البيئة التي منحها إياها مشروع Compose، التي تسلّمها الاتصالات وإرفاق كلمة المرور:
api: image: ghcr.io/example/notes-api:2.3.0 environment: CACHE_HOST: cache DATABASE_HOST: db DATABASE_NAME: notes DATABASE_PASSWORD: ${POSTGRES_PASSWORD} DATABASE_USER: notes ports: - 127.0.0.1:8080:8080 [… omitted: the labels the build writes on every service, naming the Aggregate, the component, the environment and the compiler's version] networks: - compose depends_on: - cache - db [… omitted: the CPU and memory the server's defaults reserve]وتشغّل قاعدة البيانات وذاكرة التخزين المؤقت صورتي الحاوية اللتين سمّاهما ملف Compose:
db: image: postgres:16 environment: POSTGRES_USER: notes POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_DB: notes [… omitted: the labels, the network and a health check running PostgreSQL's pg_isready] cache: image: redis:7 [… omitted: the labels, the network and a health check running redis-cli ping]يظهر فرقان عن المشروع الذي بدأت به في الملف المصيَّر. فالمنفذ يُطرح على عنوان الحلقة المحلية وحده، كما يطرحه Recipe منصة Compose للخادم العام. ووحدة التخزين المسمّاة التي ركّبها ملف Compose لبيانات قاعدة البيانات لا تظهر في أي مكان: تسردها المسودة في تعليق فوق إدخال قاعدة البيانات، ولا يأخذها أي مفتاح في Component PostgreSQL، ولا تركّب خدمة قاعدة البيانات المصيَّرة أي وحدة تخزين.
مع مساعد الذكاء الاصطناعي
يستطيع مساعد الذكاء الاصطناعي المُعَدّ بأدوات Aggrega أن يفعل الشيء نفسه: الصق هذا الموجِّه فيه.
استخدم أدوات Aggrega فقط، ولا تنشئ أي مجلد بنفسك. إذا كان مجلد مساحة العمل لا يحوي مجلد `import-compose` بعد، فاستدعِ الأداة `draft` مرة واحدة مع ضبط `example` على `import-compose` ومن دون `dir`: فهي تكتب المثال في مجلد `import-compose` جديد، يحوي مجلد `source` فيه ما تستورده هذه الصفحة. ثم استدعِ الأداة `import` مع ضبط `workspace_root` على `import-compose/source`، و `source` على `compose.yaml`، و `target` على `compose`: فهي تقرأ الإجابات التي تحتاج إليها من `import-answers.yaml` بجوار `compose.yaml`، وتكتب مسوداتها في `import-compose/source` وتجري pull لما تحتاج إليه. ثم استدعِ الأداة `validate` مع ضبط `workspace_root` على `import-compose/source` و `release` على `release.yaml`، والأداة `build` مع ضبط `workspace_root` على `import-compose/source`، و `release` على `release.yaml`، و `write` على true. أخبرني بالملفات التي كتبها البناء؛ فهي تحت `import-compose/source/dist/`.افتحه في VS Code
مع تثبيت امتداد Aggrega في Visual Studio Code، افتح المجلد import-compose/source في VS Code، حيث كتب الاستيراد مسوداته. ويحوي المجلد Release واحدًا، هو release.yaml المسوَّد، فيحدده المحرر لك، ويتحقق من المسودات مقابل ذلك Release أثناء تحريرك لها، ويعلّم ما يجده في الملف وفي لوحة Problems. ويرسم Aggrega: Show topology graph، في لوحة الأوامر، مكوِّنات Release والتوصيلات بينها. وتعمل Aggrega: Build و Aggrega: Preview build و Aggrega: Pull على Release نفسه، وكل منها زر أيضًا في لوحة Commands من عرض Aggrega في شريط النشاط؛ ويكتب Build ملفاته في dist/ الخاص بالمجلد. ويغطي استخدام Aggrega في Visual Studio Code الباقي.
شغِّله
تمثّل ghcr.io/example/notes-api:2.3.0 صورة حاوية API الخاصة بك. وهي قيمة نائبة، ويتوقف Docker Compose عندها مع Error Head "https://ghcr.io/v2/example/notes-api/manifests/2.3.0": denied. سمِّ صورة حاوية API الخاصة بك في infra.image ضمن api في release.yaml. وإلى أن يصبح API جاهزًا، يمكن لصورة حاوية تجيب عن HTTP على المنفذ 8080 أن تحل محله. واستخدم التشغيل أدناه mendhak/http-https-echo:39، التي تجيب عن كل طلب بوصف للطلب الذي تلقته:
api: infra: image: "mendhak/http-https-echo:39" port: 8080ابنِ مرة أخرى مع التغيير. لم يعد Release يطابق release.lock الذي كتبه الاستيراد، لذا يحتاج البناء إلى --auto-pull، الذي يحلّه من جديد؛ ومن دونه يتوقف البناء ويسمّي aggrega pull. ويملأ Docker Compose كلتا الإشارتين ${POSTGRES_PASSWORD} من shell لديك، لذا اضبط كلمة المرور قبل أن تبدأ مكدس Compose: فإذا تُركت دون ضبط، يحذّر Compose بالرسالة The "POSTGRES_PASSWORD" variable is not set. Defaulting to a blank string. ويكتب قيمة فارغة مكانها.
aggrega build --release release.yaml --auto-pullexport POSTGRES_PASSWORD='a password of your own'docker compose -p notes -f dist/compose/docker-compose.yaml up -d --waitتسمّي -p المشروع، كما توضح خطوة التشغيل في كيف يعمل الاستيراد. وتعود --wait متى اجتازت قاعدة البيانات وذاكرة التخزين المؤقت فحوص السلامة التي يمنحها لهما البناء وكانت حاوية API تعمل: ولا يمنح البناء API أي فحص سلامة.
اختبره
اسأل API على http://127.0.0.1:8080، العنوان الذي يطرحه published_port:
curl -sS http://127.0.0.1:8080/notes{ "path": "/notes", "headers": { "host": "127.0.0.1:8080", [… omitted: the user agent and the media types the client sent] }, "method": "GET", [… omitted: the rest of the request as the stand-in received it, the client's address and the container's host name]}ما تسلّمه الاتصالات موجود في بيئة خدمة API، تحت الأسماء التي يقرؤها API لديك:
docker compose -p notes -f dist/compose/docker-compose.yaml exec api printenv DATABASE_HOST DATABASE_USER DATABASE_NAME CACHE_HOSTdbnotesnotescacheأوقف مكدس Compose بما يلي:
docker compose -p notes -f dist/compose/docker-compose.yaml downعلى Kubernetes
يُستورد المشروع نفسه من أجل Kubernetes مع --target helm، ويقرأ الاستيراد اسم Secret، notes-db-password، من ملف الإجابات أعلاه. وتوجد مسودات Docker Compose بالفعل في source، لذا صُغ هذه في مجلد خاص بها، وسمِّ صورة حاوية API في release.yaml الخاص بها كما في Docker Compose، وابنِ منه:
aggrega import compose.yaml --target helm --output-dir helmcd helmaggrega build --release release.yaml --auto-pullيكتب البناء ملفات بيان Kubernetes عادية تحت dist/helm/ في جذر مساحة العمل، source، وهو ../dist/helm/ من helm. أنشئ Secret notes-db-password في مساحة الأسماء التي تطبّق عليها، يحمل كلمة المرور تحت مفتاح البيانات value، وطبّق المجلد كما توضح خطوة التشغيل في كيف يعمل الاستيراد. و Service الخاص بخدمة API هو api-default: أعد توجيه منفذ محلي إليه، واترك الأمر يعمل:
kubectl port-forward -n <namespace> svc/api-default 8080:8080من طرفية أخرى، يصل الطلب أعلاه إلى API على العنوان نفسه، دون تغيير، ويجيب البديل بالطريقة نفسها، "host": "127.0.0.1:8080". وتسلّم الاتصالات أسماء Services: يطبع kubectl exec -n <namespace> deploy/api -- printenv DATABASE_HOST DATABASE_USER DATABASE_NAME CACHE_HOST القيم db-default و notes و notes و cache-default. ولا يتلقى API كلمة مرور قاعدة بياناته على Kubernetes بعد: إذ يصل Secret notes-db-password إلى قاعدة البيانات وحدها، لذا لا يُضبط DATABASE_PASSWORD في حاوية api.
عند تغيّر ملف Compose
أبقِ .aggrega-import.json بجوار المسودات، وبعد تغيّر ملف Compose شغّل:
aggrega import compose.yaml --syncلا يغيّر إلا ما كتبه الاستيراد، ويقرأ ملف الإجابات نفسه. والجزء الذي تركه المصدر دون تغيير يبقى كما هو على القرص، عُدِّل أم لا؛ وحيث غيّر المصدر جزءًا عدّلته، يتوقف ويسمّي الجزء ولا يكتب شيئًا، لتسوّيه يدويًا. يصف aggrega import القواعد وكل علامة بالتفصيل.