-
Notifications
You must be signed in to change notification settings - Fork 3
/
instantclient.js
288 lines (270 loc) · 7.87 KB
/
instantclient.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env node
var Cheerio = require('cheerio');
var Decompress = require('decompress');
var path = require("path");
var fs = require("fs");
var Readline = require('readline');
var ic_config = require('./instantclient_config.json');
var ic_packages = ['basic','sdk'];
var dir = path.join(process.cwd(), ic_config.folder);
var username;
var password;
var form_v;
var form_request_id;
var form_oam_req;
var form_site2pstoretoken;
var form_locale;
var $;
var rl;
var rl_open = function(){
rl = Readline.createInterface({
input: process.stdin,
output: process.stdout
});
};
var mkdirp = function(dir, cb) {
if (dir === ".") return cb();
fs.stat(dir, function(err) {
if (err == null) return cb(); // already exists
var parent = path.dirname(dir);
mkdirp(parent, function() {
console.log('Directory (' + ic_config.folder + ') created.');
fs.mkdir(dir, cb);
});
});
};
var bytesToSize = function(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file) {
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
var request = require('request');
var j = request.jar();
var beginPrompt = function(){
console.log('');
console.log('Would you like to install the Oracle Instant Client files?');
rl_open();
rl.question('Press (Y) to Install, anything else to Cancel? ', function(answer) {
rl.close();
if(answer != 'Y' && answer != 'y') {
return;
}
agreementPrompt();
});
};
var agreementPrompt = function(){
console.log('');
console.log('You must accept the OTN Development and Distribution License Agreement for Instant Client to download this software.');
console.log('URL to Agreement: http://www.oracle.com/technetwork/licenses/instant-client-lic-152016.html');
rl_open();
rl.question('Press (Y) to Accept the License Agreement, anything else to Cancel? ', function(answer) {
rl.close();
if(answer != 'Y' && answer != 'y') {
console.log('In order to download Oracle Instant Client, you must agree to License Agreeement.');
console.log('');
return;
}
j.setCookie(request.cookie('oraclelicense=accept-ic_winx8664-cookie; domain=.oracle.com'), 'http://download.oracle.com');
credentialsPrompt();
});
};
var credentialsPrompt = function(){
var u = function(){
rl_open();
rl.question('Please enter your username: ', function(answer) {
rl.close();
if(answer) {
username = answer;
p();
}
else {
u();
}
});
},
p = function(){
rl_open();
rl.question('Please enter your password: ', function(answer) {
rl.close();
if(answer) {
password = answer;
get_oam(loginRequest);
}
else {
p();
}
});
};
console.log('');
console.log('You must have an Oracle Account to continue.');
console.log('You can register for a free account here: https://profile.oracle.com/myprofile/account/create-account.jspx');
u();
};
var get_oam = function(cb){
var url = ic_config[ic_config.defaultversion][process.platform][process.arch]['basic'].url;
request({
url: url,
method: 'GET',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
'Referer': 'http://www.oracle.com/technetwork/topics/winx64soft-089540.html',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8'
},
jar: j,
followAllRedirects: true
},
function callback(error, response, body) {
if (!error) {
$ = Cheerio.load(body);
form_v = $('input[name="v"]').val();
form_request_id = $('input[name="request_id"]').val();
form_oam_req = $('input[name="OAM_REQ"]').val();
form_site2pstoretoken = $('input[name="site2pstoretoken"]').val();
form_locale = $('input[name="locale"]').val();
cb();
}
else {
console.error(error);
}
}
);
};
var loginRequest = function() {
request({
url: 'https://login.oracle.com/oam/server/sso/auth_cred_submit',
method: 'POST',
headers: {
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
'Referer': 'https://login.oracle.com/mysso/signon.jsp',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8'
},
jar: j,
followAllRedirects: true,
form: {
'v': form_v,
'request_id': form_request_id,
'ssousername': username,
'password': password,
'OAM_REQ': form_oam_req,
'site2pstoretoken': form_site2pstoretoken,
'locale': form_locale
}
},
function callback(error, response, body) {
if (!error) {
if(response.request.uri.host == 'login.oracle.com') {
console.log('Login failed.');
credentialsPrompt();
}
else createFolder();
}
else console.error(error);
}
);
};
var createFolder = function() {
var createIt = function() {
mkdirp(dir, function(){
ic_packages.forEach(function(pkg){downloadPkg(pkg);});
});
};
if(fs.existsSync(dir)){
console.log('');
console.log('Directory (' + ic_config.folder + ') already exists!');
console.log('If you continue, it will be deleted to allow for the download and extraction of the latest files.');
rl_open();
rl.question('Press (Y) to continue, anything else to Cancel? ', function(answer) {
rl.close();
if(answer != 'Y' && answer != 'y') {
console.log('User aborted.');
console.log('');
return;
}
deleteFolderRecursive(dir);
console.log('Directory (' + ic_config.folder + ') deleted.');
createIt();
});
}
else {
createIt();
}
};
var downloadPkg = function(pkg) {
var fileObj = ic_config[ic_config.defaultversion][process.platform][process.arch][pkg];
var fn = fileObj.fn;
var fullPathToFile = path.join(dir, fn);
var url = ic_config[ic_config.defaultversion][process.platform][process.arch][pkg].url;
var fileStream = fs.createWriteStream(fullPathToFile);
request({
url: url,
method: 'GET',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
'Referer': 'https://login.oracle.com/mysso/signon.jsp',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8'
},
jar: j,
followAllRedirects: true,
form: {
'ssousername': username,
'password': password,
'OAM_REQ': form_oam_req
}
},
function callback(error, response, body) {
if (!error) {
console.log(fn + ' downloaded - ' + bytesToSize(response.headers['content-length']));
}
else console.error(error);
}
)
.pipe(fileStream)
.on('close', function() {
new Decompress()
.src(fullPathToFile)
.dest(dir)
.use(Decompress.zip({strip: 1}))
.run(function(error) {
if(error) {
console.error(error);
return;
}
console.log(fn + " extracted");
fs.unlink(fullPathToFile, function(error) {
if(error) {
console.error(error);
return;
}
console.log(fn + " deleted");
});
});
});
};
beginPrompt();