1
+ const path = require ( 'path' ) ; // Import the path module
2
+ const ENV_FILE_PATH = path . resolve ( __dirname , '../.env' ) ;
3
+
4
+ const dotenv = require ( 'dotenv' ) ;
5
+ dotenv . config ( { path : ENV_FILE_PATH } ) ;
6
+
7
+ const axios = require ( 'axios' ) ;
8
+ const fs = require ( 'fs' ) ;
9
+
10
+
11
+ const API_TOKEN_ENV_VAR = 'INFLUXDB_API_TOKEN' ;
12
+ console . log ( "File path : " + ENV_FILE_PATH ) ;
13
+
14
+
15
+ const adminUsername = process . env . ADMIN_USERNAME ;
16
+ const influxHost = 'http://influxdb:8086' ;
17
+ const adminPassword = process . env . ADMIN_PASSWORD ;
18
+ const org = process . env . ORG_NAME ;
19
+ const bucket = process . env . BUCKET_NAME ;
20
+
21
+ console . log ( adminUsername )
22
+ console . log ( adminPassword )
23
+ console . log ( org )
24
+ console . log ( bucket )
25
+
26
+ async function setupInfluxDB ( ) {
27
+ try {
28
+ // Step 1: Set up the initial admin user, org, and bucket
29
+
30
+ const setupResponse = await axios . post ( `${ influxHost } /api/v2/setup` , {
31
+ username : adminUsername ,
32
+ password : adminPassword ,
33
+ org : org ,
34
+ bucket : bucket ,
35
+ retentionRules : [ {
36
+ type : 'expire' ,
37
+ everySeconds : 0 // Retention policy (0 means infinite)
38
+ } ]
39
+ } ) ;
40
+
41
+ console . log ( 'Setup Response:' , setupResponse . data ) ;
42
+
43
+ // Step 2: Generate an API token
44
+ const tokenResponse = await axios . post ( `${ influxHost } /api/v2/authorizations` , {
45
+ orgID : setupResponse . data . org . id ,
46
+ permissions : [
47
+ {
48
+ action : 'read' ,
49
+ resource : { type : 'buckets' }
50
+ } ,
51
+ {
52
+ action : 'write' ,
53
+ resource : { type : 'buckets' }
54
+ }
55
+ ] ,
56
+ description : "Regular user token"
57
+ } , {
58
+ headers : {
59
+ 'Authorization' : `Token ${ setupResponse . data . auth . token } `
60
+ }
61
+ } ) ;
62
+
63
+ const apiToken = tokenResponse . data . token ;
64
+ console . log ( 'API Token:' , apiToken ) ;
65
+ fs . appendFileSync ( ENV_FILE_PATH , `\n${ API_TOKEN_ENV_VAR } =${ apiToken } \n` ) ;
66
+ dotenv . config ( { path : ENV_FILE_PATH } ) ;
67
+
68
+ } catch ( error ) {
69
+ console . error ( 'Error setting up InfluxDB:' , error . response ? error . response . data : error . message ) ;
70
+ }
71
+ }
72
+
73
+
74
+ module . exports = { setupInfluxDB }
0 commit comments