Skip to content

Commit

Permalink
[antlir2][container subtarget] add option to run alternate binary
Browse files Browse the repository at this point in the history
Summary:
Title.

While I'm here, rename `nspawn` to `container` since we're going to move off of
nspawn soon, and because the name of the target is `[container]`

Test Plan:
```
❯ buck2 run fbcode//antlir/antlir2/test_images/rpms:simple--layer[container] -- -- echo foo
Buck UI: https://www.internalfb.com/buck2/c288fde3-997c-4906-90e0-2a3596eeaf0f
Network: Up: 13KiB  Down: 542B  (reSessionID-c5e70c49-a5d1-4fe1-ac8b-4cdf2228c5b7)
Jobs completed: 158119. Time elapsed: 27.4s.
Cache hits: 0%. Commands: 1 (cached: 0, remote: 0, local: 1)
BUILD SUCCEEDED
foo
```

Reviewed By: sergeyfd

Differential Revision: D50121508

fbshipit-source-id: 0bd7e8f00fa4e6f07e8cd45731c62fcba9692349
  • Loading branch information
vmagro authored and facebook-github-bot committed Oct 10, 2023
1 parent 9fcec20 commit da5b083
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
14 changes: 7 additions & 7 deletions antlir/antlir2/bzl/image/layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ load("//antlir/rpm/dnf2buck:repo.bzl", "RepoInfo", "RepoSetInfo")
load("//antlir/bzl/build_defs.bzl", "config", "get_visibility")
load(":cfg.bzl", "layer_cfg")
load(":depgraph.bzl", "build_depgraph")
load(":mounts.bzl", "all_mounts", "nspawn_mount_args")
load(":mounts.bzl", "all_mounts", "container_mount_args")

