forked from coderdojo-japan/dojopaas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dojopaas.js
97 lines (91 loc) · 2.87 KB
/
dojopaas.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
var sacloud = require('sacloud');
var fs = require('fs');
var csv = require('comma-separated-values');
var Server = require('./lib/Server');
var list = __dirname + '/servers.csv';
var defaultTag = 'dojopaas'
/// 石狩第二
var zone = "31002";
var api = "https://secure.sakura.ad.jp/cloud/zone/is1b/api/cloud/1.1/"
var plan = "1001" // 1コア、1GBメモリ
var image = "112900757970" // Ubuntu 16.04
var size = 20480; // 20GB
var packetfilterid = '112900922505' // www
sacloud.API_ROOT = api;
var client = sacloud.createClient({
accessToken : process.env.SACLOUD_ACCESS_TOKEN,
accessTokenSecret : process.env.SACLOUD_ACCESS_TOKEN_SECRET,
disableLocalizeKeys: false, // (optional;default:false) false: lower-camelize the property names in response Object
debug : false // (optional;default:false) output debug requests to console.
});
client.createRequest({
method: 'GET',
path : 'server',
body : {
Filter: {
"Tags": defaultTag
}
}
}).send(function(err, result) {
if (err) throw new Error(err);
var servers = [];
for (var i=0; i<result.response.servers.length; i++) {
servers.push(result.response.servers[i].name)
}
fs.readFile(list, 'utf8', function (err, text) {
var result = new csv(text, { header: true }).parse();
var data = [];
var promises = [];
for (var i=0; i<result.length; i++) {
promises.push(new Promise(function(resolve, reject) {
var line = result[i];
if (! servers.some(function(v){ return v === line.name }) ) {
var tags = [defaultTag];
tags.push(line.branch)
var server = new Server(client);
server.create({
zone: zone,
plan: plan,
packetfilterid: packetfilterid,
name: line.name,
description: line.description,
tags: tags,
pubkey: line.pubkey,
image: image,
size: size,
resolve: resolve
})
} else {
resolve();
}
}));
}
Promise.all(promises).then(function() {
client.createRequest({
method: 'GET',
path : 'server',
body : {
Filter: {
"Tags": defaultTag
}
}
}).send(function(err, result) {
var servers = [];
for (var i=0; i<result.response.servers.length; i++) {
servers.push([
result.response.servers[i].name,
result.response.servers[i].interfaces[0].ipAddress,
result.response.servers[i].description
])
}
var list = new csv(servers, {header: ["Name", "IP Address", "Description"]}).encode();
fs.writeFile('instances.csv', list, function(error) {
if (err) throw err;
console.log('The CSV has been saved!');
});
});
}).catch(function(error) {
console.log(error)
});
});
});