forked from shaba/openuds
Upgraded OpenStack
This commit is contained in:
parent
103ad93cdf
commit
6da8d2057e
@ -27,5 +27,5 @@
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
from .ProviderLegacy import ProviderLegacy
|
||||
from .Provider import Provider
|
||||
from .provider_legacy import ProviderLegacy
|
||||
from .provider import OpenStackProvider
|
||||
|
@ -42,8 +42,8 @@ from . import openStack
|
||||
|
||||
# Not imported at runtime, just for type checking
|
||||
if typing.TYPE_CHECKING:
|
||||
from .LiveService import LiveService
|
||||
from .LivePublication import LivePublication
|
||||
from .service import LiveService
|
||||
from .publication import LivePublication
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -102,7 +102,7 @@ class LiveDeployment(UserDeployment): # pylint: disable=too-many-public-methods
|
||||
self._vmid.encode('utf8'),
|
||||
self._reason.encode('utf8'),
|
||||
pickle.dumps(self._queue, protocol=0)
|
||||
]).encode('utf8')
|
||||
])
|
||||
|
||||
def unmarshal(self, data: bytes) -> None:
|
||||
"""
|
@ -35,23 +35,32 @@ import typing
|
||||
from django.utils.translation import ugettext as _
|
||||
from uds.core.ui import gui
|
||||
|
||||
from . import openStack
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def getApi(parameters: typing.Dict[str, str]) -> openStack.Client:
|
||||
from .provider_legacy import ProviderLegacy
|
||||
from .provider import OpenStackProvider
|
||||
from uds.core.environment import Environment
|
||||
|
||||
env = Environment(parameters['ev'])
|
||||
provider: typing.Union[ProviderLegacy, OpenStackProvider]
|
||||
if parameters['legacy'] == 'true':
|
||||
provider = ProviderLegacy(env)
|
||||
else:
|
||||
provider = OpenStackProvider(env)
|
||||
|
||||
provider.unserialize(parameters['ov'])
|
||||
|
||||
return provider.api(parameters['project'], parameters['region'])
|
||||
|
||||
|
||||
def getResources(parameters: typing.Dict[str, str]) -> typing.List[typing.Dict[str, typing.Any]]:
|
||||
'''
|
||||
This helper is designed as a callback for Project Selector
|
||||
'''
|
||||
if parameters['legacy'] == 'true':
|
||||
from .ProviderLegacy import ProviderLegacy as Provider
|
||||
else:
|
||||
from .Provider import Provider
|
||||
from uds.core.environment import Environment
|
||||
logger.debug('Parameters received by getResources Helper: %s', parameters)
|
||||
env = Environment(parameters['ev'])
|
||||
provider = Provider(env)
|
||||
provider.unserialize(parameters['ov'])
|
||||
|
||||
api = provider.api(parameters['project'], parameters['region'])
|
||||
api = getApi(parameters)
|
||||
|
||||
zones = [gui.choiceItem(z, z) for z in api.listAvailabilityZones()]
|
||||
networks = [gui.choiceItem(z['id'], z['name']) for z in api.listNetworks()]
|
||||
@ -73,18 +82,7 @@ def getVolumes(parameters: typing.Dict[str, str]) -> typing.List[typing.Dict[str
|
||||
'''
|
||||
This helper is designed as a callback for Zone Selector
|
||||
'''
|
||||
if parameters['legacy'] == 'true':
|
||||
from .ProviderLegacy import ProviderLegacy as Provider
|
||||
else:
|
||||
from .Provider import Provider
|
||||
from uds.core.environment import Environment
|
||||
logger.debug('Parameters received by getVolumes Helper: %s', parameters)
|
||||
env = Environment(parameters['ev'])
|
||||
provider = Provider(env)
|
||||
provider.unserialize(parameters['ov'])
|
||||
|
||||
api = provider.api(parameters['project'], parameters['region'])
|
||||
|
||||
api = getApi(parameters)
|
||||
# Source volumes are all available for us
|
||||
# volumes = [gui.choiceItem(v['id'], v['name']) for v in api.listVolumes() if v['name'] != '' and v['availability_zone'] == parameters['availabilityZone']]
|
||||
volumes = [gui.choiceItem(v['id'], v['name']) for v in api.listVolumes() if v['name'] != '']
|
||||
|
@ -97,10 +97,11 @@ def getRecurringUrlJson(
|
||||
|
||||
url = j['next']
|
||||
|
||||
RT = typing.TypeVar('RT')
|
||||
|
||||
# Decorators
|
||||
def authRequired(func):
|
||||
def ensurer(obj: 'Client', *args, **kwargs):
|
||||
def authRequired(func: typing.Callable[..., RT]) -> typing.Callable[..., RT]:
|
||||
def ensurer(obj: 'Client', *args, **kwargs) -> RT:
|
||||
obj.ensureAuthenticated()
|
||||
try:
|
||||
return func(obj, *args, **kwargs)
|
||||
@ -111,9 +112,8 @@ def authRequired(func):
|
||||
return ensurer
|
||||
|
||||
|
||||
def authProjectRequired(func):
|
||||
|
||||
def ensurer(obj, *args, **kwargs):
|
||||
def authProjectRequired(func: typing.Callable[..., RT]) -> typing.Callable[..., RT]:
|
||||
def ensurer(obj, *args, **kwargs) -> RT:
|
||||
if obj._projectId is None: # pylint: disable=protected-access
|
||||
raise Exception('Need a project for method {}'.format(func))
|
||||
obj.ensureAuthenticated()
|
||||
@ -196,7 +196,7 @@ class Client: # pylint: disable=too-many-public-methods
|
||||
|
||||
def authPassword(self) -> None:
|
||||
# logger.debug('Authenticating...')
|
||||
data = {
|
||||
data: typing.Dict[str, typing.Any] = {
|
||||
'auth': {
|
||||
'identity': {
|
||||
'methods': [
|
||||
@ -281,7 +281,7 @@ class Client: # pylint: disable=too-many-public-methods
|
||||
)
|
||||
|
||||
@authProjectRequired
|
||||
def listServers(self, detail: bool = False, params: bool = None) -> typing.Iterable[typing.Any]:
|
||||
def listServers(self, detail: bool = False, params: typing.Optional[typing.Dict[str, str]] = None) -> typing.Iterable[typing.Any]:
|
||||
path = '/servers/' + 'detail' if detail is True else ''
|
||||
return getRecurringUrlJson(
|
||||
self._getEndpointFor('compute') + path,
|
||||
@ -437,7 +437,7 @@ class Client: # pylint: disable=too-many-public-methods
|
||||
|
||||
@authProjectRequired
|
||||
def updateSnapshot(self, snapshotId: str, name: typing.Optional[str] = None, description: typing.Optional[str] = None) -> typing.Dict[str, typing.Any]:
|
||||
data = {
|
||||
data: typing.Dict[str, typing.Any] = {
|
||||
'snapshot': {}
|
||||
}
|
||||
if name:
|
||||
|
@ -38,7 +38,7 @@ from uds.core.services import ServiceProvider
|
||||
from uds.core.ui import gui
|
||||
from uds.core.util import validators
|
||||
|
||||
from .LiveService import LiveService
|
||||
from .service import LiveService
|
||||
from . import openStack
|
||||
|
||||
# Not imported at runtime, just for type checking
|
||||
@ -54,7 +54,7 @@ INTERFACE_VALUES: typing.List[gui.ChoiceType] = [
|
||||
]
|
||||
|
||||
|
||||
class Provider(ServiceProvider):
|
||||
class OpenStackProvider(ServiceProvider):
|
||||
"""
|
||||
This class represents the sample services provider
|
||||
|
||||
@ -179,4 +179,4 @@ class Provider(ServiceProvider):
|
||||
second is an String with error, preferably internacionalizated..
|
||||
|
||||
"""
|
||||
return Provider(env, data).testConnection()
|
||||
return OpenStackProvider(env, data).testConnection()
|
@ -40,7 +40,7 @@ from uds.core.services import ServiceProvider
|
||||
from uds.core.ui import gui
|
||||
from uds.core.util import validators
|
||||
|
||||
from .LiveService import LiveService
|
||||
from .service import LiveService
|
||||
from . import openStack
|
||||
|
||||
|
||||
@ -145,7 +145,7 @@ class ProviderLegacy(ServiceProvider):
|
||||
access=self.access.value
|
||||
)
|
||||
|
||||
def sanitizeVmName(self, name):
|
||||
def sanitizeVmName(self, name: str) -> str:
|
||||
return openStack.sanitizeName(name)
|
||||
|
||||
def testConnection(self):
|
@ -31,10 +31,14 @@
|
||||
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
import logging
|
||||
import typing
|
||||
|
||||
from uds.core.services import Publication
|
||||
from uds.core.util.state import State
|
||||
|
||||
# Not imported at runtime, just for type checking
|
||||
if typing.TYPE_CHECKING:
|
||||
from .service import LiveService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -49,7 +53,7 @@ class LivePublication(Publication):
|
||||
_state: str = 'r'
|
||||
_destroyAfter: str = 'n'
|
||||
|
||||
suggestedTime: int = 20 # : Suggested recheck time if publication is unfinished in seconds
|
||||
suggestedTime = 20 # : Suggested recheck time if publication is unfinished in seconds
|
||||
|
||||
def initialize(self):
|
||||
"""
|
||||
@ -67,6 +71,9 @@ class LivePublication(Publication):
|
||||
self._state = 'r'
|
||||
self._destroyAfter = 'n'
|
||||
|
||||
def service(self) -> 'LiveService':
|
||||
return typing.cast('LiveService', super().service())
|
||||
|
||||
def marshal(self) -> bytes:
|
||||
"""
|
||||
returns data from an instance of Sample Publication serialized
|
||||
@ -81,7 +88,7 @@ class LivePublication(Publication):
|
||||
if vals[0] == 'v1':
|
||||
self._name, self._reason, self._templateId, self._state, self._destroyAfter = vals[1:]
|
||||
|
||||
def publish(self):
|
||||
def publish(self) -> str:
|
||||
"""
|
||||
Realizes the publication of the service
|
||||
"""
|
||||
@ -101,7 +108,7 @@ class LivePublication(Publication):
|
||||
|
||||
return State.RUNNING
|
||||
|
||||
def checkState(self):
|
||||
def checkState(self) -> str:
|
||||
"""
|
||||
Checks state of publication creation
|
||||
"""
|
||||
@ -118,32 +125,10 @@ class LivePublication(Publication):
|
||||
|
||||
return State.RUNNING
|
||||
|
||||
def finish(self):
|
||||
"""
|
||||
In our case, finish does nothing
|
||||
"""
|
||||
|
||||
def reasonOfError(self):
|
||||
"""
|
||||
If a publication produces an error, here we must notify the reason why
|
||||
it happened. This will be called just after publish or checkState
|
||||
if they return State.ERROR
|
||||
|
||||
Returns an string, in our case, set at checkState
|
||||
"""
|
||||
def reasonOfError(self) -> str:
|
||||
return self._reason
|
||||
|
||||
def destroy(self):
|
||||
"""
|
||||
This is called once a publication is no more needed.
|
||||
|
||||
This method do whatever needed to clean up things, such as
|
||||
removing created "external" data (environment gets cleaned by core),
|
||||
etc..
|
||||
|
||||
The retunred value is the same as when publishing, State.RUNNING,
|
||||
State.FINISHED or State.ERROR.
|
||||
"""
|
||||
def destroy(self) -> str:
|
||||
# We do not do anything else to destroy this instance of publication
|
||||
if self._state == 'error':
|
||||
return State.ERROR # Nothing to cancel
|
||||
@ -161,17 +146,14 @@ class LivePublication(Publication):
|
||||
|
||||
return State.FINISHED
|
||||
|
||||
def cancel(self):
|
||||
"""
|
||||
Do same thing as destroy
|
||||
"""
|
||||
def cancel(self) -> str:
|
||||
return self.destroy()
|
||||
|
||||
# Here ends the publication needed methods.
|
||||
# Methods provided below are specific for this publication
|
||||
# and will be used by user deployments that uses this kind of publication
|
||||
|
||||
def getTemplateId(self):
|
||||
def getTemplateId(self) -> str:
|
||||
"""
|
||||
Returns the template id associated with the publication
|
||||
"""
|
@ -39,8 +39,8 @@ from uds.core.services import Service, types as serviceTypes
|
||||
from uds.core.util import tools
|
||||
from uds.core.ui import gui
|
||||
|
||||
from .LivePublication import LivePublication
|
||||
from .LiveDeployment import LiveDeployment
|
||||
from .publication import LivePublication
|
||||
from .deployment import LiveDeployment
|
||||
from . import helpers
|
||||
|
||||
|
||||
@ -49,7 +49,9 @@ logger = logging.getLogger(__name__)
|
||||
# Not imported at runtime, just for type checking
|
||||
if typing.TYPE_CHECKING:
|
||||
from . import openStack
|
||||
from .Provider import Provider
|
||||
from .provider import OpenStackProvider
|
||||
from .provider_legacy import ProviderLegacy
|
||||
Provider = typing.Union[OpenStackProvider, ProviderLegacy]
|
||||
|
||||
|
||||
class LiveService(Service):
|
||||
@ -172,6 +174,9 @@ class LiveService(Service):
|
||||
# self.ov.value = self.parent().serialize()
|
||||
# self.ev.value = self.parent().env.key
|
||||
|
||||
def parent(self) -> 'Provider':
|
||||
return typing.cast('Provider', super().parent())
|
||||
|
||||
def initGui(self):
|
||||
"""
|
||||
Loads required values inside
|
||||
@ -193,12 +198,12 @@ class LiveService(Service):
|
||||
@property
|
||||
def api(self) -> 'openStack.Client':
|
||||
if not self._api:
|
||||
self._api = typing.cast('Provider', self.parent()).api(projectId=self.project.value, region=self.region.value)
|
||||
self._api = self.parent().api(projectId=self.project.value, region=self.region.value)
|
||||
|
||||
return self._api
|
||||
|
||||
def sanitizeVmName(self, name: str) -> str:
|
||||
return typing.cast('Provider', self.parent()).sanitizeVmName(name)
|
||||
return self.parent().sanitizeVmName(name)
|
||||
|
||||
def makeTemplate(self, templateName: str, description: typing.Optional[str] = None):
|
||||
# First, ensures that volume has not any running instances
|
Loading…
Reference in New Issue
Block a user