Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / snfdeploy / fabfile2.py @ 632ea467

History | View | Annotate | Download (8.9 kB)

1
# Too many lines in module pylint: disable-msg=C0302
2
# Too many arguments (7/5) pylint: disable-msg=R0913
3

    
4
# Copyright (C) 2010, 2011, 2012, 2013 GRNET S.A. All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or
7
# without modification, are permitted provided that the following
8
# conditions are met:
9
#
10
#   1. Redistributions of source code must retain the above
11
#      copyright notice, this list of conditions and the following
12
#      disclaimer.
13
#
14
#   2. Redistributions in binary form must reproduce the above
15
#      copyright notice, this list of conditions and the following
16
#      disclaimer in the documentation and/or other materials
17
#      provided with the distribution.
18
#
19
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
20
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A. OR
23
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
# POSSIBILITY OF SUCH DAMAGE.
31
#
32
# The views and conclusions contained in the software and
33
# documentation are those of the authors and should not be
34
# interpreted as representing official policies, either expressed
35
# or implied, of GRNET S.A.
36

    
37
"""
38
Fabric file for snf-deploy
39

40
"""
41

    
42
from __future__ import with_statement
43
from fabric.api import hide, env, settings, local, roles, execute
44
from fabric.operations import run, put, get
45
import fabric
46
import re
47
import os
48
import shutil
49
import tempfile
50
import ast
51
from snfdeploy.lib import debug, Conf, Env, disable_color
52
from snfdeploy.utils import *
53
from snfdeploy import massedit
54
from snfdeploy.components import *
55
from snfdeploy import components
56

    
57

    
58
def setup_env(args, localenv):
59
    """Setup environment"""
60
    print("Loading configuration for synnefo...")
61

    
62
    env.env = localenv
63

    
64
    env.target_node = args.node
65
    env.target_component = args.component
66
    env.target_method = args.method
67
    env.target_role = args.role
68
    env.dry_run = args.dry_run
69
    env.local = args.autoconf
70
    env.key_inject = args.key_inject
71
    env.password = env.env.password
72
    env.user = env.env.user
73
    env.shell = "/bin/bash -c"
74
    env.key_filename = args.ssh_key
75
    env.jsonfile = "/tmp/service.json"
76
    env.force = args.force
77

    
78
    if args.disable_colors:
79
        disable_color()
80

    
81
    env.roledefs = {
82
        "accounts": [env.env.accounts.ip],
83
        "cyclades": [env.env.cyclades.ip],
84
        "pithos": [env.env.pithos.ip],
85
        "cms": [env.env.cms.ip],
86
        "mq": [env.env.mq.ip],
87
        "db": [env.env.db.ip],
88
        "ns": [env.env.ns.ip],
89
        "client": [env.env.client.ip],
90
        "stats": [env.env.stats.ip],
91
        "nfs": [env.env.nfs.ip],
92
    }
93

    
94
    env.enable_lvm = False
95
    env.enable_drbd = False
96
    if ast.literal_eval(env.env.create_extra_disk) and env.env.extra_disk:
97
        env.enable_lvm = True
98
        env.enable_drbd = True
99

    
100
    env.roledefs.update({
101
        "ganeti": env.env.cluster_ips,
102
        "master": [env.env.master.ip],
103
    })
104

    
105

    
106
#
107
#
108
# Those methods retrieve info from existing installation and update env
109
#
110
#
111
@roles("db")
112
def update_env_with_user_info():
113
    user_email = env.env.user_email
114
    result = RunComponentMethod(DB, "get_user_info_from_db")
115
    r = re.compile(r"(\d+)[ |]*(\S+)[ |]*(\S+)[ |]*" + user_email, re.M)
116
    match = r.search(result)
117
    if env.dry_run:
118
        env.user_id, env.user_auth_token, env.user_uuid = \
119
            ("dummy_uid", "dummy_user_auth_token", "dummy_user_uuid")
120
    else:
121
        env.user_id, env.user_auth_token, env.user_uuid = match.groups()
122

    
123

    
124
@roles("accounts")
125
def update_env_with_service_info(service="pithos"):
126
    result = RunComponentMethod(Astakos, "get_services")
127
    r = re.compile(r"(\d+)[ ]*%s[ ]*(\S+)" % service, re.M)
128
    match = r.search(result)
129
    if env.dry_run:
130
        env.service_id, env.service_token = \
131
            ("dummy_service_id", "dummy_service_token")
132
    else:
133
        env.service_id, env.service_token = match.groups()
134

    
135

    
136
@roles("cyclades")
137
def update_env_with_backend_info():
138
    cluster_name = env.env.cluster.fqdn
139
    result = RunComponentMethod(Cyclades, "list_backends")
140
    r = re.compile(r"(\d+)[ ]*%s.*" % cluster_name, re.M)
141
    match = r.search(result)
142
    if env.dry_run:
143
        env.backend_id = "dummy_backend_id"
144
    else:
145
        env.backend_id, = match.groups()
146

    
147

    
148
#
149
#
150
# Those methods act on components after their basic setup
151
#
152
#
153
@roles("cyclades")
154
def add_ganeti_backend():
155
    RunComponentMethod(Cyclades, "add_backend")
156
    execute(update_env_with_backend_info)
157
    RunComponentMethod(Cyclades, "undrain_backend")
158

    
159

    
160
@roles("accounts")
161
def add_synnefo_user():
162
    RunComponentMethod(Astakos, "add_user")
