SlapObject.py 24.9 KB
Newer Older
1
# -*- coding: utf-8 -*-
Marco Mariani's avatar
Marco Mariani committed
2
# vim: set et sts=2:
Łukasz Nowak's avatar
Łukasz Nowak committed
3 4
##############################################################################
#
5 6
# Copyright (c) 2010, 2011, 2012 Vifib SARL and Contributors.
# All Rights Reserved.
Łukasz Nowak's avatar
Łukasz Nowak committed
7 8 9 10 11
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
12
# guarantees and support are strongly advised to contract a Free Software
Łukasz Nowak's avatar
Łukasz Nowak committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
30

Łukasz Nowak's avatar
Łukasz Nowak committed
31 32 33
import logging
import os
import pkg_resources
Marco Mariani's avatar
typos  
Marco Mariani committed
34
import pwd
Marco Mariani's avatar
Marco Mariani committed
35
import shutil
Łukasz Nowak's avatar
Łukasz Nowak committed
36
import stat
Marco Mariani's avatar
Marco Mariani committed
37
import subprocess
Marco Mariani's avatar
typos  
Marco Mariani committed
38
import tarfile
39
import tempfile
40
import textwrap
Marco Mariani's avatar
typos  
Marco Mariani committed
41 42 43
import xmlrpclib

from supervisor import xmlrpc
44
from slapos.grid.utils import (md5digest, getCleanEnvironment, bootstrapBuildout,
Marco Mariani's avatar
Marco Mariani committed
45
                               launchBuildout, SlapPopen, dropPrivileges, updateFile)
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
46
from slapos.slap.slap import NotFoundError
Marco Mariani's avatar
Marco Mariani committed
47 48 49 50 51
from slapos.grid.svcbackend import getSupervisorRPC
from slapos.grid.exception import (BuildoutFailedError, WrongPermissionError,
                                   PathDoesNotExistError)
from slapos.grid.networkcache import download_network_cached, upload_network_cached
from slapos.grid.watchdog import getWatchdogID
Łukasz Nowak's avatar
Łukasz Nowak committed
52

Marco Mariani's avatar
Marco Mariani committed
53
REQUIRED_COMPUTER_PARTITION_PERMISSION = 0o750
Łukasz Nowak's avatar
Łukasz Nowak committed
54 55 56


class Software(object):
Marco Mariani's avatar
typos  
Marco Mariani committed
57
  """This class is responsible for installing a software release"""
Marco Mariani's avatar
Marco Mariani committed
58

59
  def __init__(self, url, software_root, buildout,
Yingjie Xu's avatar
Yingjie Xu committed
60 61 62 63
      signature_private_key_file=None, signature_certificate_list=None,
      upload_cache_url=None, upload_dir_url=None, shacache_cert_file=None,
      shacache_key_file=None, shadir_cert_file=None, shadir_key_file=None,
      download_binary_cache_url=None, upload_binary_cache_url=None,
64
      download_binary_dir_url=None, upload_binary_dir_url=None,
65 66
      download_from_binary_cache_url_blacklist = [],
      upload_to_binary_cache_url_blacklist = []):
Łukasz Nowak's avatar
Łukasz Nowak committed
67 68 69 70
    """Initialisation of class parameters
    """
    self.url = url
    self.software_root = software_root
71
    self.software_url_hash = md5digest(self.url)
Łukasz Nowak's avatar
Łukasz Nowak committed
72
    self.software_path = os.path.join(self.software_root,
Yingjie Xu's avatar
Yingjie Xu committed
73
                                      self.software_url_hash)
74
    self.buildout = buildout
Łukasz Nowak's avatar
Łukasz Nowak committed
75
    self.logger = logging.getLogger('BuildoutManager')
76
    self.signature_private_key_file = signature_private_key_file
Yingjie Xu's avatar
Yingjie Xu committed
77
    self.signature_certificate_list = signature_certificate_list
78 79
    self.upload_cache_url = upload_cache_url
    self.upload_dir_url = upload_dir_url
80 81 82 83
    self.shacache_cert_file = shacache_cert_file
    self.shacache_key_file = shacache_key_file
    self.shadir_cert_file = shadir_cert_file
    self.shadir_key_file = shadir_key_file
Yingjie Xu's avatar
Yingjie Xu committed
84 85 86 87
    self.download_binary_cache_url = download_binary_cache_url
    self.upload_binary_cache_url = upload_binary_cache_url
    self.download_binary_dir_url = download_binary_dir_url
    self.upload_binary_dir_url = upload_binary_dir_url
