1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# Copyright (c) 2014-2015 Online SAS and Contributors. All Rights Reserved. |
4
|
|
|
# Edouard Bonlieu <[email protected]> |
5
|
|
|
# Julien Castets <[email protected]> |
6
|
|
|
# Manfred Touron <[email protected]> |
7
|
|
|
# Kevin Deldycke <[email protected]> |
8
|
|
|
# |
9
|
|
|
# Licensed under the BSD 2-Clause License (the "License"); you may not use this |
10
|
|
|
# file except in compliance with the License. You may obtain a copy of the |
11
|
|
|
# License at http://opensource.org/licenses/BSD-2-Clause |
12
|
|
|
|
13
|
|
|
import logging |
14
|
|
|
import os |
15
|
|
|
import time |
16
|
|
|
import itertools |
17
|
|
|
|
18
|
|
|
from juju_scaleway.exceptions import ConfigError, ProviderError |
19
|
|
|
from juju_scaleway.client import Client |
20
|
|
|
|
21
|
|
|
logger = logging.getLogger("juju.scaleway") |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def factory(): |
25
|
|
|
cfg = Scaleway.get_config() |
26
|
|
|
return Scaleway(cfg) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def validate(): |
30
|
|
|
Scaleway.get_config() |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class Scaleway(object): |
34
|
|
|
|
35
|
|
|
def __init__(self, config, client=None): |
36
|
|
|
self.config = config |
37
|
|
|
if client is None: |
38
|
|
|
self.client = Client( |
39
|
|
|
config['access_key'], |
40
|
|
|
config['secret_key']) |
41
|
|
|
|
42
|
|
|
@classmethod |
43
|
|
|
def get_config(cls): |
44
|
|
|
provider_conf = {} |
45
|
|
|
|
46
|
|
|
access_key = os.environ.get('SCALEWAY_ACCESS_KEY') |
47
|
|
|
if access_key: |
48
|
|
|
provider_conf['access_key'] = access_key |
49
|
|
|
|
50
|
|
|
secret_key = os.environ.get('SCALEWAY_SECRET_KEY') |
51
|
|
|
if secret_key: |
52
|
|
|
provider_conf['secret_key'] = secret_key |
53
|
|
|
|
54
|
|
|
if 'access_key' not in provider_conf or \ |
55
|
|
|
'secret_key' not in provider_conf: |
56
|
|
|
raise ConfigError("Missing Scaleway api credentials") |
57
|
|
|
return provider_conf |
58
|
|
|
|
59
|
|
|
def get_servers(self): |
60
|
|
|
return self.client.get_servers() |
61
|
|
|
|
62
|
|
|
def get_server(self, server_id): |
63
|
|
|
return self.client.get_server(server_id) |
64
|
|
|
|
65
|
|
|
def launch_server(self, params): |
66
|
|
|
return self.client.create_server(**params) |
67
|
|
|
|
68
|
|
|
def terminate_server(self, server_id): |
69
|
|
|
self.client.destroy_server(server_id) |
70
|
|
|
|
71
|
|
|
def wait_on(self, server): |
72
|
|
|
# Wait up to 5 minutes, in 30 sec increments |
73
|
|
|
print(server.name) |
74
|
|
|
result = self._wait_on_server(server, 30, 10) |
75
|
|
|
if not result: |
76
|
|
|
raise ProviderError("Could not provision server before timeout") |
77
|
|
|
return result |
78
|
|
|
|
79
|
|
|
def _wait_on_server(self, server, limit, delay=10): |
80
|
|
|
# Redo cci.wait to give user feedback in verbose mode. |
81
|
|
|
for count, new_server in enumerate(itertools.repeat(server.id)): |
82
|
|
|
server = self.get_server(new_server) |
83
|
|
|
if server.state == 'running': |
84
|
|
|
return True |
85
|
|
|
if count >= limit: |
86
|
|
|
return False |
87
|
|
|
if count and count % 3 == 0: |
88
|
|
|
logger.debug( |
89
|
|
|
"Waiting for server:%s ip:%s waited:%ds", |
90
|
|
|
server.name, |
91
|
|
|
server.public_ip['address'] if server.public_ip else None, |
92
|
|
|
count * delay |
93
|
|
|
) |
94
|
|
|
time.sleep(delay) |
95
|
|
|
|