# Provision Script with unique Token

**URL:** <https://forum.genieacs.com/t/provision-script-with-unique-token/4723>\
**Category:** Uncategorized\
**Created:** [November 16, 2023, 12:08pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723 "2023-11-16T12:08:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernan06](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/fernan06/32/598_2.png) [@fernan06](https://forum.genieacs.com/u/fernan06)\
**Post date:** [November 16, 2023, 12:08pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/1 "2023-11-16T12:08:51Z")

</div>

Hello,

I have a problem with a provision script which calls an EXT script.  
The provision script makes a request via an External script with an IMSI.  
The remote API does not return information directly, but returns a Token.

This Token is subsequently used to retrieve the information necessary for provisioning the device.

However, the provisioning script runs several times (to detect a stable state? cf. [Administration FAQ — GenieACS Documentation 1.2.9 documentation](https://docs.genieacs.com/en/latest/administration-faq.html#duplicate-log-entries-when-using-log-function)) therefore I obtain a new Token each time

How can I make the script only run once?

Thank you in advance for your help

Laurent

```auto
const DEBUG = false;
const now = Date.now();

let provisioned = declare("Tags.Basic_Provisioned", {value: 1});
if (provisioned.value !== undefined) {
    DEBUG && log('CPE is (already) provisioned, returning');
    return;
}

let device = declare("DeviceID.ID", {value: 1}).value[0];
 DEBUG && log('DeviceID: ' + device);
device = encodeURI(device);
 DEBUG && log('DeviceID encoded: ' + device);

let ProductClass = declare("DeviceID.ProductClass", {value: 1}).value[0];
let imsi = declare("InternetGatewayDevice.WANDevice.2.X_ATP_WANUMTSInterfaceConfig.IMSI", {value: 1}).value[0];

let token_sdm_info = declare("VirtualParameters.TokenSdmInfo", {value: now}).value[0];
let msisdn = '--';

if(!imsi){ // If imsi is empty => exit
  DEBUG && log('No IMSI returned by the CPE, ');
  return;
} 

let args = {device: device , imsi: imsi };

DEBUG && log('Args ' + JSON.stringify(args));

if(!token_sdm_info){ // If token_sdm_info is empty launch request_sdm_info else query_sdm_info
  DEBUG && log('TokenSdmInfo est vide, launch request_sdm_info()');
  request_sdm_info(args);
} else{
  DEBUG && log('TokenSdmInfo est PRESENT, EXIT');
  return;
  
}

function request_sdm_info(args){ // Demande les infos radius
  //Get the params from api
  DEBUG && log('Launch ext script fnt request_sdm_info');
  
  let config = ext('cpe-config-new', 'request_sdm_info', JSON.stringify(args));  
  DEBUG && log('Config values from API: ' + JSON.stringify(config));

  if (!config) {
    DEBUG && log('No config returned from API');
    push_metrics('Basic_Provisioning_request', JSON.stringify({ "msg": "No answer from NEP", "status": "ko" }));
    return;
  }

  if (config.code !== 202) {
    DEBUG && log('No config returned from API (' + config.message + ')');
    push_metrics('Basic_Provisioning_request', JSON.stringify({ "code": config.code, "msg": config.message, "status": "ko" }));
    return;
  }
  
  let Token = config.data.token;
  DEBUG && log('New Token value ' + Token);
  declare("VirtualParameters.TokenSdmInfo", {value: now}, {value: Token}); // Set Token in VirtualParameters.TokenSdmInfo
  commit();
  DEBUG && log('End Fnt request_sdm_info ');
  push_metrics('Basic_Provisioning_request', JSON.stringify({ "code": config.code, "msg": config.message,"token":Token, "status": "ok" }));
  return; 

}

function push_metrics(event_value, datajson) {
    let metrics_data = { "table": 'acs_events', data: { time: Math.floor(now / 1000), "device_id": device, "dept": 0, "idur": '', "event_type": "basic_provisioning", "event_value": event_value, "data": datajson } };
    DEBUG && log('Metrics_data ' + JSON.stringify(metrics_data));

    let config = ext('push-metrics-tms', 'metrics', JSON.stringify(metrics_data));
    DEBUG && log('Callback from API: ' + JSON.stringify(config));
}

```

---

<div class="post-metadata">

**Author:** ![rudymartin](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rudymartin](https://forum.genieacs.com/u/rudymartin)\
**Post date:** [November 16, 2023, 1:04pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/2 "2023-11-16T13:04:37Z")

</div>

very simple: you don’t call the api from the extension. Something as I suggested [here](https://forum.genieacs.com/t/extension-timeout-even-with-callback/2787/2)

---

<div class="post-metadata">

**Author:** ![fernan06](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/fernan06/32/598_2.png) [@fernan06](https://forum.genieacs.com/u/fernan06)\
**Post date:** [November 16, 2023, 1:31pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/3 "2023-11-16T13:31:23Z")

</div>

Hi,

Thanks for your return.  
The idea is good, but I have to make do with existing infrastructure.

To provide context, the ACS server is completely isolated from the rest of the IS (BDD).  
The data for provisioning is retrieved via a server (Swagger + RabbitMq) which receives the requests, gives a Token, after which we query the same server with the token which finally gives us the response with the parameters useful for provisioning.

Laurent

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [November 16, 2023, 5:35pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/4 "2023-11-16T17:35:10Z")

</div>

One way to accomplish this is to set a vparam for the CPE with that token. Then if there is a token set, you know the process is in motion and use that token to correlate things.

---

<div class="post-metadata">

**Author:** ![fernan06](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/fernan06/32/598_2.png) [@fernan06](https://forum.genieacs.com/u/fernan06)\
**Post date:** [November 16, 2023, 6:49pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/5 "2023-11-16T18:49:11Z")

</div>

Hello,

I already use a VParm

```auto
let token_sdm_info = declare("VirtualParameters.TokenSdmInfo", {value: now}).value[0];

```

But the problem is that the EXT script is executed 3 times each time, and therefore I generate 3 Tokens (and I only store the last one)  
Slts,

L.F.

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [November 16, 2023, 9:32pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/6 "2023-11-16T21:32:51Z")

</div>

In your provision script check for a value in the TokenSdmInfo vparam. If one exists, then don’t run your ext script again.

---

<div class="post-metadata">

**Author:** ![fernan06](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/fernan06/32/598_2.png) [@fernan06](https://forum.genieacs.com/u/fernan06)\
**Post date:** [November 17, 2023, 9:35am UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/7 "2023-11-17T09:35:37Z")

</div>

Good morning,

Indeed, this is the operation I expect.

I notice by doing a ps -edf that the EXT provisioning scripts are present 3 times, is this normal?

```auto
ps -edf | grep cpe-config
genieacs 2536350 2536323 0 02:20 ? 00:00:10 node /opt/genieacs/dist/bin/genieacs-ext cpe-config
genieacs 2536369 2536323 0 02:21 ? 00:00:05 node /opt/genieacs/dist/bin/genieacs-ext cpe-config-new
genieacs 2537050 2537026 0 05:57 ? 00:00:03 node /opt/genieacs/dist/bin/genieacs-ext cpe-config-new
genieacs 2537062 2537026 0 05:57 ? 00:00:06 node /opt/genieacs/dist/bin/genieacs-ext cpe-config
genieacs 2537714 2537687 0 09:16 ? 00:00:01 node /opt/genieacs/dist/bin/genieacs-ext cpe-config
genieacs 2537749 2537687 0 09:17 ? 00:00:00 node /opt/genieacs/dist/bin/genieacs-ext cpe-config-new

```

Thanks

Laurent

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [November 17, 2023, 5:53pm UTC](https://forum.genieacs.com/t/provision-script-with-unique-token/4723/8 "2023-11-17T17:53:19Z")

</div>

> [@fernan06](#):
>
> EXT provisioning scripts are present 3 times, is this normal

Yes. I’m going to presume you either have 3 cores, or have changed the config to allocate 3 workers for the NBI process.

Please note that if you make a change to an ext script you will need to restart the NBI process for changes to take effect.
