forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft-storage-overview.js
103 lines (93 loc) · 3.62 KB
/
raft-storage-overview.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
93
94
95
96
97
98
99
100
101
102
103
import Component from '@ember/component';
import { getOwner } from '@ember/application';
import config from '../config/environment';
import { inject as service } from '@ember/service';
export default Component.extend({
flashMessages: service(),
auth: service(),
useServiceWorker: null,
async init() {
this._super(...arguments);
if (this.useServiceWorker === false) {
return;
}
// check to see if we support ServiceWorker
if ('serviceWorker' in navigator) {
// this checks to see if there's an active service worker - if it failed to register
// for any reason, then this would be null
const worker = await navigator.serviceWorker.getRegistration(config.serviceWorkerScope);
if (worker) {
navigator.serviceWorker.addEventListener('message', this.serviceWorkerGetToken.bind(this));
this.set('useServiceWorker', true);
}
}
},
willDestroy() {
if (this.useServiceWorker) {
navigator.serviceWorker.removeEventListener('message', this.serviceWorkerGetToken);
}
this._super(...arguments);
},
serviceWorkerGetToken(event) {
const { action } = event.data;
const [port] = event.ports;
if (action === 'getToken') {
port.postMessage({ token: this.auth.currentToken });
} else {
console.error('Unknown event', event); // eslint-disable-line
port.postMessage({ error: 'Unknown request' });
}
},
actions: {
async removePeer(model) {
let { nodeId } = model;
try {
await model.destroyRecord();
} catch (e) {
let errString = e.errors ? e.errors.join(' ') : e.message || e;
this.flashMessages.danger(`There was an issue removing the peer ${nodeId}: ${errString}`);
return;
}
this.flashMessages.success(`Successfully removed the peer: ${nodeId}.`);
},
downloadViaServiceWorker() {
// the actual download happens when the user clicks the anchor link, and then the ServiceWorker
// intercepts the request and adds auth headers.
// Here we just want to notify users that something is happening before the browser starts the download
this.flashMessages.success('The snapshot download will begin shortly.');
},
async downloadSnapshot() {
// this entire method is the fallback behavior in case the browser either doesn't support ServiceWorker
// or the UI is not being run on https.
// here we're downloading the entire snapshot in memory, creating a dataurl with createObjectURL, and
// then forcing a download by clicking a link that has a download attribute
//
// this is not the default because
let adapter = getOwner(this).lookup('adapter:application');
this.flashMessages.success('The snapshot download has begun.');
let resp, blob;
try {
resp = await adapter.rawRequest('/v1/sys/storage/raft/snapshot', 'GET');
blob = await resp.blob();
} catch (e) {
let errString = e.errors ? e.errors.join(' ') : e.message || e;
this.flashMessages.danger(`There was an error trying to download the snapshot: ${errString}`);
}
let filename = 'snapshot.gz';
let file = new Blob([blob], { type: 'application/x-gzip' });
file.name = filename;
if ('msSaveOrOpenBlob' in navigator) {
navigator.msSaveOrOpenBlob(file, filename);
return;
}
let a = document.createElement('a');
let objectURL = window.URL.createObjectURL(file);
a.href = objectURL;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(objectURL);
},
},
});