|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import atexit |
|
17
|
|
|
import eventlet |
|
18
|
|
|
|
|
19
|
|
|
from pyVim import connect |
|
20
|
|
|
from pyVmomi import vim |
|
21
|
|
|
from st2actions.runners.pythonrunner import Action |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class BaseAction(Action): |
|
25
|
|
|
def __init__(self, config): |
|
26
|
|
|
super(BaseAction, self).__init__(config) |
|
27
|
|
|
if config is None: |
|
28
|
|
|
raise ValueError("No connection configuration details found") |
|
29
|
|
|
if "vsphere" in config: |
|
30
|
|
|
if config['vsphere'] is None: |
|
31
|
|
|
raise ValueError("'vsphere' config defined but empty.") |
|
32
|
|
|
else: |
|
33
|
|
|
pass |
|
34
|
|
|
elif set(['host', 'port', 'user', 'passwd']).issubset(config): |
|
35
|
|
|
pass |
|
36
|
|
|
else: |
|
37
|
|
|
raise ValueError("Check Connection configuration details") |
|
38
|
|
|
|
|
39
|
|
|
def establish_connection(self, vsphere): |
|
40
|
|
|
self.si = self._connect(vsphere) |
|
|
|
|
|
|
41
|
|
|
self.si_content = self.si.RetrieveContent() |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def _connect(self, vsphere): |
|
44
|
|
|
if vsphere: |
|
45
|
|
|
connection = self.config['vsphere'].get(vsphere) |
|
46
|
|
|
else: |
|
47
|
|
|
connection = self.config |
|
48
|
|
|
|
|
49
|
|
|
try: |
|
50
|
|
|
si = connect.SmartConnect(host=connection['host'], |
|
51
|
|
|
port=connection['port'], |
|
52
|
|
|
user=connection['user'], |
|
53
|
|
|
pwd=connection['passwd']) |
|
54
|
|
|
except: |
|
55
|
|
|
raise Exception("Unable to connect to vsphere.") |
|
56
|
|
|
|
|
57
|
|
|
atexit.register(connect.Disconnect, si) |
|
58
|
|
|
return si |
|
59
|
|
|
|
|
60
|
|
|
def _wait_for_task(self, task): |
|
61
|
|
|
while task.info.state == vim.TaskInfo.State.running: |
|
62
|
|
|
eventlet.sleep(1) |
|
63
|
|
|
return task.info.state == vim.TaskInfo.State.success |
|
64
|
|
|
|
It is generally a good practice to initialize all attributes to default values in the
__init__method: