Skip to content

Rough draft of Vultr changes. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ some level:

* Joyent SmartOS (including Triton environments)
* DigitalOcean
* Vultr
* Amazon EC2
* Generic QEMU/KVM; e.g., under `libvirtd`

Expand Down
106 changes: 106 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ fn amazon_metadata_get(log: &Logger, key: &str) -> Result<Option<String>> {
amazon_metadata_getx(log, &format!("meta-data/{}", key))
}

fn vultr_metadata_get(log: &Logger, key: &str) -> Result<Option<String>> {
let url = format!("http://169.254.169.254/v1/{}", key);

let cb = reqwest::blocking::ClientBuilder::new()
.redirect(reqwest::redirect::Policy::none())
.build()?;

loop {
match cb.get(&url).send() {
Ok(res) => {
if res.status().is_success() {
let val = res.text()?.trim_end_matches('\n').to_string();
return Ok(Some(val));
} else if res.status().as_u16() == 404 {
return Ok(None);
}

error!(log, "metadata {}: bad status {}", key, res.status());
}
Err(e) => {
error!(log, "metadata {}: bad status {}", key, e);
}
}

sleep(5_000);
}
}

fn smf_enable(log: &Logger, fmri: &str) -> Result<()> {
info!(log, "exec: svcadm enable {}", fmri);
let output = Command::new(SVCADM)
Expand Down Expand Up @@ -1087,6 +1115,11 @@ fn run(log: &Logger) -> Result<()> {
run_digitalocean(log)?;
return Ok(());
}
("Vultr", "Guess") => { // Still got to figure out what this would be
info!(log, "hypervisor type: Whatever the hell Vultr is running (from SMBIOS)");
run_vultr(log)?;
return Ok(());
}
("Xen", "HVM domU") => {
if smbios.version.contains("amazon") {
info!(log, "hypervisor type: Amazon AWS Xen (from SMBIOS)");
Expand Down Expand Up @@ -1341,6 +1374,79 @@ fn run_amazon(log: &Logger) -> Result<()> {
Ok(())
}

fn run_vultr(log: &Logger) -> Result<()> {
let ifaces = dladm_ether_list()?;
info!(log, "found these ethernet interfaces: {:?}", ifaces);
/*
* Vultr only has one nic per VM
*/
let chosen = ifaces.iter().next().map(|x| x.as_str());

if let Some(chosen) = chosen {
info!(log, "chose interface {}", chosen);
ensure_ipv4_interface_dhcp(log, "dhcp", chosen)?;
} else {
bail!("could not find an appropriate Ethernet interface!");
}

/*
* Determine the instance ID, using the metadata service:
*/
let instid = if let Some(id) = vultr_metadata_get(log, "instance-v2-id")? {
id
} else {
bail!("could not determine instance ID");
};

/*
* Load our stamp file to see if the Instance ID has changed.
*/
if let Some([id]) = read_lines(STAMP)?.as_deref() {
if id.trim() == instid {
info!(log, "this guest has already completed first \
boot processing, halting");
return Ok(());
} else {
info!(log, "guest Instance ID changed ({} -> {}), reprocessing",
id.trim(), instid);
}
}

phase_reguid_zpool(log)?;

/*
* Determine the node name for this guest:
*/
let (src, n) = if let Some(hostname) = dhcpinfo(log, "hostname")? {
("DHCP", hostname.trim().to_string())
} else if let Some(hostname) = vultr_metadata_get(log, "hostname")? {
("metadata", hostname.trim().to_string())
} else {
bail!("could not get hostname for this VM");
};
info!(log, "VM node name is \"{}\" (from {})", n, src);
phase_set_hostname(log, &n)?;

/*
* Get public key:
*/
if let Some(pk) = vultr_metadata_get(log, "public-keys")? {
let pubkeys = vec![pk];
phase_pubkeys(log, &pubkeys)?;
} else {
warn!(log, "no SSH public key?");
}

/*
* Vultr doesn't support user-data yet
*/
info!(log, "no user-data?");

write_lines(log, STAMP, &[instid])?;

Ok(())
}

fn run_smartos(log: &Logger) -> Result<()> {
let uuid = if let Mdata::Found(uuid) = mdata_get(log, "sdc:uuid")? {
uuid.trim().to_string()
Expand Down