-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
156 lines (142 loc) · 8.18 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const spawn = require('./spawn');
const fs = require('fs');
module.exports = (function () {
function preparePayload(data, prevResult) {
return result => ({ data, result: prevResult ? [].concat(prevResult).concat(result) : result })
}
/*1. Storage devices must be formatted as a physical volume before it can participate in the LVM infrastructure (PV)*/
/**
* @param {string} volumePath one or more space seperated physical volume paths (ex. /dev/std1 /dev/sdd1)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var createPhysicalVolume = (volumePath, data) =>
spawn('pvcreate', [volumePath]).then(preparePayload(data));
/*2. Agregate the physical volume(s) into a single contiguous pool of storage - Volume group (VG)*/
/**
* @param {string} name volume group name
* @param {string} volumePath one or more space separated physical volume paths (ex. /dev/std1 /dev/sdd1)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var createVolumeGroup = (name, volumePath, data) =>
spawn('vgcreate', [name, volumePath, '-y']).then(preparePayload(data));
/*3. Create the logical volume*/
/**
* @param {string} name logical volume name
* @param {string|number} sizeGB size of the LV
* @param {string} groupName name of the volume group
* @param {string} [cmdInput] lvcreate cmd input
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var createLogicalVolume = (name, sizeGB, groupName, cmdInput, data) =>
spawn('lvcreate', ['-L', `${sizeGB}G`, '-n', name, groupName], cmdInput)
.then(preparePayload(data));
/**
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} fileSystem type of file system (ex. ext3)
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var formatLogicalVolume = (name, groupName, fileSystem, physicalVolumeLocation, data) =>
spawn('mkfs', ['-t', fileSystem, `${physicalVolumeLocation}/${groupName}/${name}`])
.then(preparePayload(data))
/**
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} mountPath path of mount point (ex /mnt/name)
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string[] }>} Promise if resolved it will contain the data that was passed to the fn and the cmd results
*/
var mountVolume = (name, groupName, fileSystem, mountPath, physicalVolumeLocation, data) => {
return new Promise(function (resolve, reject) {
fs.exists(mountPath, function (exists) {
const createDirFn = exists ? function () { return Promise.resolve(); } : function () { return spawn('mkdir', [mountPath]) };
createDirFn().then(
result => spawn('mount', ['-t', fileSystem, `${physicalVolumeLocation}/${groupName}/${name}`, mountPath])
.then(preparePayload(data, result))
).then(data => resolve(data)).catch(err => reject(err));
});
});
}
/**
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {string|number} newSize add size of volume (larger than the current size)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var extendVolumeWith = (name, groupName, physicalVolumeLocation, newSize, data) =>
spawn('lvextend', [`-L+${newSize}G`, `${physicalVolumeLocation}/${groupName}/${name}`])
.then(preparePayload(data))
/**
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {string|number} newSize add size of volume (larger than the current size)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var extendVolumeTo = (name, groupName, physicalVolumeLocation, newSize, data) =>
spawn('lvextend', [`-L`, `${newSize}G`, `${physicalVolumeLocation}/${groupName}/${name}`])
.then(preparePayload(data))
/**
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {string|number} newSize remove size of volume (less than the current size)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var reduceVolumeWith = (name, groupName, physicalVolumeLocation, newSize, data) =>
spawn('lvreduce', ['-f', `-L-${newSize}G`, `${physicalVolumeLocation}/${groupName}/${name}`])
.then(preparePayload(data))
/**
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {string|number} newSize remove size of volume (less than the current size)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var reduceVolumeTo = (name, groupName, physicalVolumeLocation, newSize, data) =>
spawn('lvreduce', ['-f', `-L`, `${newSize}G`, `${physicalVolumeLocation}/${groupName}/${name}`])
.then(preparePayload(data))
/**
* @param {string} mountPath path of mount point (ex /mnt/name)
* @param {string} name logical volume name
* @param {string} groupName name of the volume group
* @param {string} physicalVolumeLocation location of the logical volume (ex. /dev)
* @param {any} [data] data to be passed to promise resolve func.
* @return {Promise<{ data: any, result: string[] }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var removeVolume = (mountPath, name, groupName, physicalVolumeLocation, data) =>
spawn('umount', [mountPath]).then(
result => spawn('lvremove', ['-f', `${physicalVolumeLocation}/${groupName}/${name}`])
.then(preparePayload(data, result))
)
/**
* @param {string} groupName name of the volume group
* @return {Promise<{ data: any, result: string }>} Promise if resolved it will contain the data that was passed to the fn and the cmd result
*/
var removeVolumeGroup = (groupName, data) => spawn('vgremove', [groupName]).then(preparePayload(data))
return {
createPhysicalVolume,
createVolumeGroup,
createLogicalVolume,
formatLogicalVolume,
mountVolume,
extendVolumeWith,
extendVolumeTo,
reduceVolumeWith,
reduceVolumeTo,
removeVolume,
removeVolumeGroup
};
}());