packaging to ship with systemd services and script
[sfa.git] / setup.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4
5 """
6 Installation script for the sfa module
7 """
8
9 # as fas as pushing onto pypi, I have been using this page
10 # http://peterdowns.com/posts/first-time-with-pypi.html
11 # for setting up the whole business
12
13 import sys, os, os.path
14 from glob import glob
15 import shutil
16 from distutils.core import setup
17
18 # check for the correct version of python
19 major, minor = sys.version_info [0:2]
20 if (major, minor) != (2, 7):
21     print ("Sorry, the SFA package is currently available only with python-2.7")
22     exit(1)
23
24
25 # while cleaning up we might not have this..
26 try:
27     from sfa.util.version import version_tag
28 except:
29     version_tag='cleaningup'
30
31 scripts = glob("clientbin/*.py") + [
32     'config/sfa-config-tty',
33     'config/sfa-config',
34     'sfa/server/sfa-start.py',
35     'systemd/sfa-db-init.sh',
36     'sfatables/sfatables',
37     'keyconvert/keyconvert.py',
38     'flashpolicy/sfa_flashpolicy.py',
39 ]
40
41 packages = [
42     'sfa',
43     'sfa/trust',
44     'sfa/storage',
45     'sfa/util',
46     'sfa/server',
47     'sfa/methods',
48     'sfa/generic',
49     'sfa/managers',
50     'sfa/importer',
51     'sfa/rspecs',
52     'sfa/rspecs/elements',
53     'sfa/rspecs/elements/versions',
54     'sfa/rspecs/versions',
55     'sfa/client',
56     'sfa/planetlab',
57     'sfa/nitos',
58     'sfa/dummy',
59     'sfa/openstack',
60     'sfa/federica',
61     'sfa/iotlab',
62     'sfatables',
63     'sfatables/commands',
64     'sfatables/processors',
65     ]
66
67 data_files = [
68     ('/etc/sfa/',
69      [  'config/aggregates.xml',
70         'config/registries.xml',
71         'config/default_config.xml',
72         'config/api_versions.xml',
73         'config/sfi_config',
74         'config/topology',
75         'sfa/managers/pl/pl.rng',
76         'sfa/trust/credential.xsd',
77         'sfa/trust/top.xsd',
78         'sfa/trust/sig.xsd',
79         'sfa/trust/xml.xsd',
80         'sfa/trust/protogeni-rspec-common.xsd',
81         'flashpolicy/sfa_flashpolicy_config.xml',
82     ]),
83     ('/etc/sfatables/matches/', glob('sfatables/matches/*.xml')),
84     ('/etc/sfatables/targets/', glob('sfatables/targets/*.xml')),
85     ('/usr/share/sfa/migrations', glob('sfa/storage/migrations/*.*') ),
86     ('/usr/share/sfa/migrations/versions', glob('sfa/storage/migrations/versions/*') ),
87     ('/usr/share/sfa/examples/', glob('sfa/examples/*' ) + [ 'cron.d/sfa.cron' ] ),
88 ]
89
90
91 initscripts = [ 'sfa' ]
92 if not os.path.isfile('/etc/redhat-release'):
93     initscripts.append('functions.sfa')
94 data_files.append(
95     ('/etc/init.d/',
96      [ "init.d/%s"%x for x in initscripts ]))
97
98
99 services = ['sfa-db', 'sfa-aggregate', 'sfa-registry']
100 data_files.append(
101     ('/usr/lib/systemd/system',
102      ['systemd/{}.service'.format(service)
103       for service in services]))
104
105
106 # sfatables processors
107 processor_files = [f for f in glob('sfatables/processors/*')
108                    if os.path.isfile(f)]
109 data_files.append(('/etc/sfatables/processors/', processor_files))
110 processor_subdirs = [d for d in glob('sfatables/processors/*')
111                      if os.path.isdir(d)]
112 for d in processor_subdirs:
113     etc_dir = os.path.join("/etc/sfatables/processors", os.path.basename(d))
114     d_files = [f for f in glob(d + '/*') if os.path.isfile(f)]
115     data_files.append((etc_dir, processor_files))
116
117
118 if sys.argv[1] in ['uninstall', 'remove', 'delete', 'clean']:
119     python_path = sys.path
120     site_packages_path = [ os.path.join(p, 'sfa') for p in python_path if p.endswith('site-packages')]
121     site_packages_path += [ os.path.join(p, 'sfatables') for p in python_path if p.endswith('site-packages')]
122     remove_dirs = ['/etc/sfa/', '/etc/sfatables'] + site_packages_path
123     remove_bins = [ '/usr/bin/' + os.path.basename(bin) for bin in scripts ]
124     remove_files = (remove_bins
125                     + ["/etc/init.d/{}}".format(x)
126                        for x in initscripts ]
127                     + ["/usr/lib/systemd/system/{}".format(x)
128                        for x in services])
129
130     # remove files
131     def feedback (file, msg):
132         print ("removing", file, "...", msg)
133     for filepath in remove_files:
134         try:
135             os.remove(filepath)
136             feedback(filepath, "success")
137         except:
138             feedback(filepath, "failed")
139     # remove directories
140     for directory in remove_dirs:
141         try:
142             shutil.rmtree(directory)
143             feedback (directory, "success")
144         except:
145             feedback (directory, "failed")
146 else:
147     # avoid repeating what's in the specfile already
148     try:
149         with open("LICENSE.txt") as l:
150             license = l.read()
151     except:
152         license = "Could not open file LICENSE.txt"
153     try:
154         with open("index.html") as r:
155             long_description = r.read()
156     except:
157         long_description = "Unable to read index.html"
158
159     setup(
160         name             = 'sfa',
161         packages         = packages,
162         data_files       = data_files,
163         version          = version_tag,
164         keywords         = ['federation', 'testbeds', 'SFA', 'SfaWrap'],
165         url              = "http://svn.planet-lab.org/wiki/SFATutorial",
166         author           = "Thierry Parmentelat, Tony Mack, Scott Baker",
167         author_email     = "thierry.parmentelat@inria.fr, tmack@princeton.cs.edu, smbaker@gmail.com",
168         download_url     = "http://build.onelab.eu/sfa/{v}/sfa-{v}.tar.gz".format(v=version_tag),
169         description      = "SFA Wrapper with drivers for PlanetLab and IotLab and others",
170         license          = license,
171         long_description = long_description,
172         scripts          = scripts,
173 )