-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Dockerfile and S3 nfs (#509)
closes #507
- Loading branch information
Showing
6 changed files
with
244 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
logs | ||
node_modules | ||
run | ||
typings | ||
.cnpmcore* | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# https://stackoverflow.com/questions/65612411/forcing-docker-to-use-linux-amd64-platform-by-default-on-macos/69636473#69636473 | ||
FROM node:18 | ||
|
||
# Create app directory | ||
WORKDIR /usr/src/app | ||
|
||
# Install app dependencies | ||
COPY . . | ||
|
||
RUN npm config set registry https://registry.npmmirror.com \ | ||
&& npm install -g npminstall \ | ||
&& npmupdate -c \ | ||
&& npm run tsc | ||
|
||
ENV NODE_ENV=production | ||
ENV EGG_SERVER_ENV=prod | ||
|
||
EXPOSE 7001 | ||
CMD ["npm", "run", "start:foreground"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import assert from 'assert'; | ||
import { randomUUID } from 'crypto'; | ||
import { join } from 'path'; | ||
import { EggAppConfig, PowerPartial } from 'egg'; | ||
import OSSClient from 'oss-cnpm'; | ||
|
@@ -31,7 +32,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = { | |
checkChangesStreamInterval: 500, | ||
changesStreamRegistry: 'https://replicate.npmjs.com', | ||
changesStreamRegistryMode: ChangesStreamMode.streaming, | ||
registry: 'http://localhost:7001', | ||
registry: process.env.CNPMCORE_CONFIG_REGISTRY || 'http://localhost:7001', | ||
alwaysAuth: false, | ||
allowScopes: [ | ||
'@cnpm', | ||
|
@@ -43,7 +44,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = { | |
admins: { | ||
cnpmcore_admin: '[email protected]', | ||
}, | ||
enableWebAuthn: false, | ||
enableWebAuthn: !!process.env.CNPMCORE_CONFIG_ENABLE_WEB_AUTHN, | ||
enableCDN: false, | ||
cdnCacheControlHeader: 'public, max-age=300', | ||
cdnVaryHeader: 'Accept, Accept-Encoding', | ||
|
@@ -58,6 +59,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = { | |
export default (appInfo: EggAppConfig) => { | ||
const config = {} as PowerPartial<EggAppConfig>; | ||
|
||
config.keys = process.env.CNPMCORE_EGG_KEYS || randomUUID(); | ||
config.cnpmcore = cnpmcoreConfig; | ||
|
||
// override config from framework / plugin | ||
|
@@ -118,6 +120,24 @@ export default (appInfo: EggAppConfig) => { | |
'Cache-Control': 'max-age=0, s-maxage=60', | ||
}, | ||
}); | ||
} else if (process.env.CNPMCORE_NFS_TYPE === 's3') { | ||
assert(process.env.CNPMCORE_NFS_S3_CLIENT_ENDPOINT, 'require env CNPMCORE_NFS_S3_CLIENT_ENDPOINT'); | ||
assert(process.env.CNPMCORE_NFS_S3_CLIENT_ID, 'require env CNPMCORE_NFS_S3_CLIENT_ID'); | ||
assert(process.env.CNPMCORE_NFS_S3_CLIENT_SECRET, 'require env CNPMCORE_NFS_S3_CLIENT_SECRET'); | ||
assert(process.env.CNPMCORE_NFS_S3_CLIENT_BUCKET, 'require env CNPMCORE_NFS_S3_CLIENT_BUCKET'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const S3Client = require('s3-cnpmcore'); | ||
config.nfs.client = new S3Client({ | ||
region: process.env.CNPMCORE_NFS_S3_CLIENT_REGION || 'default', | ||
endpoint: process.env.CNPMCORE_NFS_S3_CLIENT_ENDPOINT, | ||
credentials: { | ||
accessKeyId: process.env.CNPMCORE_NFS_S3_CLIENT_ID, | ||
secretAccessKey: process.env.CNPMCORE_NFS_S3_CLIENT_SECRET, | ||
}, | ||
bucket: process.env.CNPMCORE_NFS_S3_CLIENT_BUCKET, | ||
forcePathStyle: !!process.env.CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE, | ||
disableURL: !!process.env.CNPMCORE_NFS_S3_CLIENT_DISABLE_URL, | ||
}); | ||
} | ||
|
||
config.logger = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# 通过 Docker 部署 cnpmcore | ||
|
||
## 构建镜像 | ||
|
||
```bash | ||
docker build -t cnpmcore . | ||
``` | ||
|
||
## 通过环境变量配置参数 | ||
|
||
需要在 docker 容器中配置数据存储参数,否则启动会失败,cnpmcore 镜像要求数据存储与计算分离。 | ||
|
||
### MySQL | ||
|
||
```bash | ||
CNPMCORE_MYSQL_DATABASE=cnpmcore | ||
CNPMCORE_MYSQL_HOST=127.0.0.1 | ||
CNPMCORE_MYSQL_PORT=3306 | ||
CNPMCORE_MYSQL_USER=your-db-user-name | ||
CNPMCORE_MYSQL_PASSWORD=your-db-user-password | ||
``` | ||
|
||
### Redis | ||
|
||
```bash | ||
CNPMCORE_REDIS_HOST=127.0.0.1 | ||
CNPMCORE_REDIS_PORT=6379 | ||
CNPMCORE_REDIS_PASSWORD=your-redis-password | ||
CNPMCORE_REDIS_DB=1 | ||
``` | ||
|
||
### 文件存储 | ||
|
||
目前支持的文件存储服务有阿里云 OSS、AWS S3,以及兼容 S3 的 minio。 | ||
|
||
#### OSS | ||
|
||
```bash | ||
CNPMCORE_NFS_TYPE=oss | ||
CNPMCORE_NFS_OSS_ENDPOINT==https://your-oss-endpoint | ||
CNPMCORE_NFS_OSS_BUCKET=your-bucket-name | ||
CNPMCORE_NFS_OSS_ID=oss-ak | ||
CNPMCORE_NFS_OSS_SECRET=oss-sk | ||
``` | ||
|
||
#### S3 / minio | ||
|
||
```bash | ||
CNPMCORE_NFS_TYPE=s3 | ||
CNPMCORE_NFS_S3_CLIENT_ENDPOINT=https://your-s3-endpoint | ||
CNPMCORE_NFS_S3_CLIENT_BUCKET=your-bucket-name | ||
CNPMCORE_NFS_S3_CLIENT_ID=s3-ak | ||
CNPMCORE_NFS_S3_CLIENT_SECRET=s3-sk | ||
CNPMCORE_NFS_S3_CLIENT_DISABLE_URL=true | ||
``` | ||
|
||
如果使用的是 minio,请务必设置 `CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE=true` | ||
|
||
```bash | ||
CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE=true | ||
``` | ||
|
||
### 日志 | ||
|
||
```bash | ||
CNPMCORE_LOG_DIR=/var/log/cnpmcore | ||
``` | ||
|
||
### registry 域名 | ||
|
||
```bash | ||
CNPMCORE_CONFIG_REGISTRY=https://your-registry.com | ||
``` | ||
|
||
### 使用 `config.prod.js` 覆盖 | ||
|
||
直接覆盖 `/usr/src/app/config/config.prod.js` 文件也可以实现生产配置自定义。 | ||
|
||
```js | ||
module.exports = { | ||
cnpmcore: { | ||
registry: 'https://your-registry.com', | ||
enableWebAuthn: true, | ||
}, | ||
orm: { | ||
database: 'cnpmcore', | ||
host: '127.0.0.1', | ||
port: 3306, | ||
user: 'your-db-user-name', | ||
password: 'your-db-user-password', | ||
}, | ||
redis: { | ||
client: { | ||
port: 6379, | ||
host: '127.0.0.1', | ||
password: 'your-redis-password', | ||
db: 1, | ||
}, | ||
}, | ||
nfs: { | ||
client: new (require('s3-cnpmcore'))({ | ||
region: 'default', | ||
endpoint: 'https://your-s3-endpoint', | ||
credentials: { | ||
accessKeyId: 's3-ak', | ||
secretAccessKey: 's3-sk', | ||
}, | ||
bucket: 'your-bucket-name', | ||
forcePathStyle: true, | ||
disableURL: true, | ||
}), | ||
}, | ||
logger: { | ||
dir: '/var/log/cnpmcore', | ||
}, | ||
}; | ||
``` | ||
|
||
通过 docker volumes 设置配置文件 | ||
|
||
```bash | ||
docker run -p 7001:7001 -it --rm \ | ||
-v /path-to/config.prod.js:/usr/src/app/config/config.prod.js \ | ||
--name cnpmcore-prod cnpmcore | ||
``` | ||
|
||
## 运行容器 | ||
|
||
```bash | ||
docker run -p 7001:7001 -it --rm \ | ||
-e CNPMCORE_CONFIG_REGISTRY=https://your-registry.com \ | ||
-e CNPMCORE_MYSQL_DATABASE=cnpmcore \ | ||
-e CNPMCORE_MYSQL_HOST=127.0.0.1 \ | ||
-e CNPMCORE_MYSQL_PORT=3306 \ | ||
-e CNPMCORE_MYSQL_USER=your-db-user-name \ | ||
-e CNPMCORE_MYSQL_PASSWORD=your-db-user-password \ | ||
-e CNPMCORE_NFS_TYPE=s3 \ | ||
-e CNPMCORE_NFS_S3_CLIENT_ENDPOINT=https://your-s3-endpoint \ | ||
-e CNPMCORE_NFS_S3_CLIENT_BUCKET=your-bucket-name \ | ||
-e CNPMCORE_NFS_S3_CLIENT_ID=s3-ak \ | ||
-e CNPMCORE_NFS_S3_CLIENT_SECRET=s3-sk \ | ||
-e CNPMCORE_NFS_S3_CLIENT_FORCE_PATH_STYLE=true \ | ||
-e CNPMCORE_NFS_S3_CLIENT_DISABLE_URL=true \ | ||
-e CNPMCORE_REDIS_HOST=127.0.0.1 \ | ||
-e CNPMCORE_REDIS_PORT=6379 \ | ||
-e CNPMCORE_REDIS_PASSWORD=your-redis-password \ | ||
-e CNPMCORE_REDIS_DB=1 \ | ||
--name cnpmcore-prod cnpmcore | ||
``` | ||
|
||
## 演示地址 | ||
|
||
https://registry-demo.fengmk2.com:9443 | ||
|
||
管理员账号:cnpmcore_admin/12345678 | ||
|
||
通过 npm login 可以登录 | ||
|
||
```bash | ||
npm login --registry=https://registry-demo.fengmk2.com:9443 | ||
``` | ||
|
||
查看当前登录用户 | ||
|
||
```bash | ||
npm whoami --registry=https://registry-demo.fengmk2.com:9443 | ||
``` | ||
|
||
## fengmk2/cnpmcore 镜像 | ||
|
||
https://hub.docker.com/r/fengmk2/cnpmcore | ||
|
||
```bash | ||
docker pull fengmk2/cnpmcore | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters