forked from peebles/node-convoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (148 loc) · 3.98 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
157
158
159
160
161
162
163
164
165
166
"use strict";
let request = require( 'request' );
class Convoy {
constructor( url ) {
this.opts = {};
this.opts.convoySocket = url || "/var/run/convoy/convoy.sock"
}
_request( method, url, data, cb ) {
request({
url: 'http://unix:' + this.opts.convoySocket+':'+url,
headers:{
host: require( 'os' ).hostname(), // request() sets host to the socket path, which convoy daemon does not like.
},
method: method,
json: data,
}, ( err, res, body ) => {
if ( err ) return cb( err );
if ( res.statusCode >= 300 ) {
let msg = res.statusMessage;
if ( body ) {
if ( typeof body == 'object' )
msg = JSON.stringify( body );
else
msg = body;
}
return cb( new Error( msg ) );
}
if ( ! body ) return cb();
if ( typeof body == 'object' )
return cb( null, body );
else
return cb( null, JSON.parse( body ) );
});
}
// PROBABLY NOT SUPPOSED TO EXPOSE THIS TO THE PUBLIC
volumeMount( args, cb ) {
// {
// VolumeName string
// MountPoint string
// }
args.Verbose = true; // or you don't get JSON response
this._request( 'POST', '/volumes/mount', args, cb );
}
// PROBABLY NOT SUPPOSED TO EXPOSE THIS TO THE PUBLIC
volumeUnmount( args, cb ) {
// {
// VolumeName string
// }
this._request( 'POST', '/volumes/unmount', args, cb );
}
volumeCreate( args, cb ) {
// {
// Name string
// DriverName string (eg: ebs, devicemapper, vfs)
// Size string (eg: "40G", "500M" or "107374182400")
// BackupURL string (create a volume from a backup)
// DriverVolumeID string (??)
// Type string (driver-specific type)
// IOPS int64 (only used for ebs, type=io1)
// PrepareForVM bool (??)
// }
args.Verbose = true; // or you don't get JSON response
if ( args.Size ) {
if ( ! args.Size.match( /^[0-9]+$/ ) ) {
let m = args.Size.toLowerCase().match( /^([0-9]+)([kmgt])$/ );
if ( ! m ) return cb( new Error( 'Size format not recognized: must be a string like "40G"' ) );
let size = Number( m[1] );
let kb = 1024;
let mb = 1024 * kb;
let gb = 1024 * mb;
let tb = 1024 * gb;
switch( m[2] ) {
case 'k': size *= kb; break;
case 'm': size *= mb; break;
case 'g': size *= gb; break;
case 't': size *= tb; break;
}
args.Size = size;
}
else {
args.Size = Number( args.Size );
}
}
this._request( 'POST', '/volumes/create', args, cb );
}
volumeDelete( args, cb ) {
// {
// VolumeName string
// ReferenceOnly bool
// }
this._request( 'DELETE', '/volumes/', args, cb );
}
volumeInspect( args, cb ) {
// {
// VolumeName string
// }
this._request( 'GET', '/volumes/', args, cb );
}
snapshotCreate( args, cb ) {
// {
// Name string
// VolumeName string
// }
args.Verbose = true; // or you don't get JSON response
this._request( 'POST', '/snapshots/create', args, cb );
}
snapshotDelete( args, cb ) {
// {
// SnapshotName string
// }
this._request( 'DELETE', '/snapshots/', args, cb );
}
snapshotInspect( args, cb ) {
// {
// SnapshotName string
// }
this._request( 'GET', '/snapshots/', args, cb );
}
backupCreate( args, cb ) {
// {
// URL string
// SnapshotName string
// }
args.Verbose = true; // or you don't get JSON response
this._request( 'POST', '/backups/create', args, cb );
}
backupDelete( args, cb ) {
// {
// URL string
// }
this._request( 'DELETE', '/backups', args, cb );
}
info( cb ) {
this._request( 'GET', '/info', {}, cb );
}
volumes( cb ) {
this._request( 'GET', '/volumes/list', {}, cb );
}
backups( args, cb ) {
// {
// URL string
// VolumeName string (optional)
// SnapshotName string (optional)
// }
this._request( 'GET', '/backups/list', args, cb );
}
}
module.exports = Convoy;