How to read values from db in provision script

I need to trigger firmware upgrade to CPE based on the software version that the CPE reports. Download URL is present in db and would like to read the name, url, filesize, version etc from db in provision script, compare the version and trigger the firmware update to CPE accordingly .

Please let me know how to do this.

Good morning,
I have the same problem.
Did you solve it?

You can do it with an ext script, using moongose for example.

do you have a concrete example to do this?
Thanks for your help

1 Like

Yes, for sure.

First you’ll need create a ext directory in /opt/genieacs, I think you already did it in installation. After in you provision script you can call you external script, this way:

const scriptReturn = ext('filename', 'fileFunction', param1, param2)

In you script you can do whatever you want, like find something at mongo and return, for exemple.

Exemple of an ext script:

const fetch = require('node-fetch')

function configFile(args, callback) {
  let params = JSON.parse(args[0])
  let req = fetch(
    `http://localhost:3333/verification/device-id/${params.deviceId}`,
  ).then((req) => {
    let res = req.json().then((res) => {
      return callback(null, JSON.stringify(res))
    })
  })
}

exports.configFile = configFile

This script fetch to url that’s my API, but you can have your all code here.

Provision script :

let files = ext(‘file-fetch.js’, ‘file’);//, JSON.stringify(args));
log(files);
if (!files) {
log(‘No files returned from API’);
return;
}

Ext script :

const fetch = require(‘node-fetch’)

function file () {

    const response =  fetch('http://192.168.1.12:7557/files');

// const body = response.text();

// console.log(body);
console.log(response);
}
exports.file = file;

I think my script is correct but it gives me the error “Extension timed out”

Check:

  1. If the name of file it’s right
  2. did you restart cmwp service after create the file
  3. your directory its in the right path
  4. Are you sure that const response = fetch('http://192.168.1.12:7557/files'); returns something?

In your script you have no returns, just a console.log()

I want to retrieve the different values ​​of filename below. :

I checked points 1 to 3. OK
For the point 4 you see result in my browser.

Provision script :

let callback = “”
const files = ext(‘file-fetch.js’, ‘file’,JSON.stringify(callback) );
if (!files) {
log(‘No files returned from API’);
log(callback);
return;
}

Ext script :

const fetch = require(‘node-fetch’);

async function file(callback) {
try {
const response = await fetch(‘http://192.168.1.12:7557/files’);
const body = await response.text();
console.log(body);

           return callback(body)


      } catch (error) {
        console.error('Une erreur s\'est produite :', error);
     }

}

exports.file = file;

I have add a command ‘return’ and i same error “Extension timed out”

I did a test with " mongoose " and I got the same error.

Ext script :

Could you please share your all provision script? What args do you give to ext script?

It seems that you don’t need args, so, try this:

async function file(callback) {
    let req = fetch(
        `http://192.168.1.12:7557/files`,
    ).then((req) => {
        let res = req.json().then((res) => {
        return callback(null, JSON.stringify(res))
        })
    })
}
    
exports.file = file;

let callback = “”
const files = ext(‘file-fetch2.js’, ‘file’,callback);//,JSON.stringify(callback) );
if (!files) {
log(‘No files returned from API’);
log(callback);
return;
}

Your script return this error :

name: TypeError
message: callback is not a function
stack: |-
TypeError: callback is not a function
at /opt/genieacs/ext/file-fetch2.js:9:16
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

In your provision try:

const files = ext('file-fetch2', 'file');

if(!files) {
   log('No files returned from API')
}

You don’t need to give extension file (.js)

This doesn’t work I still have the same error

I presume you are trying to automatically update the config/firmware on your devices? A more resilient way is outlined in this post.

Also, every time you change an external script, you have to restart the CWMP process for the changes to be picked up.

Hello yes . I want to retrieve the names of the configuration files added to genieacs so that I can then use the script to retrieve the most recent configuration file for each router by filtering the file names with the serial numbers of the routers. Concretely I want to be able to do versioning. THANKS