Skip to content

Commit

Permalink
refactor(cli): keep alteration scripts folder writable by gid 0
Browse files Browse the repository at this point in the history
facilitates running in rootless container with r/w mounted alteration-scripts
directory (fixed #6327)
  • Loading branch information
bpow authored and wangsijie committed Sep 25, 2024
1 parent f5b17f3 commit d848b61
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ RUN rm -rf .scripts pnpm-*.yaml packages/cloud
FROM node:20-alpine as app

Check warning on line 40 in Dockerfile

View workflow job for this annotation

GitHub Actions / main-dockerize

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/
WORKDIR /etc/logto
COPY --from=builder /etc/logto .
RUN mkdir -p /etc/logto/packages/cli/alteration-scripts && chmod g+w /etc/logto/packages/cli/alteration-scripts
EXPOSE 3001
ENTRYPOINT ["npm", "run"]
CMD ["start"]
23 changes: 21 additions & 2 deletions packages/cli/src/commands/database/alteration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,27 @@ export const getAlterationFiles = async (): Promise<AlterationFile[]> => {
}

// We need to copy alteration files to execute in the CLI context to make `slonik` available
await fs.rm(localAlterationDirectory, { force: true, recursive: true });
await fs.cp(alterationDirectory, localAlterationDirectory, { recursive: true });
// Notice that we don't remove the folder,
// this ensures that the writabiliy remains (and also allows this to be a separately-mounted directory.
if (!existsSync(localAlterationDirectory)) {
await fs.mkdir(localAlterationDirectory, { recursive: true });
}

const oldFiles = await fs.readdir(localAlterationDirectory);
await Promise.all(
oldFiles.map(async (file) =>
fs.rm(path.join(localAlterationDirectory, file), { force: true, recursive: true })
)
);
const newFiles = await fs.readdir(alterationDirectory);
await Promise.all(
newFiles.map(async (file) =>
fs.cp(path.join(alterationDirectory, file), path.join(localAlterationDirectory, file), {
recursive: true,
preserveTimestamps: true,
})
)
);

const directory = await fs.readdir(localAlterationDirectory);
const files = directory.filter((file) => alterationFilenameRegex.test(file));
Expand Down

0 comments on commit d848b61

Please sign in to comment.