Skip to content

Commit 32185a0

Browse files
author
vasilis Kef
committed
kubernetes support
1 parent b79f1b2 commit 32185a0

File tree

8 files changed

+1285
-161
lines changed

8 files changed

+1285
-161
lines changed

plugin.yaml

+10-6
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ node_types:
111111
Indicate whether the resource exists or if Cloudify should create the resource.
112112
type: boolean
113113
default: false
114-
multicloud:
114+
configured:
115+
description: Indicate whether kubernetes is configured
115116
type: boolean
116117
default: false
117118
resource_id:
@@ -133,14 +134,14 @@ node_types:
133134
interfaces:
134135
cloudify.interfaces.lifecycle:
135136
create:
136-
implementation: mist.plugin.server.create
137+
implementation: mist.plugin.kubernetes.create
137138
inputs:
138139
use_external_resource:
139140
default: false
140141
mist_config:
141142
default: {}
142143
start:
143-
implementation: mist.plugin.server.start
144+
implementation: mist.plugin.kubernetes.start
144145
inputs:
145146
start_retry_interval:
146147
description: Polling interval until the server is active in seconds
@@ -183,6 +184,10 @@ node_types:
183184
Indicate whether the resource exists or if Cloudify should create the resource.
184185
type: boolean
185186
default: false
187+
configured:
188+
description: Indicate whether kubernetes is configured
189+
type: boolean
190+
default: false
186191
resource_id:
187192
description: >
188193
Either the name or ID of the resource in Cloudify. If this is an existing
@@ -202,14 +207,14 @@ node_types:
202207
interfaces:
203208
cloudify.interfaces.lifecycle:
204209
create:
205-
implementation: mist.plugin.server.create
210+
implementation: mist.plugin.kubernetes.create
206211
inputs:
207212
use_external_resource:
208213
default: false
209214
mist_config:
210215
default: {}
211216
start:
212-
implementation: mist.plugin.server.start
217+
implementation: mist.plugin.kubernetes.start
213218
inputs:
214219
start_retry_interval:
215220
description: Polling interval until the server is active in seconds
@@ -246,4 +251,3 @@ node_types:
246251
relationships:
247252
cloudify.mist.relationships.server_connected_to_keypair:
248253
derived_from: cloudify.relationships.connected_to
249-

plugin/connection.py

+56-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from cloudify import ctx
21
from mistclient import MistClient
2+
from cloudify import ctx
33
from cloudify.exceptions import NonRecoverableError
44

55

@@ -8,47 +8,52 @@ class MistConnectionClient(object):
88
"""Provides functions for getting the Mist Client
99
"""
1010

11-
def __init__(self):
11+
def __init__(self, **kwargs):
1212
self._client = None
1313
self._cloud = None
1414
self._machine = None
15-
15+
if kwargs.get("properties"):
16+
self.properties = kwargs.get("properties")
17+
self.ctx = False
18+
else:
19+
self.properties = ctx.node.properties
20+
self.ctx = True
1621
@property
1722
def client(self):
1823
"""Represents the MistConnection Client
1924
"""
2025
if self._client is None:
21-
if ctx.node.properties['mist_config'].get("mist_uri"):
22-
mist_uri = ctx.node.properties['mist_config']["mist_uri"]
26+
if self.properties['mist_config'].get("mist_uri"):
27+
mist_uri = self.properties['mist_config']["mist_uri"]
2328
else:
2429
mist_uri = "https://mist.io"
25-
if ctx.node.properties['mist_config'].get("api_token"):
26-
token = ctx.node.properties['mist_config']['api_token']
30+
if self.properties['mist_config'].get("api_token"):
31+
token = self.properties['mist_config']['api_token']
2732
self._client = MistClient(mist_uri= mist_uri,
2833
api_token= token)
2934
else:
3035
self._client = MistClient(mist_uri= mist_uri,
31-
email=ctx.node.properties['mist_config']['username'],
32-
password=ctx.node.properties['mist_config']['password'])
36+
email=self.properties['mist_config']['username'],
37+
password=self.properties['mist_config']['password'])
3338
return self._client
3439

