SlapObject.py 35.3 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
import os
import pkg_resources
Marco Mariani's avatar
typos  
Marco Mariani committed
33
import pwd
Marco Mariani's avatar
Marco Mariani committed
34
import shutil
Łukasz Nowak's avatar
Łukasz Nowak committed
35
import stat
Marco Mariani's avatar
Marco Mariani committed
36
import subprocess
Marco Mariani's avatar
typos  
Marco Mariani committed
37
import tarfile
38
import tempfile
39
import textwrap
40
import time
Marco Mariani's avatar
typos  
Marco Mariani committed
41 42 43
import xmlrpclib

from supervisor import xmlrpc
Marco Mariani's avatar
Marco Mariani committed
44

45 46
from slapos.grid.utils import (md5digest, getCleanEnvironment,
                               SlapPopen, dropPrivileges, updateFile)
Marco Mariani's avatar
Marco Mariani committed
47
from slapos.grid import utils  # for methods that could be mocked, access them through the module
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
48
from slapos.slap.slap import NotFoundError
Marco Mariani's avatar
Marco Mariani committed
49 50
from slapos.grid.svcbackend import getSupervisorRPC
from slapos.grid.exception import (BuildoutFailedError, WrongPermissionError,
51
                                   PathDoesNotExistError, DiskSpaceError)
Marco Mariani's avatar
Marco Mariani committed
52
from slapos.grid.networkcache import download_network_cached, upload_network_cached
53
from slapos.human import bytes2human
54 55 56


WATCHDOG_MARK = '-on-watch'
Łukasz Nowak's avatar
Łukasz Nowak committed
57

Marco Mariani's avatar
Marco Mariani committed
58
REQUIRED_COMPUTER_PARTITION_PERMISSION = 0o750
Łukasz Nowak's avatar
Łukasz Nowak committed
59

60 61
CP_STORAGE_FOLDER_NAME = 'DATA'

62 63 64
# XXX not very clean. this is changed when testing
PROGRAM_PARTITION_TEMPLATE = pkg_resources.resource_stream(__name__,
            'templates/program_partition_supervisord.conf.in').read()
Łukasz Nowak's avatar
Łukasz Nowak committed
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
def free_space(path, fn):
  while True:
    try:
      disk = os.statvfs(path)
      return fn(disk)
    except OSError:
      pass
    if os.sep not in path:
      break
    path = os.path.split(path)[0]


def free_space_root(path):
  """
  Returns free space available to the root user, in bytes.

  A non-existent path can be provided, and the ancestors
  will be queried instead.
  """
  return free_space(path, lambda d: d.bsize * d.f_bfree)


def free_space_nonroot(path):
  """
  Returns free space available to non-root users, in bytes.

  A non-existent path can be provided, and the ancestors
  will be queried instead.
  """
  return free_space(path, lambda d: d.f_bsize * d.f_bavail)


Łukasz Nowak's avatar
Łukasz Nowak committed
99
class Software(object):
Marco Mariani's avatar
typos  
Marco Mariani committed
100
  """This class is responsible for installing a software release"""
Marco Mariani's avatar
Marco Mariani committed
101

Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
102 103
  # XXX: "url" parameter should be named "key", "target" or alike to be more generic.
  #      The key is an url in the case of Buildout.
104
  def __init__(self, url, software_root, buildout, logger,
Marco Mariani's avatar
Marco Mariani committed
105 106 107 108 109
               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,
               download_binary_dir_url=None, upload_binary_dir_url=None,
Marco Mariani's avatar
Marco Mariani committed
110
               download_from_binary_cache_url_blacklist=None,
111 112
               upload_to_binary_cache_url_blacklist=None,
               software_min_free_space=None):
Łukasz Nowak's avatar
Łukasz Nowak committed
113 114
    """Initialisation of class parameters
    """
Marco Mariani's avatar
Marco Mariani committed
115 116 117 118 119 120 121

    if download_from_binary_cache_url_blacklist is None:
      download_from_binary_cache_url_blacklist = []

    if upload_to_binary_cache_url_blacklist is None:
      upload_to_binary_cache_url_blacklist = []

Łukasz Nowak's avatar
Łukasz Nowak committed
122 123
    self.url = url
    self.software_root = software_root
