forked from ottomatica/slim
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ottomatica#23 from gjabell/kvm
Add KVM support
- Loading branch information
Showing
13 changed files
with
367 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
const { error } = require('../logger'); | ||
const { error, ok } = require('../logger'); | ||
|
||
const env = require('../env'); | ||
const images = require('../images'); | ||
const micro = require('../micro'); | ||
const providers = require('../providers'); | ||
|
||
const { registry } = env.vars(); | ||
|
||
exports.command = 'delete <vm|image> <name>'; | ||
exports.desc = 'Delete a microkernel image or vm'; | ||
|
||
exports.builder = () => {}; | ||
exports.builder = yargs => { | ||
yargs.options({ | ||
provider: { | ||
alias: 'p', | ||
choices: ['virtualbox', 'hyperkit', 'kvm'], | ||
default: 'virtualbox', | ||
description: 'the vm provider to use', | ||
type: 'string' | ||
} | ||
}); | ||
}; | ||
|
||
exports.handler = async argv => { | ||
const { vm, image, name } = argv; | ||
// both vm and image have the same value | ||
const { vm: command, name, provider } = argv; | ||
|
||
if (vm === 'vm') { | ||
if (name) await micro.delete(name); | ||
} else if (image === 'image') { | ||
if (await images.exists(name, registry)) { | ||
await fs.remove(path.resolve(registry, name)); | ||
} else { | ||
error(`${name} image not found`); | ||
try { | ||
switch (command) { | ||
case 'vm': | ||
await providers[provider].delete(name); | ||
break; | ||
case 'image': | ||
await fs.remove(path.resolve(registry, name)); | ||
break; | ||
} | ||
ok(`${name} deleted`); | ||
} catch (e) { | ||
error(e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,38 @@ | ||
const env = require('../env'); | ||
const images = require('../images'); | ||
const { info } = require('../logger'); | ||
|
||
const { registry } = env.vars(); | ||
const images = require('../images'); | ||
|
||
exports.command = 'images'; | ||
exports.desc = 'List available images'; | ||
|
||
exports.builder = () => {}; | ||
|
||
exports.handler = async () => { | ||
let table = await images.list(registry); | ||
let transformed = table.reduce((table, {image, ...x}) => { | ||
table[image] = x; | ||
return table; | ||
}, {}); | ||
let table = await images.list(); | ||
|
||
if (table.length === 0) { | ||
info('No images'); | ||
return; | ||
} | ||
|
||
let transformed = table | ||
.map(i => ({ | ||
image: i.image, | ||
size: sizeToHumanSize(i.size), | ||
description: i.description, | ||
providers: i.providers.join(', ') | ||
})) | ||
.reduce((table, {image, ...x}) => { | ||
table[image] = x; | ||
return table; | ||
}, {} | ||
); | ||
|
||
console.table(transformed); | ||
}; | ||
|
||
function sizeToHumanSize(size) { | ||
if( size == 0 ) return 0; | ||
var i = Math.floor( Math.log(size) / Math.log(1024) ); | ||
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { info } = require('../logger'); | ||
|
||
const providers = require('../providers'); | ||
|
||
exports.command = 'vms'; | ||
exports.desc = 'List virtual machines'; | ||
|
||
exports.builder = yargs => { | ||
yargs.options({ | ||
provider: { | ||
alias: 'p', | ||
choices: Object.keys(providers), | ||
description: 'the vm provider to use', | ||
type: 'string' | ||
} | ||
}); | ||
}; | ||
|
||
exports.handler = async argv => { | ||
let { provider } = argv; | ||
|
||
let vms = []; | ||
if (provider) { | ||
vms.push(...await providers[provider].list()); | ||
} else { | ||
for (const provider of Object.keys(providers)) { | ||
vms.push(...(await providers[provider].list()).map(v => ({...v, provider: provider}))); | ||
} | ||
} | ||
|
||
if (vms.length === 0) { | ||
info('No virtual machines'); | ||
return; | ||
} | ||
|
||
let transformed = vms.reduce((table, {id, ...x}) => { | ||
table[id] = x; | ||
return table; | ||
}, {}); | ||
|
||
console.table(transformed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// TODO add hyperkit | ||
const kvm = require('./providers/kvm'); | ||
const vbox = require('./providers/virtualbox'); | ||
|
||
const providers = { | ||
'kvm': kvm, | ||
'virtualbox': vbox | ||
}; | ||
|
||
module.exports = providers; |
Oops, something went wrong.