~cloud-init-dev/cloud-init/trunk

188 by Scott Moser
add vi modelines to python files
1
# vi: ts=4 expandtab
26 by Soren Hansen
Update license to GPLv3 (dropping the "or later" bit).
2
#
3
#    Distutils magic for ec2-init
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
4
#
26 by Soren Hansen
Update license to GPLv3 (dropping the "or later" bit).
5
#    Copyright (C) 2009 Canonical Ltd.
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
6
#    Copyright (C) 2012 Yahoo! Inc.
26 by Soren Hansen
Update license to GPLv3 (dropping the "or later" bit).
7
#
8
#    Author: Soren Hansen <soren@canonical.com>
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
9
#    Author: Joshua Harlow <harlowja@yahoo-inc.com>
26 by Soren Hansen
Update license to GPLv3 (dropping the "or later" bit).
10
#
11
#    This program is free software: you can redistribute it and/or modify
12
#    it under the terms of the GNU General Public License version 3, as
13
#    published by the Free Software Foundation.
14
#
15
#    This program is distributed in the hope that it will be useful,
16
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
#    GNU General Public License for more details.
19
#
20
#    You should have received a copy of the GNU General Public License
21
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
22
23
from glob import glob
24
25
import os
1032.1.1 by Joshua Harlow
Only use datafiles and initsys addon outside virtualenvs
26
import sys
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
27
559.2.265 by Joshua Harlow
Use setuptools instead of disttools, this seems to be needed for requirements to work
28
import setuptools
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
29
from setuptools.command.install import install
30
31
from distutils.errors import DistutilsArgError
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
32
559.2.340 by Joshua Harlow
Use the new tool created to get the version.
33
import subprocess
34
559.2.369 by harlowja
Copy the tiny_p from the packager code
35
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
36
def is_f(p):
37
    return os.path.isfile(p)
38
981.2.19 by Scott Moser
pep8 fixes (2 unrelated to this mp)
39
559.2.369 by harlowja
Copy the tiny_p from the packager code
40
def tiny_p(cmd, capture=True):
41
    # Darn python 2.6 doesn't have check_output (argggg)
42
    stdout = subprocess.PIPE
43
    stderr = subprocess.PIPE
44
    if not capture:
45
        stdout = None
46
        stderr = None
47
    sp = subprocess.Popen(cmd, stdout=stdout,
1163.1.1 by Scott Moser
setup.py: pep8/flake8 changes only
48
                          stderr=stderr, stdin=None,
49
                          universal_newlines=True)
559.2.343 by Joshua Harlow
Use the standard utils now in tools for reading requires/dependencies/versions.
50
    (out, err) = sp.communicate()
1000.1.3 by Scott Moser
further remove evidence of pylint.
51
    ret = sp.returncode
800 by Scott Moser
pylint fixes
52
    if ret not in [0]:
1163.1.1 by Scott Moser
setup.py: pep8/flake8 changes only
53
        raise RuntimeError("Failed running %s [rc=%s] (%s, %s)" %
54
                           (cmd, ret, out, err))
559.2.343 by Joshua Harlow
Use the standard utils now in tools for reading requires/dependencies/versions.
55
    return (out, err)
56
981.2.19 by Scott Moser
pep8 fixes (2 unrelated to this mp)
57
1163.1.4 by Scott Moser
update setup.py to install generator
58
def pkg_config_read(library, var):
59
    fallbacks = {
1216.1.6 by Scott Moser
fix setup.py for flake8
60
        'systemd': {
61
            'systemdsystemunitdir': '/lib/systemd/system',
62
            'systemdsystemgeneratordir': '/lib/systemd/system-generators',
63
        }
1163.1.4 by Scott Moser
update setup.py to install generator
64
    }
65
    cmd = ['pkg-config', '--variable=%s' % var, library]
981.1.2 by Dimitri John Ledkov
Use pkg-config
66
    try:
67
        (path, err) = tiny_p(cmd)
1216.1.6 by Scott Moser
fix setup.py for flake8
68
    except Exception:
1163.1.4 by Scott Moser
update setup.py to install generator
69
        return fallbacks[library][var]
981.1.2 by Dimitri John Ledkov
Use pkg-config
70
    return str(path).strip()
