summaryrefslogtreecommitdiff
blob: a458505036da940bf93ad0baf3b82bcfe57cd0c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/python -O
#
# /usr/sbin/webapp-config
#       Python script for managing the deployment of web-based
#       applications
#
#       Originally written for the Gentoo Linux distribution
#
# Copyright (c) 1999-2007 Authors
#       Released under v2 of the GNU GPL
#
# Author(s)     Stuart Herbert
#               Renat Lumpau   <rl03@gentoo.org>
#               Gunnar Wrobel  <wrobel@gentoo.org>
#
# ========================================================================

# ========================================================================
# Dependencies
# ------------------------------------------------------------------------

import os, os.path

from WebappConfig.debug        import OUT
from WebappConfig.worker       import WebappRemove, WebappAdd
from WebappConfig.permissions  import get_group, get_user

from WebappConfig.wrapper      import package_installed

# ========================================================================
# Server classes
# ------------------------------------------------------------------------

class Basic:

    name   = 'Basic Server'
    desc   = 'supports installation on all webservers'
    dep    = ''

    def set_server_user(self):
        self.vhost_server_uid = get_user(0)
        self.vhost_server_gid = get_group(0)

    def __init__(self,
                 directories,
                 permissions,
                 handler,
                 flags,
                 pm):

        if self.dep and not self.supported(pm):
            print(self.dep)
            OUT.die('Your configuration file sets the server type "' + self.name
                    + '"\nbut the corresponding package does not seem to be '
                    'installed!\nPlease "emerge ' + self.dep + '" or correct '
                    'your settings.')

        try:
            self.set_server_user()
        except KeyError:
            OUT.die('The user for the server type "' + self.name
                    + '" does not exist!')

        self.__sourced   = directories['source']
        self.__destd     = directories['destination']
        self.__hostroot  = directories['hostroot']
        self.__vhostroot = directories['vhostroot']

        # + server owned
        if permissions['file']['server-owned'][0] == 0:
            permissions['file']['server-owned'][0] = self.vhost_server_uid
            permissions['dir']['server-owned'][0]  = self.vhost_server_uid
        if permissions['file']['server-owned'][1] == 0:
            permissions['file']['server-owned'][1] = self.vhost_server_gid
            permissions['dir']['server-owned'][1]  = self.vhost_server_gid
            # and config owned directories have server gid
            permissions['dir']['config-owned'][1]  = self.vhost_server_gid
            # allows server and config owned
            permissions['file']['config-server-owned'][1] = self.vhost_server_gid
            permissions['dir']['config-server-owned'][1]  = self.vhost_server_gid

        self.__perm      = permissions
        self.__handler   = handler
        self.__flags     = flags

        self.__ws        = handler['source']
        self.__content   = handler['content']
        self.__protect   = handler['protect']
        self.__dotconfig = handler['dotconfig']
        self.__ebuild    = handler['ebuild']
        self.__db        = handler['db']

        self.__v         = flags['verbose']
        self.__p         = flags['pretend']

        wd = WebappRemove(self.__content,
                          self.__v,
                          self.__p)

        handler['removal'] = wd

        self.__del       = wd

        # Set by the install function
        self.__add       = None


    def upgrade(self, new_category, new_package, new_version):

        # I have switched the order of upgrades
        # we are now removing the olde app and then installing the new one
        # I am not sure why it was the other way around before
        # and this way seems more intuitive and also has the benefit
        # of working -- rl03

        # first remove the older app

        OUT.info('Removing old version ' + self.__dotconfig.packagename())

        self.clean()

        # now install the new one
        self.__content.set_category(new_category)
        self.__content.set_version(new_version)
        self.__content.set_package(new_package)
        self.__db.set_category(new_category)
        self.__db.set_version(new_version)
        self.__db.set_package(new_package)

        self.install(True)

    def clean(self):

        self.file_behind_flag = False

        OUT.debug('Basic server clean', 7)

        self.file_behind_flag |= self.__del.remove_files()

        self.file_behind_flag |= self.__del.remove_dirs()

        OUT.info('Any files or directories listed above must be removed b'
                 'y hand')

        # okay, let's finish off
        #
        # we don't need the contents file anymore

        self.file_behind_flag |= not self.__content.kill()

        # right - we need to run the hook scripts now
        # if they fail, we don't actually care

        # run the hooks

        self.__ebuild.run_hooks('clean', self)

        # do we need the dotconfig file?
        #
        # if the .webapp file is the only one in the dir, we believe
        # that we can remove it

        self.__dotconfig.kill()

        # is the installation directory empty?

        if not os.listdir(self.__destd) and os.path.isdir(self.__destd):
            if not self.__p:
                os.rmdir(self.__destd)
        else:
            OUT.notice('--- ' + self.__destd)

        # update the list of installs

        self.__db.remove(self.__destd)

        # did we leave anything behind?

        if self.file_behind_flag:
            OUT.warn('Remove whatever is listed above by hand')


    def install(self, upgrade = False):

        self.config_protected_dirs = []

        OUT.debug('Basic server install', 7)

        # The root of the virtual install location needs to exist

        if not os.path.isdir(self.__destd) and not self.__p:

            OUT.debug('Directory missing', 7)

            dir = self.__destd
            dirs = []

            while dir != '/':
                dirs.insert(0, dir)
                dir = os.path.dirname(dir)

            a = self.__perm['dir']['install-owned'][2]('0755')
            OUT.debug('Strange')

            # Create the directories
            for i in dirs:
                if not os.path.isdir(i):
                    os.mkdir(i)
                    os.chmod(i, 
                             self.__perm['dir']['install-owned'][2]('0755'))
                    os.chown(i,
                             self.__perm['dir']['install-owned'][0],
                             self.__perm['dir']['install-owned'][1])

                if self.__v:
                    OUT.info('  Creating installation directory: '
                             + i)

        # Create the handler for installing

        self.__flags['relative'] = True

        wa = WebappAdd(self.__sourced,
                       self.__destd,
                       self.__perm,
                       self.__handler,
                       self.__flags)

        self.__add = wa

        OUT.info('Installing ' + self.__ws.package_name() + '...')

        # we need to create the directories to place our files in
        # and we need to copy in the files

        OUT.info('  Creating required directories', 1)
        OUT.info('  Linking in required files', 1)
        OUT.info('    This can take several minutes for larger apps', 1)

        self.__add.mkdirs()

        self.config_protected_dirs += self.__add.config_protected_dirs

        # Create the second handler for installing the root files

        self.__flags['relative'] = False

        wa = WebappAdd(self.__hostroot,
                       self.__vhostroot,
                       self.__perm,
                       self.__handler,
                       self.__flags)

        self.__add = wa

        self.__add.mkdirs()

        self.config_protected_dirs += self.__add.config_protected_dirs

        OUT.info('  Files and directories installed', 1)

        self.__dotconfig.write(self.__ws.category,
                               self.__ws.pn,
                               self.__ws.pvr,
                               self.__flags['host'],
                               self.__flags['orig'],
                               str(self.__perm['file']['config-owned'][0])
                               + ':' + str(self.__perm['file']['config-owned'][1]),)

        self.__db.add(self.__destd,
                      self.__perm['file']['config-owned'][0],
                      self.__perm['file']['config-owned'][1])

        # run the hooks

        self.__ebuild.run_hooks('install', self)

        # show the post-installation instructions

        if not upgrade:
            self.__ebuild.show_postinst(self)
        else:
            self.__ebuild.show_postupgrade(self)

        # to finish, we need to tell the user if they need to run
        # etc-update or not

        if self.config_protected_dirs:

            # work out whether this directory is part of the
            # CONFIG_PROTECT list or not

            self.__protect.how_to_update(self.config_protected_dirs)

        self.__content.write()

        # and we're done

        OUT.info('Install completed - success', 1)

    def supported(self, pm):
        # I don't think we should be forcing to have a webserver installed -- rl03
        # Maybe, but the test should then be disabled somewhere else.
        # Reverted back to the original version for now -- wrobel
        if self.dep and package_installed(self.dep, pm):
            return True
        return False

