Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit 8b54dd7

Browse files
bors[bot]Tobin C. Harding
andauthored
Merge #1711
1711: Add data directory configuration option r=tcharding a=tcharding Currently we have `--seed-file` for the seed file and a config option for the database. Both of these files default to the systems data directory with `comit/` appended. It makes more sense to configure these two items in the same fashion. Add configuration option ``` [Data] dir = path/to/data/dir ``` Use this path with `cnd.sqlite` and `seed.pem` appended for the database and seed file respectively. Remove `--seed-file` command line option and the database configuration option. Resolves: #1583 Co-authored-by: Tobin C. Harding <[email protected]>
2 parents beb0343 + a99f70f commit 8b54dd7

File tree

13 files changed

+147
-102
lines changed

13 files changed

+147
-102
lines changed

api_tests/lib/cnd_runner.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from "fs";
2+
import * as path from "path";
23
import { promisify } from "util";
34
import { CndInstance } from "../lib_sdk/cnd_instance";
45
import { CND_CONFIGS } from "./config";
@@ -43,8 +44,10 @@ export class CndRunner {
4344
ledgerConfig
4445
);
4546

46-
if (await existsAsync(cndconfig.dbPath)) {
47-
await unlinkAsync(cndconfig.dbPath); // delete the old database for the new test
47+
const db = path.join(cndconfig.data, "cnd.sqlite");
48+
49+
if (await existsAsync(db)) {
50+
await unlinkAsync(db); // delete the old database for the new test
4851
}
4952

5053
await process.start();

api_tests/lib/config.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import tempfile from "tempfile";
1+
import * as tmp from "tmp";
22
import { BitcoinNodeConfig } from "./bitcoin";
33
import { EthereumNodeConfig } from "./ethereum";
44
import { LedgerConfig } from "./ledger_runner";
55

66
export interface CndConfigFile {
77
http_api: HttpApi;
8-
database?: { sqlite: string };
8+
data?: { dir: string };
99
network: { listen: string[] };
1010
}
1111

@@ -15,7 +15,7 @@ export interface HttpApi {
1515

1616
export class E2ETestActorConfig {
1717
public readonly seed: Uint8Array;
18-
public readonly dbPath: string;
18+
public readonly data: string;
1919

2020
constructor(
2121
public readonly httpApiPort: number,
@@ -26,7 +26,11 @@ export class E2ETestActorConfig {
2626
this.httpApiPort = httpApiPort;
2727
this.comitPort = comitPort;
2828
this.seed = new Uint8Array(Buffer.from(seed, "hex"));
29-
this.dbPath = tempfile(`.${this.name}.sqlite`);
29+
30+
const tmpobj = tmp.dirSync();
31+
tmpobj.removeCallback(); // Manual cleanup
32+
33+
this.data = tmpobj.name;
3034
}
3135

3236
public generateCndConfigFile(ledgerConfig: LedgerConfig): CndConfigFile {
@@ -37,8 +41,8 @@ export class E2ETestActorConfig {
3741
port: this.httpApiPort,
3842
},
3943
},
40-
database: {
41-
sqlite: this.dbPath,
44+
data: {
45+
dir: this.data,
4246
},
4347
network: {
4448
listen: [`/ip4/0.0.0.0/tcp/${this.comitPort}`],

api_tests/lib_sdk/cnd_instance.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { JsonMap, stringify } from "@iarna/toml";
22
import { ChildProcess, spawn } from "child_process";
33
import * as fs from "fs";
4-
import { PEMObject } from "pem-ts";
54
import tempWrite from "temp-write";
65
import { promisify } from "util";
76
import { CndConfigFile, E2ETestActorConfig } from "../lib/config";
@@ -39,27 +38,20 @@ export class CndInstance {
3938
"config.toml"
4039
);
4140

42-
const pemObject = new PEMObject("SEED", this.actorConfig.seed);
43-
const seedFile = await tempWrite(pemObject.encoded, "seed.pem");
44-
45-
this.process = spawn(
46-
bin,
47-
["--config", configFile, "--seed-file", seedFile],
48-
{
49-
cwd: this.projectRoot,
50-
stdio: [
51-
"ignore", // stdin
52-
await openAsync(
53-
this.logDir + "/cnd-" + this.actorConfig.name + ".log",
54-
"w"
55-
), // stdout
56-
await openAsync(
57-
this.logDir + "/cnd-" + this.actorConfig.name + ".log",
58-
"w"
59-
), // stderr
60-
],
61-
}
62-
);
41+
this.process = spawn(bin, ["--config", configFile], {
42+
cwd: this.projectRoot,
43+
stdio: [
44+
"ignore", // stdin
45+
await openAsync(
46+
this.logDir + "/cnd-" + this.actorConfig.name + ".log",
47+
"w"
48+
), // stdout
49+
await openAsync(
50+
this.logDir + "/cnd-" + this.actorConfig.name + ".log",
51+
"w"
52+
), // stderr
53+
],
54+
});
6355

6456
this.process.on("exit", (code: number, signal: number) => {
6557
console.log(

api_tests/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"rimraf": "^3.0.0",
5656
"satoshi-bitcoin": "^1.0.4",
5757
"temp-write": "^4.0.0",
58-
"tempfile": "^3.0.0",
5958
"testcontainers": "^2.1.0",
59+
"tmp": "^0.1.0",
6060
"ts-node": "^8.5.4",
6161
"tslint": "^5.20.1",
6262
"tslint-config-prettier": "^1.18.0",
@@ -67,5 +67,8 @@
6767
"prettier": {
6868
"trailingComma": "es5",
6969
"tabWidth": 4
70+
},
71+
"devDependencies": {
72+
"@types/tmp": "^0.1.0"
7073
}
7174
}

api_tests/yarn.lock

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
dependencies:
155155
tempfile "*"
156156

157+
"@types/tmp@^0.1.0":
158+
version "0.1.0"
159+
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.1.0.tgz#19cf73a7bcf641965485119726397a096f0049bd"
160+
integrity sha512-6IwZ9HzWbCq6XoQWhxLpDjuADodH/MKXRUIDFudvgjcVdjFknvmR+DNsoUeer4XPrEnrZs04Jj+kfV9pFsrhmA==
161+
157162
"@types/tv4@*":
158163
version "1.2.29"
159164
resolved "https://registry.yarnpkg.com/@types/tv4/-/tv4-1.2.29.tgz#4c6d2222b03245dd2104f4fd67f54d1658985911"
@@ -2557,6 +2562,13 @@ rfdc@^1.1.4:
25572562
resolved "https://registry.yarnpkg.com/rfdc/-/rfdc-1.1.4.tgz#ba72cc1367a0ccd9cf81a870b3b58bd3ad07f8c2"
25582563
integrity sha512-5C9HXdzK8EAqN7JDif30jqsBzavB7wLpaubisuQIGHWf2gUXSpzy6ArX/+Da8RjFpagWsCn+pIgxTMAmKw9Zug==
25592564

2565+
rimraf@^2.6.3:
2566+
version "2.7.1"
2567+
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
2568+
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
2569+
dependencies:
2570+
glob "^7.1.3"
2571+
25602572
rimraf@^3.0.0:
25612573
version "3.0.0"
25622574
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
@@ -2914,7 +2926,7 @@ temp-write@^4.0.0:
29142926
temp-dir "^1.0.0"
29152927
uuid "^3.3.2"
29162928

2917-
tempfile@*, tempfile@^3.0.0:
2929+
tempfile@*:
29182930
version "3.0.0"
29192931
resolved "https://registry.yarnpkg.com/tempfile/-/tempfile-3.0.0.tgz#5376a3492de7c54150d0cc0612c3f00e2cdaf76c"
29202932
integrity sha512-uNFCg478XovRi85iD42egu+eSFUmmka750Jy7L5tfHI5hQKKtbPnxaSaXAbBqCDYrw3wx4tXjKwci4/QmsZJxw==
@@ -2974,6 +2986,13 @@ tiny-secp256k1@^1.1.0, tiny-secp256k1@^1.1.1:
29742986
elliptic "^6.4.0"
29752987
nan "^2.13.2"
29762988

2989+
tmp@^0.1.0:
2990+
version "0.1.0"
2991+
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.1.0.tgz#ee434a4e22543082e294ba6201dcc6eafefa2877"
2992+
integrity sha512-J7Z2K08jbGcdA1kkQpJSqLF6T0tdQqpR2pnSUXsIchbPdTI9v3e85cLW0d6WDhwuAleOV71j2xWs8qMPfK7nKw==
2993+
dependencies:
2994+
rimraf "^2.6.3"
2995+
29772996
to-buffer@^1.1.1:
29782997
version "1.1.1"
29792998
resolved "https://registry.yarnpkg.com/to-buffer/-/to-buffer-1.1.1.tgz#493bd48f62d7c43fcded313a03dcadb2e1213a80"

cnd/src/cli.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ use std::path::PathBuf;
33
#[derive(structopt::StructOpt, Debug)]
44
#[structopt(name = "COMIT network daemon")]
55
pub struct Options {
6-
/// Path to configuration file.
6+
/// Path to configuration file
77
#[structopt(short = "c", long = "config", parse(from_os_str))]
88
pub config_file: Option<PathBuf>,
99

10-
/// Path to secret seed file.
11-
#[structopt(short = "s", long = "seed-file", parse(from_os_str))]
12-
pub seed_file: Option<PathBuf>,
13-
14-
/// Dump the current configuration and exit.
10+
/// Dump the current configuration and exit
1511
#[structopt(long = "dump-config")]
1612
pub dump_config: bool,
1713
}

cnd/src/config/file.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::{Bitcoin, Database, Ethereum, Network, Socket};
1+
use crate::config::{Bitcoin, Data, Ethereum, Network, Socket};
22
use config as config_rs;
33
use log::LevelFilter;
44
use std::{ffi::OsStr, path::Path};
@@ -12,7 +12,7 @@ use std::{ffi::OsStr, path::Path};
1212
pub struct File {
1313
pub network: Option<Network>,
1414
pub http_api: Option<HttpApi>,
15-
pub database: Option<Database>,
15+
pub data: Option<Data>,
1616
pub logging: Option<Logging>,
1717
pub bitcoin: Option<Bitcoin>,
1818
pub ethereum: Option<Ethereum>,
@@ -23,7 +23,7 @@ impl File {
2323
File {
2424
network: Option::None,
2525
http_api: Option::None,
26-
database: Option::None,
26+
data: Option::None,
2727
logging: Option::None,
2828
bitcoin: Option::None,
2929
ethereum: Option::None,
@@ -160,8 +160,8 @@ port = 8000
160160
[http_api.cors]
161161
allowed_origins = "all"
162162
163-
[database]
164-
sqlite = "/tmp/foobar.sqlite"
163+
[data]
164+
dir = "/tmp/comit/"
165165
166166
[logging]
167167
level = "DEBUG"
@@ -188,8 +188,8 @@ node_url = "http://example.com/"
188188
allowed_origins: AllowedOrigins::All(All::All),
189189
}),
190190
}),
191-
database: Some(Database {
192-
sqlite: PathBuf::from("/tmp/foobar.sqlite"),
191+
data: Some(Data {
192+
dir: PathBuf::from("/tmp/comit/"),
193193
}),
194194
logging: Some(Logging {
195195
level: Some(LevelFilter::Debug),

cnd/src/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{net::IpAddr, path::PathBuf};
99
pub use self::{file::File, settings::Settings};
1010

1111
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
12-
pub struct Database {
13-
pub sqlite: PathBuf,
12+
pub struct Data {
13+
pub dir: PathBuf,
1414
}
1515

1616
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]

cnd/src/config/settings.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use crate::config::{file, Bitcoin, Database, Ethereum, File, Network, Socket};
1+
use crate::config::{file, Bitcoin, Data, Ethereum, File, Network, Socket};
22
use anyhow::Context;
33
use log::LevelFilter;
44
use reqwest::Url;
5-
use std::{
6-
net::{IpAddr, Ipv4Addr},
7-
path::Path,
8-
};
5+
use std::net::{IpAddr, Ipv4Addr};
96

107
/// This structs represents the settings as they are used through out the code.
118
///
@@ -17,7 +14,7 @@ use std::{
1714
pub struct Settings {
1815
pub network: Network,
1916
pub http_api: HttpApi,
20-
pub database: Database,
17+
pub data: Data,
2118
pub logging: Logging,
2219
pub bitcoin: Bitcoin,
2320
pub ethereum: Ethereum,
@@ -28,7 +25,7 @@ impl From<Settings> for File {
2825
let Settings {
2926
network,
3027
http_api: HttpApi { socket, cors },
31-
database,
28+
data,
3229
logging: Logging { level, structured },
3330
bitcoin,
3431
ethereum,
@@ -46,7 +43,7 @@ impl From<Settings> for File {
4643
},
4744
}),
4845
}),
49-
database: Some(database),
46+
data: Some(data),
5047
logging: Some(file::Logging {
5148
level: Some(level),
5249
structured: Some(structured),
@@ -108,7 +105,7 @@ impl Settings {
108105
let File {
109106
network,
110107
http_api,
111-
database,
108+
data,
112109
logging,
113110
bitcoin,
114111
ethereum,
@@ -143,14 +140,14 @@ impl Settings {
143140
HttpApi { socket, cors }
144141
})
145142
.unwrap_or_default(),
146-
database: {
147-
let default_database_path = crate::data_dir()
148-
.map(|dir| Path::join(&dir, "cnd.sqlite"))
149-
.context("unable to determine default database path")?;
150-
database.unwrap_or_else(|| Database {
151-
sqlite: default_database_path,
143+
data: {
144+
let default_data_dir =
145+
crate::data_dir().context("unable to determine default data path")?;
146+
data.unwrap_or_else(|| Data {
147+
dir: default_data_dir,
152148
})
153149
},
150+
154151
logging: {
155152
let Logging {
156153
level: default_level,

cnd/src/db/mod.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ use crate::{
2525
swap_protocols::{Role, SwapId},
2626
};
2727
use diesel::{self, prelude::*, sqlite::SqliteConnection};
28-
use std::{path::Path, sync::Arc};
28+
use std::{
29+
ffi::OsStr,
30+
path::{Path, PathBuf},
31+
sync::Arc,
32+
};
2933

3034
/// This module provides persistent storage by way of Sqlite.
3135
@@ -39,15 +43,28 @@ pub struct Sqlite {
3943
impl Sqlite {
4044
/// Return a handle that can be used to access the database.
4145
///
42-
/// When this returns an Sqlite database exists at 'path', a
43-
/// successful connection to the database has been made, and
44-
/// the database migrations have been run.
45-
pub fn new(path: &Path) -> anyhow::Result<Self> {
46-
ensure_folder_tree_exists(path)?;
46+
/// When this returns, an Sqlite database file 'cnd.sql' exists in 'dir', a
47+
/// successful connection to the database has been made, and the database
48+
/// migrations have been run.
49+
pub fn new_in_dir<D: AsRef<OsStr>>(dir: D) -> anyhow::Result<Self> {
50+
let dir = Path::new(&dir);
51+
let path = db_path_from_dir(dir);
52+
Sqlite::new(&path)
53+
}
54+
55+
/// Return a handle that can be used to access the database.
56+
///
57+
/// Reads or creates an SQLite database file at 'file'. When this returns
58+
/// an Sqlite database exists, a successful connection to the database has
59+
/// been made, and the database migrations have been run.
60+
pub fn new(file: &Path) -> anyhow::Result<Self> {
61+
ensure_folder_tree_exists(file)?;
4762

48-
let connection = SqliteConnection::establish(&format!("file:{}", path.display()))?;
63+
let connection = SqliteConnection::establish(&format!("file:{}", file.display()))?;
4964
embedded_migrations::run(&connection)?;
5065

66+
log::info!("SQLite database file: {}", file.display());
67+
5168
Ok(Sqlite {
5269
connection: Arc::new(async_std::sync::Mutex::new(connection)),
5370
})
@@ -86,6 +103,12 @@ impl Sqlite {
86103
}
87104
}
88105

106+
// Construct an absolute path to the database file using 'dir' as the base.
107+
fn db_path_from_dir(dir: &Path) -> PathBuf {
108+
let path = dir.to_path_buf();
109+
path.join("cnd.sqlite")
110+
}
111+
89112
fn ensure_folder_tree_exists(path: &Path) -> anyhow::Result<()> {
90113
if let Some(parent) = path.parent() {
91114
std::fs::create_dir_all(parent)?;

0 commit comments

Comments
 (0)