巴拉纳- 组成- parser
围绕脚本打包 编曲 解析并验证 Docker 编译用于 Balena 应用程序的文件.
概览
该项目提供了一个Node.js库,它使用Go 二进制(由作曲-go所建)来解析多克作曲文件并使它们与balena相通使用. 它处理:
- 解析标准 Docker 编译文件
- 验证具体巴莱纳的限制
- 将作曲配置转换为相容巴莱纳格式
- 将构件转化为建筑物/推土机图像描述符
所需资源
- 节点.js 20+
- Go 1.24+(用于当地建筑)
安装
npm i @balena/compose-parser
建筑
该项目由两个主要部分组成:
- 前进二进制 (单位:千美元)
lib/main.go:环绕作曲的包装器,输出结构化的JSON - 类型脚本库 (单位:千美元)
lib/: 调用 Go 二进制和处理结果的 Node.js 库
大楼
# Build everything
npm run build
# Build Go binary only
npm run build:go
测试
单位测试
npm run test
运行 TypeScript 单元测试,直接测试库功能. 这不涵盖全部功能,因为由于CI的限制,使用Go二进制的测试在多克进行.
整合测试
npm run test:compose
# Or the following if running build:go first:
npm run test:integration
集成测试验证了剖析输入编解文件的编解-去包装器的功能. npm run test:compose 用于在 Docker 中运行测试,但如果一个构建的 Go 二进制可用,则可以直接运行测试 npm run build:go。 。 。 .
开发脚本
# Check for linting issues
npm run lint
# Fix auto-fixable linting issues
npm run lint-fix
API 密码
基本用途
import { parse, defaultComposition, toImageDescriptors } from '@balena/compose-parser';
// Parse a compose file
const composition = await parse('docker-compose.yml');
// Parse multiple compose files (later files override earlier ones)
const composition = await parse([
'docker-compose.yml',
'docker-compose.override.yml'
]);
// Convert composition to image descriptors for building/pulling
const imageDescriptors = toImageDescriptors(composition);
分析选项
默认 parse() 在您命名的作曲文件外, 读取任何文件 。 不是这个
进程的环境,而不是 env_file, (中文(简体) ), label_file, (中文(简体) ), include 或者说
extends.file。 。 。 .
// Written by whoever is running this process
const trusted = await parse('docker-compose.yml', { hostEnvironment: true });
// Someone else's file. Keep the defaults, and skip interpolation if it has been
// interpolated already
const untrusted = await parse('docker-compose.yml', { skipInterpolation: true });
| 选项 | 默认 | 生效时 |
|---|---|---|
hostEnvironment | false | ${VAR} 和无值 environment 从此进程环境中解析条目,以及 env_file, (中文(简体) ), label_file, (中文(简体) ), include 财务报告和审定财务报表 extends.file 被跟踪并折叠入 |
skipInterpolation | false | ${VAR} 参考文献为左逐字记录 |
两种选择涵盖不同的东西,这就是为什么它们是分开的.
environment:
- SOME_VARIABLE # valueless, resolved by name, hostEnvironment
- OTHER=${SOME_VARIABLE} # substitution, skipInterpolation
替换仍然发生在默认情况下, 相对于空环境,所以
${VAR} 成为空字符串,同时 ${VAR:-fallback} 给它的倒置.
这使得一个写作文件 围绕默认工作.
hostEnvironment 也允许二次文件, 并且没有限制其位置
点。 一个不信任的文件可以命名此进程可以读取的任何路径, 包括
/proc/<pid>/environ留下它就是 保持解析的药效.
两种默认在 v1. 0. 0 之前有效, 所以一个呼叫者依赖其中之一 现在必须这么说.
序列化
serialize() 写一个解析成单自成一体的成分
docker-compose.yml。与上面的解析选项对等以移动一个作曲
从一个能够解决它的机器到一个不能尝试的机器.
import * as fs from 'node:fs/promises';
import { parse, serialize } from '@balena/compose-parser';
// On the user's machine: resolve everything against their environment and files
const composition = await parse('docker-compose.yml', { hostEnvironment: true });
await fs.writeFile('folded.yml', serialize(composition));
// On the builder: re-read it without touching the environment or the filesystem
const reparsed = await parse('folded.yml', { skipInterpolation: true });
// deep-equals `composition`
输出没有 env_file, (中文(简体) ), label_file, (中文(简体) ), include 或者说 extends,从
parse() 已经叠入.
无论读取输出 必须 分析一下 skipInterpolation: true。 。 。 .
数值已经插入到第一个剖析中,a $ 还在
输出是字面意思;再次插入会改变它,或从
读者自己的环境.
在视窗上, serialize() 写入 build.context 与 / 分离器,以及
如果上下文位于与作曲文件不同的驱动器上,则丢出.
图像描述符
这个 toImageDescriptors() 函数将一个构成转换为可用于图像操作的描述符 :
const descriptors = toImageDescriptors(composition);
// Returns array of:
// {
// serviceName: string,
// image: string | BuildConfig,
// contract?: ContractObject // if service has contract requirement labels
// }
巴莱纳特定地物
合同标签
支持验证合同要求标签:
io.balena.features.requires.sw.supervisor: 主管版本的 semver 范围io.balena.features.requires.hw.device-type: 设备类型io.balena.features.requires.arch.sw: 建筑(aarch64, (中文(简体) ),amd64, (中文(简体) ),armv7hf页:1io.balena.features.requires.sw.l4t: L4T 版本范围