setup.py 7.52 KB
Newer Older
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

16
17
18
19
import os
import threading
import logging

20
from agkyra.syncer.utils import join_path, ThreadSafeDict
21
from agkyra.syncer.database import SqliteFileStateDB
22
from agkyra.syncer.messaging import Messager
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
23
from agkyra.syncer import utils
24

25
from kamaki.clients import ClientError, KamakiSSLError
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

from kamaki.clients.astakos import AstakosClient
from kamaki.clients.pithos import PithosClient
from kamaki.clients.utils import https

logger = logging.getLogger(__name__)


DEFAULT_CACHE_NAME = '.agkyra_cache'
DEFAULT_CACHE_HIDE_NAME = 'hidden'
DEFAULT_CACHE_STAGE_NAME = 'staged'
DEFAULT_CACHE_FETCH_NAME = 'fetched'
GLOBAL_SETTINGS_NAME = '.agkyra'
DEFAULT_DBNAME = "syncer.db"
DEFAULT_ACTION_MAX_WAIT = 10
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
41
42
DEFAULT_PITHOS_LIST_INTERVAL = 5
DEFAULT_CONNECTION_RETRY_LIMIT = 3
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
43
INSTANCES_NAME = 'instances'
44
45
46
47

thread_local_data = threading.local()


Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
48
49
50
51
52
def get_instance(elems):
    data = "".join(elems)
    return utils.hash_string(data)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def ssl_fall_back(method):
    """Catch an SSL error while executing a method, patch kamaki and retry"""
    def wrap(self, *args, **kwargs):
        try:
            return method(self, *args, **kwargs)
        except KamakiSSLError as ssle:
            logger.debug('Kamaki SSL failed %s' % ssle)
            logger.info(
                'Kamaki SSL failed, fall back to certifi (mozilla certs)')
            import certifi
            https.patch_with_certs(certifi.where())
            return method(self, *args, **kwargs)
    wrap.__name__ = method.__name__
    wrap.__doc__ = method.__doc__
    return wrap


70
class SyncerSettings():
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
71
    def __init__(self, auth_url, auth_token, container, local_root_path,
72
                 *args, **kwargs):
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
73
        self.auth_url = utils.normalize_standard_suffix(auth_url)
74
        self.auth_token = auth_token
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
75
        self.container = utils.normalize_standard_suffix(container)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
76
77
78
79

        self.ignore_ssl = kwargs.get("ignore_ssl", False)
        if self.ignore_ssl:
            https.patch_ignore_ssl()
80
81
82
        elif kwargs.get('ca_certs', None):
            https.patch_with_certs(kwargs['ca_certs'])

83
84
85
        self.endpoint = self._get_pithos_client(
            auth_url, auth_token, container)

86
87
88
        home_dir = os.path.expanduser('~')
        default_settings_path = join_path(home_dir, GLOBAL_SETTINGS_NAME)
        self.settings_path = kwargs.get("agkyra_path", default_settings_path)
89
        self.create_dir(self.settings_path)
90

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
91
92
93
94
95
96
97
98
99
100
101
        self.instances_path = join_path(self.settings_path, INSTANCES_NAME)
        self.create_dir(self.instances_path)

        self.local_root_path = utils.normalize_local_suffix(local_root_path)
        self.create_dir(self.local_root_path)

        self.user_id = self.endpoint.account
        self.instance = get_instance(
            [self.auth_url, self.user_id,
             self.container, self.local_root_path])
        self.instance_path = join_path(self.instances_path, self.instance)
102
103
104
105
106
107
        self.create_dir(self.instance_path)

        self.dbname = kwargs.get("dbname", DEFAULT_DBNAME)
        self.full_dbname = join_path(self.instance_path, self.dbname)
        self.get_db(initialize=True)

108
        self.cache_name = kwargs.get("cache_name", DEFAULT_CACHE_NAME)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        self.cache_path = join_path(self.local_root_path, self.cache_name)
        self.create_dir(self.cache_path)

        self.cache_hide_name = kwargs.get("cache_hide_name",
                                          DEFAULT_CACHE_HIDE_NAME)
        self.cache_hide_path = join_path(self.cache_path, self.cache_hide_name)
        self.create_dir(self.cache_hide_path)

        self.cache_stage_name = kwargs.get("cache_stage_name",
                                           DEFAULT_CACHE_STAGE_NAME)
        self.cache_stage_path = join_path(self.cache_path,
                                          self.cache_stage_name)
        self.create_dir(self.cache_stage_path)

        self.cache_fetch_name = kwargs.get("cache_fetch_name",
                                           DEFAULT_CACHE_FETCH_NAME)
        self.cache_fetch_path = join_path(self.cache_path,
                                          self.cache_fetch_name)
        self.create_dir(self.cache_fetch_path)

129
        self.heartbeat = ThreadSafeDict()
130
131
        self.action_max_wait = kwargs.get("action_max_wait",
                                          DEFAULT_ACTION_MAX_WAIT)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
132
133
134
135
136
137
138
        self.pithos_list_interval = kwargs.get("pithos_list_interval",
                                               DEFAULT_PITHOS_LIST_INTERVAL)

        self.connection_retry_limit = kwargs.get(
            "connection_retry_limit", DEFAULT_CONNECTION_RETRY_LIMIT)
        self.endpoint.CONNECTION_RETRY_LIMIT = self.connection_retry_limit

139
        self.messager = Messager()
140

141
142
143
144
145
146
147
148
149
        self.mtime_lag = self.determine_mtime_lag()

    def determine_mtime_lag(self):
        st = os.stat(self.cache_path)
        mtime = st.st_mtime
        if mtime.is_integer():
            return 1.1
        return 0

150
151
152
153
154
155
156
157
    def get_db(self, initialize=False):
        dbs = getattr(thread_local_data, "dbs", None)
        if dbs is not None:
            db = dbs.get(self.full_dbname)
        else:
            db = None

        if db is None:
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
158
159
            logger.debug("Connecting db: '%s', thread: %s" %
                         (self.full_dbname, threading.current_thread().ident))
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            db = SqliteFileStateDB(self.full_dbname, initialize=initialize)
            if dbs is None:
                thread_local_data.dbs = {}
            thread_local_data.dbs[self.full_dbname] = db
        return db

    def create_dir(self, path):
        if os.path.exists(path):
            if os.path.isdir(path):
                return
            raise Exception("Cannot create dir '%s'; file exists" % path)
        logger.warning("Creating dir: '%s'" % path)
        os.mkdir(path)
        return path

175
    @ssl_fall_back
176
177
178
179
180
181
182
183
184
185
186
187
188
    def _get_pithos_client(self, auth_url, token, container):
        try:
            astakos = AstakosClient(auth_url, token)
        except ClientError:
            logger.error("Failed to authenticate user token")
            raise
        try:
            PITHOS_URL = astakos.get_endpoint_url(PithosClient.service_type)
        except ClientError:
            logger.error("Failed to get endpoints for Pithos")
            raise
        try:
            account = astakos.user_info['id']
189
            client = PithosClient(PITHOS_URL, token, account, container)
190
191
192
        except ClientError:
            logger.error("Failed to initialize Pithos client")
            raise
193
194
195
196
        try:
            client.get_container_info(container)
        except ClientError as e:
            if e.status == 404:
197
198
                logger.warning(
                    "Container '%s' does not exist, creating..." % container)
199
200
201
202
203
204
205
206
207
                try:
                    client.create_container(container)
                except ClientError:
                    logger.error("Failed to create container '%s'" % container)
                    raise
            else:
                raise

        return client