This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsphere.coffee
144 lines (123 loc) · 4.59 KB
/
vsphere.coffee
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
132
133
134
135
136
137
138
139
140
141
142
143
144
module.exports = (env) ->
Promise = env.require 'bluebird'
_ = env.require 'lodash'
nvs = require 'vsphere'
exec = require 'node-ssh-exec'
fs = require 'fs'
class vSphereClient extends env.plugins.Plugin
init: (app, @framework, @config) =>
###
#param @host = target vcenter server or esxi host
#param @user = vcenter or esxi root user
#param @pass = vcenter or esxi root user password
#param @ssl = verify ssl (true|false)
#param @debug = debug output (on|off)
###
@host = @config.host
@user = @config.user
@pass = @config.password
@key = @config.keyFile if @config.keyFile
@ssl = @config.ssl
debug = @config.debug
@vc = new nvs.Client(@host, @user, @pass, @ssl)
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass 'vSphereControl',
configDef: deviceConfigDef.vSphereControl
createCallback: (config, lastState) => new vSphereControl(config, @, lastState)
@framework.deviceManager.on 'discover', (eventData) =>
@framework.deviceManager.discoverMessage('pimatic-vsphere', "scanning vsphere host for virtual machines")
@vc.getVMinContainerPowerState(@vc.serviceContent.rootFolder).once('result', (result) =>
for i, vm of result
do (vm) =>
if debug
env.logger.debug JSON.stringify(vm)
deviceConfig =
id: "vsphere-control-" + vm.name
name: vm.name
class: 'vSphereControl'
state: vm.powerState
vmid: vm.obj[Object.keys(vm.obj)[1]]
@framework.deviceManager.discoveredDevice 'pimatic-vsphere', "#{deviceConfig.name}", deviceConfig
)
@framework.on "after init", =>
mobileFrontend = @framework.pluginManager.getPlugin 'mobile-frontend'
if mobileFrontend?
mobileFrontend.registerAssetFile 'js', "pimatic-vsphere/devices/vspherecontrol-template.coffee"
mobileFrontend.registerAssetFile 'html', "pimatic-vsphere/devices/vspherecontrol-template.html"
else
env.logger.warn "vshpere could not find the mobile-frontend. No gui will be available"
class vSphereControl extends env.devices.Device
template: "vSphereControl"
attributes:
state:
description: "virtual machine state"
type: "string"
actions:
powerAction:
params:
action:
type: "string"
sshCommand:
params:
command:
type: "string"
###
#param @config device configuration
#param @plugin used for plugin wide debug settings (true or false)
#param @lastStae is the last state saved from sqlite db
###
constructor: (@config, @plugin, lastState) ->
###
#param @id = device id set by user
#param @name = device name set by user
#param @state = virtual machine power state
#param @debug = plugin wide debug setting (true or false)
#param @timers used for clearing Intervals on destroy
###
@id = @config.id
@name = @config.name
@state = lastState?.state?.value or @config.state
@vmid = @config.vmid
@debug = @plugin.config.debug
@timers = []
@timers.push setInterval( ( =>
@getState()
), @config.interval
)
super(@config, @plugin, lastState)
destroy: () ->
for timerId in @timers
clearInterval timerId
super()
getState: () ->
if @debug
env.logger.debug @state
Promise.resolve @state
powerAction: (action) ->
if @debug
env.logger.debug "button #{action} for #{@config.name} was pressed"
switch action.toString()
when "on" then @sshCommand("vim-cmd vmsvc/power.on #{@config.vmid}")
when "off" then @sshCommand("vim-cmd vmsvc/power.off #{@config.vmid}")
when "restart" then @sshCommand("vim-cmd vmsvc/power.reboot #{@config.vmid}")
sshCommand: (@command) ->
#https://www.npmjs.com/package/node-ssh-exec
if _.isEmpty(@key)
config = {
host: @plugin.host,
username: @plugin.user,
password: @plugin.pass
}
else
config = {
host: @plugin.host,
username: @plugin.user,
privateKey: fs.readFileSync(@key)
}
exec(config, @command, (error, response) ->
if error
env.logger.error error
else
env.logger.info response
)
return new vSphereClient