124
    self.software_url_hash = md5digest(self.url)
Łukasz Nowak's avatar
Łukasz Nowak committed
125
    self.software_path = os.path.join(self.software_root,
Yingjie Xu's avatar
Yingjie Xu committed
126
                                      self.software_url_hash)
127
    self.buildout = buildout
128
    self.logger = logger
129
    self.signature_private_key_file = signature_private_key_file
Yingjie Xu's avatar
Yingjie Xu committed
130
    self.signature_certificate_list = signature_certificate_list
131 132
    self.upload_cache_url = upload_cache_url
    self.upload_dir_url = upload_dir_url
133 134 135 136
    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
137 138 139 140
    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
141 142 143 144
    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
145 146 147 148 149 150 151 152 153 154 155
    self.software_min_free_space = software_min_free_space

  def check_free_space(self):
    required = self.software_min_free_space
    available = free_space_nonroot(self.software_path)

    if available < required:
      msg = "Not enough space for {path}: available {available}, required {required} (option 'software_min_free_space')"
      raise DiskSpaceError(msg.format(path=self.software_path,
                                      available=bytes2human(available),
                                      required=bytes2human(required)))
Łukasz Nowak's avatar
Łukasz Nowak committed
156 157

  def install(self):
Yingjie Xu's avatar
Yingjie Xu committed
158 159 160
    """ Fetches binary cache if possible.
    Installs from buildout otherwise.
    """
161
    self.logger.info("Installing software release %s..." % self.url)
Yingjie Xu's avatar
Yingjie Xu committed
162
    cache_dir = tempfile.mkdtemp()
163 164 165

    self.check_free_space()

166 167 168 169 170 171 172 173 174 175 176 177 178
    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
179
        try:
180 181
          self.logger.info("Extracting archive of cached software release...")
          tar.extractall(path=self.software_root)
Yingjie Xu's avatar
Yingjie Xu committed
182 183
        finally:
          tar.close()
184 185
      else:
        self._install_from_buildout()
186
        # Upload to binary cache if possible and allowed
Marco Mariani's avatar
Marco Mariani committed
187 188
        if all([self.software_root, self.url, self.software_url_hash,
                self.upload_binary_cache_url, self.upload_binary_dir_url]):
189 190 191 192 193
          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: "
Marco Mariani's avatar
Marco Mariani committed
194
                               "Software Release URL is blacklisted.")
195 196 197
              break
          if not blacklisted:
            self.uploadSoftwareRelease(tarpath)
198 199
    finally:
      shutil.rmtree(cache_dir)
200

201 202 203 204 205 206 207 208 209 210 211 212 213
  def _set_ownership(self, path):
    """
    If running as root: copy ownership of software_root to path
    If not running as root: do nothing
    """
    if os.getuid():
      return
    root_stat = os.stat(self.software_root)
    path_stat = os.stat(path)
    if (root_stat.st_uid != path_stat.st_uid or
          root_stat.st_gid != path_stat.st_gid):
      os.chown(path, root_stat.st_uid, root_stat.st_gid)

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  def _additional_buildout_parameters(self, extends_cache):
    yield 'buildout:extends-cache=%s' % extends_cache
    yield 'buildout:directory=%s' % self.software_path

    if (self.signature_private_key_file or
          self.upload_cache_url or
          self.upload_dir_url):
      yield 'buildout:networkcache-section=networkcache'

    for networkcache_option, value in [
        ('signature-private-key-file', self.signature_private_key_file),
        ('upload-cache-url', self.upload_cache_url),
        ('upload-dir-url', self.upload_dir_url),
        ('shacache-cert-file', self.shacache_cert_file),
        ('shacache-key-file', self.shacache_key_file),
        ('shadir-cert-file', self.shadir_cert_file),
        ('shadir-key-file', self.shadir_key_file)
    ]:
      if value:
        yield 'networkcache:%s=%s' % (networkcache_option, value)

