-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSnodePkg.js
131 lines (100 loc) · 3.39 KB
/
OSnodePkg.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
'use strict';
var pkgcloud = require('pkgcloud');
const fs = require('fs');
var os = require('os');
const path = require('path');
var controller = '<ip-addr>';
var createConnection
var conn
class OSPkgRequest {
createConnection() {
conn = {
provider: 'openstack',
username: '<username>',
password: '<userpassword>',
authUrl: 'http://' + controller + ':5000',
region: 'RegionOne'
};
}
templateUpload(template = 'data', callback) {
//Upload a file to swift
this.createConnection();
var storageClient = pkgcloud.storage.createClient(conn);
const templateFile = os.tmpdir().concat(".yaml");
fs.writeFile(templateFile, template, function (err) {
if (err) {
return console.log(err);
} else {
var readStream = fs.createReadStream(templateFile);
var writeStream = storageClient.upload({
container: '<container-name>',
remote: 'abcd.yml'
});
writeStream.on('error', function (err) {
console.error("Erorr:" + JSON.stringify(err, null, 4));
fs.unlink(templateFile);
callback(err);
});
writeStream.on('success', function (file) {
console.log("Template created." + JSON.stringify(file, null, 4));
var newObject = JSON.stringify(file);
console.log("New Object", newObject);
var templateId = file.name;
fs.unlink(templateFile);
callback(newObject);
});
readStream.pipe(writeStream);
}
});
}
createNewStack(templateName) {
//create new stack
setTimeout(function () {
console.log('okkkkz')
}, 2000)
this.createConnection();
console.log("Template Name", templateName);
var orchClient = pkgcloud.orchestration.createClient(conn);
var storageClient = pkgcloud.storage.createClient(conn);
var req = storageClient.download({
container: '<container-name>',
remote: templateName
});
var data = '';
req.setEncoding('utf8');
req.on('data', function (buffer) {
data += buffer;
console.log('stream data ' + buffer);
});
req.on('end', function () {
console.log('final output ' + data);
orchClient.createStack({
name: templateName,
timeout: 60,
template: data
}, function (err1, stack) {
if (err1) {
console.error("Stack failed:" + JSON.stringify(err1, null, 4));
} else {
console.log("Stack created:" + JSON.stringify(stack, null, 4));
}
});
});
}
}
var osm = new OSPkgRequest();
var template = `
heat_template_version: 2015-10-15
description: network and instances
resources:
net-a:
type: OS::Neutron::Net
properties:
name: network-a
`
//console.log(template);
osm.templateUpload(template, function (data) {
console.log(data);
});
console.log('-----------------------\nUpload done\n--------------');
osm.createNewStack('abcd.yml');