88 89 90 91
    self.download_from_binary_cache_url_blacklist = \
        download_from_binary_cache_url_blacklist
    self.upload_to_binary_cache_url_blacklist = \
        upload_to_binary_cache_url_blacklist
Łukasz Nowak's avatar
Łukasz Nowak committed
92 93

  def install(self):
Yingjie Xu's avatar
Yingjie Xu committed
94 95 96
    """ Fetches binary cache if possible.
    Installs from buildout otherwise.
    """
97
    self.logger.info("Installing software release %s..." % self.url)
Yingjie Xu's avatar
Yingjie Xu committed
98
    cache_dir = tempfile.mkdtemp()
99 100 101 102 103 104 105 106 107 108 109 110 111
    try:
      tarpath = os.path.join(cache_dir, self.software_url_hash)
      # Check if we can download from cache
      if (not os.path.exists(self.software_path)) \
          and download_network_cached(
              self.download_binary_cache_url,
              self.download_binary_dir_url,
              self.url, self.software_root,
              self.software_url_hash,
              tarpath, self.logger,
              self.signature_certificate_list,
              self.download_from_binary_cache_url_blacklist):
        tar = tarfile.open(tarpath)
Yingjie Xu's avatar
Yingjie Xu committed
112
        try:
113 114
          self.logger.info("Extracting archive of cached software release...")
          tar.extractall(path=self.software_root)
Yingjie Xu's avatar
Yingjie Xu committed
115 116
        finally:
          tar.close()
117 118
      else:
        self._install_from_buildout()
119
        # Upload to binary cache if possible and allowed
120 121
        if (self.software_root and self.url and self.software_url_hash \
                               and self.upload_binary_cache_url \
122 123 124 125 126 127 128 129 130 131
                               and self.upload_binary_dir_url):
          blacklisted = False
          for url in self.upload_to_binary_cache_url_blacklist:
            if self.url.startswith(url):
              blacklisted = True
              self.logger.info("Can't upload to binary cache: "
                  "Software Release URL is blacklisted.")
              break
          if not blacklisted:
            self.uploadSoftwareRelease(tarpath)
132 133
    finally:
      shutil.rmtree(cache_dir)
134

