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
|
|
|
CONNECTION_ITEMS = ['host', 'port', 'user', 'passwd'] |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class BaseAction(Action): |
27
|
|
|
def __init__(self, config): |
28
|
|
|
super(BaseAction, self).__init__(config) |
29
|
|
|
if config is None: |
30
|
|
|
raise ValueError("No connection configuration details found") |
31
|
|
|
if "vsphere" in config: |
32
|
|
|
if config['vsphere'] is None: |
33
|
|
|
raise ValueError("'vsphere' config defined but empty.") |
34
|
|
|
else: |
35
|
|
|
pass |
36
|
|
|
else: |
37
|
|
|
for item in CONNECTION_ITEMS: |
38
|
|
|
if item in config: |
39
|
|
|
pass |
40
|
|
|
else: |
41
|
|
|
raise KeyError("Config.yaml Mising: %s" % (item)) |
42
|
|
|
|
43
|
|
|
def establish_connection(self, vsphere): |
44
|
|
|
self.si = self._connect(vsphere) |
|
|
|
|
45
|
|
|
self.si_content = self.si.RetrieveContent() |
|
|
|
|
46
|
|
|
|
47
|
|
|
def _connect(self, vsphere): |
48
|
|
|
if vsphere: |
49
|
|
|
connection = self.config['vsphere'].get(vsphere) |
50
|
|
|
for item in CONNECTION_ITEMS: |
51
|
|
|
if item in connection: |
52
|
|
|
pass |
53
|
|
|
else: |
54
|
|
|
raise KeyError("Config.yaml Mising: vsphere:%s:%s" |
55
|
|
|
% (vsphere, item)) |
56
|
|
|
else: |
57
|
|
|
connection = self.config |
58
|
|
|
|
59
|
|
|
try: |
60
|
|
|
si = connect.SmartConnect(host=connection['host'], |
61
|
|
|
port=connection['port'], |
62
|
|
|
user=connection['user'], |
63
|
|
|
pwd=connection['passwd']) |
64
|
|
|
except Exception as e: |
65
|
|
|
raise Exception(e) |
66
|
|
|
|
67
|
|
|
atexit.register(connect.Disconnect, si) |
68
|
|
|
return si |
69
|
|
|
|
70
|
|
|
def _wait_for_task(self, task): |
71
|
|
|
while task.info.state == vim.TaskInfo.State.running: |
72
|
|
|
eventlet.sleep(1) |
73
|
|
|
return task.info.state == vim.TaskInfo.State.success |
74
|
|
|
|
It is generally a good practice to initialize all attributes to default values in the
__init__
method: