Skip to content

Commit

Permalink
Support for Rancher Desktop on mac (#1219)
Browse files Browse the repository at this point in the history
* Support for Rancher Desktop on mac

Signed-off-by: Prabhu Subramanian <[email protected]>

* Fix for 1206

Signed-off-by: Prabhu Subramanian <[email protected]>

---------

Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu authored Jul 5, 2024
1 parent cc2d1bd commit c9460a9
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 15 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/npm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,19 @@ jobs:
run: |
corepack enable
corepack pnpm install
node bin/cdxgen.js -t docker -o bom.json ghcr.io/cyclonedx/cdxgen:latest
oras attach --artifact-type sbom/cyclonedx ghcr.io/cyclonedx/cdxgen:latest ./bom.json:application/json
node bin/cdxgen.js -t docker -o cdxgen-oci-image.cdx.json ghcr.io/cyclonedx/cdxgen:latest
oras attach --artifact-type sbom/cyclonedx ghcr.io/cyclonedx/cdxgen:latest ./cdxgen-oci-image.cdx.json:application/json
oras discover -o tree ghcr.io/cyclonedx/cdxgen:latest
continue-on-error: true
if: startsWith(github.ref, 'refs/tags/')
- name: Attach cdx sbom to release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
cdxgen-oci-image.cdx.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
containers-deno:
runs-on: ubuntu-latest
permissions:
Expand Down
1 change: 1 addition & 0 deletions binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ const OS_DISTRO_ALIAS = {
"debian-13.5": "trixie",
"debian-12": "bookworm",
"debian-12.5": "bookworm",
"debian-12.6": "bookworm",
"debian-11": "bullseye",
"debian-11.5": "bullseye",
"debian-10": "buster",
Expand Down
86 changes: 76 additions & 10 deletions docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let isDockerRootless = false;
let isContainerd = !!process.env.CONTAINERD_ADDRESS;
const WIN_LOCAL_TLS = "http://localhost:2375";
let isWinLocalTLS = false;
let isNerdctl = undefined;

if (
!process.env.DOCKER_HOST &&
Expand All @@ -49,6 +50,56 @@ if (
isContainerd = true;
}

/**
* Detect if Rancher desktop is running on a mac.
*/
export function detectRancherDesktop() {
// Detect Rancher desktop and nerdctl on a mac
if (_platform() === "darwin") {
const limaHome = join(
homedir(),
"Library",
"Application Support",
"rancher-desktop",
"lima",
);
const limactl = join(
"/Applications",
"Rancher Desktop.app",
"Contents",
"Resources",
"resources",
"darwin",
"lima",
"bin",
"limactl",
);
// Is Rancher Desktop running
if (existsSync(limactl) || existsSync(limaHome)) {
const result = spawnSync("rdctl", ["list-settings"], {
encoding: "utf-8",
});
if (result.status !== 0 || result.error) {
if (
isNerdctl === undefined &&
result.stderr?.includes("connection refused")
) {
console.warn(
"Ensure Rancher Desktop is running prior to invoking cdxgen. To start from the command line, type the command 'rdctl start'",
);
isNerdctl = false;
}
} else {
if (DEBUG_MODE) {
console.log("Rancher Desktop found!");
}
isNerdctl = true;
}
}
}
return isNerdctl;
}

// Cache the registry auth keys
const registry_auth_keys = {};

Expand Down Expand Up @@ -288,7 +339,7 @@ const getDefaultOptions = (forRegistry) => {
};

export const getConnection = async (options, forRegistry) => {
if (isContainerd) {
if (isContainerd || isNerdctl) {
return undefined;
}
if (!dockerConn) {
Expand Down Expand Up @@ -368,10 +419,15 @@ export const getConnection = async (options, forRegistry) => {
"Ensure Docker for Desktop is running as an administrator with 'Exposing daemon on TCP without TLS' setting turned on.",
opts,
);
} else if (_platform() === "darwin") {
console.warn(
"Ensure Podman Desktop (open-source) or Docker for Desktop (May require subscription) is running.",
);
} else if (_platform() === "darwin" && !isNerdctl) {
if (detectRancherDesktop()) {
return undefined;
}
if (isNerdctl === undefined) {
console.warn(
"Ensure Podman Desktop (open-source) or Docker for Desktop (May require subscription) is running.",
);
}
} else {
console.warn(
"Ensure docker/podman service or Docker for Desktop is running.",
Expand Down Expand Up @@ -497,11 +553,14 @@ export const parseImageName = (fullImageName) => {
};

/**
* Prefer cli on windows or when using tcp/ssh based host.
* Prefer cli on windows, nerdctl on mac, or when using tcp/ssh based host.
*
* @returns boolean true if we should use the cli. false otherwise
*/
const needsCliFallback = () => {
if (_platform() === "darwin" && detectRancherDesktop()) {
return true;
}
return (
isWin ||
(process.env.DOCKER_HOST &&
Expand Down Expand Up @@ -532,7 +591,10 @@ export const getImage = async (fullImageName) => {
return undefined;
}
if (needsCliFallback()) {
const dockerCmd = process.env.DOCKER_CMD || "docker";
let dockerCmd = process.env.DOCKER_CMD || "docker";
if (!process.env.DOCKER_CMD && detectRancherDesktop()) {
dockerCmd = "nerdctl";
}
let result = spawnSync(dockerCmd, ["pull", fullImageName], {
encoding: "utf-8",
});
Expand Down Expand Up @@ -956,14 +1018,18 @@ export const exportImage = async (fullImageName) => {
let manifestFile = join(tempDir, "manifest.json");
// Windows containers use index.json
const manifestIndexFile = join(tempDir, "index.json");
// On Windows, fallback to invoking cli
// On Windows or on mac with Rancher Desktop, fallback to invoking cli
if (needsCliFallback()) {
const imageTarFile = join(tempDir, "image.tar");
let dockerCmd = process.env.DOCKER_CMD || "docker";
if (!process.env.DOCKER_CMD && detectRancherDesktop()) {
dockerCmd = "nerdctl";
}
console.log(
`About to export image ${fullImageName} to ${imageTarFile} using docker cli`,
`About to export image ${fullImageName} to ${imageTarFile} using ${dockerCmd} cli`,
);
const result = spawnSync(
"docker",
dockerCmd,
["save", "-o", imageTarFile, fullImageName],
{
encoding: "utf-8",
Expand Down
2 changes: 1 addition & 1 deletion docs/ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ sudo nerdctl --snapshotter nydus run --rm -v $HOME/.m2:/root/.m2 -v $(pwd):/app
## Lima VM usage
Refer to the dedicated [readme](../contrib/lima/README.md)
Refer to the dedicated [readme](../contrib/lima/README.md). Rancher Desktop on macOS with nerdctl is supported by default.
## Export as protobuf binary
Expand Down
2 changes: 1 addition & 1 deletion types/binary.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions types/docker.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Detect if Rancher desktop is running on a mac.
*/
export function detectRancherDesktop(): any;
export const isWin: boolean;
export const DOCKER_HUB_REGISTRY: "docker.io";
export function getDirs(dirPath: string, dirName: string, hidden?: boolean, recurse?: boolean): string[];
Expand Down
2 changes: 1 addition & 1 deletion types/docker.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4376,4 +4376,16 @@ test("hasAnyProjectType tests", () => {
false,
),
).toBeFalsy();
expect(
hasAnyProjectType(["js", "docker"], {
projectType: ["universal"],
excludeType: [],
}),
).toBeTruthy();
expect(
hasAnyProjectType(["js"], {
projectType: ["universal"],
excludeType: ["js"],
}),
).toBeFalsy();
});

0 comments on commit c9460a9

Please sign in to comment.