Yingjie Xu's avatar
Yingjie Xu committed
235
  def _install_from_buildout(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
236 237 238
    """ Fetches buildout configuration from the server, run buildout with
    it. If it fails, we notify the server.
    """
239
    root_stat = os.stat(self.software_root)
240
    os.environ = getCleanEnvironment(logger=self.logger,
241
                                     home_path=pwd.getpwuid(root_stat.st_uid).pw_dir)
Łukasz Nowak's avatar
Łukasz Nowak committed
242 243
    if not os.path.isdir(self.software_path):
      os.mkdir(self.software_path)
244 245
      self._set_ownership(self.software_path)

246
    extends_cache = tempfile.mkdtemp()
247 248
    self._set_ownership(extends_cache)

249
    try:
250
      buildout_cfg = os.path.join(self.software_path, 'buildout.cfg')
251 252 253 254 255
      if not os.path.exists(buildout_cfg):
        self._create_buildout_profile(buildout_cfg, self.url)

      additional_parameters = list(self._additional_buildout_parameters(extends_cache))
      additional_parameters.extend(['-c', buildout_cfg])
256

257 258 259
      utils.bootstrapBuildout(path=self.software_path,
                              buildout=self.buildout,
                              logger=self.logger,
260 261
                              additional_buildout_parameter_list=additional_parameters)

262 263 264
      utils.launchBuildout(path=self.software_path,
                           buildout_binary=os.path.join(self.software_path, 'bin', 'buildout'),
                           logger=self.logger,
265
                           additional_buildout_parameter_list=additional_parameters)
266 267
    finally:
      shutil.rmtree(extends_cache)
Łukasz Nowak's avatar
Łukasz Nowak committed
268

269 270 271 272 273 274 275
  def _create_buildout_profile(self, buildout_cfg, url):
    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]
276 277
          extends =
            {url}
278 279
          """.format(url=url)))
    self._set_ownership(buildout_cfg)
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  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
304 305 306
  def destroy(self):
    """Removes software release."""
    def retry(func, path, exc):
Marco Mariani's avatar
typos  
Marco Mariani committed
307
      # inspired by slapos.buildout hard remover
Łukasz Nowak's avatar
Łukasz Nowak committed
308 309 310
      if func == os.path.islink:
        os.unlink(path)
      else:
Marco Mariani's avatar
Marco Mariani committed
311
        os.chmod(path, 0o600)
Łukasz Nowak's avatar
Łukasz Nowak committed
312
        func(path)
Łukasz Nowak's avatar
Łukasz Nowak committed
313
    try:
314
      if os.path.exists(self.software_path):
Łukasz Nowak's avatar
Łukasz Nowak committed
315
        self.logger.info('Removing path %r' % self.software_path)
316
        shutil.rmtree(self.software_path, onerror=retry)
Łukasz Nowak's avatar
Łukasz Nowak committed
317 318 319
      else:
        self.logger.info('Path %r does not exists, no need to remove.' %
            self.software_path)
320 321
    except IOError as exc:
      raise IOError("I/O error while removing software (%s): %s" % (self.url, exc))
Łukasz Nowak's avatar
Łukasz Nowak committed
322 323 324


class Partition(object):
Marco Mariani's avatar
Marco Mariani committed
325
  """This class is responsible of the installation of an instance
