SlapObject.py 19.2 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak committed
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
##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
#
# 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
# guarantees and support are strongly adviced to contract a Free Software
# 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.
#
##############################################################################
import logging
import os
import shutil
import subprocess
import pkg_resources
import stat
33
import tempfile
Łukasz Nowak's avatar
Łukasz Nowak committed
34 35 36
from supervisor import xmlrpc
import xmlrpclib
import pwd
37
import utils
Łukasz Nowak's avatar
Łukasz Nowak committed
38 39 40 41 42 43 44 45 46
from svcbackend import getSupervisorRPC
from exception import BuildoutFailedError, WrongPermissionError, \
    PathDoesNotExistError

REQUIRED_COMPUTER_PARTITION_PERMISSION = '0750'


class Software(object):
  """This class is responsible of installing a software release"""
47
  def __init__(self, url, software_root, console, buildout,
48
      signature_private_key_file=None, upload_cache_url=None,
49 50
      upload_dir_url=None, shacache_cert_file=None, shacache_key_file=None,
      shadir_cert_file=None, shadir_key_file=None):
Łukasz Nowak's avatar
Łukasz Nowak committed
51 52 53 54 55
    """Initialisation of class parameters
    """
    self.url = url
    self.software_root = software_root
    self.software_path = os.path.join(self.software_root,
56
                                      utils.getSoftwareUrlHash(self.url))
57
    self.buildout = buildout
Łukasz Nowak's avatar
Łukasz Nowak committed
58 59
    self.logger = logging.getLogger('BuildoutManager')
    self.console = console
60
    self.signature_private_key_file = signature_private_key_file
61 62
    self.upload_cache_url = upload_cache_url
    self.upload_dir_url = upload_dir_url
63 64 65 66
    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
Łukasz Nowak's avatar
Łukasz Nowak committed
67 68 69 70 71 72

  def install(self):
    """ Fetches buildout configuration from the server, run buildout with
    it. If it fails, we notify the server.
    """
    self.logger.info("Installing software release %s..." % self.url)
73
    root_stat_info = os.stat(self.software_root)
74 75
    os.environ = utils.getCleanEnvironment(pwd.getpwuid(root_stat_info.st_uid
      ).pw_dir)
Łukasz Nowak's avatar
Łukasz Nowak committed
76 77
    if not os.path.isdir(self.software_path):
      os.mkdir(self.software_path)
78
    extends_cache = tempfile.mkdtemp()
Łukasz Nowak's avatar
Łukasz Nowak committed
79 80
    if os.getuid() == 0:
      # In case when running as root copy ownership, to simplify logic
81
      for path in [self.software_path, extends_cache]:
Łukasz Nowak's avatar
Łukasz Nowak committed
82 83 84 85 86
        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)
87 88 89
    try:
      buildout_parameter_list = [
        'buildout:extends-cache=%s' % extends_cache,
90 91 92
        'buildout:directory=%s' % self.software_path,]

      if self.signature_private_key_file or \
93 94
          self.upload_cache_url or \
            self.upload_dir_url is not None:
95
        buildout_parameter_list.append('buildout:networkcache-section=networkcache')
Lucas Carvalho's avatar
Lucas Carvalho committed
96 97 98
      for  buildout_option, value in (
         ('%ssignature-private-key-file=%s', self.signature_private_key_file),
         ('%supload-cache-url=%s', self.upload_cache_url),
99 100 101 102 103 104
         ('%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
105 106 107
        if value:
          buildout_parameter_list.append( \
              buildout_option % ('networkcache:', value))
108 109

      buildout_parameter_list.extend(['-c', self.url])
110
      utils.bootstrapBuildout(self.software_path, self.buildout,
111 112
          additional_buildout_parametr_list=buildout_parameter_list,
          console=self.console)
113
      utils.launchBuildout(self.software_path,
114 115 116 117 118
                     os.path.join(self.software_path, 'bin', 'buildout'),
                     additional_buildout_parametr_list=buildout_parameter_list,
                     console=self.console)
    finally:
      shutil.rmtree(extends_cache)
Łukasz Nowak's avatar
Łukasz Nowak committed
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

  def remove(self):
    """Removes the part that was installed.
    """
    try:
      shutil.rmtree(self.software_path)
    except IOError as error:
      error_string = "I/O error while removing software (%s): %s" % (self.url,
                                                                     error)
      raise IOError(error_string)


class Partition(object):
  """This class is responsible of the installation of a instance
  """
  def __init__(self,
               software_path,
               instance_path,
               supervisord_partition_configuration_path,
               supervisord_socket,
               computer_partition,
               computer_id,
               partition_id,
               server_url,
               software_release_url,
144
               buildout,
Łukasz Nowak's avatar
Łukasz Nowak committed
145 146 147 148
               certificate_repository_path=None,
               console=False
               ):
    """Initialisation of class parameters"""
149
    self.buildout = buildout
Łukasz Nowak's avatar
Łukasz Nowak committed
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
    self.software_path = software_path
    self.instance_path = instance_path
    self.run_path = os.path.join(self.instance_path, 'etc', 'run')
    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.console = console

    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))
      partition_certificate = self.computer_partition.getCertificate()
      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]:
      os.chmod(f, 0400)
      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)

  def install(self):
    """ Creates configuration file from template in software_path, then
    installs the software partition with the help of buildout
    """
    # XXX: Shall be no op in case if revision had not changed
    #      It requires implementation of revision on server
    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)
205 206
    instance_stat_info = os.stat(self.instance_path)
    permission = oct(stat.S_IMODE(instance_stat_info.st_mode))
