Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/compose-mount-nostr-config-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nostream": patch
---

`nostream start` now mounts the configuration directory it prepared settings in (`NOSTR_CONFIG_DIR`) into the relay container. The compose file mounted `.nostr` in the project whatever `NOSTR_CONFIG_DIR` was, so a relay started with another directory read the default settings from the project instead, without the admission restrictions saved in the directory the CLI had set up. A plain `docker compose up` still mounts `.nostr` in the project.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ walkthrough, see the development quick start in
## Configuration

You can change the default folder by setting the `NOSTR_CONFIG_DIR` environment variable to a different path.
The `nostream` CLI mounts that folder into the relay container, so `nostream start` runs with the settings in it.

Run nostream using one of the quick-start guides at least once and `nostream/.nostr/settings.yaml` will be created.
Any changes made to the settings file will be read on the next start.
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ services:
# DEBUG: "knex:query"
user: node:node
volumes:
- ${PWD}/.nostr:/home/node/.nostr
# The nostream CLI sets NOSTR_CONFIG_HOST_DIR to the directory it prepared settings in, so the relay reads those
# settings. A separate name keeps this off NOSTR_CONFIG_DIR, which here is the path inside the container.
- ${NOSTR_CONFIG_HOST_DIR:-${PWD}/.nostr}:/home/node/.nostr
ports:
- 127.0.0.1:8008:8008
depends_on:
Expand Down
11 changes: 8 additions & 3 deletions src/cli/utils/docker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
import os from 'os'
import { join } from 'path'
import { join, resolve } from 'path'

import { getProjectPath } from './paths'
import { getConfigBaseDir, getProjectPath } from './paths'
import { runCommand } from './process'

export type ComposeOptions = {
Expand All @@ -26,9 +26,14 @@ export const buildComposeArgs = (files: string[], args: string[]): string[] => {
return [...out, ...args]
}

// The directory the CLI prepares settings in (NOSTR_CONFIG_DIR, or .nostr in the project) on the host. The compose file
// mounts it into the relay under another name, because NOSTR_CONFIG_DIR is the path inside the container there, so a
// relay started from a different configuration directory does not read the defaults from the project's .nostr instead.
export const composeConfigEnv = (): NodeJS.ProcessEnv => ({ NOSTR_CONFIG_HOST_DIR: resolve(getConfigBaseDir()) })

export const runDockerCompose = async ({ files, args, env }: ComposeOptions): Promise<number> => {
const composeArgs = buildComposeArgs(files, args)
return runCommand('docker', ['compose', ...composeArgs], { env })
return runCommand('docker', ['compose', ...composeArgs], { env: { ...composeConfigEnv(), ...env } })
}

export const createPortOverrideComposeFile = (port: number): string => {
Expand Down
69 changes: 67 additions & 2 deletions test/unit/cli/docker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { expect } from 'chai'
import fs from 'fs'
import yaml from 'js-yaml'
import os from 'os'
import path from 'path'
import sinon from 'sinon'

import { buildComposeArgs, createPortOverrideComposeFile } from '../../../src/cli/utils/docker'
import * as processUtils from '../../../src/cli/utils/process'
import {
buildComposeArgs,
composeConfigEnv,
createPortOverrideComposeFile,
runDockerCompose,
} from '../../../src/cli/utils/docker'

describe('cli docker utils', () => {
afterEach(() => {
sinon.restore()
})

it('includes only existing compose files', () => {
const existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((input) => String(input).includes('docker-compose.yml'))
const existsSyncStub = sinon
.stub(fs, 'existsSync')
.callsFake((input) => String(input).includes('docker-compose.yml'))

const args = buildComposeArgs(['docker-compose.yml', 'docker-compose.tor.yml'], ['up'])

Expand All @@ -31,4 +40,60 @@ describe('cli docker utils', () => {

fs.unlinkSync(tempFile)
})

describe('the configuration directory the relay is started with', () => {
let previousConfigDir: string | undefined

beforeEach(() => {
previousConfigDir = process.env.NOSTR_CONFIG_DIR
})

afterEach(() => {
if (previousConfigDir === undefined) {
delete process.env.NOSTR_CONFIG_DIR
} else {
process.env.NOSTR_CONFIG_DIR = previousConfigDir
}
})

it('is the one NOSTR_CONFIG_DIR names', () => {
process.env.NOSTR_CONFIG_DIR = '/srv/relay-conf'

expect(composeConfigEnv()).to.deep.equal({ NOSTR_CONFIG_HOST_DIR: '/srv/relay-conf' })
})

it('is an absolute path, since the compose file mounts it', () => {
process.env.NOSTR_CONFIG_DIR = 'conf/relay'

expect(composeConfigEnv()).to.deep.equal({ NOSTR_CONFIG_HOST_DIR: path.resolve('conf/relay') })
})

it('is .nostr in the project when NOSTR_CONFIG_DIR is not set', () => {
delete process.env.NOSTR_CONFIG_DIR

expect(composeConfigEnv()).to.deep.equal({ NOSTR_CONFIG_HOST_DIR: path.join(process.cwd(), '.nostr') })
})

it('reaches docker compose next to the variables the command adds', async () => {
process.env.NOSTR_CONFIG_DIR = '/srv/relay-conf'
const runCommandStub = sinon.stub(processUtils, 'runCommand').resolves(0)

await runDockerCompose({ files: [], args: ['up'], env: { DEBUG: 'primary:*' } })

expect(runCommandStub.firstCall.args[0]).to.equal('docker')
expect(runCommandStub.firstCall.args[2]).to.deep.equal({
env: { NOSTR_CONFIG_HOST_DIR: '/srv/relay-conf', DEBUG: 'primary:*' },
})
})
})

it('mounts the configuration directory the CLI prepared into the relay, and .nostr when there is none', () => {
const compose = yaml.load(fs.readFileSync(path.join(process.cwd(), 'docker-compose.yml'), 'utf-8')) as {
services: { nostream: { volumes: string[] } }
}

const mounts = compose.services.nostream.volumes.filter((volume) => volume.endsWith(':/home/node/.nostr'))

expect(mounts).to.deep.equal(['${NOSTR_CONFIG_HOST_DIR:-${PWD}/.nostr}:/home/node/.nostr'])
})
})
Loading