Skip to content

Commit

Permalink
Pass network to deploy scripts (ordinals#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Aug 23, 2022
1 parent c2c2c0b commit 71b27e0
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 77 deletions.
2 changes: 1 addition & 1 deletion deploy/bitcoind.service
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/bitcoind \
-datadir=/var/lib/bitcoind \
-signet \
-chain=${CHAIN} \
-txindex

# Make sure the config directory is readable by the service user
Expand Down
5 changes: 3 additions & 2 deletions deploy/checkout
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
set -euxo pipefail

BRANCH=$1
DOMAIN=$2
CHAIN=$2
DOMAIN=$3

if [[ ! -d ord ]]; then
git clone https://github.com/casey/ord.git
Expand All @@ -14,4 +15,4 @@ cd ord
git fetch origin
git checkout -B $BRANCH
git reset --hard origin/$BRANCH
./deploy/setup $DOMAIN
./deploy/setup $CHAIN $DOMAIN
2 changes: 1 addition & 1 deletion deploy/ord.service
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ExecStart=/usr/local/bin/ord \
--cookie-file /var/lib/bitcoind/signet/.cookie \
--data-dir /var/lib/ord \
--max-index-size 1TiB \
--network signet \
--chain ${CHAIN} \
server \
--acme-cache /var/lib/ord/acme-cache \
--acme-contact mailto:[email protected] \
Expand Down
14 changes: 11 additions & 3 deletions deploy/setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

set -euxo pipefail

CHAIN=$1
DOMAIN=$2

touch ~/.hushlogin

sed -i -E 's/#?PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

DOMAIN=$1
mkdir -p \
/etc/systemd/system/bitcoind.service.d \
/etc/systemd/system/ord.service.d

printf "[Service]\nEnvironment=CHAIN=%s\n" $CHAIN \
| tee /etc/systemd/system/bitcoind.service.d/override.conf \
> /etc/systemd/system/ord.service.d/override.conf

hostnamectl set-hostname $DOMAIN

Expand Down Expand Up @@ -58,11 +67,10 @@ setfacl -m ord:r /var/lib/bitcoind/signet/.cookie

cp deploy/ord.service /etc/systemd/system/
systemctl daemon-reload
systemctl stop ord
systemctl enable ord
systemctl restart ord

while ! curl --fail https://$DOMAIN/status; do
echo Waiting for ord
echo "Waiting for ord at https://$DOMAIN/status…"
sleep 1
done
8 changes: 5 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ watch +args='test':
install-dev-deps:
cargo install cargo-criterion

deploy branch domain:
deploy branch chain domain:
ssh root@{{domain}} "mkdir -p deploy \
&& apt-get update --yes \
&& apt-get upgrade --yes \
&& apt-get install --yes git rsync"
rsync -avz deploy/checkout root@{{domain}}:deploy/checkout
ssh root@{{domain}} 'cd deploy && ./checkout {{branch}} {{domain}}'
ssh root@{{domain}} 'cd deploy && ./checkout {{branch}} {{chain}} {{domain}}'

deploy-signet branch="master": (deploy branch "signet.ordinals.com")
deploy-mainnet branch="master": (deploy branch "signet" "signet.ordinals.com")

deploy-signet branch="master": (deploy branch "main" "ordinals.com")

log unit domain="signet.ordinals.com":
ssh root@{{domain}} 'journalctl -fu {{unit}}'
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use {
io::{self, Write},
net::ToSocketAddrs,
ops::{Add, AddAssign, Deref, Sub},
path::PathBuf,
path::{Path, PathBuf},
process,
str::FromStr,
sync::{
Expand Down
91 changes: 66 additions & 25 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, clap::ValueEnum};

#[derive(Debug, Parser)]
pub(crate) struct Options {
Expand All @@ -8,20 +8,48 @@ pub(crate) struct Options {
cookie_file: Option<PathBuf>,
#[clap(long)]
rpc_url: Option<String>,
#[clap(long, default_value = "bitcoin")]
pub(crate) network: Network,
#[clap(long, arg_enum, default_value = "mainnet")]
pub(crate) chain: Chain,
#[clap(long)]
data_dir: Option<PathBuf>,
}

#[derive(ValueEnum, Copy, Clone, Debug)]
pub(crate) enum Chain {
Main,
Mainnet,
Regtest,
Signet,
Test,
Testnet,
}

impl Chain {
pub(crate) fn network(self) -> Network {
match self {
Self::Main | Self::Mainnet => Network::Bitcoin,
Self::Regtest => Network::Regtest,
Self::Signet => Network::Signet,
Self::Test | Self::Testnet => Network::Testnet,
}
}

pub(crate) fn join_network_with_data_dir(self, data_dir: &Path) -> PathBuf {
match self.network() {
Network::Bitcoin => data_dir.to_owned(),
other => data_dir.join(other.to_string()),
}
}
}

impl Options {
pub(crate) fn rpc_url(&self) -> String {
self
.rpc_url
.as_ref()
.unwrap_or(&format!(
"127.0.0.1:{}",
match self.network {
match self.chain.network() {
Network::Bitcoin => "8332",
Network::Regtest => "18443",
Network::Signet => "38332",
Expand All @@ -36,7 +64,7 @@ impl Options {
return Ok(cookie_file.clone());
}

let mut path = if cfg!(target_os = "linux") {
let path = if cfg!(target_os = "linux") {
dirs::home_dir()
.ok_or_else(|| anyhow!("Failed to retrieve home dir"))?
.join(".bitcoin")
Expand All @@ -46,9 +74,7 @@ impl Options {
.join("Bitcoin")
};

if !matches!(self.network, Network::Bitcoin) {
path.push(self.network.to_string())
}
let path = self.chain.join_network_with_data_dir(&path);

Ok(path.join(".cookie"))
}
Expand All @@ -58,13 +84,11 @@ impl Options {
return Ok(data_dir.clone());
}

let mut path = dirs::data_dir()
let path = dirs::data_dir()
.ok_or_else(|| anyhow!("Failed to retrieve data dir"))?
.join("ord");

if !matches!(self.network, Network::Bitcoin) {
path.push(self.network.to_string())
}
let path = self.chain.join_network_with_data_dir(&path);

if let Err(err) = fs::create_dir_all(&path) {
bail!("Failed to create data dir `{}`: {err}", path.display());
Expand All @@ -81,23 +105,18 @@ mod tests {
#[test]
fn rpc_url_overrides_network() {
assert_eq!(
Arguments::try_parse_from(&[
"ord",
"--rpc-url=127.0.0.1:1234",
"--network=signet",
"index"
])
.unwrap()
.options
.rpc_url(),
Arguments::try_parse_from(&["ord", "--rpc-url=127.0.0.1:1234", "--chain=signet", "index"])
.unwrap()
.options
.rpc_url(),
"127.0.0.1:1234"
);
}

#[test]
fn cookie_file_overrides_network() {
assert_eq!(
Arguments::try_parse_from(&["ord", "--cookie-file=/foo/bar", "--network=signet", "index"])
Arguments::try_parse_from(&["ord", "--cookie-file=/foo/bar", "--chain=signet", "index"])
.unwrap()
.options
.cookie_file()
Expand All @@ -121,7 +140,7 @@ mod tests {

#[test]
fn uses_network_defaults() {
let arguments = Arguments::try_parse_from(&["ord", "--network=signet", "index"]).unwrap();
let arguments = Arguments::try_parse_from(&["ord", "--chain=signet", "index"]).unwrap();

assert_eq!(arguments.options.rpc_url(), "127.0.0.1:38332");

Expand Down Expand Up @@ -154,7 +173,7 @@ mod tests {

#[test]
fn othernet_cookie_file_path() {
let arguments = Arguments::try_parse_from(&["ord", "--network=signet", "index"]).unwrap();
let arguments = Arguments::try_parse_from(&["ord", "--chain=signet", "index"]).unwrap();

let cookie_file = arguments
.options
Expand All @@ -181,10 +200,32 @@ mod tests {

#[test]
fn othernet_data_dir() {
let arguments = Arguments::try_parse_from(&["ord", "--network=signet", "index"]).unwrap();
let arguments = Arguments::try_parse_from(&["ord", "--chain=signet", "index"]).unwrap();

let data_dir = arguments.options.data_dir().unwrap().display().to_string();

assert!(data_dir.ends_with("/ord/signet"));
}

#[test]
fn network_accepts_aliases() {
fn check_network_alias(alias: &str, suffix: &str) {
let data_dir = Arguments::try_parse_from(&["ord", "--chain", alias, "index"])
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();

assert!(data_dir.ends_with(suffix), "{data_dir}");
}

check_network_alias("main", "ord");
check_network_alias("mainnet", "ord");
check_network_alias("regtest", "ord/regtest");
check_network_alias("signet", "ord/signet");
check_network_alias("test", "ord/testnet");
check_network_alias("testnet", "ord/testnet");
}
}
8 changes: 4 additions & 4 deletions src/purse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Purse {
let wallet = bdk::wallet::Wallet::new(
Bip84((seed.clone(), None), KeychainKind::External),
None,
options.network,
options.chain.network(),
SqliteDatabase::new(
data_dir
.join("wallet.sqlite")
Expand Down Expand Up @@ -53,7 +53,7 @@ impl Purse {
let wallet = bdk::wallet::Wallet::new(
Bip84((seed.clone(), None), KeychainKind::External),
None,
options.network,
options.chain.network(),
SqliteDatabase::new(
data_dir
.join("wallet.sqlite")
Expand Down Expand Up @@ -101,11 +101,11 @@ impl Purse {
auth: Auth::Cookie {
file: options.cookie_file()?,
},
network: options.network,
network: options.chain.network(),
wallet_name: wallet_name_from_descriptor(
Bip84(key, KeychainKind::External),
None,
options.network,
options.chain.network(),
&Secp256k1::new(),
)?,
sync_params: None,
Expand Down
Loading

0 comments on commit 71b27e0

Please sign in to comment.