我正在尝试获取 Rackspace 中特定“堆栈”的“服务器”列表。由于某些服务器位于资源组(嵌套)中,因此我必须对 Stack 资源进行一些处理以获取服务器列表。我通过异步调用一一获取每台服务器。如何将它们捆绑在一起并一次性发送给客户?下面是代码:
app.js
function setClient(credentials)
{
var client = pkgcloud.orchestration.createClient({
provider: 'rackspace',
username: credentials.uname,
apiKey: credentials.key,
region: 'HKG'
});
return client;
}
app.io.route('ready', function(req) {
var client = setClient(req.session.credentials);
client.getStacks(function (err, stacks) {
if (err) {
req.io.emit("error", { error: err.result.error.message });
return;
}
if (stacks.length > 0) {
var stack = stacks[0].name;
getSer(stack, client, req, 'init');
}
req.io.emit("showStacks", { stacks: stacks });
});
});
app.io.route('create_stack', function(req) {
var stack = req.data.stack_name;
var tmpl = req.data.content;
var client = setClient(req.session.credentials);
client.createStack({
name: stack,
timeout: 60,
template: tmpl // Heat template
}, handle(req));
});
function handle(req) {
return function handle(err, stack)
{
var client = setClient(req.session.credentials);
if (err) {
req.io.emit("error", { error: err.result.error.message });
console.dir(err.result.error.message);
return;
}
client.getStacks(function (err, stacks) {
req.io.emit("showStacks", { stacks: stacks });
});
req.io.emit("status", {status: 'Stack created'});
// Wait for status: CREATE_COMPLETE on our stack, and then callback
stack.setWait({ status: 'CREATE_COMPLETE' }, 5000, function (err) {
if (err) {
req.io.emit("error", { error: err.result.error.message });
console.dir(err);
return;
}
client.getStacks(function (err, stacks) {
req.io.emit("showStacks", { stacks: stacks });
});
client.getStack(stack, function (err, stack) {
if (err) {
req.io.emit("error", { error: err.result.error.message });
console.dir(err.result.error.message);
return;
}
req.io.emit("showStack", { stack: stack });
});
getSer(stack, client, req, 'create');
});
}
}
function getSer(stack, client, req, mode)
{
client.getResources(stack, function(err, resources) {
if (err) {
console.dir(err);
req.io.emit("error", { error: err.result.error.message });
return;
}
resources.forEach(function (resource) {
var nestedlink = '';
var nested = false;
if (resource.links != undefined) {
resource.links.forEach(function (link) {
if (link.rel == "nested") {
nestedlink = link.href;
nested = true;
}
});
}
if (nested) {
var stack_name = nestedlink.substr(nestedlink.indexOf('stacks/')+7, nestedlink.lastIndexOf('/'));
getSer(stack_name, client, req, mode);
} else {
var serId = resource.physicalResourceId;
var ser = serClient(req.session.credentials);
ser.getServer(serId, function (err, server) {
if (mode == 'init')
req.io.emit("showServer", { server: server});
else if (mode == 'create')
req.io.emit("stackCreate", { server: server});
});
}
});
});
}
实际上,问题是 pkgcloud 有一个方法“getResources”,它给定堆栈名称返回资源。从中我得到不在组中的服务器的“physical_resource_id”(server_id),但对于通过“OS::Heat::ResourceGroup”创建的服务器,因为资源列表只有一个“嵌套的'链接。所以我从嵌套的“链接”中获取 stack_name 并获取它的 server-id var stack_name = nestedlink.substr(nestedlink.indexOf('stacks/')+7, nestedlink.lastIndexOf('/'));
。如果 pkgcloud 有类似给定堆栈名称的东西,它会返回其中的服务器列表,生活会很容易。