71
1163.1.4 by Scott Moser
update setup.py to install generator
72
981.1.2 by Dimitri John Ledkov
Use pkg-config
73
INITSYS_FILES = {
74
    'sysvinit': [f for f in glob('sysvinit/redhat/*') if is_f(f)],
992.1.5 by Harm Weites
fix: To install the new freebsd sysvinit scripts, accept a new
75
    'sysvinit_freebsd': [f for f in glob('sysvinit/freebsd/*') if is_f(f)],
981.1.2 by Dimitri John Ledkov
Use pkg-config
76
    'sysvinit_deb': [f for f in glob('sysvinit/debian/*') if is_f(f)],
1163.1.4 by Scott Moser
update setup.py to install generator
77
    'systemd': [f for f in (glob('systemd/*.service') +
78
                            glob('systemd/*.target')) if is_f(f)],
79
    'systemd.generators': [f for f in glob('systemd/*-generator') if is_f(f)],
981.1.2 by Dimitri John Ledkov
Use pkg-config
80
    'upstart': [f for f in glob('upstart/*') if is_f(f)],
81
}
82
INITSYS_ROOTS = {
83
    'sysvinit': '/etc/rc.d/init.d',
992.1.5 by Harm Weites
fix: To install the new freebsd sysvinit scripts, accept a new
84
    'sysvinit_freebsd': '/usr/local/etc/rc.d',
981.1.2 by Dimitri John Ledkov
Use pkg-config
85
    'sysvinit_deb': '/etc/init.d',
1163.1.4 by Scott Moser
update setup.py to install generator
86
    'systemd': pkg_config_read('systemd', 'systemdsystemunitdir'),
87
    'systemd.generators': pkg_config_read('systemd',
88
                                          'systemdsystemgeneratordir'),
981.1.2 by Dimitri John Ledkov
Use pkg-config
89
    'upstart': '/etc/init/',
90
}
1163.1.4 by Scott Moser
update setup.py to install generator
91
INITSYS_TYPES = sorted([f.partition(".")[0] for f in INITSYS_ROOTS.keys()])
981.1.2 by Dimitri John Ledkov
Use pkg-config
92
992.2.2 by Scott Moser
setup.py: remove read_datafiles, use globals for ETC and USR
93
# Install everything in the right location and take care of Linux (default) and
94
# FreeBSD systems.
95
USR = "/usr"
96
ETC = "/etc"
1025.5.5 by Joshua Harlow
USR_LIB_EXEC varies depending on system
97
USR_LIB_EXEC = "/usr/lib"
1121.2.4 by Daniel Watkins
Add udev rules for Azure ephemeral disks.
98
LIB = "/lib"
992.2.2 by Scott Moser
setup.py: remove read_datafiles, use globals for ETC and USR
99
if os.uname()[0] == 'FreeBSD':
100
    USR = "/usr/local"
1025.5.5 by Joshua Harlow
USR_LIB_EXEC varies depending on system
101
    USR_LIB_EXEC = "/usr/local/lib"
992.2.2 by Scott Moser
setup.py: remove read_datafiles, use globals for ETC and USR
102
    ETC = "/usr/local/etc"
1025.5.5 by Joshua Harlow
USR_LIB_EXEC varies depending on system
103
elif os.path.isfile('/etc/redhat-release'):
104
    USR_LIB_EXEC = "/usr/libexec"
992.2.2 by Scott Moser
setup.py: remove read_datafiles, use globals for ETC and USR
105
25 by Soren Hansen
* Distutils added
106
1032.1.1 by Joshua Harlow
Only use datafiles and initsys addon outside virtualenvs
107
# Avoid having datafiles installed in a virtualenv...
108
def in_virtualenv():
109
    try:
110
        if sys.real_prefix == sys.prefix:
111
            return False
112
        else:
113
            return True
114
    except AttributeError:
115
        return False
116
117
559.2.340 by Joshua Harlow
Use the new tool created to get the version.
118
def get_version():
119
    cmd = ['tools/read-version']
559.2.343 by Joshua Harlow
Use the standard utils now in tools for reading requires/dependencies/versions.
120
    (ver, _e) = tiny_p(cmd)
616.1.1 by Scott Moser
pylint: setup.py
121
    return str(ver).strip()
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
122
123
559.2.343 by Joshua Harlow
Use the standard utils now in tools for reading requires/dependencies/versions.
124
def read_requires():
125
    cmd = ['tools/read-dependencies']
126
    (deps, _e) = tiny_p(cmd)
616.1.1 by Scott Moser
pylint: setup.py
127
    return str(deps).splitlines()
559.2.83 by Joshua Harlow
Update with parsing of a requirments file, changelog for this new refactoring stuff and setup.py for both of those.
128
249 by Scott Moser
fix setup.py to handle directories in doc
129
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
130
# TODO: Is there a better way to do this??
559.2.435 by Scott Moser
setup.py: rename "daemon type" to "init system"
131
class InitsysInstallData(install):
616.1.1 by Scott Moser
pylint: setup.py
132
    init_system = None
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
133
    user_options = install.user_options + [
559.2.435 by Scott Moser
setup.py: rename "daemon type" to "init system"
134
        # This will magically show up in member variable 'init_sys'
135
        ('init-system=', None,
1163.1.1 by Scott Moser
setup.py: pep8/flake8 changes only
136
         ('init system(s) to configure (%s) [default: None]' %
137
          (", ".join(INITSYS_TYPES)))),
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
138
    ]
139
140
    def initialize_options(self):
141
        install.initialize_options(self)
981.2.3 by Dimitri John Ledkov
fix ups
142
        self.init_system = ""
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
143
144
    def finalize_options(self):
145
        install.finalize_options(self)
