19
19
20
20
logging .basicConfig (level = logging .INFO )
21
21
TIMEOUT = 10
22
+ # Influx commands need to be given the port of InfluxDB inside the container, which is always 8086 unless
23
+ # overridden inside the InfluxDB config
24
+ INFLUX_CONTAINER_PORT = 8086
22
25
23
26
24
27
def parse_arguments () -> Namespace :
@@ -47,7 +50,7 @@ def parse_arguments() -> Namespace:
47
50
return parser .parse_args ()
48
51
49
52
50
- def retrieve_influxDB_token (args ) -> str :
53
+ def retrieve_influxDB_token_json (args ) -> str :
51
54
"""
52
55
Retrieve the created RW token from InfluxDB.
53
56
@@ -61,11 +64,10 @@ def retrieve_influxDB_token(args) -> str:
61
64
"""
62
65
63
66
token_json = ""
64
- dockerExecProcess = ""
65
67
authListCommand = ['docker' , 'exec' , '-t' , args .influxdb_container_name , 'influx' , 'auth' , 'list' , '--json' ]
66
68
if args .server_protocol == "https" :
67
69
authListCommand .append ('--host' )
68
- authListCommand .append ('https://{}:{}' .format (args .influxdb_container_name , args . influxdb_port ))
70
+ authListCommand .append ('https://{}:{}' .format (args .influxdb_container_name , INFLUX_CONTAINER_PORT ))
69
71
70
72
if bool (strtobool (args .skip_tls_verify )):
71
73
authListCommand .append ('--skip-verify' )
@@ -78,48 +80,47 @@ def retrieve_influxDB_token(args) -> str:
78
80
if dockerExecProcess .stderr :
79
81
logging .error (dockerExecProcess .stderr )
80
82
if (len (token_json ) == 0 ):
81
- logging .error ('Failed to retrieve InfluxDB RW token data from Docker! Retrieved token was: {}' .format (token_json ))
83
+ logging .error ('Failed to retrieve InfluxDB RW token data from Docker! Retrieved data was: {}' .format (token_json ))
82
84
exit (1 )
83
- influxdb_rw_token = next ( d for d in json .loads (token_json ) if d [ 'description' ] == 'greengrass_readwrite' ) ['token' ]
84
- if (len (influxdb_rw_token ) == 0 ):
85
- logging .error ('Failed to parse InfluxDB RW token! Retrieved token was: {}' . format ( influxdb_rw_token ) )
85
+ influxdb_token = json .loads (token_json )[ 0 ] ['token' ]
86
+ if (len (influxdb_token ) == 0 ):
87
+ logging .error ('Retrieved InfluxDB tokens was empty!' )
86
88
exit (1 )
87
89
88
- return influxdb_rw_token
90
+ return token_json
89
91
90
92
91
- def listen_to_token_requests (args , influxdb_rw_token ) -> None :
93
+ def listen_to_token_requests (args , influxdb_token_json ) -> None :
92
94
"""
93
95
Setup a new IPC subscription over local pub/sub to listen to token requests and vend tokens.
94
96
95
97
Parameters
96
98
----------
97
99
args(Namespace): Parsed arguments
98
- influxdb_rw_token (str): InfluxDB RW token
100
+ influxdb_token_json (str): InfluxDB token JSON string
99
101
100
102
Returns
101
103
-------
102
104
None
103
105
"""
104
106
105
107
try :
106
- influxDB_data = {}
107
- influxDB_data ['InfluxDBContainerName' ] = args .influxdb_container_name
108
- influxDB_data ['InfluxDBOrg' ] = args .influxdb_org
109
- influxDB_data ['InfluxDBBucket' ] = args .influxdb_bucket
110
- influxDB_data ['InfluxDBPort' ] = args .influxdb_port
111
- influxDB_data ['InfluxDBInterface' ] = args .influxdb_interface
112
- influxDB_data ['InfluxDBRWToken' ] = influxdb_rw_token
113
- influxDB_data ['InfluxDBServerProtocol' ] = args .server_protocol
114
- influxDB_data ['InfluxDBSkipTLSVerify' ] = args .skip_tls_verify
115
- influxDB_json = json .dumps (influxDB_data )
108
+ influxdb_metadata = {}
109
+ influxdb_metadata ['InfluxDBContainerName' ] = args .influxdb_container_name
110
+ influxdb_metadata ['InfluxDBOrg' ] = args .influxdb_org
111
+ influxdb_metadata ['InfluxDBBucket' ] = args .influxdb_bucket
112
+ influxdb_metadata ['InfluxDBPort' ] = args .influxdb_port
113
+ influxdb_metadata ['InfluxDBInterface' ] = args .influxdb_interface
114
+ influxdb_metadata ['InfluxDBServerProtocol' ] = args .server_protocol
115
+ influxdb_metadata ['InfluxDBSkipTLSVerify' ] = args .skip_tls_verify
116
+ influxdb_metadata_json = json .dumps (influxdb_metadata )
116
117
117
118
logging .info ('Successfully retrieved InfluxDB parameters!' )
118
119
119
120
ipc_client = awsiot .greengrasscoreipc .connect ()
120
121
request = SubscribeToTopicRequest ()
121
122
request .topic = args .subscribe_topic
122
- handler = InfluxDBTokenStreamHandler (influxDB_json , args .publish_topic )
123
+ handler = InfluxDBTokenStreamHandler (influxdb_metadata_json , influxdb_token_json , args .publish_topic )
123
124
operation = ipc_client .new_subscribe_to_topic (handler )
124
125
operation .activate (request )
125
126
logging .info ('Successfully subscribed to topic: {}' .format (args .subscribe_topic ))
@@ -138,8 +139,8 @@ def listen_to_token_requests(args, influxdb_rw_token) -> None:
138
139
if __name__ == "__main__" :
139
140
try :
140
141
args = parse_arguments ()
141
- influxdb_rw_token = retrieve_influxDB_token (args )
142
- listen_to_token_requests (args , influxdb_rw_token )
142
+ influxdb_token_json = retrieve_influxDB_token_json (args )
143
+ listen_to_token_requests (args , influxdb_token_json )
143
144
# Keep the main thread alive, or the process will exit.
144
145
while True :
145
146
time .sleep (10 )
0 commit comments