def _map_image(
ctx: AnalysisContext,
Expand Down Expand Up @@ -69,7 +69,7 @@ def _map_image(

return cmd, out

def _nspawn_sub_target(nspawn_binary: Dependency, subvol: Artifact, mounts: list[mount_record]) -> list[Provider]:
def _container_sub_target(binary: Dependency, subvol: Artifact, mounts: list[mount_record]) -> list[Provider]:
dev_mode_args = cmd_args()
if REPO_CFG.artifacts_require_repo:
dev_mode_args = cmd_args(
Expand All @@ -80,9 +80,9 @@ def _nspawn_sub_target(nspawn_binary: Dependency, subvol: Artifact, mounts: list
DefaultInfo(),
RunInfo(cmd_args(
"sudo",
nspawn_binary[RunInfo],
binary[RunInfo],
cmd_args(subvol, format = "--subvol={}"),
cmd_args([nspawn_mount_args(mount) for mount in mounts]),
cmd_args([container_mount_args(mount) for mount in mounts]),
dev_mode_args,
)),
]
Expand Down Expand Up @@ -393,7 +393,7 @@ def _impl_with_features(features: ProviderCollection, *, ctx: AnalysisContext) -
DefaultInfo(
sub_targets = {
"build": [DefaultInfo(build_script), RunInfo(cmd_args(build_script))],
"container": _nspawn_sub_target(ctx.attrs._run_nspawn, final_subvol, mounts = phase_mounts),
"container": _container_sub_target(ctx.attrs._run_container, final_subvol, mounts = phase_mounts),
"features": [DefaultInfo(features_json_artifact)],
"logs": [DefaultInfo(all_logs, sub_targets = {
key: [DefaultInfo(artifact)]
Expand All @@ -420,7 +420,7 @@ def _impl_with_features(features: ProviderCollection, *, ctx: AnalysisContext) -
mounts = all_mounts(features = all_features, parent_layer = parent_layer_info)
# @oss-disable

sub_targets["container"] = _nspawn_sub_target(ctx.attrs._run_nspawn, final_subvol, mounts)
sub_targets["container"] = _container_sub_target(ctx.attrs._run_container, final_subvol, mounts)
sub_targets["debug"] = [DefaultInfo(sub_targets = debug_sub_targets)]

providers = [
Expand Down Expand Up @@ -508,7 +508,7 @@ _layer_attrs = {
default = None,
),
"_objcopy": attrs.exec_dep(default = "fbsource//third-party/binutils:objcopy"),
"_run_nspawn": attrs.exec_dep(default = "//antlir/antlir2/nspawn_in_subvol:nspawn"),
"_run_container": attrs.exec_dep(default = "//antlir/antlir2/container_subtarget:run"),
"_selected_target_arch": attrs.default_only(attrs.string(
default = arch_select(aarch64 = "aarch64", x86_64 = "x86_64"),
doc = "CPU arch that this layer is being built for. This is always " +
Expand Down
2 changes: 1 addition & 1 deletion antlir/antlir2/bzl/image/mounts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def all_mounts(

return mounts

def nspawn_mount_args(mount: mount_record) -> cmd_args:
def container_mount_args(mount: mount_record) -> cmd_args:
if mount.layer:
return cmd_args("--bind-mount-ro", mount.layer.src.subvol_symlink, mount.layer.mountpoint)
elif mount.host:
Expand Down
4 changes: 2 additions & 2 deletions antlir/antlir2/bzl/package/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _default_attrs = {
"_antlir2_packager": attrs.default_only(attrs.exec_dep(default = "//antlir/antlir2/antlir2_packager:antlir2-packager")),
"_dot_meta_feature": attrs.dep(default = "//antlir/antlir2/bzl/package:dot-meta"),
"_objcopy": attrs.default_only(attrs.exec_dep(default = "fbsource//third-party/binutils:objcopy")),
"_run_nspawn": attrs.default_only(attrs.exec_dep(default = "//antlir/antlir2/nspawn_in_subvol:nspawn")),
"_run_container": attrs.exec_dep(default = "//antlir/antlir2/container_subtarget:run"),
"_target_arch": attrs.default_only(attrs.string(
default = arch_select(aarch64 = "aarch64", x86_64 = "x86_64"),
)),
Expand Down Expand Up @@ -81,7 +81,7 @@ def _generic_impl(
"_antlir2": ctx.attrs._antlir2,
"_dot_meta_feature": ctx.attrs._dot_meta_feature,
"_objcopy": ctx.attrs._objcopy,
"_run_nspawn": ctx.attrs._run_nspawn,
"_run_container": ctx.attrs._run_container,
"_target_arch": ctx.attrs._target_arch,
}, with_artifacts = True).promise.map(partial(
_generic_impl_with_layer,
Expand Down
4 changes: 2 additions & 2 deletions antlir/antlir2/bzl/package/stamp_buildinfo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _impl(ctx: AnalysisContext) -> Promise:
"target_arch": ctx.attrs._target_arch,
"_feature_feature_targets": [ctx.attrs._dot_meta_feature],
"_objcopy": ctx.attrs._objcopy,
"_run_nspawn": ctx.attrs._run_nspawn,
"_run_container": ctx.attrs._run_container,
"_selected_target_arch": ctx.attrs._target_arch,
}, with_artifacts = True).promise.map(lambda l: [l[LayerInfo], l[DefaultInfo]])

Expand All @@ -27,7 +27,7 @@ stamp_buildinfo_rule = rule(
"_antlir2": attrs.exec_dep(default = "//antlir/antlir2/antlir2:antlir2"),
"_dot_meta_feature": attrs.dep(default = "//antlir/antlir2/bzl/package:dot-meta"),
"_objcopy": attrs.default_only(attrs.exec_dep(default = "fbsource//third-party/binutils:objcopy")),
"_run_nspawn": attrs.default_only(attrs.exec_dep(default = "//antlir/antlir2/nspawn_in_subvol:nspawn")),
"_run_container": attrs.exec_dep(default = "//antlir/antlir2/container_subtarget:run"),
"_target_arch": attrs.default_only(attrs.string(
default = arch_select(aarch64 = "aarch64", x86_64 = "x86_64"),
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("//antlir/bzl:build_defs.bzl", "rust_binary")
oncall("twimage")

rust_binary(
name = "nspawn",
name = "run",
srcs = glob(["src/**/*.rs"]),
visibility = ["PUBLIC"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use std::collections::HashMap;
use std::ffi::OsString;
use std::os::unix::process::CommandExt;
use std::path::PathBuf;

Expand All @@ -31,6 +32,8 @@ struct Args {
/// `--user` run command as a given user
#[clap(long, default_value = "root")]
user: String,
#[clap(last = true)]
cmd: Vec<OsString>,
}

fn init_logging() {
Expand Down Expand Up @@ -76,8 +79,13 @@ fn main() -> anyhow::Result<()> {
cmd_builder.inputs(repo_root.into_path_buf());
cmd_builder.inputs(PathBuf::from("/usr/local/fbcode"));
}

let mut cmd = args.cmd.into_iter();
let program = cmd.next().unwrap_or(OsString::from("/bin/bash"));

Err(isolate(cmd_builder.build())?
.command("/bin/bash")?
.command(program)?
.args(cmd)
.exec()
.into())
}

0 comments on commit da5b083

Please sign in to comment.