163

    
164

    
165
@roles("accounts")
166
def activate_user():
167
    execute(update_env_with_user_info)
168
    RunComponentMethod(Astakos, "activate_user")
169

    
170

    
171
@roles("accounts")
172
def import_service():
173
    f = env.jsonfile
174
    PutToComponent(Astakos, f + ".local", f)
175
    RunComponentMethod(Astakos, "import_service")
176

    
177

    
178
@roles("ns")
179
def update_ns_for_node(node_info):
180
    RunComponentMethod(NS, "update_ns_for_node", node_info)
181

    
182

    
183
@roles("nfs")
184
def update_exports_for_node(node_info):
185
    RunComponentMethod(NFS, "update_exports", node_info)
186

    
187

    
188
@roles("master")
189
def add_ganeti_node(node_info):
190
    RunComponentMethod(Master, "add_node", node_info)
191

    
192

    
193
@roles("db")
194
def allow_db_access(node_info):
195
    RunComponentMethod(DB, "allow_access_in_db", node_info, "all", "trust")
196

    
197

    
198
@roles("accounts")
199
def set_default_quota():
200
    RunComponentMethod(Astakos, "set_default_quota")
201

    
202

    
203
@roles("cyclades")
204
def add_public_networks():
205
    RunComponentMethod(Cyclades, "add_network")
206
    if ast.literal_eval(env.env.testing_vm):
207
        RunComponentMethod(Cyclades, "add_network6")
208

    
209

    
210
@roles("client")
211
def add_image():
212
    RunComponentMethod(Kamaki, "fetch_image")
213
    RunComponentMethod(Kamaki, "upload_image")
214
    RunComponentMethod(Kamaki, "register_image")
215

    
216

    
217
#
218
#
219
# Those methods do the basic setup of a synnefo role
220
#
221
#
222
@roles("ns")
223
def setup_ns_role():
224
    SetupSynnefoRole("ns")
225

    
226

    
227
@roles("nfs")
228
def setup_nfs_role():
229
    SetupSynnefoRole("nfs")
230

    
231

    
232
@roles("db")
233
def setup_db_role():
234
    SetupSynnefoRole("db")
235
    if ast.literal_eval(env.env.testing_vm):
236
        RunComponentMethod(DB, "make_db_fast")
237

    
238

    
239
@roles("mq")
240
def setup_mq_role():
241
    SetupSynnefoRole("mq")
242

    
243

    
244
@roles("accounts")
245
def setup_astakos_role():
246
    node_info = get_node_info(env.host)
247
    execute(allow_db_access, node_info)
248
    SetupSynnefoRole("astakos")
249
    RunComponentMethod(Astakos, "export_service")
250
    f = env.jsonfile
251
    GetFromComponent(Astakos, f, f + ".local")
252
    execute(import_service)
253

    
254

    
255
@roles("pithos")
256
def setup_pithos_role():
257
    node_info = get_node_info(env.host)
258
    execute(allow_db_access, node_info)
259
    execute(update_env_with_service_info, "pithos")
260
    SetupSynnefoRole("pithos")
261
    RunComponentMethod(Pithos, "export_service")
262
    f = env.jsonfile
263
    GetFromComponent(Pithos, f, f + ".local")
264
    execute(import_service)
265

    
266

    
267
@roles("cyclades")
268
def setup_cyclades_role():
269
    node_info = get_node_info(env.host)
270
    execute(allow_db_access, node_info)
271
    execute(update_env_with_service_info, "cyclades")
272
    SetupSynnefoRole("cyclades")
273
    RunComponentMethod(Cyclades, "export_service")
274
    f = env.jsonfile
275
    GetFromComponent(Cyclades, f, f + ".local")
276
    execute(import_service)
277

    
278

    
279
@roles("cms")
280
def setup_cms_role():
281
    SetupSynnefoRole("cms")
282

    
283

    
284
@roles("ganeti")
285
def setup_ganeti_role():
286
    if not env.host:
287
        return
288
    node_info = get_node_info(env.host)
289
    execute(update_exports_for_node, node_info)
290
    SetupSynnefoRole("ganeti")
291
    execute(add_ganeti_node, node_info)
292
    #FIXME: prepare_lvm ????
293

    
294

    
295
@roles("master")
296
def setup_master_role():
297
    node_info = get_node_info(env.host)
298
    execute(update_exports_for_node, node_info)
299
    execute(update_ns_for_node, env.env.cluster)
300
    SetupSynnefoRole("master")
301

    
302

    
303
@roles("stats")
304
def setup_stats_role():
305
    SetupSynnefoRole("stats")
306

    
307

    
308
@roles("client")
309
def setup_client_role():
310
    execute(update_env_with_user_info)
311
    SetupSynnefoRole("client")
312

    
313

    
314
def setup():
315
    node_info = get_node_info(env.target_node)
316
    if not node_info:
317
        debug("setup", "Please give a valid node identifier")
318
        return
319
    execute(update_ns_for_node, node_info)
320
    env.host = env.host_string = node_info.ip
321
    if env.target_role:
322
        SetupSynnefoRole(env.target_role)
323
        return
324
    if not env.target_component:
325
        debug("setup", "Please give a valid Component")
326
        return
327
    component_class = getattr(components, env.target_component)
328
    if not env.target_method:
329
        debug("setup", "Please give a valid Component method")
330
        return
331
    RunComponentMethod(component_class, env.target_method)