Skip to content

Commit ce4f50f

Browse files
committed
[VM] No Depot switch
For those with Depot access defined in their OS environment there is no way to get a true vault pin of a VM. This is an unusual use case so add a negative flag to prevent installation of depot and setting the parameters.
1 parent 4d920a8 commit ce4f50f

4 files changed

Lines changed: 52 additions & 19 deletions

File tree

‎kt/commands/vm/command.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@
7171
@click.option("--test", is_flag=True, help="Build the kernel and run kselftests")
7272
@click.option("--vcpus", type=int, default=12, help="Number of virtual CPUs (default: 12)")
7373
@click.option("--memory", type=int, default=32768, help="Memory in MiB (default: 32768)")
74+
@click.option("--no-depot", is_flag=True, help="Skip depot client installation and channel setup")
7475
@click.argument("kernel_workspace", required=False, shell_complete=ShellCompletion.show_kernel_workspaces)
75-
def vm(kernel_workspace, console, destroy, override, override_base, list_all, test, vcpus, memory):
76+
def vm(kernel_workspace, console, destroy, override, override_base, list_all, test, vcpus, memory, no_depot):
7677
if not list_all and not kernel_workspace:
7778
raise click.UsageError("kernel_workspace is required unless --list-all is specified")
7879

@@ -86,4 +87,5 @@ def vm(kernel_workspace, console, destroy, override, override_base, list_all, te
8687
test=test,
8788
vcpus=vcpus,
8889
memory=memory,
90+
no_depot=no_depot,
8991
)