Łukasz Nowak's avatar
Łukasz Nowak committed
326
  """
327 328
  retention_lock_delay_filename = '.slapos-retention-lock-delay'
  retention_lock_date_filename = '.slapos-retention-lock-date'
Marco Mariani's avatar
Marco Mariani committed
329

Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
330 331
  # XXX: we should give the url (or the "key") instead of the software_path
  #      then compute the path from it, like in Software.
Łukasz Nowak's avatar
Łukasz Nowak committed
332 333 334 335 336 337 338 339 340 341
  def __init__(self,
               software_path,
               instance_path,
               supervisord_partition_configuration_path,
               supervisord_socket,
               computer_partition,
               computer_id,
               partition_id,
               server_url,
               software_release_url,
342
               buildout,
343
               logger,
Łukasz Nowak's avatar
Łukasz Nowak committed
344
               certificate_repository_path=None,
345
               retention_delay='0',
346
               instance_min_free_space=None,
347 348
               instance_storage_home='',
               ipv4_global_network='',
Łukasz Nowak's avatar
Łukasz Nowak committed
349 350
               ):
    """Initialisation of class parameters"""
351
    self.buildout = buildout
352
    self.logger = logger
Łukasz Nowak's avatar
Łukasz Nowak committed
353 354 355
    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
356
    self.service_path = os.path.join(self.instance_path, 'etc', 'service')
Łukasz Nowak's avatar
Łukasz Nowak committed
357 358 359 360 361 362 363 364
    self.supervisord_partition_configuration_path = \
        supervisord_partition_configuration_path
    self.supervisord_socket = supervisord_socket
    self.computer_partition = computer_partition
    self.computer_id = computer_id
    self.partition_id = partition_id
    self.server_url = server_url
    self.software_release_url = software_release_url
365
    self.instance_storage_home = instance_storage_home
366
    self.ipv4_global_network = ipv4_global_network
Łukasz Nowak's avatar
Łukasz Nowak committed
367 368 369 370 371 372 373 374 375 376

    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()

377 378 379
    try:
      self.retention_delay = float(retention_delay)
    except ValueError:
380 381 382 383 384 385 386 387 388 389 390
      self.logger.warn('Retention delay value (%s) is not valid, ignoring.' \
                       % self.retention_delay)
      self.retention_delay = 0

    self.retention_lock_delay_file_path = os.path.join(
        self.instance_path, self.retention_lock_delay_filename
    )
    self.retention_lock_date_file_path = os.path.join(
        self.instance_path, self.retention_lock_date_filename
    )

391 392 393 394 395 396 397 398 399 400 401 402 403
    self.instance_min_free_space = instance_min_free_space


  def check_free_space(self):
    required = self.instance_min_free_space
    available = free_space_nonroot(self.instance_path)

    if available < required:
      msg = "Not enough space for {path}: available {available}, required {required} (option 'instance_min_free_space')"
      raise DiskSpaceError(msg.format(path=self.instance_path,
                                      available=bytes2human(available),
                                      required=bytes2human(required)))

Łukasz Nowak's avatar
Łukasz Nowak committed
404
  def _updateCertificate(self):
405 406 407 408 409 410 411 412
    try:
      partition_certificate = self.computer_partition.getCertificate()
    except NotFoundError:
      raise NotFoundError('Partition %s is not known by SlapOS Master.' %
          self.partition_id)

    uid, gid = self.getUserGroupId()

Marco Mariani's avatar
Marco Mariani committed
413
    for name, path in [('certificate', self.cert_file), ('key', self.key_file)]:
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
      new_content = partition_certificate[name]
      old_content = None
      if os.path.exists(path):
        old_content = open(path).read()

      if old_content != new_content:
        if old_content is None:
          self.logger.info('Missing %s file. Creating %r' % (name, path))
        else:
          self.logger.info('Changed %s content. Updating %r' % (name, path))

        with os.fdopen(os.open(path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o400), 'wb') as fout:
          fout.write(new_content)
        os.chown(path, uid, gid)

Łukasz Nowak's avatar
Łukasz Nowak committed
429 430 431 432 433 434 435
  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 de Saint Martin's avatar
Cédric de Saint Martin committed
436
  def addServiceToGroup(self, partition_id, runner_list, path, extension=''):
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
437 438 439
    uid, gid = self.getUserGroupId()
    for runner in runner_list:
      self.partition_supervisor_configuration += '\n' + \
440
          PROGRAM_PARTITION_TEMPLATE % {
Marco Mariani's avatar
Marco Mariani committed
441 442 443 444 445 446 447 448 449 450 451
              '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,
              # As supervisord has no environment to inherit, setup a minimalistic one
              'HOME': pwd.getpwuid(uid).pw_dir,
              'USER': pwd.getpwuid(uid).pw_name,
          }
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
452

453
  def updateSymlink(self, sr_symlink, software_path):
454
    if os.path.lexists(sr_symlink):
455
      if not os.path.islink(sr_symlink):
456
        self.logger.debug('Not a symlink: %s, has been ignored' % sr_symlink)
457 458 459 460 461
        return
      os.unlink(sr_symlink)
    os.symlink(software_path, sr_symlink)
    os.lchown(sr_symlink, *self.getUserGroupId())

Łukasz Nowak's avatar
Łukasz Nowak committed
462 463 464 465
  def install(self):
    """ Creates configuration file from template in software_path, then
    installs the software partition with the help of buildout
    """
Marco Mariani's avatar
Marco Mariani committed
466
    self.logger.info("Installing Computer Partition %s..."
Łukasz Nowak's avatar
Łukasz Nowak committed
467
        % self.computer_partition.getId())
468 469 470

    self.check_free_space()

Łukasz Nowak's avatar
Łukasz Nowak committed
471 472 473 474 475
    # 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)
476 477 478 479

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

480
    instance_stat_info = os.stat(self.instance_path)
Marco Mariani's avatar
Marco Mariani committed
481
    permission = stat.S_IMODE(instance_stat_info.st_mode)
Łukasz Nowak's avatar
Łukasz Nowak committed
482
    if permission != REQUIRED_COMPUTER_PARTITION_PERMISSION:
Marco Mariani's avatar
Marco Mariani committed
483 484 485 486
      raise WrongPermissionError('Wrong permissions in %s: actual '
                                 'permissions are: 0%o, wanted are 0%o' %
                                 (self.instance_path, permission,
                                  REQUIRED_COMPUTER_PARTITION_PERMISSION))
487 488
    os.environ = getCleanEnvironment(logger=self.logger,
                                     home_path=pwd.getpwuid(instance_stat_info.st_uid).pw_dir)
489

490 491 492 493
    # Check that Software Release directory is present
    if not os.path.exists(self.software_path):
      # XXX What should it raise?
      raise IOError('Software Release %s is not present on system.\n'
Marco Mariani's avatar
Marco Mariani committed
494
                    'Cannot deploy instance.' % self.software_release_url)
495

496
    # Generate buildout instance profile from template in Software Release
497 498
    template_location = os.path.join(self.software_path, 'instance.cfg')
    if not os.path.exists(template_location):
499 500 501 502 503 504 505 506
      # Backward compatibility: "instance.cfg" file was named "template.cfg".
      if os.path.exists(os.path.join(self.software_path, 'template.cfg')):
        template_location = os.path.join(self.software_path, 'template.cfg')
      else:
        # No template: Software Release is either inconsistent or not correctly installed.
        # XXX What should it raise?
        raise IOError('Software Release %s is not correctly installed.\nMissing file: %s' % (
            self.software_release_url, template_location))
Łukasz Nowak's avatar
Łukasz Nowak committed
507
    config_location = os.path.join(self.instance_path, 'buildout.cfg')
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
508
    self.logger.debug("Copying %r to %r" % (template_location, config_location))
509 510
    shutil.copy(template_location, config_location)

Łukasz Nowak's avatar
Łukasz Nowak committed
511 512 513
    # fill generated buildout with additional information
    buildout_text = open(config_location).read()
    buildout_text += '\n\n' + pkg_resources.resource_string(__name__,
Marco Mariani's avatar
Marco Mariani committed
514
        'templates/buildout-tail.cfg.in') % {
Marco Mariani's avatar
Marco Mariani committed
515 516 517 518 519 520
            '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,
521
            'storage_home': self.instance_storage_home,
522
            'global_ipv4_network_prefix': self.ipv4_global_network,
Marco Mariani's avatar
Marco Mariani committed
523
        }
Łukasz Nowak's avatar
Łukasz Nowak committed
524
    open(config_location, 'w').write(buildout_text)
Marco Mariani's avatar
Marco Mariani committed
525
    os.chmod(config_location, 0o640)
Łukasz Nowak's avatar
Łukasz Nowak committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    # 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')
541
      self.logger.info("Falling back to default buildout %r" %
Łukasz Nowak's avatar
Łukasz Nowak committed
542 543 544
        buildout_binary)
    else:
      if len(bootstrap_candidate_list) != 1:
Marco Mariani's avatar
typos  
Marco Mariani committed
545
        raise ValueError('More than one bootstrap candidate found.')
Łukasz Nowak's avatar
Łukasz Nowak committed
546 547 548 549
      # 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
550
      first_line = open(bootstrap_file, 'r').readline()
Łukasz Nowak's avatar
Łukasz Nowak committed
551
      invocation_list = []
Marco Mariani's avatar
Marco Mariani committed
552 553
      if first_line.startswith('#!'):
        invocation_list = first_line[2:].split()
Łukasz Nowak's avatar
Łukasz Nowak committed
554
      invocation_list.append(bootstrap_file)
Marco Mariani's avatar
Marco Mariani committed
555

Łukasz Nowak's avatar
Łukasz Nowak committed
556 557
      self.logger.debug('Invoking %r in %r' % (' '.join(invocation_list),
        self.instance_path))
Marco Mariani's avatar
Marco Mariani committed
558
      process_handler = SlapPopen(invocation_list,
559
                                  preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger),
Marco Mariani's avatar
Marco Mariani committed
560
                                  cwd=self.instance_path,
561 562
                                  env=getCleanEnvironment(logger=self.logger,
                                                          home_path=pwd.getpwuid(uid).pw_dir),
Marco Mariani's avatar
Marco Mariani committed
563
                                  stdout=subprocess.PIPE,
564 565
                                  stderr=subprocess.STDOUT,
                                  logger=self.logger)
Łukasz Nowak's avatar
Łukasz Nowak committed
566
      if process_handler.returncode is None or process_handler.returncode != 0:
567
        message = 'Failed to bootstrap buildout in %r.' % (self.instance_path)
568 569
        self.logger.error(message)
        raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output))
Łukasz Nowak's avatar
Łukasz Nowak committed
570 571 572 573
      buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')

    if not os.path.exists(buildout_binary):
      # use own buildout generation
574 575 576 577 578 579
      utils.bootstrapBuildout(path=self.instance_path,
                              buildout=self.buildout,
                              logger=self.logger,
                              additional_buildout_parameter_list=
                                ['buildout:bin-directory=%s' %
                                    os.path.join(self.instance_path, 'sbin')])
Łukasz Nowak's avatar
Łukasz Nowak committed
580
      buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout')
581

Łukasz Nowak's avatar
Łukasz Nowak committed
582
    # Launches buildout
583 584 585
    utils.launchBuildout(path=self.instance_path,
                         buildout_binary=buildout_binary,
                         logger=self.logger)
586
    self.generateSupervisorConfigurationFile()
587
    self.createRetentionLockDelay()
588 589 590 591 592 593 594 595 596 597

  def generateSupervisorConfigurationFile(self):
    """
    Generates supervisord configuration file from template.

    check if CP/etc/run exists and it is a directory
    iterate over each file in CP/etc/run
    iterate over each file in CP/etc/service adding WatchdogID to their name
    if at least one is not 0o750 raise -- partition has something funny
    """
Łukasz Nowak's avatar
Łukasz Nowak committed
598
    runner_list = []
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
599
    service_list = []
Łukasz Nowak's avatar
Łukasz Nowak committed
600 601 602
    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
603 604 605 606 607
    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
608 609 610 611 612 613 614
          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()
Marco Mariani's avatar
Marco Mariani committed
615
      self.partition_supervisor_configuration = group_partition_template % {
Marco Mariani's avatar
Marco Mariani committed
616 617 618 619
          'instance_id': partition_id,
          'program_list': ','.join(['_'.join([partition_id, runner])
                                    for runner in runner_list + service_list])
      }
Marco Mariani's avatar
Marco Mariani committed
620
      # Same method to add to service and run
Marco Mariani's avatar
Marco Mariani committed
621 622
      self.addServiceToGroup(partition_id, runner_list, self.run_path)
      self.addServiceToGroup(partition_id, service_list, self.service_path,
623
                             extension=WATCHDOG_MARK)
Marco Mariani's avatar
Marco Mariani committed
624
      updateFile(self.supervisord_partition_configuration_path,
Marco Mariani's avatar
Marco Mariani committed
625
                 self.partition_supervisor_configuration)
Łukasz Nowak's avatar
Łukasz Nowak committed
626 627 628 629 630 631 632 633
    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()
634 635
    try:
      supervisor.startProcessGroup(partition_id, False)
636 637
    except xmlrpclib.Fault as exc:
      if exc.faultString.startswith('BAD_NAME:'):
Marco Mariani's avatar
Marco Mariani committed
638
        self.logger.info("Nothing to start on %s..." %
639 640 641
                         self.computer_partition.getId())
    else:
      self.logger.info("Requested start of %s..." % self.computer_partition.getId())
Łukasz Nowak's avatar
Łukasz Nowak committed
642 643 644 645 646

  def stop(self):
    """Asks supervisord to stop the instance."""
    partition_id = self.computer_partition.getId()
    try:
647
      supervisor = self.getSupervisorRPC()
Łukasz Nowak's avatar
Łukasz Nowak committed
648
      supervisor.stopProcessGroup(partition_id, False)
649 650
    except xmlrpclib.Fault as exc:
      if exc.faultString.startswith('BAD_NAME:'):
Łukasz Nowak's avatar
Łukasz Nowak committed
651 652 653 654 655 656 657
        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."
    """