Yingjie Xu's avatar
Yingjie Xu committed
135
  def _install_from_buildout(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
136 137 138
    """ Fetches buildout configuration from the server, run buildout with
    it. If it fails, we notify the server.
    """
139
    root_stat_info = os.stat(self.software_root)
Marco Mariani's avatar
Marco Mariani committed
140
    os.environ = getCleanEnvironment(pwd.getpwuid(root_stat_info.st_uid
141
      ).pw_dir)
Łukasz Nowak's avatar
Łukasz Nowak committed
142 143
    if not os.path.isdir(self.software_path):
      os.mkdir(self.software_path)
144
    extends_cache = tempfile.mkdtemp()
Łukasz Nowak's avatar
Łukasz Nowak committed
145 146
    if os.getuid() == 0:
      # In case when running as root copy ownership, to simplify logic
147
      for path in [self.software_path, extends_cache]:
Łukasz Nowak's avatar
Łukasz Nowak committed
148 149 150 151 152
        path_stat_info = os.stat(path)
        if root_stat_info.st_uid != path_stat_info.st_uid or\
             root_stat_info.st_gid != path_stat_info.st_gid:
            os.chown(path, root_stat_info.st_uid,
                root_stat_info.st_gid)
153 154 155
    try:
      buildout_parameter_list = [
        'buildout:extends-cache=%s' % extends_cache,
156 157 158
        'buildout:directory=%s' % self.software_path,]

      if self.signature_private_key_file or \
159 160
          self.upload_cache_url or \
            self.upload_dir_url is not None:
161
        buildout_parameter_list.append('buildout:networkcache-section=networkcache')
Lucas Carvalho's avatar
Lucas Carvalho committed
162 163 164
      for  buildout_option, value in (
         ('%ssignature-private-key-file=%s', self.signature_private_key_file),
         ('%supload-cache-url=%s', self.upload_cache_url),
165 166 167 168 169 170
         ('%supload-dir-url=%s', self.upload_dir_url),
         ('%sshacache-cert-file=%s', self.shacache_cert_file),
         ('%sshacache-key-file=%s', self.shacache_key_file),
         ('%sshadir-cert-file=%s', self.shadir_cert_file),
         ('%sshadir-key-file=%s', self.shadir_key_file),
         ):
Lucas Carvalho's avatar
Lucas Carvalho committed
171 172 173
        if value:
          buildout_parameter_list.append( \
              buildout_option % ('networkcache:', value))
174

175 176 177 178
      buildout_cfg = os.path.join(self.software_path, 'buildout.cfg')
      self.createProfileIfMissing(buildout_cfg, self.url)

      buildout_parameter_list.extend(['-c', buildout_cfg])
Marco Mariani's avatar
Marco Mariani committed
179
      bootstrapBuildout(self.software_path, self.buildout,
180
          additional_buildout_parametr_list=buildout_parameter_list)
Marco Mariani's avatar
Marco Mariani committed
181
      launchBuildout(self.software_path,
182
                     os.path.join(self.software_path, 'bin', 'buildout'),
183
                     additional_buildout_parametr_list=buildout_parameter_list)
184 185
    finally:
      shutil.rmtree(extends_cache)
Łukasz Nowak's avatar
Łukasz Nowak committed
186

187 188 189 190 191 192 193 194 195 196 197 198 199
  def createProfileIfMissing(self, buildout_cfg, url):
    root_stat_info = os.stat(self.software_root)
    if not os.path.exists(buildout_cfg):
      with open(buildout_cfg, 'wb') as fout:
        fout.write(textwrap.dedent("""\
            # Created by slapgrid. extends {url}
            # but you can change it for development purposes.

            [buildout]
            extends = {url}
            """.format(url=url)))
        os.chown(buildout_cfg, root_stat_info.st_uid, root_stat_info.st_gid)

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  def uploadSoftwareRelease(self, tarpath):
    """
    Try to tar and upload an installed Software Release.
    """
    self.logger.info("Creating archive of software release...")
    tar = tarfile.open(tarpath, "w:gz")
    try:
      tar.add(self.software_path, arcname=self.software_url_hash)
    finally:
      tar.close()
    self.logger.info("Trying to upload archive of software release...")
    upload_network_cached(
        self.software_root,
        self.url, self.software_url_hash,
        self.upload_binary_cache_url,
        self.upload_binary_dir_url,
        tarpath, self.logger,
        self.signature_private_key_file,
        self.shacache_cert_file,
        self.shacache_key_file,
        self.shadir_cert_file,
        self.shadir_key_file)

Łukasz Nowak's avatar
Łukasz Nowak committed
223 224 225
  def destroy(self):
    """Removes software release."""
    def retry(func, path, exc):
Marco Mariani's avatar
typos  
Marco Mariani committed
226
      # inspired by slapos.buildout hard remover
Łukasz Nowak's avatar
Łukasz Nowak committed
227 228 229
      if func == os.path.islink:
        os.unlink(path)
      else:
Marco Mariani's avatar
Marco Mariani committed
230
        os.chmod(path, 0o600)
Łukasz Nowak's avatar
Łukasz Nowak committed
231
        func(path)
Łukasz Nowak's avatar
Łukasz Nowak committed
232
    try:
233
      if os.path.exists(self.software_path):
Łukasz Nowak's avatar
Łukasz Nowak committed
234
        self.logger.info('Removing path %r' % self.software_path)
235
        shutil.rmtree(self.software_path, onerror=retry)
Łukasz Nowak's avatar
Łukasz Nowak committed
236 237 238
      else:
        self.logger.info('Path %r does not exists, no need to remove.' %
            self.software_path)
Łukasz Nowak's avatar
Łukasz Nowak committed
239 240 241 242 243 244 245
    except IOError as error:
      error_string = "I/O error while removing software (%s): %s" % (self.url,
                                                                     error)
      raise IOError(error_string)


class Partition(object):
Marco Mariani's avatar
Marco Mariani committed
246
  """This class is responsible of the installation of an instance
Łukasz Nowak's avatar
Łukasz Nowak committed
247
  """
Marco Mariani's avatar
Marco Mariani committed
248

Łukasz Nowak's avatar
Łukasz Nowak committed
249 250 251 252 253 254 255 256 257 258
  def __init__(self,
               software_path,
               instance_path,
               supervisord_partition_configuration_path,
               supervisord_socket,
               computer_partition,
               computer_id,
               partition_id,
               server_url,
               software_release_url,
259
               buildout,
Łukasz Nowak's avatar
Łukasz Nowak committed
260 261 262
               certificate_repository_path=None,
               ):
    """Initialisation of class parameters"""
263
    self.buildout = buildout
Łukasz Nowak's avatar
Łukasz Nowak committed
264 265 266
    self.software_path = software_path
    self.instance_path = instance_path
    self.run_path = os.path.join(self.instance_path, 'etc', 'run')
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
267
    self.service_path = os.path.join(self.instance_path, 'etc', 'service')
Łukasz Nowak's avatar
Łukasz Nowak committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    self.supervisord_partition_configuration_path = \
        supervisord_partition_configuration_path
    self.supervisord_socket = supervisord_socket
    self.computer_partition = computer_partition
    self.logger = logging.getLogger('Partition')
    self.computer_id = computer_id
    self.partition_id = partition_id
    self.server_url = server_url
    self.software_release_url = software_release_url

    self.key_file = ''
    self.cert_file = ''
    if certificate_repository_path is not None:
      self.key_file = os.path.join(certificate_repository_path,
          self.partition_id + '.key')
      self.cert_file = os.path.join(certificate_repository_path,
          self.partition_id + '.crt')
      self._updateCertificate()

  def _updateCertificate(self):
    if not os.path.exists(self.key_file) or \
        not os.path.exists(self.cert_file):
      self.logger.info('Certificate and key not found, downloading to %r and '
          '%r' % (self.cert_file, self.key_file))
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
292 293 294 295 296
      try:
        partition_certificate = self.computer_partition.getCertificate()
      except NotFoundError:
        raise NotFoundError('Partition %s is not known from SlapOS Master.' % \
            self.partition_id)
Łukasz Nowak's avatar
Łukasz Nowak committed
297 298 299
      open(self.key_file, 'w').write(partition_certificate['key'])
      open(self.cert_file, 'w').write(partition_certificate['certificate'])
    for f in [self.key_file, self.cert_file]:
Marco Mariani's avatar
Marco Mariani committed
300
      os.chmod(f, 0o400)
Łukasz Nowak's avatar
Łukasz Nowak committed
301 302 303 304 305 306 307 308 309
      os.chown(f, *self.getUserGroupId())

  def getUserGroupId(self):
    """Returns tuple of (uid, gid) of partition"""
    stat_info = os.stat(self.instance_path)
    uid = stat_info.st_uid
    gid = stat_info.st_gid
    return (uid, gid)

Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  def addServiceToGroup(self, partition_id,
                        runner_list, path, extension = ''):
    uid, gid = self.getUserGroupId()
    program_partition_template = pkg_resources.resource_stream(__name__,
            'templates/program_partition_supervisord.conf.in').read()
    for runner in runner_list:
      self.partition_supervisor_configuration += '\n' + \
          program_partition_template % dict(
        program_id='_'.join([partition_id, runner]),
        program_directory=self.instance_path,
        program_command=os.path.join(path, runner),
        program_name=runner+extension,
        instance_path=self.instance_path,
        user_id=uid,
        group_id=gid,
Marco Mariani's avatar
typos  
Marco Mariani committed
325
        # As supervisord has no environment to inherit, setup a minimalistic one
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
326 327 328 329
        HOME=pwd.getpwuid(uid).pw_dir,
        USER=pwd.getpwuid(uid).pw_name,
      )

330
  def updateSymlink(self, sr_symlink, software_path):
331
    if os.path.lexists(sr_symlink):
332 333 334 335 336 337 338
      if not os.path.islink(sr_symlink):
        self.logger.debug('Not a symlink: %s, has been ignored' % (sr_symlink))
        return
      os.unlink(sr_symlink)
    os.symlink(software_path, sr_symlink)
    os.lchown(sr_symlink, *self.getUserGroupId())

Łukasz Nowak's avatar
Łukasz Nowak committed
339 340 341 342 343 344 345 346 347 348 349
  def install(self):
    """ Creates configuration file from template in software_path, then
    installs the software partition with the help of buildout
    """
    self.logger.info("Installing Computer Partition %s..." \
        % self.computer_partition.getId())
    # Checks existence and permissions of Partition directory
    # Note : Partitions have to be created and configured before running slapgrid
    if not os.path.isdir(self.instance_path):
      raise PathDoesNotExistError('Please create partition directory %s'
                                           % self.instance_path)
350 351 352 353

    sr_symlink = os.path.join(self.instance_path, 'software_release')
    self.updateSymlink(sr_symlink, self.software_path)

354
    instance_stat_info = os.stat(self.instance_path)
Marco Mariani's avatar
Marco Mariani committed
355
    permission = stat.S_IMODE(instance_stat_info.st_mode)
Łukasz Nowak's avatar
Łukasz Nowak committed
356
    if permission != REQUIRED_COMPUTER_PARTITION_PERMISSION:
Marco Mariani's avatar
Marco Mariani committed
357 358 359 360
      raise WrongPermissionError('Wrong permissions in %s: actual '
                                 'permissions are: 0%o, wanted are 0%o' %
                                 (self.instance_path, permission,
                                  REQUIRED_COMPUTER_PARTITION_PERMISSION))
Marco Mariani's avatar
Marco Mariani committed
361
    os.environ = getCleanEnvironment(pwd.getpwuid(
362
      instance_stat_info.st_uid).pw_dir)
Łukasz Nowak's avatar
Łukasz Nowak committed
363
    # Generates buildout part from template
364 365 366 367
    template_location = os.path.join(self.software_path, 'instance.cfg')
    # Backward compatibility: "instance.cfg" file was named "template.cfg".
    if not os.path.exists(template_location):
      template_location = os.path.join(self.software_path, 'template.cfg')
Łukasz Nowak's avatar
Łukasz Nowak committed
368
    config_location = os.path.join(self.instance_path, 'buildout.cfg')
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
369
    self.logger.debug("Copying %r to %r" % (template_location, config_location))
370 371 372 373 374
    try:
      shutil.copy(template_location, config_location)
    except IOError, e:
      # Template not found on SR, we notify user.
      raise IOError('Software Release %s is not correctly installed.\n'
375
          '%s' % (self.software_release_url, e))
Łukasz Nowak's avatar
Łukasz Nowak committed
376 377 378 379 380 381 382 383 384 385 386 387
    # fill generated buildout with additional information
    buildout_text = open(config_location).read()
    buildout_text += '\n\n' + pkg_resources.resource_string(__name__,
        'templates/buildout-tail.cfg.in') % dict(
      computer_id=self.computer_id,
      partition_id=self.partition_id,
      server_url=self.server_url,
      software_release_url=self.software_release_url,
      key_file=self.key_file,
      cert_file=self.cert_file
    )
    open(config_location, 'w').write(buildout_text)
Marco Mariani's avatar
Marco Mariani committed
388
    os.chmod(config_location, 0o640)
Łukasz Nowak's avatar
Łukasz Nowak committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    # Try to find the best possible buildout:
    #  *) if software_root/bin/bootstrap exists use this one to bootstrap
    #     locally
    #  *) as last resort fallback to buildout binary from software_path
    bootstrap_candidate_dir = os.path.abspath(os.path.join(self.software_path,
      'bin'))
    if os.path.isdir(bootstrap_candidate_dir):
      bootstrap_candidate_list = [q for q in os.listdir(bootstrap_candidate_dir)
        if q.startswith('bootstrap')]
    else:
      bootstrap_candidate_list = []
    uid, gid = self.getUserGroupId()
    os.chown(config_location, -1, int(gid))
    if len(bootstrap_candidate_list) == 0:
      buildout_binary = os.path.join(self.software_path, 'bin', 'buildout')
      self.logger.warning("Falling back to default buildout %r" %
        buildout_binary)
    else:
      if len(bootstrap_candidate_list) != 1:
Marco Mariani's avatar
typos  
Marco Mariani committed
408
        raise ValueError('More than one bootstrap candidate found.')
Łukasz Nowak's avatar
Łukasz Nowak committed
409 410 411 412
      # Reads uid/gid of path, launches buildout with thoses privileges
      bootstrap_file = os.path.abspath(os.path.join(bootstrap_candidate_dir,
        bootstrap_candidate_list[0]))

Marco Mariani's avatar
Marco Mariani committed
413
      first_line = open(bootstrap_file, 'r').readline()
Łukasz Nowak's avatar
Łukasz Nowak committed
414
      invocation_list = []
Marco Mariani's avatar
Marco Mariani committed
415 416
      if first_line.startswith('#!'):
        invocation_list = first_line[2:].split()
Łukasz Nowak's avatar
Łukasz Nowak committed
417
      invocation_list.append(bootstrap_file)
Marco Mariani's avatar
Marco Mariani committed
418

Łukasz Nowak's avatar
Łukasz Nowak committed
419 420
      self.logger.debug('Invoking %r in %r' % (' '.join(invocation_list),
        self.instance_path))
Marco Mariani's avatar
Marco Mariani committed
421 422 423 424 425 426
      process_handler = SlapPopen(invocation_list,
                                  preexec_fn=lambda: dropPrivileges(uid, gid),
                                  cwd=self.instance_path,
                                  env=getCleanEnvironment(pwd.getpwuid(uid).pw_dir),
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
Łukasz Nowak's avatar
Łukasz Nowak committed
427
      if process_handler.returncode is None or process_handler.returncode != 0:
428
        message = 'Failed to bootstrap buildout in %r.' % (self.instance_path)
429 430
        self.logger.error(message)
        raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output))
