Skip to content
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

Fleetctl: Update dependencies, improve error handling, ensure compatibility #24845

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 changes/24268-update-fleetctl-deps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Update fleetctl dependencies that cause warnings
8 changes: 4 additions & 4 deletions tools/fleetctl-npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
},
"homepage": "https://fleetdm.com",
"dependencies": {
"axios": "^0.28.0",
"rimraf": "3.0.2",
"tar": "^6.1.9"
"axios": "1.7.9",
"rimraf": "6.0.1",
"tar": "7.4.3"
},
"keywords": [
"osquery",
"security"
]
}
}
42 changes: 23 additions & 19 deletions tools/fleetctl-npm/run.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env node

const child = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
import { spawnSync } from "child_process";
import { mkdirSync, existsSync } from "fs";
import { type } from "os";
import { join } from "path";

const axios = require("axios");
const rimraf = require("rimraf");
const tar = require("tar");
import axios from "axios";
import { rimrafSync } from "rimraf";
import { extract } from "tar";

const { version } = require("./package.json");
import { version } from "./package.json";

// Strip any v4.0.0-1 style suffix (but not -rc1) so that the correct package is
// downloaded if there is a mistake in the NPM publish and we need to release a
Expand All @@ -19,37 +19,37 @@ if (!strippedVersion.startsWith("v")) {
strippedVersion = `v${strippedVersion}`;
}

const binDir = path.join(__dirname, "install");
const binDir = join(__dirname, "install");
// Determine the install directory by version so that we can detect when we need
// to upgrade to a new version.
const installDir = path.join(binDir, strippedVersion);
const installDir = join(binDir, strippedVersion);

const platform = (() => {
switch (os.type()) {
switch (type()) {
case "Windows_NT":
return "windows";
case "Linux":
return "linux";
case "Darwin":
return "macos";
default:
throw new Error(`platform ${os.type} unrecognized`);
throw new Error(`platform ${type} unrecognized`);
}
})();

const binName = platform === "windows" ? "fleetctl.exe" : "fleetctl";
const binPath = path.join(installDir, binName);
const binPath = join(installDir, binName);

const install = async () => {
const url = `https://github.com/fleetdm/fleet/releases/download/fleet-${strippedVersion}/fleetctl_${strippedVersion}_${platform}.tar.gz`;

fs.mkdirSync(installDir, { recursive: true });
mkdirSync(installDir, { recursive: true });

try {
const response = await axios({ url, responseType: "stream" });

// Strip the outer directory when extracting. Just get the binary.
const tarWriter = tar.extract({ strip: 1, cwd: installDir });
const tarWriter = extract({ strip: 1, cwd: installDir });
response.data.pipe(tarWriter);

// Need to return a promise with the writer to ensure we can await for it to complete.
Expand All @@ -58,14 +58,18 @@ const install = async () => {
tarWriter.on("error", reject);
});
} catch (err) {
throw new Error(`download archive ${url}: ${err.message}`);
if (axios.isAxiosError(err)) {
throw new Error(`download archive ${url}: ${err.message}`);
} else {
throw err;
}
}
};

const run = async () => {
if (!fs.existsSync(binPath)) {
if (!existsSync(binPath)) {
// Remove any existing binaries before installing the new one.
rimraf.sync(binDir);
rimrafSync(binDir);
console.log(`Installing fleetctl ${strippedVersion}...`);
try {
await install();
Expand Down Expand Up @@ -105,7 +109,7 @@ const run = async () => {

const [, , ...args] = process.argv;
const options = { cwd: process.cwd(), stdio: "inherit" };
const { status, error } = child.spawnSync(binPath, args, options);
const { status, error } = spawnSync(binPath, args, options);

if (error) {
console.error(error);
Expand Down
Loading