26
26
# On startup, the script subscribes to topics based on the request type of either CSR or Keys
27
27
# publishes the request to corresponding topic and calls RegisterThing.
28
28
29
- parser = argparse .ArgumentParser (description = "Fleet Provisioning sample script." )
30
- parser .add_argument ('--endpoint' , required = True , help = "Your AWS IoT custom endpoint, not including a port. " +
31
- "Ex: \" w6zbse3vjd5b4p-ats.iot.us-west-2.amazonaws.com\" " )
32
- parser .add_argument ('--cert' , help = "File path to your client certificate, in PEM format" )
33
- parser .add_argument ('--key' , help = "File path to your private key file, in PEM format" )
34
- parser .add_argument ('--root-ca' , help = "File path to root certificate authority, in PEM format. " +
35
- "Necessary if MQTT server uses a certificate that's not already in " +
36
- "your trust store" )
37
- parser .add_argument ('--client-id' , default = "test-" + str (uuid4 ()), help = "Client ID for MQTT connection." )
38
- parser .add_argument ('--use-websocket' , default = False , action = 'store_true' ,
39
- help = "To use a websocket instead of raw mqtt. If you " +
40
- "specify this option you must specify a region for signing." )
41
- parser .add_argument ('--signing-region' , default = 'us-east-1' , help = "If you specify --use-web-socket, this " +
42
- "is the region that will be used for computing the Sigv4 signature" )
43
- parser .add_argument ('--proxy-host' , help = "Hostname of proxy to connect to." )
44
- parser .add_argument ('--proxy-port' , type = int , default = 8080 , help = "Port of proxy to connect to." )
45
- parser .add_argument ('--verbosity' , choices = [x .name for x in io .LogLevel ], default = io .LogLevel .NoLogs .name ,
46
- help = 'Logging level' )
47
- parser .add_argument ("--csr" , help = "File path to your client CSR in PEM format" )
48
- parser .add_argument ("--templateName" , help = "Template name" )
49
- parser .add_argument ("--templateParameters" , help = "Values for Template Parameters" )
29
+ # Parse arguments
30
+ import command_line_utils ;
31
+ cmdUtils = command_line_utils .CommandLineUtils ("Fleet Provisioning - Provision device using either the keys or CSR." )
32
+ cmdUtils .add_common_mqtt_commands ()
33
+ cmdUtils .add_common_websocket_commands ()
34
+ cmdUtils .add_common_proxy_commands ()
35
+ cmdUtils .add_common_logging_commands ()
36
+ cmdUtils .register_command ("client_id" , "<str>" , "Client ID to use for MQTT connection (optional, default='test-*')." , default = "test-" + str (uuid4 ()))
37
+ cmdUtils .register_command ("csr" , "<path>" , "Path to CSR in Pem format (optional)." )
38
+ cmdUtils .register_command ("template_name" , "<str>" , "The name of your provisioning template." )
39
+ cmdUtils .register_command ("template_parameters" , "<json>" , "Template parameters json." )
40
+ cmdUtils .update_command ("cert" , new_help_output = "Path to your certificate in PEM format. If this is not set you must specify use_websocket" )
41
+ args = cmdUtils .get_args ()
50
42
51
43
# Using globals to simplify sample code
52
44
is_sample_done = threading .Event ()
53
- args = parser .parse_args ()
54
45
55
46
io .init_logging (getattr (io .LogLevel , args .verbosity ), 'stderr' )
56
47
mqtt_connection = None
@@ -240,7 +231,7 @@ def waitForRegisterThingResponse():
240
231
http_proxy_options = proxy_options ,
241
232
on_connection_interrupted = on_connection_interrupted ,
242
233
on_connection_resumed = on_connection_resumed ,
243
- ca_filepath = args .root_ca ,
234
+ ca_filepath = args .ca_file ,
244
235
client_id = args .client_id ,
245
236
clean_session = False ,
246
237
keep_alive_secs = 30 )
@@ -250,7 +241,7 @@ def waitForRegisterThingResponse():
250
241
endpoint = args .endpoint ,
251
242
cert_filepath = args .cert ,
252
243
pri_key_filepath = args .key ,
253
- ca_filepath = args .root_ca ,
244
+ ca_filepath = args .ca_file ,
254
245
client_id = args .client_id ,
255
246
on_connection_interrupted = on_connection_interrupted ,
256
247
on_connection_resumed = on_connection_resumed ,
@@ -321,7 +312,7 @@ def waitForRegisterThingResponse():
321
312
createcertificatefromcsr_subscribed_rejected_future .result ()
322
313
323
314
324
- registerthing_subscription_request = iotidentity .RegisterThingSubscriptionRequest (template_name = args .templateName )
315
+ registerthing_subscription_request = iotidentity .RegisterThingSubscriptionRequest (template_name = args .template_name )
325
316
326
317
print ("Subscribing to RegisterThing Accepted topic..." )
327
318
registerthing_subscribed_accepted_future , _ = identity_client .subscribe_to_register_thing_accepted (
@@ -352,9 +343,9 @@ def waitForRegisterThingResponse():
352
343
raise Exception ('CreateKeysAndCertificate API did not succeed' )
353
344
354
345
registerThingRequest = iotidentity .RegisterThingRequest (
355
- template_name = args .templateName ,
346
+ template_name = args .template_name ,
356
347
certificate_ownership_token = createKeysAndCertificateResponse .certificate_ownership_token ,
357
- parameters = json .loads (args .templateParameters ))
348
+ parameters = json .loads (args .template_parameters ))
358
349
else :
359
350
print ("Publishing to CreateCertificateFromCsr..." )
360
351
csrPath = open (args .csr , 'r' ).read ()
@@ -369,9 +360,9 @@ def waitForRegisterThingResponse():
369
360
raise Exception ('CreateCertificateFromCsr API did not succeed' )
370
361
371
362
registerThingRequest = iotidentity .RegisterThingRequest (
372
- template_name = args .templateName ,
363
+ template_name = args .template_name ,
373
364
certificate_ownership_token = createCertificateFromCsrResponse .certificate_ownership_token ,
374
- parameters = json .loads (args .templateParameters ))
365
+ parameters = json .loads (args .template_parameters ))
375
366
376
367
print ("Publishing to RegisterThing topic..." )
377
368
registerthing_publish_future = identity_client .publish_register_thing (registerThingRequest , mqtt .QoS .AT_LEAST_ONCE )
0 commit comments