forked from Azure/iothub-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiothub-explorer-export-devices.js
92 lines (82 loc) · 3.84 KB
/
iothub-explorer-export-devices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var program = require('commander');
var azureStorage = require('azure-storage');
var inputError = require('./common.js').inputError;
var serviceError = require('./common.js').serviceError;
var printSuccess = require('./common.js').printSuccess;
var getSas = require('./common.js').getSas;
var Registry = require('azure-iothub').Registry;
program
.description('Export devices in bulk from the device identity registry. The list of devices gets exported to a blob and then downloaded to a local file.')
.usage('[options] --storage <storage-connection-string> --output <file-path>')
.option('-l, --login <connection-string>', 'connection string to use to authenticate with your IoT Hub instance')
.option('-o, --output <file-path>', 'output file containing device descriptions')
.option('-s, --storage <storage-connection-string>', 'Azure blob storage connection string to be used during bulk export')
.option('-x, --exclude-keys', 'exclude symmetric keys in output (keys are included by default)')
.parse(process.argv);
if(!program.storage) inputError('A storage connection string with permissions to create a blob is required');
if(!program.output) inputError('An output file path is required.');
var outputFile = program.output;
var storageConnectionString = program.storage;
var excludeKeys = program.excludeKeys || false;
var sas = getSas(program.login);
var registry = Registry.fromSharedAccessSignature(sas);
var blobSvc = azureStorage.createBlobService(storageConnectionString);
var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);
var outputSharedAccessPolicy = {
AccessPolicy: {
Permissions: 'rwd',
Start: startDate,
Expiry: expiryDate
},
};
var outputContainerName = 'exportcontainer';
var deviceFile = 'devices.txt';
blobSvc.createContainerIfNotExists(outputContainerName, function (error) {
if (error) {
serviceError('Could not create output container: ' + error.message);
} else {
var outputSasToken = blobSvc.generateSharedAccessSignature(outputContainerName, null, outputSharedAccessPolicy);
var outputSasUrl = blobSvc.getUrl(outputContainerName, null, outputSasToken);
registry.exportDevicesToBlob(outputSasUrl, excludeKeys, function (error, result) {
if (error) {
serviceError('Could not start device export job: ' + error.message);
} else {
var jobId = result.jobId;
var interval = setInterval(function () {
registry.getJob(jobId, function (error, result) {
if (error) {
serviceError('Could not get export job status: ' + error.message + ' : ' + error.responseBody);
} else {
var status = result.status;
printSuccess('Job Id: '+ jobId + ': ' + status);
if (status === "completed") {
clearInterval(interval);
blobSvc.getBlobToLocalFile(outputContainerName, deviceFile, outputFile, function(err) {
if(err) {
serviceError('Could not download export to local file: ' + err.message);
} else {
printSuccess('Exported devices to: ' + outputFile);
blobSvc.deleteBlob(outputContainerName, deviceFile, function(err){
if(err) {
serviceError('Could not delete temporary export blob');
} else {
printSuccess('Temporary export blob deleted.');
}
});
}
});
}
}
});
}, 1000);
}
});
}
});