Łukasz Nowak's avatar
Łukasz Nowak committed
431 432 433 434
      buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')

    if not os.path.exists(buildout_binary):
      # use own buildout generation
Marco Mariani's avatar
Marco Mariani committed
435
      bootstrapBuildout(self.instance_path, self.buildout,
436
        ['buildout:bin-directory=%s'% os.path.join(self.instance_path,
437
        'sbin')])
Łukasz Nowak's avatar
Łukasz Nowak committed
438 439
      buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')
    # Launches buildout
Marco Mariani's avatar
Marco Mariani committed
440
    launchBuildout(self.instance_path, buildout_binary)
Łukasz Nowak's avatar
Łukasz Nowak committed
441 442 443 444
    # Generates supervisord configuration file from template
    self.logger.info("Generating supervisord config file from template...")
    # check if CP/etc/run exists and it is a directory
    # iterate over each file in CP/etc/run
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
445
    # iterate over each file in CP/etc/service adding WatchdogID to their name
Marco Mariani's avatar
Marco Mariani committed
446
    # if at least one is not 0o750 raise -- partition has something funny
Łukasz Nowak's avatar
Łukasz Nowak committed
447
    runner_list = []
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
448
    service_list = []
Łukasz Nowak's avatar
Łukasz Nowak committed
449 450 451
    if os.path.exists(self.run_path):
      if os.path.isdir(self.run_path):
        runner_list = os.listdir(self.run_path)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