981.2.1 by Dimitri John Ledkov
Modernise packaging, and allow multiple init system installation (based on smoser pastebins).
146
981.2.3 by Dimitri John Ledkov
fix ups
147
        if self.init_system and isinstance(self.init_system, str):
148
            self.init_system = self.init_system.split(",")
981.2.1 by Dimitri John Ledkov
Modernise packaging, and allow multiple init system installation (based on smoser pastebins).
149
150
        if len(self.init_system) == 0:
1163.1.1 by Scott Moser
setup.py: pep8/flake8 changes only
151
            raise DistutilsArgError(
152
                ("You must specify one of (%s) when"
981.2.1 by Dimitri John Ledkov
Modernise packaging, and allow multiple init system installation (based on smoser pastebins).
153
                 " specifying init system(s)!") % (", ".join(INITSYS_TYPES)))
154
155
        bad = [f for f in self.init_system if f not in INITSYS_TYPES]
156
        if len(bad) != 0:
157
            raise DistutilsArgError(
158
                "Invalid --init-system: %s" % (','.join(bad)))
981.2.2 by Dimitri John Ledkov
refactor
159
1052.1.1 by Barry Warsaw
* Added a simple tox.ini file
160
        for system in self.init_system:
1163.1.4 by Scott Moser
update setup.py to install generator
161
            # add data files for anything that starts with '<system>.'
162
            datakeys = [k for k in INITSYS_ROOTS
163
                        if k.partition(".")[0] == system]
164
            for k in datakeys:
165
                self.distribution.data_files.append(
166
                    (INITSYS_ROOTS[k], INITSYS_FILES[k]))
981.2.2 by Dimitri John Ledkov
refactor
167
        # Force that command to reinitalize (with new file list)
168
        self.distribution.reinitialize_command('install_data', True)
559.2.427 by Joshua Harlow
Add the ability to have setup.py have a CLI option that specifies the daemon type
169
170
1032.1.1 by Joshua Harlow
Only use datafiles and initsys addon outside virtualenvs
171
if in_virtualenv():
172
    data_files = []
173
    cmdclass = {}
174
else:
175
    data_files = [
176
        (ETC + '/cloud', glob('config/*.cfg')),
177
        (ETC + '/cloud/cloud.cfg.d', glob('config/cloud.cfg.d/*')),
178
        (ETC + '/cloud/templates', glob('templates/*')),
1025.5.8 by Joshua Harlow
Update with trunk and resolve conflicts
179
        (USR_LIB_EXEC + '/cloud-init', ['tools/uncloud-init',
180
                                        'tools/write-ssh-key-fingerprints']),
1032.1.1 by Joshua Harlow
Only use datafiles and initsys addon outside virtualenvs
181
        (USR + '/share/doc/cloud-init', [f for f in glob('doc/*') if is_f(f)]),
182
        (USR + '/share/doc/cloud-init/examples',
183
            [f for f in glob('doc/examples/*') if is_f(f)]),
1047 by Scott Moser
tools/run-pep8: remove leading ',' fed to --ignore
184
        (USR + '/share/doc/cloud-init/examples/seed',
1032.1.1 by Joshua Harlow
Only use datafiles and initsys addon outside virtualenvs
185
            [f for f in glob('doc/examples/seed/*') if is_f(f)]),
1178.2.16 by Scott Moser
commit the systemd waiting mechanism
186
        (LIB + '/udev/rules.d', [f for f in glob('udev/*.rules')]),
1032.1.1 by Joshua Harlow
Only use datafiles and initsys addon outside virtualenvs
187
    ]
188
    # Use a subclass for install that handles
189
    # adding on the right init system configuration files
190
    cmdclass = {
191
        'install': InitsysInstallData,
192
    }
193
194
1052.1.2 by Barry Warsaw
Only install cheetah (and only run the cheetah templating test) when in Python
195
requirements = read_requires()
196
if sys.version_info < (3,):
197
    requirements.append('cheetah')
198
1163.1.1 by Scott Moser
setup.py: pep8/flake8 changes only
199
setuptools.setup(
200
    name='cloud-init',
201
    version=get_version(),
202
    description='EC2 initialisation magic',
203
    author='Scott Moser',
204
    author_email='scott.moser@canonical.com',
205
    url='http://launchpad.net/cloud-init/',
206
    packages=setuptools.find_packages(exclude=['tests']),
1236.1.1 by Joshua Harlow
Make the bin/cloud-init an actual console entrypoint
207
    scripts=['tools/cloud-init-per'],
1163.1.1 by Scott Moser
setup.py: pep8/flake8 changes only
208
    license='GPLv3',
209
    data_files=data_files,
210
    install_requires=requirements,
211
    cmdclass=cmdclass,
1236.1.1 by Joshua Harlow
Make the bin/cloud-init an actual console entrypoint
212
    entry_points={
213
        'console_scripts': [
214
            'cloud-init = cloudinit.cmd.main:main'
215
        ],
216
    }
1216.1.6 by Scott Moser
fix setup.py for flake8
217
)