Skip to content

Commit b3ede80

Browse files
committed
0.8.1: move ssh command after bootstrap
Signed-off-by: Gyuho Lee <[email protected]>
1 parent a31f817 commit b3ede80

File tree

2 files changed

+114
-104
lines changed

2 files changed

+114
-104
lines changed

avalanche-ops/src/aws/spec.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ pub struct RegionalResource {
189189
#[serde(default)]
190190
pub ec2_key_path: String,
191191
#[serde(default)]
192-
pub ssh_commands_path: String,
192+
pub ssh_commands_path_anchor_nodes: String,
193+
#[serde(default)]
194+
pub ssh_commands_path_non_anchor_nodes: String,
195+
#[serde(default)]
196+
pub ssh_commands_path_dev_machine: String,
193197

194198
/// CloudFormation stack name for EC2 instance role.
195199
/// READ ONLY -- DO NOT SET.
@@ -306,7 +310,9 @@ impl RegionalResource {
306310
region: String::from("us-west-2"),
307311
ec2_key_name: String::new(),
308312
ec2_key_path: String::new(),
309-
ssh_commands_path: String::new(),
313+
ssh_commands_path_anchor_nodes: String::new(),
314+
ssh_commands_path_non_anchor_nodes: String::new(),
315+
ssh_commands_path_dev_machine: String::new(),
310316

311317
cloudformation_ec2_instance_role: None,
312318
cloudformation_ec2_instance_profile_arn: None,
@@ -684,7 +690,21 @@ impl Spec {
684690
region: reg.to_string(),
685691
ec2_key_name: format!("{id}-ec2-key"),
686692
ec2_key_path: get_ec2_key_path(&spec_file_path, reg.as_str()),
687-
ssh_commands_path: get_ssh_commands_path(&spec_file_path, reg.as_str()),
693+
ssh_commands_path_anchor_nodes: get_ssh_commands_path(
694+
&spec_file_path,
695+
reg.as_str(),
696+
"anchor-nodes",
697+
),
698+
ssh_commands_path_non_anchor_nodes: get_ssh_commands_path(
699+
&spec_file_path,
700+
reg.as_str(),
701+
"non-anchor-nodes",
702+
),
703+
ssh_commands_path_dev_machine: get_ssh_commands_path(
704+
&spec_file_path,
705+
reg.as_str(),
706+
"dev-machine",
707+
),
688708
..RegionalResource::default()
689709
};
690710
if let Some(nlb_acm_certificate_arn) = opts.nlb_acm_certificate_arns.get(reg) {
@@ -2179,11 +2199,11 @@ fn get_ec2_key_path(spec_file_path: &str, region: &str) -> String {
21792199
)
21802200
}
21812201

2182-
fn get_ssh_commands_path(spec_file_path: &str, region: &str) -> String {
2202+
fn get_ssh_commands_path(spec_file_path: &str, region: &str, kind: &str) -> String {
21832203
let path = Path::new(spec_file_path);
21842204
let parent_dir = path.parent().unwrap();
21852205
let name = path.file_stem().unwrap();
2186-
let new_name = format!("{}-ssh-commands.{region}.sh", name.to_str().unwrap(),);
2206+
let new_name = format!("{}-ssh-commands.{region}.{kind}.sh", name.to_str().unwrap(),);
21872207
String::from(
21882208
parent_dir
21892209
.join(Path::new(new_name.as_str()))

avalancheup-aws/src/apply/mod.rs

Lines changed: 89 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,19 +1316,12 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
13161316
public_ip: public_ip,
13171317
};
13181318
println!("\n{}\n", ssh_command.to_string());
1319-
1320-
let output = ssh_command
1321-
.run("tail -10 /var/log/cloud-init-output.log")
1322-
.unwrap();
1323-
println!("init script std output:\n{}\n", output.stdout);
1324-
println!("init script std err:\n{}\n", output.stderr);
1325-
13261319
ssh_commands.push(ssh_command);
13271320
}
13281321
println!();
13291322

1330-
ec2::SshCommands(ssh_commands)
1331-
.sync(&regional_resource.ssh_commands_path)
1323+
ec2::SshCommands(ssh_commands.clone())
1324+
.sync(&regional_resource.ssh_commands_path_anchor_nodes)
13321325
.unwrap();
13331326

13341327
// wait for anchor nodes to generate certs and node ID and post to remote storage
@@ -1410,6 +1403,20 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
14101403

14111404
log::info!("waiting for anchor nodes bootstrap and ready (to be safe)");
14121405
sleep(Duration::from_secs(15)).await;
1406+
1407+
for ssh_command in ssh_commands.iter() {
1408+
let output = ssh_command
1409+
.run("tail -10 /var/log/cloud-init-output.log")
1410+
.unwrap();
1411+
println!(
1412+
"{} (anchor node) init script std output:\n{}\n",
1413+
ssh_command.instance_id, output.stdout
1414+
);
1415+
println!(
1416+
"{} (anchor node) init script std err:\n{}\n",
1417+
ssh_command.instance_id, output.stderr
1418+
);
1419+
}
14131420
}
14141421
}
14151422

@@ -1748,60 +1755,41 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
17481755
}
17491756
}
17501757