452 453 454 455 456
    if os.path.exists(self.service_path):
      if os.path.isdir(self.service_path):
        service_list = os.listdir(self.service_path)
    if len(runner_list) == 0 and len(service_list) == 0:
      self.logger.warning('No runners nor services found for partition %r' %
Łukasz Nowak's avatar
Łukasz Nowak committed
457 458 459 460 461 462 463
          self.partition_id)
      if os.path.exists(self.supervisord_partition_configuration_path):
        os.unlink(self.supervisord_partition_configuration_path)
    else:
      partition_id = self.computer_partition.getId()
      group_partition_template = pkg_resources.resource_stream(__name__,
          'templates/group_partition_supervisord.conf.in').read()
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
464
      self.partition_supervisor_configuration = group_partition_template % dict(
Łukasz Nowak's avatar
Łukasz Nowak committed
465 466
          instance_id=partition_id,
          program_list=','.join(['_'.join([partition_id, runner])
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
467 468 469 470 471
            for runner in runner_list+service_list]))
      # Same method to add to service and run 
      self.addServiceToGroup(partition_id, runner_list,self.run_path)
      self.addServiceToGroup(partition_id, service_list,self.service_path,
                             extension=getWatchdogID())
Marco Mariani's avatar
Marco Mariani committed
472
      updateFile(self.supervisord_partition_configuration_path,
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
473
          self.partition_supervisor_configuration)