3540
@property
3641
def cloud(self):
3742
"""Represents the Mist Cloud
3843
"""
3944
if self._cloud is None:
40-
if ctx.node.properties['parameters'].get("cloud_id"):
45+
if self.properties['parameters'].get("cloud_id"):
4146
self._cloud = self.client.clouds(
42-
id=ctx.node.properties['parameters']['cloud_id'])[0]
43-
elif ctx.node.properties['parameters'].get("cloud_name"):
44-
cloud_search = self.client.clouds(search=ctx.node.properties['parameters'][
47+
id=self.properties['parameters']['cloud_id'])[0]
48+
elif self.properties['parameters'].get("cloud_name"):
49+
cloud_search = self.client.clouds(search=self.properties['parameters'][
4550
'cloud_name'])
4651
if len(cloud_search) > 1:
4752
raise NonRecoverableError("Found more then one cloud with name {0}".format(
48-
ctx.node.properties['parameters']['cloud_name']))
53+
self.properties['parameters']['cloud_name']))
4954
elif len(cloud_search) == 0:
5055
raise NonRecoverableError("Did not find cloud with name {0}".format(
51-
ctx.node.properties['parameters']['cloud_name']))
56+
self.properties['parameters']['cloud_name']))
5257
self._cloud = cloud_search[0]
5358
return self._cloud
5459

@@ -57,12 +62,13 @@ def machine(self):
5762
"""Represents a Mist Machine
5863
"""
5964
self.cloud.update_machines()
60-
if ctx.node.properties.get('use_external_resource',''):
61-
ctx.logger.info('use external resource enabled')
62-
if not ctx.node.properties["resource_id"]:
65+
if self.properties.get('use_external_resource',''):
66+
if self.ctx:
67+
ctx.logger.info('use external resource enabled')
68+
if not self.properties["resource_id"]:
6369
raise NonRecoverableError(
6470
"Cannot use external resource without defining resource_id")
65-
machines = self.cloud.machines(id=ctx.node.properties["resource_id"])
71+
machines = self.cloud.machines(id=self.properties["resource_id"])
6672
if not len(machines):
6773
raise NonRecoverableError(
6874
"External resource not found")
@@ -71,15 +77,41 @@ def machine(self):
7177
"External resource state {0}".format(machines[0].info["state"]))
7278
return machines[0]
7379

80+
if self.ctx:
81+
if ctx.instance.runtime_properties.get('machine_id'):
82+
return self.cloud.machines(id=ctx.instance.runtime_properties['machine_id'])[0]
83+
machines = self.cloud.machines(search=self.properties['parameters']["name"])
84+
if len(machines) > 1:
85+
if self.ctx:
86+
ctx.logger.info('Found multiple machines with the same name')
87+
for m in machines:
88+
if m.info["state"] in ["running", "stopped"]:
89+
machines[0] = m
90+
break
91+
if self.ctx:
92+
ctx.instance.runtime_properties['machine_id']=machines[0].info["id"]
93+
return machines[0]
94+
95+
96+
def other_machine(self, kwargs):
97+
self.cloud.update_machines()
98+
if kwargs.get('use_external_resource',''):
99+
if not kwargs["resource_id"]:
100+
raise NonRecoverableError(
101+
"Cannot use external resource without defining resource_id")
102+
machines = self.cloud.machines(id=kwargs["resource_id"])
103+
if not len(machines):
104+
raise NonRecoverableError(
105+
"External resource not found")
106+
if machines[0].info["state"] in ["error","terminated"]:
107+
raise NonRecoverableError(
108+
"External resource state {0}".format(machines[0].info["state"]))
109+
return machines[0]
74110

75-
if ctx.instance.runtime_properties.get('machine_id'):
76-
return self.cloud.machines(id=ctx.instance.runtime_properties['machine_id'])[0]
77-
machines = self.cloud.machines(search=ctx.node.properties['parameters']["name"])
111+
machines = self.cloud.machines(search=kwargs["name"])
78112
if len(machines) > 1:
79-
ctx.logger.info('Found multiple machines with the same name')
80113
for m in machines:
81114
if m.info["state"] in ["running","stopped"]:
82115
machines[0] = m
83116
break
84-
ctx.instance.runtime_properties['machine_id']=machines[0].info["id"]
85117
return machines[0]

0 commit comments

Comments
 (0)