Marco Mariani's avatar
Marco Mariani committed
658
    self.logger.info("Destroying Computer Partition %s..."
Łukasz Nowak's avatar
Łukasz Nowak committed
659
        % self.computer_partition.getId())
660 661 662 663 664

    self.createRetentionLockDate()
    if not self.checkRetentionIsAuthorized():
      return False

Łukasz Nowak's avatar
Łukasz Nowak committed
665 666 667 668
    # 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
669
      uid, gid = self.getUserGroupId()
Łukasz Nowak's avatar
Łukasz Nowak committed
670
      self.logger.debug('Invoking %r' % destroy_executable_location)
Marco Mariani's avatar
Marco Mariani committed
671
      process_handler = SlapPopen([destroy_executable_location],
672
                                  preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger),
Marco Mariani's avatar
Marco Mariani committed
673
                                  cwd=self.instance_path,
674 675
                                  env=getCleanEnvironment(logger=self.logger,
                                                          home_path=pwd.getpwuid(uid).pw_dir),
Marco Mariani's avatar
Marco Mariani committed
676
                                  stdout=subprocess.PIPE,
677 678
                                  stderr=subprocess.STDOUT,
                                  logger=self.logger)
Łukasz Nowak's avatar
Łukasz Nowak committed
679
      if process_handler.returncode is None or process_handler.returncode != 0:
