-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateBucket.js
More file actions
97 lines (90 loc) · 3.45 KB
/
Copy pathcreateBucket.js
File metadata and controls
97 lines (90 loc) · 3.45 KB
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
"use strict";
// console.log ('Creating bucket...');
const fs = require ('node:fs');
const headers = {
"Content-Type": "application/json"
};
if (process.env.GORDON_ADMIN_TOKEN) {
headers.Authorization = `Bearer ${process.env.GORDON_ADMIN_TOKEN}`
}
if (process.env.GORDON_ADMIN_TOKEN_FILE) {
headers.Authorization = `Bearer ${fs.readFileSync (process.env.GORDON_ADMIN_TOKEN_FILE).toString ()}`
}
let accessKey = undefined;
async function create (bucketName) {
// this needs to quietly fail so that the logs are not cluttered with nonsense
try {
// create a new bucket
let response = await fetch (`http://${process.env.GORDON_ADMIN_ENDPOINT}/v1/bucket`, {
method: "POST",
headers: headers,
body: JSON.stringify ({
globalAlias: bucketName
})
});
var bucket = await response.json ();
if (bucket.code == "BucketAlreadyExists") {
console.log (bucket.message);
process.exit (0);
}
if (process.argv.includes ("--debug")) {
console.debug ("bucket:", bucket);
}
// create a new key
if (!process.env.GARAGE_ACCESS_KEY_ID) {
response = await fetch (`http://${process.env.GORDON_ADMIN_ENDPOINT}/v1/key`, {
method: "POST",
headers: headers,
body: JSON.stringify ({
name: `${process.env.GORDON_NEW_BUCKET_NAME}-key`
})
});
accessKey = await response.json ();
if (process.argv.includes ("--debug")) {
console.debug ('accessKey:', accessKey);
}
}
// use an existing key
else {
response = await fetch (`http://${process.env.GORDON_ADMIN_ENDPOINT}/v1/key?` + new URLSearchParams({
id: process.env.GARAGE_ACCESS_KEY_ID,
showSecretKey: "true",
}).toString());
accessKey = await response.json ();
console.debug ('accessKey:', accessKey);
}
// allow the key to access the bucket
response = await fetch (`http://${process.env.GORDON_ADMIN_ENDPOINT}/v1/bucket/allow`, {
method: "POST",
headers: headers,
body: JSON.stringify ({
bucketId: bucket.id,
accessKeyId: accessKey.accessKeyId,
permissions: {
read: true,
write: true,
owner: true
}
})
});
if (process.argv.includes ("--debug")) {
console.debug ("response:", response);
}
} catch (error) {
console.error ("Unable to create bucket", process.env.GORDON_NEW_BUCKET_NAME);
process.exit (1);
}
// any good bucket has an id
if (bucket && bucket.id) {
console.log ("=====================================================================================");
// output the required keys
console.log (" bucket:", bucket.globalAliases[0] || bucket.id);
console.log (" access_key_id:", accessKey.accessKeyId);
console.log (" secret_access_key:", accessKey.secretAccessKey);
console.log ("=====================================================================================");
} else {
console.error ("Unable to create bucket", process.env.GORDON_NEW_BUCKET_NAME);
process.exit (1);
}
}
create (process.env.GORDON_NEW_BUCKET_NAME);