2024-10-15 15:17:05 +00:00
module . exports = {
friendlyName : 'Upload software' ,
description : '' ,
files : [ 'newSoftware' ] ,
inputs : {
newSoftware : {
type : 'ref' ,
description : 'An Upstream with an incoming file upload.' ,
required : true ,
} ,
teams : {
type : [ 'string' ] ,
description : 'An array of team IDs that this profile will be added to'
}
} ,
exits : {
success : {
outputDescription : 'The new software has been uploaded' ,
outputType : { } ,
} ,
softwareAlreadyExistsOnThisTeam : {
description : 'A software with this name already exists on the Fleet Instance' ,
statusCode : 409 ,
} ,
2024-10-22 02:13:06 +00:00
softwareUploadFailed : {
description : 'An unexpected error occurred communicating with the Fleet API'
}
2024-10-15 15:17:05 +00:00
} ,
fn : async function ( { newSoftware , teams } ) {
let uploadedSoftware ;
if ( ! teams ) {
uploadedSoftware = await sails . uploadOne ( newSoftware , { bucket : sails . config . uploads . bucketWithPostfix } ) ;
let datelessFilename = uploadedSoftware . filename . replace ( /^\d{4}-\d{2}-\d{2}\s/ , '' ) ;
// Build a dictonary of information about this software to return to the softwares page.
let newSoftwareInfo = {
name : datelessFilename ,
platform : _ . endsWith ( datelessFilename , '.deb' ) ? 'Linux' : _ . endsWith ( datelessFilename , '.pkg' ) ? 'macOS' : 'Windows' ,
createdAt : Date . now ( ) ,
uploadFd : uploadedSoftware . fd ,
uploadMime : uploadedSoftware . type ,
} ;
await UndeployedSoftware . create ( newSoftwareInfo ) ;
} else {
2024-10-22 02:13:06 +00:00
uploadedSoftware = await sails . uploadOne ( newSoftware , { bucket : sails . config . uploads . bucketWithPostfix } ) ;
2024-10-15 15:17:05 +00:00
for ( let teamApid of teams ) {
var WritableStream = require ( 'stream' ) . Writable ;
await sails . cp ( uploadedSoftware . fd , { bucket : sails . config . uploads . bucketWithPostfix } , {
adapter : ( ) => {
return {
ls : undefined ,
rm : undefined ,
read : undefined ,
receive : ( unusedOpts ) => {
// This `_write` method is invoked each time a new file is received
// from the Readable stream (Upstream) which is pumping filestreams
// into this receiver. (filename === `__newFile.filename`).
var receiver _ _ = WritableStream ( { objectMode : true } ) ;
// Create a new drain (writable stream) to send through the individual bytes of this file.
receiver _ _ . _write = ( _ _newFile , encoding , doneWithThisFile ) => {
let axios = require ( 'axios' ) ;
let FormData = require ( 'form-data' ) ;
let form = new FormData ( ) ;
form . append ( 'team_id' , teamApid ) ;
form . append ( 'software' , _ _newFile , {
filename : uploadedSoftware . filename ,
contentType : 'application/octet-stream'
} ) ;
( async ( ) => {
await axios . post ( ` ${ sails . config . custom . fleetBaseUrl } /api/v1/fleet/software/package ` , form , {
headers : {
Authorization : ` Bearer ${ sails . config . custom . fleetApiToken } ` ,
... form . getHeaders ( )
} ,
} ) ;
} ) ( )
. then ( ( ) => {
// console.log('ok supposedly a file is finished uploading');
doneWithThisFile ( ) ;
} )
. catch ( ( err ) => {
doneWithThisFile ( err ) ;
} ) ;
} ; //ƒ
return receiver _ _ ;
}
} ;
}
} )
2024-10-22 02:13:06 +00:00
. intercept ( { response : { status : 409 } } , async ( error ) => {
await sails . rm ( sails . config . uploads . prefixForFileDeletion + uploadedSoftware . fd ) ;
2024-10-15 15:17:05 +00:00
return { 'softwareAlreadyExistsOnThisTeam' : error } ;
2024-10-22 02:13:06 +00:00
} )
. intercept ( { name : 'AxiosError' } , async ( error ) => {
await sails . rm ( sails . config . uploads . prefixForFileDeletion + uploadedSoftware . fd ) ;
2024-12-02 20:46:07 +00:00
sails . log . warn ( ` When attempting to upload a software installer, an unexpected error occurred communicating with the Fleet API, ${ require ( 'util' ) . inspect ( error , { depth : 2 } )} ` ) ;
2024-10-22 02:13:06 +00:00
return { 'softwareUploadFailed' : error } ;
2024-10-15 15:17:05 +00:00
} ) ;
}
// Remove the file from the s3 bucket after it has been sent to the Fleet server.
await sails . rm ( sails . config . uploads . prefixForFileDeletion + uploadedSoftware . fd ) ;
}
return ;
}
} ;