‎kt/commands/vm/impl.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def main(
1616
test: bool = False,
1717
vcpus: int = 12,
1818
memory: int = 32768,
19+
no_depot: bool = False,
1920
):
2021
if list_all:
2122
VmCommand.list_all()
@@ -31,7 +32,12 @@ def main(
3132
return
3233

3334
vm_instance = Vm.setup_and_spinup(
34-
kernel_workspace_name=name, override=override, override_base=override_base, vcpus=vcpus, memory=memory
35+
kernel_workspace_name=name,
36+
override=override,
37+
override_base=override_base,
38+
vcpus=vcpus,
39+
memory=memory,
40+
no_depot=no_depot,
3541
)
3642
config = Config.load()
3743

‎kt/ktlib/vm.py‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def setup_and_spinup(
142142
override_base: bool = False,
143143
vcpus: int = 12,
144144
memory: int = 32768,
145+
no_depot: bool = False,
145146
):
146147
"""
147148
Setup and spin up a VM from a kernel workspace name.
@@ -152,6 +153,7 @@ def setup_and_spinup(
152153
override_base: If True, destroy and recreate the VM but override the base image as well
153154
vcpus: Number of virtual CPUs
154155
memory: Memory in MiB
156+
no_depot: If True, skip depot client installation and channel setup
155157
156158
Returns:
157159
VmInstance: The running VM instance
@@ -163,7 +165,7 @@ def setup_and_spinup(
163165
vm.destroy()
164166

165167
vm.setup(override_base=override_base)
166-
vm_instance = vm.spin_up(config=config, vcpus=vcpus, memory=memory)
168+
vm_instance = vm.spin_up(config=config, vcpus=vcpus, memory=memory, no_depot=no_depot)
167169

168170
return vm_instance
169171

@@ -186,7 +188,7 @@ def _download_source_image(self, override_base: bool = False):
186188
logging.info(f"Downloading image from {self._get_vm_url()}")
187189
wget.download(self._get_vm_url(), out=str(self.qcow2_source_path))
188190

189-
def _setup_cloud_init(self, config: Config):
191+
def _setup_cloud_init(self, config: Config, no_depot: bool = False):
190192
yaml = YAML()
191193
yaml.preserve_quotes = True
192194
yaml.width = 4096
@@ -231,26 +233,29 @@ def _setup_cloud_init(self, config: Config):
231233
)
232234
data["runcmd"].append("dnf clean all")
233235

234-
# Depot: always install the client, but only login+enable if credentials are present
235-
data["runcmd"].append("dnf install -y https://depot.ciq.com/public/files/depot-client/depot/depot.x86_64.rpm")
236-
depot_user = os.environ.get("DEPOT_USER")
237-
depot_token = os.environ.get("DEPOT_TOKEN")
238-
if depot_user and depot_token and self.depot_channels:
239-
data["runcmd"].append(f"depot login -u {depot_user} -t {depot_token}")
240-
for channel in self.depot_channels:
241-
data["runcmd"].append(f"depot enable {channel} -y")
236+
# Depot: install the client and login+enable if credentials are present
237+
if not no_depot:
238+
data["runcmd"].append(
239+
"dnf install -y https://depot.ciq.com/public/files/depot-client/depot/depot.x86_64.rpm"
240+
)
241+
depot_user = os.environ.get("DEPOT_USER")
242+
depot_token = os.environ.get("DEPOT_TOKEN")
243+
if depot_user and depot_token and self.depot_channels:
244+
data["runcmd"].append(f"depot login -u {depot_user} -t {depot_token}")
245+
for channel in self.depot_channels:
246+
data["runcmd"].append(f"depot enable {channel} -y")
242247

243248
# Install packages needed later
244249
data["runcmd"].append([str(config.base_path / Path("kernel-src-tree-tools") / Path("kernel_install_dep.sh"))])
245250
# Write this to image cloud_init
246251
with open(self.cloud_init_path, "w") as f:
247252
yaml.dump(data, f)
248253

249-
def _create_image(self, config: Config, vcpus: int = 12, memory: int = 32768):
254+
def _create_image(self, config: Config, vcpus: int = 12, memory: int = 32768, no_depot: bool = False):
250255
# Make sure the dir exists
251256
self.qcow2_path.parent.mkdir(parents=True, exist_ok=True)
252257

253-
self._setup_cloud_init(config=config)
258+
self._setup_cloud_init(config=config, no_depot=no_depot)
254259
# Copy qcow2 image to work dir
255260
self.qcow2_source_path.copy(self.qcow2_path)
256261

@@ -282,11 +287,11 @@ def _resize_disk(self):
282287
def setup(self, override_base: bool = False):
283288
self._download_source_image(override_base=override_base)
284289

285-
def spin_up(self, config: Config, vcpus: int = 12, memory: int = 32768) -> VmInstance:
290+
def spin_up(self, config: Config, vcpus: int = 12, memory: int = 32768, no_depot: bool = False) -> VmInstance:
286291
if not VirtHelper.exists(vm_name=self.name):
287292
logging.info(f"VM {self.name} does not exist, creating from scratch...")
288293

289-
self._create_image(config=config, vcpus=vcpus, memory=memory)
294+
self._create_image(config=config, vcpus=vcpus, memory=memory, no_depot=no_depot)
290295
return VmInstance(name=self.name, kernel_workspace=self.kernel_workspace)
291296

292297
logging.info(f"Vm {self.name} already exists")

‎tests/kt/ktlib/test_vm.py‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _make_config():
9090
return config
9191

9292

93-
def _build_vm_and_get_runcmd(vm_image_url=None, depot_channels=None, depot_env=None):
93+
def _build_vm_and_get_runcmd(vm_image_url=None, depot_channels=None, depot_env=None, no_depot=False):
9494
"""Build a Vm, call _setup_cloud_init, and capture the generated runcmd."""
9595
config = _make_config()
9696

@@ -139,7 +139,7 @@ def __exit__(self_inner, *args):
139139
return _real_open(path, mode)
140140

141141
with patch("builtins.open", side_effect=fake_open), patch.dict(os.environ, env):
142-
vm._setup_cloud_init(config=config)
142+
vm._setup_cloud_init(config=config, no_depot=no_depot)
143143

144144
content = captured_yaml.get("content", "")
145145
if content.startswith("#cloud-config\n"):
@@ -166,7 +166,7 @@ def test_cloud_init_no_vault_pin_when_no_image_url():
166166
assert not any("vault/rocky" in s for s in runcmd_strs)
167167

168168

169-
def test_cloud_init_depot_install_always_present():
169+
def test_cloud_init_depot_install_present_by_default():
170170
runcmd = _build_vm_and_get_runcmd(vm_image_url=None)
171171
runcmd_strs = [str(c) for c in runcmd]
172172
assert any("depot.x86_64.rpm" in s for s in runcmd_strs)
@@ -205,3 +205,23 @@ def test_cloud_init_ordering():
205205
dep_script_idx = next(i for i, s in enumerate(runcmd_strs) if "kernel_install_dep.sh" in s)
206206

207207
assert vault_idx < depot_install_idx < depot_login_idx < dep_script_idx
208+
209+
210+
def test_cloud_init_no_depot_skips_all_depot_commands():
211+
"""no_depot=True skips depot even when creds and channels are present."""
212+
runcmd = _build_vm_and_get_runcmd(
213+
depot_channels=["lts-8.6"],
214+
depot_env=("myuser", "mytoken"),
215+
no_depot=True,
216+
)
217+
runcmd_strs = [str(c) for c in runcmd]
218+
assert not any("depot" in s for s in runcmd_strs)
219+
assert any("kernel_install_dep.sh" in s for s in runcmd_strs)
220+
221+
222+
def test_cloud_init_no_depot_skips_depot_install():
223+
"""no_depot=True omits the depot RPM install (no creds variant)."""
224+
runcmd = _build_vm_and_get_runcmd(no_depot=True)
225+
runcmd_strs = [str(c) for c in runcmd]
226+
assert not any("depot.x86_64.rpm" in s for s in runcmd_strs)
227+
assert any("kernel_install_dep.sh" in s for s in runcmd_strs)

0 commit comments

Comments
 (0)