680 681
        message = 'Failed to destroy Computer Partition in %r.' % \
            self.instance_path
682 683
        self.logger.error(message)
        raise subprocess.CalledProcessError(message, process_handler.output)
Łukasz Nowak's avatar
Łukasz Nowak committed
684 685 686 687 688 689
    # 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
690 691 692 693 694

      # 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)
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
      data_base_link = os.path.join(self.instance_path, CP_STORAGE_FOLDER_NAME)
      if self.instance_storage_home and os.path.exists(data_base_link) and \
                                os.path.isdir(data_base_link):
        for filename in os.listdir(data_base_link):
          data_symlink = os.path.join(data_base_link, filename)
          partition_data_path = os.path.join(self.instance_storage_home,
                                                    filename, self.partition_id)
          if os.path.lexists(data_symlink):
            os.unlink(data_symlink)
          if os.path.exists(partition_data_path):
            self.cleanupFolder(partition_data_path)

      self.cleanupFolder(self.instance_path)
      
      # Cleanup all Data storage location of this partition
      
711 712 713 714

      if os.path.exists(self.supervisord_partition_configuration_path):
        os.remove(self.supervisord_partition_configuration_path)
      self.updateSupervisor()
715 716
    except IOError as exc:
      raise IOError("I/O error while freeing partition (%s): %s" % (self.instance_path, exc))
