Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Fix script crash if no VMs are found
Browse files Browse the repository at this point in the history
Signed-off-by: Kristián Feldsam <feldsam@gmail.com>
  • Loading branch information
feldsam committed May 26, 2017
1 parent 8cae537 commit 2a7b969
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions one-inv
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ program
// connect to one
var one = new opennebula(config.user+':'+config.token, config.address);

if(program.list){
if(program.list) {
getAll(function(err, groups, hosts) {
if(err){
if(err) {
console.error(err);
process.exit(1);
}
Expand All @@ -28,15 +28,15 @@ if(program.list){
})
}

if(program.host){
if(program.host) {
getAll(function(err, groups, hosts) {
if(err){
if(err) {
console.error(err);
process.exit(1);
}

// check if host exists
if(typeof hosts[program.host] === "undefined"){
if(typeof hosts[program.host] === "undefined") {
console.error("Host with name %s does't exists!", program.host);
process.exit(1);
}
Expand All @@ -45,7 +45,7 @@ if(program.host){
})
}

if(!program.list && !program.host){
if(!program.list && !program.host) {
program.help();
}

Expand All @@ -59,6 +59,10 @@ function getAll(callback) {
return callback(err);
}

if(Object.keys(data).length === 0 || typeof data[0] === "undefined") {
return callback("No VMs found!");
}

for (var key in data) {
var vm = data[key];
var labels = vm.USER_TEMPLATE.LABELS;
Expand Down Expand Up @@ -87,11 +91,15 @@ function getAll(callback) {
}
}

if(Object.keys(groups).length === 0) {
return callback("No VMs with labels found (except skiped one)!");
}

callback(null, groups, hosts);
});
}

function getHostName(vm){
function getHostName(vm) {
// get hostname key
if (!config.useVmName && config.hostnameUserTemplateVar !== "" && typeof vm.USER_TEMPLATE[config.hostnameUserTemplateVar] !== "undefined") {
return vm.USER_TEMPLATE[config.hostnameUserTemplateVar];
Expand All @@ -107,14 +115,14 @@ function getHostName(vm){
return vm.NAME;
}

function getVMIp(vm){
function getVMIp(vm) {
var nic = vm.TEMPLATE.NIC;

if(typeof nic.NETWORK_ID !== "undefined"){
if(typeof nic.NETWORK_ID !== "undefined") {
nic = Array(nic);
}

for(k in nic){
for(k in nic) {
var net = nic[k];

// return first
Expand Down

0 comments on commit 2a7b969

Please sign in to comment.