Łukasz Nowak's avatar
Łukasz Nowak committed
474 475 476 477 478 479 480 481
    self.updateSupervisor()

  def start(self):
    """Asks supervisord to start the instance. If this instance is not
    installed, we install it.
    """
    supervisor = self.getSupervisorRPC()
    partition_id = self.computer_partition.getId()
482 483 484 485 486 487 488 489
    try:
      supervisor.startProcessGroup(partition_id, False)
    except xmlrpclib.Fault, e:
      if e.faultString.startswith('BAD_NAME:'):
        self.logger.info("Nothing to start on %s..." % \
                         self.computer_partition.getId())
    else:
      self.logger.info("Requested start of %s..." % self.computer_partition.getId())
Łukasz Nowak's avatar
Łukasz Nowak committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

  def stop(self):
    """Asks supervisord to stop the instance."""
    supervisor = self.getSupervisorRPC()
    partition_id = self.computer_partition.getId()
    try:
      supervisor.stopProcessGroup(partition_id, False)
    except xmlrpclib.Fault, e:
      if e.faultString.startswith('BAD_NAME:'):
        self.logger.info('Partition %s not known in supervisord, ignoring' % partition_id)
    else:
      self.logger.info("Requested stop of %s..." % self.computer_partition.getId())

  def destroy(self):
    """Destroys the partition and makes it available for subsequent use."
    """
    self.logger.info("Destroying Computer Partition %s..." \
        % self.computer_partition.getId())
    # Launches "destroy" binary if exists
    destroy_executable_location = os.path.join(self.instance_path, 'sbin',
        'destroy')
    if os.path.exists(destroy_executable_location):
