mirror of
https://github.com/fleetdm/fleet
synced 2026-05-04 05:48:26 +00:00
91 lines
3.2 KiB
JavaScript
91 lines
3.2 KiB
JavaScript
module.exports = {
|
|
|
|
|
|
friendlyName: 'Replace placeholder host values',
|
|
|
|
|
|
description: 'This script will update Host records to have the correct hardwareSerialNumber and uuid values.',
|
|
extendedDescription: 'This script was designed to be run after the new columns have been added to the Host table, and all existing rows have had these column set to a placeholder value.',
|
|
|
|
inputs: {
|
|
dry: {
|
|
description: 'Do a dry run instead of actually modifying the database?',
|
|
type: 'boolean',
|
|
defaultsTo: false
|
|
},
|
|
},
|
|
|
|
fn: async function ({dry}) {
|
|
|
|
let assert = require('assert');
|
|
|
|
if (!sails.config.custom.fleetBaseUrl || !sails.config.custom.fleetApiToken) {
|
|
throw new Error('sails.config.custom.fleetBaseUrl and sails.config.custom.fleetApiToken must both be provided.');
|
|
}
|
|
let fleetBaseUrl = sails.config.custom.fleetBaseUrl;
|
|
let headers = {
|
|
Authorization: `Bearer ${sails.config.custom.fleetApiToken}`
|
|
};
|
|
|
|
if (sails.config.custom.fleetApiOptionalCookie) {
|
|
headers['Cookie'] = sails.config.custom.fleetApiOptionalCookie;
|
|
}
|
|
|
|
let page = 0;
|
|
let HOSTS_PAGE_SIZE = 100;
|
|
|
|
let hostRecordsToUpdate = [];
|
|
|
|
await sails.helpers.flow.until(async()=>{
|
|
let hosts;
|
|
let byHostFleetApidsSeenInLatestFleetScan = {};
|
|
{
|
|
let responseData = await sails.helpers.http.get.with({
|
|
url: '/api/v1/fleet/hosts',
|
|
data: { page: page, per_page: HOSTS_PAGE_SIZE},//eslint-disable-line camelcase
|
|
baseUrl: fleetBaseUrl,
|
|
headers
|
|
})
|
|
.timeout(120000)
|
|
.retry(['requestFailed', {name: 'TimeoutError'}]);
|
|
assert(_.isArray(responseData.hosts));
|
|
assert(responseData.hosts.every((host) => _.isNumber(host.id)));
|
|
hosts = responseData.hosts;
|
|
}//∫
|
|
|
|
for (let host of hosts) {
|
|
byHostFleetApidsSeenInLatestFleetScan[host.id] = {
|
|
fleetApid: host.id,
|
|
hardwareSerialNumber: host.hardware_serial !== '' ? host.hardware_serial : 'N/A',// Note: If a host does not have a hardware_serial value (e.g., ChromeOS hosts), this value will be set to 'N/A'.
|
|
uuid: host.uuid,
|
|
};//∞
|
|
}
|
|
|
|
let recognizedHosts = await Host.find({ fleetApid: { in: Object.keys(byHostFleetApidsSeenInLatestFleetScan).map((key)=>Number(key)) } });
|
|
|
|
for (let $host of recognizedHosts){
|
|
if( $host.uuid === '?' || $host.hardwareSerialNumber === '?') { // If you used something else for the placeholder value, modify this line
|
|
hostRecordsToUpdate.push(byHostFleetApidsSeenInLatestFleetScan[$host.fleetApid]);
|
|
}
|
|
}
|
|
|
|
if(hosts.length < HOSTS_PAGE_SIZE) {
|
|
return true;
|
|
}
|
|
});
|
|
|
|
if(dry) {
|
|
sails.log(`Dry run: Would have updated ${hostRecordsToUpdate.length} host records with placeholder uuid and hardwareSerialNumber values.`);
|
|
} else {
|
|
for(let updatedHost of hostRecordsToUpdate){
|
|
await Host.updateOne({fleetApid: updatedHost.fleetApid}).set({uuid: updatedHost.uuid, hardwareSerialNumber: updatedHost.hardwareSerialNumber});
|
|
}
|
|
sails.log(`Updated ${hostRecordsToUpdate.length} host records with real uuid and hardwareSerialNumber values.`);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|