1751-
println!();
17521758
let f = File::open(&regional_resource.ec2_key_path).unwrap();
17531759
f.set_permissions(PermissionsExt::from_mode(0o444)).unwrap();
1760+
1761+
println!();
1762+
let mut ssh_commands = Vec::new();
17541763
for d in droplets {
1755-
let (instance_ip, ip_kind) =
1764+
let (public_ip, ip_mode) =
17561765
if let Some(public_ip) = instance_id_to_public_ip.get(&d.instance_id) {
17571766
(public_ip.clone(), "elastic")
17581767
} else {
17591768
(d.public_ipv4.clone(), "ephemeral")
17601769
};
1761-
// ssh -o "StrictHostKeyChecking no" -i [ec2_key_path] [user name]@[public IPv4/DNS name]
1762-
// aws ssm start-session --region [region] --target [instance ID]
1763-
println!(
1764-
"# change SSH key permission
1765-
chmod 400 {}
1766-
# instance '{}' ({}, {}) -- IP kind {}
1767-
ssh -o \"StrictHostKeyChecking no\" -i {} ubuntu@{}
1768-
# download to local machine
1769-
scp -i {} ubuntu@{}:REMOTE_FILE_PATH LOCAL_FILE_PATH
1770-
scp -i {} -r ubuntu@{}:REMOTE_DIRECTORY_PATH LOCAL_DIRECTORY_PATH
1771-
# upload to remote machine
1772-
scp -i {} LOCAL_FILE_PATH ubuntu@{}:REMOTE_FILE_PATH
1773-
scp -i {} -r LOCAL_DIRECTORY_PATH ubuntu@{}:REMOTE_DIRECTORY_PATH
1774-
# SSM session (requires SSM agent)
1775-
aws ssm start-session --region {} --target {}
1776-
",
1777-
regional_resource.ec2_key_path,
1778-
//
1779-
d.instance_id,
1780-
d.instance_state_name,
1781-
d.availability_zone,
1782-
ip_kind,
1783-
//
1784-
regional_resource.ec2_key_path,
1785-
instance_ip,
1786-
//
1787-
regional_resource.ec2_key_path,
1788-
instance_ip,
1789-
//
1790-
regional_resource.ec2_key_path,
1791-
instance_ip,
1792-
//
1793-
regional_resource.ec2_key_path,
1794-
instance_ip,
1795-
//
1796-
regional_resource.ec2_key_path,
1797-
instance_ip,
1798-
//
1799-
regional_resource.region,
1800-
d.instance_id,
1801-
);
1770+
1771+
let ssh_command = ec2::SshCommand {
1772+
ec2_key_path: regional_resource.ec2_key_path.clone(),
1773+
user_name: String::from("ubuntu"),
1774+
1775+
region: region.clone(),
1776+
availability_zone: d.availability_zone,
1777+
1778+
instance_id: d.instance_id,
1779+
instance_state_name: d.instance_state_name,
1780+
1781+
ip_mode: ip_mode.to_string(),
1782+
public_ip: public_ip,
1783+
};
1784+
println!("\n{}\n", ssh_command.to_string());
1785+
ssh_commands.push(ssh_command);
18021786
}
18031787
println!();
18041788

1789+
ec2::SshCommands(ssh_commands.clone())
1790+
.sync(&regional_resource.ssh_commands_path_non_anchor_nodes)
1791+
.unwrap();
1792+
18051793
// wait for non anchor nodes to generate certs and node ID and post to remote storage
18061794
// TODO: set timeouts
18071795
let mut regional_non_anchor_nodes = Vec::new();
@@ -1877,6 +1865,20 @@ aws ssm start-session --region {} --target {}
18771865

18781866
log::info!("waiting for non-anchor nodes bootstrap and ready (to be safe)");
18791867
sleep(Duration::from_secs(20)).await;
1868+
1869+
for ssh_command in ssh_commands.iter() {
1870+
let output = ssh_command
1871+
.run("tail -10 /var/log/cloud-init-output.log")
1872+
.unwrap();
1873+
println!(
1874+
"{} (non-anchor node) init script std output:\n{}\n",
1875+
ssh_command.instance_id, output.stdout
1876+
);
1877+
println!(
1878+
"{} (non-anchor node) init script std err:\n{}\n",
1879+
ssh_command.instance_id, output.stderr
1880+
);
1881+
}
18801882
}
18811883
}
18821884