Łukasz Nowak's avatar
Łukasz Nowak committed
717

718 719
    return True

720 721 722 723 724 725 726 727 728
  def cleanupFolder(self, folder_path):
    """Delete all files and folders in a specified directory
    """
    for root, dirs, file_list in os.walk(folder_path):
      for directory in dirs:
        shutil.rmtree(os.path.join(folder_path, directory))
      for file in file_list:
        os.remove(os.path.join(folder_path, file))

Łukasz Nowak's avatar
Łukasz Nowak committed
729 730 731 732 733 734 735 736 737 738 739
  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
740
    #       In future it will not be needed, as update command
Łukasz Nowak's avatar
Łukasz Nowak committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    #       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)
756 757 758 759 760 761 762 763 764 765
      for i in xrange(0, 10):
        # Some process may be still running, be nice and wait for them to be stopped.
        try:
          supervisor.removeProcessGroup(gname)
          break
        except:
          if i == 9:
            raise
          time.sleep(1)

Łukasz Nowak's avatar
Łukasz Nowak committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779
      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')
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

  def _set_ownership(self, path):
    """
    If running as root: copy ownership of software_path to path
    If not running as root: do nothing
    """
    if os.getuid():
      return
    root_stat = os.stat(self.software_path)
    path_stat = os.stat(path)
    if (root_stat.st_uid != path_stat.st_uid or
          root_stat.st_gid != path_stat.st_gid):
      os.chown(path, root_stat.st_uid, root_stat.st_gid)

  def checkRetentionIsAuthorized(self):
    """
    Check if retention is authorized by checking retention lock delay or
    retention lock date.

    A retention lock delay is a delay which is:
     * Defined by the user/machine who requested the instance
     * Hardcoded the first time the instance is deployed, then is read-only
       during the whole lifetime of the instance
     * Triggered the first time the instance is requested to be destroyed
       (retention will be ignored).
       From this point, it is not possible to destroy the instance until the
       delay is over.
     * Accessible in read-only mode from the partition

    A retention lock date is the date computed from (date of first
    retention request + retention lock delay in days).

    Example:
     * User requests an instance with delay as 10 (days) to a SlapOS Master
     * SlapOS Master transmits this information to the SlapOS Node (current code)
     * SlapOS Node hardcodes this delay at first deployment
     * User requests retention of instance
     * SlapOS Node tries to destroy for the first time: it doesn't actually
       destroy, but it triggers the creation of a retention lock date from
       from the hardcoded delay. At this point it is not possible to
       destroy instance until current date + 10 days.
     * SlapOS Node continues to try to destroy: it doesn't do anything until
       retention lock date is reached.
    """
    retention_lock_date = self.getExistingRetentionLockDate()
    now = time.time()
    if not retention_lock_date:
      if self.getExistingRetentionLockDelay() > 0:
        self.logger.info('Impossible to destroy partition yet because of retention lock.')
        return False
      # Else: OK to destroy
    else:
      if now < retention_lock_date:
        self.logger.info('Impossible to destroy partition yet because of retention lock.')
        return False
      # Else: OK to destroy
    return True

  def createRetentionLockDelay(self):
    """
    Create a retention lock delay for the current partition.
    If retention delay is not specified, create it wth "0" as value
    """
    if os.path.exists(self.retention_lock_delay_file_path):
      return
    with open(self.retention_lock_delay_file_path, 'w') as delay_file_path:
      delay_file_path.write(str(self.retention_delay))
    self._set_ownership(self.retention_lock_delay_file_path)

  def getExistingRetentionLockDelay(self):
    """
    Return the retention lock delay of current partition (created at first
    deployment) if exist.
    Return -1 otherwise.
    """
    retention_delay = -1
    if os.path.exists(self.retention_lock_delay_file_path):
      with open(self.retention_lock_delay_file_path) as delay_file_path:
        retention_delay = float(delay_file_path.read())
    return retention_delay

  def createRetentionLockDate(self):
    """
    If retention lock delay > 0:
    Create a retention lock date for the current partition from the
    retention lock delay.
    Do nothing otherwise.
    """
    if os.path.exists(self.retention_lock_date_file_path):
      return
    retention_delay = self.getExistingRetentionLockDelay()
    if retention_delay <= 0:
      return
    now = int(time.time())
    retention_date = now + retention_delay * 24 * 3600
    with open(self.retention_lock_date_file_path, 'w') as date_file_path:
      date_file_path.write(str(retention_date))
    self._set_ownership(self.retention_lock_date_file_path)

  def getExistingRetentionLockDate(self):
    """
    Return the retention lock delay of current partition if exist.
    Return None otherwise.
    """
    if os.path.exists(self.retention_lock_date_file_path):
      with open(self.retention_lock_date_file_path) as date_file_path:
        return float(date_file_path.read())
    else:
      return None