This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdriver.py
176 lines (139 loc) · 5.79 KB
/
driver.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2015-2018 Cisco Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from typing import Dict
import os
from molecule import logger
from molecule.api import Driver
from molecule import util
LOG = logger.get_logger(__name__)
class GCE(Driver):
"""
The class responsible for managing `GCE`_ instances. `GCE`_
is `not` the default driver used in Molecule.
GCE is somewhat different than other cloud providers. There is not
an Ansible module for managing ssh keys. This driver assumes the developer
has deployed project wide ssh key.
Molecule leverages Ansible's `gce_module`_, by mapping variables from
``molecule.yml`` into ``create.yml`` and ``destroy.yml``.
.. _`gce_module`: https://docs.ansible.com/ansible/latest/gce_module.html
.. code-block:: yaml
driver:
name: gce
platforms:
- name: instance
.. code-block:: bash
$ pip install molecule[gce]
Change the options passed to the ssh client.
.. code-block:: yaml
driver:
name: gce
ssh_connection_options:
- '-o ControlPath=~/.ansible/cp/%r@%h-%p'
.. important::
Molecule does not merge lists, when overriding the developer must
provide all options.
Provide a list of files Molecule will preserve, relative to the scenario
ephemeral directory, after any ``destroy`` subcommand execution.
.. code-block:: yaml
driver:
name: gce
safe_files:
- foo
.. _`GCE`: https://cloud.google.com/compute/docs/
""" # noqa
def __init__(self, config=None):
super(GCE, self).__init__(config)
self._name = "gce"
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def login_cmd_template(self):
connection_options = " ".join(self.ssh_connection_options)
return (
"ssh {{address}} "
"-l {{user}} "
"-p {{port}} "
"-i {{identity_file}} "
"{}"
).format(connection_options)
@property
def default_safe_files(self):
return [self.instance_config]
@property
def default_ssh_connection_options(self):
return self._get_ssh_connection_options()
def login_options(self, instance_name):
d = {"instance": instance_name}
return util.merge_dicts(d, self._get_instance_config(instance_name))
def ansible_connection_options(self, instance_name):
try:
d = self._get_instance_config(instance_name)
if "instance_os_type" in d:
if d["instance_os_type"] == "linux":
return {
"ansible_user": d["user"],
"ansible_host": d["address"],
"ansible_port": d["port"],
"ansible_private_key_file": d["identity_file"],
"ansible_connection": "ssh",
"ansible_ssh_common_args": " ".join(
self.ssh_connection_options
),
}
if d["instance_os_type"] == "windows":
return {
"ansible_user": d["user"],
"ansible_host": d["address"],
"ansible_password": d["password"],
"ansible_port": d["port"],
"ansible_connection": "winrm",
"ansible_winrm_transport": d["winrm_transport"],
"ansible_winrm_server_cert_validation": d[
"winrm_server_cert_validation"
],
"ansible_become_method": "runas",
}
except StopIteration:
return {}
except IOError:
# Instance has yet to be provisioned , therefore the
# instance_config is not on disk.
return {}
def _get_instance_config(self, instance_name):
instance_config_dict = util.safe_load_file(self._config.driver.instance_config)
return next(
item for item in instance_config_dict if item["instance"] == instance_name
)
def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
def template_dir(self):
"""Return path to its own cookiecutterm templates. It is used by init
command in order to figure out where to load the templates from.
"""
return os.path.join(os.path.dirname(__file__), "cookiecutter")
@property
def required_collections(self) -> Dict[str, str]:
# https://galaxy.ansible.com/google/cloud
return {"google.cloud": "1.0.2", "community.crypto": "1.8.0"}