File size: 10,299 Bytes
b2e2343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
import os.path

from fabric.api import settings, run, cd, lcd, put, get, local, env, with_settings
from fabric.colors import green

from .. import versions
from .. import distro
from .. import tools

class Deployment(object):
    """The core :class:`Deployment <Deployment>` object. All Fabric tasks built with
    Parcel will probably use this an instance of this class.
    """

    virtual = "vp"
    build_dir = '.parcel'
    
    # these are the full text versions of the scripts
    prerm = None
    postrm = None
    preinst = None
    postinst = None
    
    # these are a list representation of commands to go into the scripts
    # if the control script templating is used
    prerm_lines = []
    postrm_lines = []
    preinst_lines = []
    postinst_lines = []
    
    def __init__(self, app_name, build_deps=[], run_deps=[], path=".", base=None, arch=distro.Debian(), version=None):

        #: The architecture of the build host. This should be a :class:`Distro <Distro>` object. 
        self.arch = arch
        remotehome = run('echo $HOME').strip()

        # if path isn't set, make it the home directory of the build user
        if base is None:
            base = remotehome
        elif not base.startswith('/'):
            base = os.path.join(remotehome, base)
        
        # update and install missing build dependency packages
        arch.update_packages()
        if build_deps:
            arch.build_deps(build_deps)
            
        # the version in the archives of this package if we have been built and uploaded before.
        self.version = arch.version(app_name).next() if version is None else versions.Version(version)
        
        #: The name of the resulting package.
        self.app_name = app_name

        #: A list of packages that must be installed to run the resulting package.
        self.run_deps = run_deps

        #: A list of packages that need to be installed to build the software.
        self.build_deps = build_deps
        self.pkg_name = app_name.lower()

        #: The directory that will be used as the base level directory.
        self.path = os.path.realpath(path)

        #: Location of files during build on build host. Default is user's home directory.
        #: If path is relative, it's relative to the remote user's home directory. If the path is absolute,
        #: it's used as is.
        self.base_path = os.path.join(remotehome,self.build_dir)

        self.pkg_name = app_name.lower()
        self.root_path = os.path.join(self.base_path,"root")                    # where the final root fs is located
        
        # the path the app will be installed into
        self.app_path = os.path.join(base,'%s-%s'%(self.pkg_name,self.version))
        
        # the build path
        self.build_path = os.path.join(self.root_path, self.app_path[1:])                # cut the first / off app_path

        print "ROOT_PATH", self.root_path
        print "BASE_PATH",self.base_path
        print "APP PATH",self.app_path
        print "BUILD PATH",self.build_path

        self._clean()
        
    def prepare_app(self, branch=None, requirements="requirements.txt"):
        """Creates the necessary directories on the build server, checks out the desired branch (None means current),
        creates a virtualenv and populates it with dependencies from requirements.txt. 
        As a bonus it also fixes the shebangs (#!) of all scripts in the virtualenv to point the correct Python path
        on the target system."""
        self._sync_app()
        self._add_venv(requirements)
            
    def add_to_root_fs(self,localfile,remotepath):
        """Add a local file to the root package path.
        If remote path ends in /, the filename is copied into
        that directory. If the remote path doesn't end in /, it represents
        the final filename.
        """
        while remotepath[0]=='/':
            remotepath=remotepath[1:]
        put(localfile,os.path.join(self.root_path,remotepath))

    def add_data_to_root_fs(self, data, remotepath):
        """Copies data in file on remotepath (relative to final root)"""
        while remotepath[0]=='/':
            remotepath=remotepath[1:]
        tools.write_contents_to_remote(data,os.path.join(self.root_path, remotepath))
            
    def compile_python(self):
        # compile all python (with virtual python)
        vpython_path = os.path.join(self.venv_path,'bin/python')
        command = '%s -c "import compileall;compileall.compile_dir(\'%s\', force=1)"'%(vpython_path, self.build_path)
        run(command)

    def clear_py_files(self):
        # clear all .py files
        run('find "%s" -name "*.py" -exec rm {} \;'%(self.build_path))

    def add_prerm(self, lines):
        """Add lines to the prerm file"""
        self.prerm_lines.extend(lines)
        
    def add_postrm(self, lines):
        """Add lines to the postrm file"""        
        self.postrm_lines.extend(lines)
        
    def add_preinst(self, lines):
        """Add lines to the preinst file"""        
        self.preinst_lines.extend(lines)
        
    def add_postinst(self, lines):
        """Add lines to the postinst file"""
        self.postinst_lines.extend(lines)

    def build_package(self, templates=True):
        """Takes the whole app including the virtualenv, packages it using fpm and downloads it to the local host.
	    The version of the package is the build number - which is just the latest package version in our Ubuntu repositories plus one.
	    """

        # add install and remove templates, use defaults if not supplied
        if templates:
            if not self.prerm:
                self.arch.defaults.prerm_template
                self.write_prerm_template(self.arch.defaults.prerm_template)
            if not self.postrm:
                self.write_postrm_template(self.arch.defaults.postrm_template)
            if not self.preinst:
                self.write_preinst_template(self.arch.defaults.preinst_template)
            if not self.postinst:
                self.write_postinst_template(self.arch.defaults.postinst_template)
        
        with cd(self.base_path):
            self.deps_str = '-d ' + ' -d '.join(self.run_deps)
            self.dirs_str = '.'
            
            if self.prerm or self.postrm or self.preinst or self.postinst:
                run("rm -rf installscripts && mkdir -p installscripts")
            
            # render pre/posts
            hooks = []
            if self.prerm:
                prerm = self.prerm.format(self)
                tools.write_contents_to_remote(prerm,'installscripts/prerm')
                hooks.extend(['--before-remove', '../installscripts/prerm'])
                
            if self.postrm:
                postrm = self.postrm.format(self)
                tools.write_contents_to_remote(postrm,'installscripts/postrm')
                hooks.extend(['--after-remove', '../installscripts/postrm'])
            
            if self.preinst:
                tools.write_contents_to_remote(self.preinst,'installscripts/preinst')
                hooks.extend(['--before-install', '../installscripts/preinst'])
            
            if self.postinst:
                tools.write_contents_to_remote(self.postinst,'installscripts/postinst')
                hooks.extend(['--after-install', '../installscripts/postinst'])
            
            self.hooks_str = ' '.join(hooks)
            
        self.arch.build_package(deployment=self)


    def write_prerm_template(self, template):
        """Take a template prerm script and format it with appname and prerm_lines
        If you call this function you must supply a template string that includes {app_name} and {lines}."""
        self.prerm = template.format(app_name=self.app_name, lines="\n        ".join(self.prerm_lines))

    def write_postrm_template(self, template):
        """Take a template postrm script and format it with appname and postrm_lines
        If you call this function you must supply a template string that includes {app_name} and {lines}."""
        self.postrm = template.format(app_name=self.app_name, lines="\n        ".join(self.postrm_lines))

    def write_preinst_template(self, template):
        """Take a template preinst script and format it with appname and preinst_lines
        If you call this function you must supply a template string that includes {app_name} and {lines}."""
        self.preinst = template.format(app_name=self.app_name, lines="\n        ".join(self.preinst_lines))

    def write_postinst_template(self, template):
        """Take a template postinst script and format it with appname and postinst_lines
        If you call this function you must supply a template string that includes {app_name} and {lines}."""
        self.postinst = template.format(app_name=self.app_name, lines="\n        ".join(self.postinst_lines))

    def _clean(self):
        """Make sure the root filesystem directory is empty."""
        run('rm -rf "%s"'%self.root_path)
        
    def _sync_app(self):
        """There is no revision control at the moment so... just copy directory over."""
        print self.build_path
        tools.rsync([self.path+'/'],self.build_path,rsync_ignore='.rsync-ignore')

    def _add_venv(self,requirements="requirements.txt"):
        """Builds virtualenv on remote host and installs from requirements.txt.
        
        :param requirements: The name of the requirements.txt file.
        """
        self.venv_path = os.path.join(self.build_path, self.virtual)
        run('virtualenv %s'%(self.venv_path))
        if requirements and os.path.exists(requirements):
            run('PIP_DOWNLOAD_CACHE="%s" %s install -r %s'%(
                self.arch.pip_download_cache,
	            os.path.join(self.venv_path, 'bin/pip'),
	            os.path.join(self.build_path, requirements))
            )
            
        # venv_root is final path
        self.venv_root = os.path.join(self.app_path, self.virtual)
        
        # lets make sure this venv is relinked on installation
        self.add_postinst(['virtualenv "%s"'%self.venv_root])
        
        # and we have the virtualenv executable
        self.run_deps.append('python-virtualenv')