class Apache(Basic):

    name   = 'Apache'
    desc   = 'supports installation on Apache 1 & 2'
    dep    = '>=www-servers/apache-1.3'

    def set_server_user(self):
        self.vhost_server_uid = get_user('apache')
        self.vhost_server_gid = get_group('apache')

class Lighttpd(Basic):

    name   = 'Lighttpd'
    desc   = 'supports installation on lighttpd'
    dep    = 'www-servers/lighttpd'

    def set_server_user(self):
        self.vhost_server_uid = get_user('lighttpd')
        self.vhost_server_gid = get_group('lighttpd')

class Cherokee(Basic):

    name   = 'Cherokee'
    desc   = 'supports installation on Cherokee'
    dep    = 'www-servers/cherokee'

    def set_server_user(self):
        self.vhost_server_uid = get_user('cherokee')
        self.vhost_server_gid = get_group('cherokee')

class Nginx(Basic):

    name   = 'Nginx'
    desc   = 'supports installation on Nginx'
    dep    = 'www-servers/nginx'

    def set_server_user(self):          
        self.vhost_server_uid = get_user('nginx')
        self.vhost_server_gid = get_group('nginx')

class Gatling(Basic):

    name   = 'Gatling'
    desc   = 'supports installation on Gatling'
    dep    = 'www-servers/gatling'

    def set_server_user(self):          
        self.vhost_server_uid = get_user('gatling')
        self.vhost_server_gid = get_group('gatling')

class Tracd(Basic):

    name   = 'Tracd'
    desc   = 'supports installation on Trac standalone server'
    dep    = 'www-apps/trac'

    def set_server_user(self):
        self.vhost_server_uid = get_user('tracd')
        self.vhost_server_gid = get_group('tracd')

class uWSGI(Basic):

    name   = 'uWSGI'
    desc   = 'supports installation on uWSGI'
    dep    = 'www-servers/uwsgi'

    def set_server_user(self):
        self.vhost_server_uid = get_user('uwsgi')
        self.vhost_server_gid = get_group('uwsgi')

def listservers():

    OUT.notice('\n'.join(['apache',
                          'lighttpd',
                          'cherokee',
                          'nginx',
                          'gatling',
                          'tracd',
                          'uwsgi',]))