@@ -2756,8 +2758,6 @@ default-spec --log-level=info --funded-keys={funded_keys} --region={region} --up
27562758
instance_id_to_public_ip.insert(instance_id, public_ip);
27572759
}
27582760

2759-
let ec2_key_path = regional_resource.ec2_key_path.clone();
2760-
27612761
let user_name = {
27622762
if dev_machine.os_type == "al2" {
27632763
"ec2-user"
@@ -2766,6 +2766,7 @@ default-spec --log-level=info --funded-keys={funded_keys} --region={region} --up
27662766
}
27672767
};
27682768

2769+
let mut ssh_commands = Vec::new();
27692770
for d in droplets {
27702771
// ssh -o "StrictHostKeyChecking no" -i [ec2_key_path] [user name]@[public IPv4/DNS name]
27712772
// aws ssm start-session --region [region] --target [instance ID]
@@ -2776,54 +2777,28 @@ default-spec --log-level=info --funded-keys={funded_keys} --region={region} --up
27762777
d.public_ipv4.clone()
27772778
};
27782779

2779-
println!(
2780-
"
2781-
# change SSH key permission
2782-
chmod 400 {}
2783-
# instance '{}' ({}, {}) -- ip mode '{}'
2784-
ssh -o \"StrictHostKeyChecking no\" -i {} {}@{}
2785-
# download to local machine
2786-
scp -i {} {}@{}:REMOTE_FILE_PATH LOCAL_FILE_PATH
2787-
scp -i {} -r {}@{}:REMOTE_DIRECTORY_PATH LOCAL_DIRECTORY_PATH
2788-
# upload to remote machine
2789-
scp -i {} LOCAL_FILE_PATH {}@{}:REMOTE_FILE_PATH
2790-
scp -i {} -r LOCAL_DIRECTORY_PATH {}@{}:REMOTE_DIRECTORY_PATH
2791-
# SSM session (requires SSM agent)
2792-
aws ssm start-session --region {} --target {}
2793-
",
2794-
ec2_key_path,
2795-
//
2796-
d.instance_id,
2797-
d.instance_state_name,
2798-
d.availability_zone,
2799-
spec.machine.ip_mode,
2800-
//
2801-
ec2_key_path,
2802-
user_name,
2803-
public_ip,
2804-
//
2805-
ec2_key_path,
2806-
user_name,
2807-
public_ip,
2808-
//
2809-
ec2_key_path,
2810-
user_name,
2811-
public_ip,
2812-
//
2813-
ec2_key_path,
2814-
user_name,
2815-
public_ip,
2816-
//
2817-
ec2_key_path,
2818-
user_name,
2819-
public_ip,
2820-
//
2821-
spec.resource.regions[0],
2822-
d.instance_id,
2823-
);
2780+
let ssh_command = ec2::SshCommand {
2781+
ec2_key_path: regional_resource.ec2_key_path.clone(),
2782+
user_name: user_name.to_string(),
2783+
2784+
region: spec.resource.regions[0].clone(),
2785+
availability_zone: d.availability_zone,
2786+
2787+
instance_id: d.instance_id,
2788+
instance_state_name: d.instance_state_name,
2789+
2790+
ip_mode: spec.machine.ip_mode.clone(),
2791+
public_ip: public_ip,
2792+
};
2793+
println!("\n{}\n", ssh_command.to_string());
2794+
ssh_commands.push(ssh_command);
28242795
}
28252796
println!();
28262797

2798+
ec2::SshCommands(ssh_commands.clone())
2799+
.sync(&regional_resource.ssh_commands_path_dev_machine)
2800+
.unwrap();
2801+
28272802
spec.resource
28282803
.regional_resources
28292804
.insert(spec.resource.regions[0].clone(), regional_resource);
@@ -2848,6 +2823,21 @@ aws ssm start-session --region {} --target {}
28482823
.await
28492824
.unwrap();
28502825

2826+
sleep(Duration::from_secs(10)).await;
2827+
for ssh_command in ssh_commands.iter() {
2828+
let output = ssh_command
2829+
.run("tail -10 /var/log/cloud-init-output.log")
2830+
.unwrap();
2831+
println!(
2832+
"{} (dev machine) init script std output:\n{}\n",
2833+
ssh_command.instance_id, output.stdout
2834+
);
2835+
println!(
2836+
"{} (dev machine) init script std err:\n{}\n",
2837+
ssh_command.instance_id, output.stderr
2838+
);
2839+
}
2840+
28512841
//
28522842
//
28532843
//

0 commit comments

Comments
 (0)