Skip to content

Commit 01e1be6

Browse files
committed
imp: tags update, breaking change fixes, faucet fn addn
1 parent 16e3b21 commit 01e1be6

File tree

10 files changed

+132
-26
lines changed

10 files changed

+132
-26
lines changed

templates/cardano-parts-project/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*.cert
22
/cloudFormation/
3-
/.direnv
3+
/.direnv*
44
/.envrc.local
55
/.gcroots
66
/*.log

templates/cardano-parts-project/Justfile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,17 @@ cardano-testnet isNg *ARGS:
284284
cf STACKNAME:
285285
#!/usr/bin/env nu
286286
mkdir cloudFormation
287+
let secretName = (nix eval --raw '.#cardano-parts.cluster.infra.generic.costCenter')
288+
let costCenter = (
289+
just sops-decrypt-binary secrets/tf/cluster.tfvars
290+
| lines
291+
| where { |it| $it =~ $secretName }
292+
| parse $"($secretName) = \"{secret}\""
293+
| get 0.secret
294+
| to text
295+
)
287296
nix eval --json '.#cloudFormation.{{STACKNAME}}' | from json | save --force 'cloudFormation/{{STACKNAME}}.json'
288-
rain deploy --debug --termination-protection --yes ./cloudFormation/{{STACKNAME}}.json
297+
rain deploy --debug --params costCenter=($costCenter) --termination-protection ./cloudFormation/{{STACKNAME}}.json
289298

290299
# Prep dbsync for delegation analysis
291300
dbsync-prep ENV HOST ACCTS="501":
@@ -401,11 +410,11 @@ dedelegate-pools ENV *IDXS=null:
401410
--wallet-mnemonic <(just sops-decrypt-binary secrets/envs/{{ENV}}/utxo-keys/faucet.mnemonic) \
402411
--delegation-index "$i"
403412

404-
TXID=$(eval "$CARDANO_CLI" latest transaction txid --tx-file tx-deleg-account-$i-restore.txsigned)
413+
TXID=$(eval "$CARDANO_CLI" latest transaction txid --tx-file tx-deleg-account-$i-restore.txsigned | jq -r .txhash)
405414
EXISTS="true"
406415

407416
while [ "$EXISTS" = "true" ]; do
408-
EXISTS=$(eval "$CARDANO_CLI" latest query tx-mempool tx-exists $TXID | jq -r .exists)
417+
EXISTS=$(eval "$CARDANO_CLI" latest query tx-mempool tx-exists $TXID | jq -r .exists || true)
409418
if [ "$EXISTS" = "true" ]; then
410419
echo "Pool de-delegation index $i tx still exists in the mempool, sleeping 5s: $TXID"
411420
else

templates/cardano-parts-project/flake/cloudFormation/terraformState.nix

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,45 @@ with lib; {
1212
Key = n;
1313
Value = v;
1414
}) {
15-
inherit (config.flake.cardano-parts.cluster.infra.generic) organization tribe function repo;
16-
environment = "generic";
15+
inherit
16+
(config.flake.cardano-parts.cluster.infra.generic)
17+
environment
18+
function
19+
organization
20+
owner
21+
project
22+
repo
23+
tribe
24+
;
1725
})
1826
++ [
1927
{
2028
Key = "Name";
2129
Value = name;
2230
}
31+
{
32+
Key = "costCenter";
33+
Value = {
34+
Ref = "costCenter";
35+
};
36+
}
2337
];
2438
in {
2539
AWSTemplateFormatVersion = "2010-09-09";
2640
Description = "Terraform state handling";
2741

42+
# The costCenter parameter will be passed to the configuration via a secrets file.
43+
# For details, see the just recipe: cf
44+
Parameters = {
45+
costCenter = {
46+
Type = "String";
47+
Description = "The costCenter tag";
48+
};
49+
};
50+
2851
# Resources here will be created in the AWS_REGION and AWS_PROFILE from your
2952
# environment variables.
3053
# Execute this using: `just cf terraformState`
31-
3254
Resources = {
3355
kmsKey = {
3456
Type = "AWS::KMS::Key";

templates/cardano-parts-project/flake/cluster.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
# function = "cardano-parts";
3636
# repo = "https://github.com/input-output-hk/UPDATE_ME";
3737

38+
# owner = "ioe";
39+
# environment = "testnets";
40+
# project = "cardano-playground";
41+
42+
# This is the tf var secrets name located in secrets/tf/cluster.tfvars
43+
# costCenter = "tag_costCenter";
44+
3845
# By default abort and warn if the ip-module is missing:
3946
# abortOnMissingIpModule = true;
4047
# warnOnMissingIpModule = true;

templates/cardano-parts-project/flake/colmena.nix

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
}: let
88
inherit (config.flake) nixosModules nixosConfigurations;
99
# inherit (config.flake.cardano-parts.cluster.infra.aws) domain regions;
10-
11-
cfgGeneric = config.flake.cardano-parts.cluster.infra.generic;
1210
in
1311
with builtins;
1412
with lib; {
@@ -39,7 +37,8 @@ in
3937

4038
# Since all machines are assigned a group, this is a good place to include default aws instance tags
4139
aws.instance.tags = {
42-
inherit (cfgGeneric) organization tribe function repo;
40+
# This group environment name will override the
41+
# flake.cluster.infra.generic environment name for aws instances.
4342
environment = config.flake.cardano-parts.cluster.groups.${name}.meta.environmentName;
4443
group = name;
4544
};

templates/cardano-parts-project/flake/opentofu/cluster.nix

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ with lib; let
138138
groupMultivalueDnsAttrs = mkMultivalueDnsAttrs "groupRelayMultivalueDns" groupMultivalueDnsList;
139139

140140
mkCustomRoute53Records = import ./cluster/route53.nix-import;
141+
142+
sensitiveString = {
143+
type = "string";
144+
sensitive = true;
145+
nullable = false;
146+
};
147+
148+
defaultTags = {
149+
inherit
150+
(infra.generic)
151+
environment
152+
function
153+
organization
154+
owner
155+
project
156+
repo
157+
tribe
158+
;
159+
160+
# costCenter is saved as a secret
161+
costCenter = "\${var.${infra.generic.costCenter}}";
162+
};
141163
in {
142164
flake.opentofu.cluster = inputs.cardano-parts.inputs.terranix.lib.terranixConfiguration {
143165
inherit system;
@@ -161,13 +183,19 @@ in {
161183
};
162184
};
163185

186+
variable = {
187+
# costCenter tag should remain secret in public repos
188+
"${infra.generic.costCenter}" = sensitiveString;
189+
};
190+
164191
provider.aws = forEach (attrNames cluster.regions) (region: {
165192
inherit region;
166193
alias = underscore region;
167-
default_tags.tags = {
168-
inherit (infra.generic) organization tribe function repo;
169-
environment = "generic";
170-
};
194+
195+
# Default tagging is inconsistent across aws resources, but including
196+
# it may help tag some resources that might have otherwise been
197+
# missed.
198+
default_tags.tags = defaultTags;
171199
});
172200

173201
# Common parameters:
@@ -324,6 +352,8 @@ in {
324352
gateway_id = "\${data.aws_internet_gateway.${region}.id}";
325353
}
326354
];
355+
356+
tags = defaultTags;
327357
};
328358
}
329359
);
@@ -346,6 +376,7 @@ in {
346376
+ " cidrsubnet(${ipv6CidrBlock}, ${toString ipv6SubnetCidrBits} - parseint(tolist(regex(\"/([0-9]+)$\", ${ipv6CidrBlock}))[0], 10), each.key)}";
347377

348378
availability_zone = "\${each.value.availability_zone}";
379+
tags = defaultTags;
349380
};
350381
});
351382

@@ -358,6 +389,7 @@ in {
358389
${region} = {
359390
provider = awsProviderFor region;
360391
assign_generated_ipv6_cidr_block = true;
392+
tags = defaultTags;
361393
};
362394
}
363395
);
@@ -378,6 +410,10 @@ in {
378410
vpc_security_group_ids = [
379411
"\${aws_security_group.common_${underscore region}[0].id}"
380412
];
413+
414+
# Provider level `default_tags` are automatically inherited at
415+
# the instance level. Instance specific tags defined in
416+
# flake/colmena.nix are merged.
381417
tags = {Name = name;} // node.aws.instance.tags or {};
382418

383419
root_block_device = {
@@ -386,14 +422,9 @@ in {
386422
iops = node.aws.instance.root_block_device.iops or 3000;
387423
throughput = node.aws.instance.root_block_device.throughput or 125;
388424
delete_on_termination = true;
389-
tags =
390-
# Root block device tags aren't applied like the other
391-
# resources since terraform-aws-provider v5.39.0.
392-
#
393-
# We need to strip the following tag attrs or tofu
394-
# constantly tries to re-apply them.
395-
{Name = name;}
396-
// removeAttrs (node.aws.instance.tags or {}) ["organization" "tribe" "function" "repo"];
425+
426+
# Default tags are not inherited to the volume level automatically.
427+
tags = defaultTags // {Name = name;} // node.aws.instance.tags or {};
397428
};
398429

399430
metadata_options = {
@@ -433,6 +464,7 @@ in {
433464
aws_iam_instance_profile.ec2_profile = {
434465
name = "ec2Profile";
435466
role = "\${aws_iam_role.ec2_role.name}";
467+
tags = defaultTags;
436468
};
437469

438470
aws_iam_role.ec2_role = {
@@ -447,6 +479,8 @@ in {
447479
}
448480
];
449481
};
482+
483+
tags = defaultTags;
450484
};
451485

452486
aws_iam_role_policy_attachment = let
@@ -495,6 +529,8 @@ in {
495529
}
496530
];
497531
};
532+
533+
tags = defaultTags;
498534
};
499535

500536
tls_private_key.bootstrap.algorithm = "ED25519";
@@ -508,13 +544,16 @@ in {
508544
provider = awsProviderFor region;
509545
key_name = "bootstrap";
510546
public_key = "\${tls_private_key.bootstrap.public_key_openssh}";
547+
tags = defaultTags;
511548
};
512549
});
513550

514551
aws_eip = mapNodes (name: node: {
515552
inherit (node.aws.instance) count;
516553
provider = awsProviderFor node.aws.region;
517554
instance = "\${aws_instance.${name}[0].id}";
555+
556+
# Provider level `default_tags` are automatically inherited.
518557
tags = {Name = name;} // node.aws.instance.tags or {};
519558
});
520559

@@ -592,6 +631,8 @@ in {
592631
protocol = "-1";
593632
})
594633
];
634+
635+
tags = defaultTags;
595636
};
596637
});
597638

templates/cardano-parts-project/scripts/bash-fns.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ submit() (
1515

1616
EXISTS="true"
1717
while [ "$EXISTS" = "true" ]; do
18-
EXISTS=$(cardano-cli latest query tx-mempool tx-exists "$TXID" | jq -re .exists)
18+
EXISTS=$(cardano-cli latest query tx-mempool tx-exists "$TXID" | jq -re .exists || true)
1919
if [ "$EXISTS" = "true" ]; then
2020
echo "The transaction still exists in the mempool, sleeping 5s: $TXID"
2121
else
@@ -199,3 +199,30 @@ return-utxo() (
199199

200200
submit "$BASENAME.signed"
201201
)
202+
203+
# A handy faucet submission function with mempool monitoring, usable on custom networks.
204+
# CARDANO_NODE_{NETWORK_ID,SOCKET_PATH}, TESTNET_MAGIC should already be exported.
205+
# SEND_ADDR, LOVELACE, RICH_ADDR and RICH vars need to be provided.
206+
faucet() (
207+
SEND_ADDR="$1"
208+
LOVELACE="$2"
209+
210+
UTXOS=$(cardano-cli query utxo --address "$RICH_ADDR")
211+
UTXO=$(jq -r 'to_entries | max_by(.value.value.lovelace) | { (.key): .value }' <<< "$UTXOS")
212+
UTXO_TX=$(jq -r 'keys[0]' <<< "$UTXO")
213+
214+
cardano-cli latest transaction build \
215+
--tx-in "$UTXO_TX" \
216+
--tx-out "$SEND_ADDR+$LOVELACE" \
217+
--change-address "$RICH_ADDR" \
218+
--testnet-magic "$TESTNET_MAGIC" \
219+
--out-file faucet.txbody
220+
221+
cardano-cli latest transaction sign \
222+
--tx-body-file faucet.txbody \
223+
--signing-key-file "$RICH.skey" \
224+
--testnet-magic "$TESTNET_MAGIC" \
225+
--out-file faucet.txsigned
226+
227+
submit faucet.txsigned
228+
)

templates/cardano-parts-project/scripts/lib/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def createTransaction(start, end, txin, payments_txouts, utxo_address, utxo_sign
5353

5454
signed_tx = signTx(tx_prefix, utxo_signing_key_str)
5555

56-
p = subprocess.run([cardanoCliStr(), "latest", "transaction", "txid", "--tx-file", signed_tx], capture_output=True, text=True)
56+
p = subprocess.run([cardanoCliStr(), "latest", "transaction", "txid", "--output-text", "--tx-file", signed_tx], capture_output=True, text=True)
5757
new_txin = p.stdout.rstrip()
5858
return (f"{new_txin}#0", tx_out_amount, fee)
5959

@@ -66,6 +66,7 @@ def estimateFeeTx(txbody, txin_count, txout_count, pparams) -> int:
6666
"--tx-out-count", str(txout_count),
6767
"--witness-count", "1",
6868
"--protocol-params-file", pparams,
69+
"--output-text",
6970
"--tx-body-file", txbody]
7071

7172
p = subprocess.run(cmd, capture_output=True, text=True)

templates/cardano-parts-project/scripts/recipes/governance.just

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ vote-with-pool ENV POOL ACTION_ID ACTION_IDX VOTE:
992992
EXISTS="true"
993993

994994
while [ "$EXISTS" = "true" ]; do
995-
EXISTS=$(eval "$CARDANO_CLI" latest query tx-mempool tx-exists $TXID | jq -r .exists)
995+
EXISTS=$(eval "$CARDANO_CLI" latest query tx-mempool tx-exists $TXID | jq -r .exists || true)
996996
if [ "$EXISTS" = "true" ]; then
997997
echo "Vote transaction still exists in the mempool, sleeping 5s: $TXID"
998998
else

templates/cardano-parts-project/scripts/restore-delegation-accounts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def signTx(tx_body, utxo_signing_key_str, stake_signing_key, out_file):
261261
if p.returncode != 0:
262262
print(p.stderr)
263263
raise Exception("Unknown error signing transaction")
264-
cli_args = ["cardano-cli", "latest", "transaction", "txid", "--tx-file", out_file]
264+
cli_args = ["cardano-cli", "latest", "transaction", "txid", "--output-text", "--tx-file", out_file]
265265
p = subprocess.run(cli_args, input=None, capture_output=True, text=True)
266266
if p.returncode != 0:
267267
print(p.stderr)

0 commit comments

Comments
 (0)