Marco Mariani's avatar
Marco Mariani committed
512
      uid, gid = self.getUserGroupId()
Łukasz Nowak's avatar
Łukasz Nowak committed
513
      self.logger.debug('Invoking %r' % destroy_executable_location)
Marco Mariani's avatar
Marco Mariani committed
514 515 516 517 518 519
      process_handler = SlapPopen([destroy_executable_location],
                                   preexec_fn=lambda: dropPrivileges(uid, gid),
                                   cwd=self.instance_path,
                                   env=getCleanEnvironment(pwd.getpwuid(uid).pw_dir),
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
Łukasz Nowak's avatar
Łukasz Nowak committed
520
      if process_handler.returncode is None or process_handler.returncode != 0:
521 522
        message = 'Failed to destroy Computer Partition in %r.' % \
            self.instance_path
523 524
        self.logger.error(message)
        raise subprocess.CalledProcessError(message, process_handler.output)
Łukasz Nowak's avatar
Łukasz Nowak committed
525 526 527 528 529 530
    # Manually cleans what remains
    try:
      for f in [self.key_file, self.cert_file]:
        if f:
          if os.path.exists(f):
            os.unlink(f)
Marco Mariani's avatar
Marco Mariani committed
531 532 533 534 535 536

      # better to manually remove symlinks because rmtree might choke on them
      sr_symlink = os.path.join(self.instance_path, 'software_release')
      if os.path.islink(sr_symlink):
        os.unlink(sr_symlink)

Łukasz Nowak's avatar
Łukasz Nowak committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
      for root, dirs, file_list in os.walk(self.instance_path):
        for directory in dirs:
          shutil.rmtree(os.path.join(self.instance_path, directory))
        for file in file_list:
          os.remove(os.path.join(self.instance_path, file))
        if os.path.exists(self.supervisord_partition_configuration_path):
          os.remove(self.supervisord_partition_configuration_path)
        self.updateSupervisor()
    except IOError as error:
      error_string = "I/O error while freeing partition (%s): %s" \
                     % (self.instance_path, error)
      raise IOError(error_string)

  def fetchInformations(self):
    """Fetch usage informations with buildout, returns it.
    """
    raise NotImplementedError

  def getSupervisorRPC(self):
    return getSupervisorRPC(self.supervisord_socket)

  def updateSupervisor(self):
    """Forces supervisord to reload its configuration"""
    # Note: This method shall wait for results from supervisord
    #       In future it will be not needed, as update command
    #       is going to be implemented on server side.
    self.logger.debug('Updating supervisord')
    supervisor = self.getSupervisorRPC()
    # took from supervisord.supervisorctl.do_update
    result = supervisor.reloadConfig()
    added, changed, removed = result[0]

    for gname in removed:
      results = supervisor.stopProcessGroup(gname)
      fails = [res for res in results
               if res['status'] == xmlrpc.Faults.FAILED]
      if fails:
        self.logger.warning('Problem while stopping process %r, will try later' % gname)
      else:
        self.logger.info('Stopped %r' % gname)
      supervisor.removeProcessGroup(gname)
      self.logger.info('Removed %r' % gname)

    for gname in changed:
      results = supervisor.stopProcessGroup(gname)
      self.logger.info('Stopped %r' % gname)

      supervisor.removeProcessGroup(gname)
      supervisor.addProcessGroup(gname)
      self.logger.info('Updated %r' % gname)

    for gname in added:
      supervisor.addProcessGroup(gname)
      self.logger.info('Updated %r' % gname)
    self.logger.debug('Supervisord updated')