Łukasz Nowak's avatar
Łukasz Nowak committed
207 208 209 210 211 212
    if permission != REQUIRED_COMPUTER_PARTITION_PERMISSION:
      raise WrongPermissionError('Wrong permissions in %s : actual ' \
                                          'permissions are : %s, wanted ' \
                                          'are %s' %
                                          (self.instance_path, permission,
                                            REQUIRED_COMPUTER_PARTITION_PERMISSION))
213 214
    os.environ = utils.getCleanEnvironment(pwd.getpwuid(
      instance_stat_info.st_uid).pw_dir)
Łukasz Nowak's avatar
Łukasz Nowak committed
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
    # Generates buildout part from template
    # TODO how to fetch the good template? Naming conventions?
    template_location = os.path.join(self.software_path, 'template.cfg')
    config_location = os.path.join(self.instance_path, 'buildout.cfg')
    self.logger.debug("Coping %r to %r" % (template_location, config_location))
    shutil.copy(template_location, config_location)
    # 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)
    os.chmod(config_location, 0640)
    # 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:
        raise ValueError('More then one bootstrap candidate found.')
      # 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]))

      file = open(bootstrap_file, 'r')
      line = file.readline()
      file.close()
      invocation_list = []
      if line.startswith('#!'):
        invocation_list = line[2:].split()
      invocation_list.append(bootstrap_file)
      self.logger.debug('Invoking %r in %r' % (' '.join(invocation_list),
        self.instance_path))
      kw = dict()
      if not self.console:
        kw.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
270 271 272
      process_handler = utils.SlapPopen(invocation_list,
        preexec_fn=lambda: utils.dropPrivileges(uid, gid), cwd=self.instance_path,
        env=utils.getCleanEnvironment(pwd.getpwuid(uid).pw_dir), **kw)
Łukasz Nowak's avatar
Łukasz Nowak committed
273 274 275 276 277 278 279 280 281 282 283
      result_std = process_handler.communicate()[0]
      if self.console:
        result_std = 'Please consult messages above.'
      if process_handler.returncode is None or process_handler.returncode != 0:
        message = 'Failed to bootstrap buildout in %r:\n%s\n' % (
            self.instance_path, result_std)
        raise BuildoutFailedError(message)
      buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')

    if not os.path.exists(buildout_binary):
      # use own buildout generation
284
      utils.bootstrapBuildout(self.instance_path, self.buildout,
285 286
        ['buildout:bin-directory=%s'% os.path.join(self.instance_path,
        'sbin')], console=self.console)
Łukasz Nowak's avatar
Łukasz Nowak committed
287 288
      buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')
    # Launches buildout
289
    utils.launchBuildout(self.instance_path,
Łukasz Nowak's avatar
Łukasz Nowak committed
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
                   buildout_binary, console=self.console)
    # 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
    # if at least one is not 0750 raise -- partition has something funny
    runner_list = []
    if os.path.exists(self.run_path):
      if os.path.isdir(self.run_path):
        runner_list = os.listdir(self.run_path)
    if len(runner_list) == 0:
      self.logger.warning('No runners found for partition %r' %
          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()
      program_partition_template = pkg_resources.resource_stream(__name__,
          'templates/program_partition_supervisord.conf.in').read()
      group_partition_template = pkg_resources.resource_stream(__name__,
          'templates/group_partition_supervisord.conf.in').read()
      partition_supervisor_configuration = group_partition_template % dict(
          instance_id=partition_id,
          program_list=','.join(['_'.join([partition_id, runner])
            for runner in runner_list]))
      for runner in runner_list:
        partition_supervisor_configuration += '\n' + \
            program_partition_template % dict(
          program_id='_'.join([partition_id, runner]),
          program_directory=self.instance_path,
          program_command=os.path.join(self.run_path, runner),
          program_name=runner,
          instance_path=self.instance_path,
          user_id=uid,
          group_id=gid,
          # As supervisord has no environment to inherit setup minimalistic one
          HOME=pwd.getpwuid(uid).pw_dir,
          USER=pwd.getpwuid(uid).pw_name,
        )
329
      utils.updateFile(self.supervisord_partition_configuration_path,
Łukasz Nowak's avatar
Łukasz Nowak committed
330 331 332 333 334 335 336 337 338
          partition_supervisor_configuration)
    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()
339 340 341 342 343 344 345 346
    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
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

  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())
    # Gets actual buildout binary
    buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')
    if not os.path.exists(buildout_binary):
      buildout_binary = os.path.join(self.software_path, 'bin', 'buildout')
    # Launches "destroy" binary if exists
    destroy_executable_location = os.path.join(self.instance_path, 'sbin',
        'destroy')
    if os.path.exists(destroy_executable_location):
      # XXX: we should factorize this code
      uid, gid = None, None
      stat_info = os.stat(self.instance_path)
      uid = stat_info.st_uid
      gid = stat_info.st_gid
      self.logger.debug('Invoking %r' % destroy_executable_location)
      kw = dict()
      if not self.console:
        kw.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
382 383 384
      process_handler = utils.SlapPopen([destroy_executable_location],
        preexec_fn=lambda: utils.dropPrivileges(uid, gid), cwd=self.instance_path,
        env=utils.getCleanEnvironment(pwd.getpwuid(uid).pw_dir), **kw)
Łukasz Nowak's avatar
Łukasz Nowak committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      result_std = process_handler.communicate()[0]
      if self.console:
        result_std = 'Please consult messages above'
      if process_handler.returncode is None or process_handler.returncode != 0:
        message = 'Failed to destroy Computer Partition in %r:\n%s\n' % (
            self.instance_path, result_std)
        raise subprocess.CalledProcessError(message)
    # Manually cleans what remains
    try:
      for f in [self.key_file, self.cert_file]:
        if f:
          if os.path.exists(f):
            os.unlink(f)
      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')