format.py 39.6 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 33 34 35
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010, 2011 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 advised 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.
#
##############################################################################
from optparse import OptionParser, Option
from xml_marshaller import xml_marshaller
import ConfigParser
import grp
import logging
import netaddr
import netifaces
import os
Łukasz Nowak's avatar
Łukasz Nowak committed
36
import pwd
Łukasz Nowak's avatar
Łukasz Nowak committed
37 38 39 40 41 42 43
import random
import slapos.slap as slap
import socket
import subprocess
import sys
import time

Vincent Pelletier's avatar
Vincent Pelletier committed
44 45 46 47
class OS(object):
  _os = os

  def __init__(self, config):
48
    self._dry_run = config.dry_run
Vincent Pelletier's avatar
Vincent Pelletier committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    self._verbose = config.verbose
    self._logger = config.logger
    add = self._addWrapper
    add('chown')
    add('chmod')
    add('makedirs')
    add('mkdir')

  def _addWrapper(self, name):
    def wrapper(*args, **kw):
      if self._verbose:
        arg_list = [repr(x) for x in args] + [
          '%s=%r' % (x, y) for x, y in kw.iteritems()]
        self._logger.debug('%s(%s)' % (
          name,
          ', '.join(arg_list)
        ))
66 67
      if not self._dry_run:
        getattr(self._os, name)(*args, **kw)
Vincent Pelletier's avatar
Vincent Pelletier committed
68 69 70 71
    setattr(self, name, wrapper)

  def __getattr__(self, name):
    return getattr(self._os, name)
Łukasz Nowak's avatar
Łukasz Nowak committed
72

73 74 75 76
class UsageError(Exception):
  pass

class NoAddressOnBridge(Exception):
Łukasz Nowak's avatar
Łukasz Nowak committed
77
  """
78 79 80 81 82
  Exception raised if there's not address on the bridge to construct IPv6
  address with.

  Attributes:
    brige: String, the name of the bridge.
Łukasz Nowak's avatar
Łukasz Nowak committed
83 84
  """

85 86 87 88
  def __init__(self, bridge):
    super(NoAddressOnBridge, self).__init__(
      'No IPv6 found on bridge %s to construct IPv6 with.' % (bridge, )
    )
Łukasz Nowak's avatar
Łukasz Nowak committed
89

90 91 92 93 94 95 96 97 98 99 100 101
class AddressGenerationError(Exception):
  """
  Exception raised if the generation of an IPv6 based on the prefix obtained
  from the bridge failed.

  Attributes:
    addr: String, the invalid address the exception is raised for.
  """
  def __init__(self, addr):
    super(AddressGenerationError, self).__init__(
      'Generated IPv6 %s seems not to be a valid IP.' % addr
    )
Łukasz Nowak's avatar
Łukasz Nowak committed
102 103 104 105 106 107

def callAndRead(argument_list, raise_on_error=True):
  popen = subprocess.Popen(argument_list, stdout=subprocess.PIPE,
      stderr=subprocess.STDOUT)
  result = popen.communicate()[0]
  if raise_on_error and popen.returncode != 0:
Vincent Pelletier's avatar
Vincent Pelletier committed
108 109
    raise ValueError('Issue during invoking %r, result was:\n%s' % (
      argument_list, result))
Łukasz Nowak's avatar
Łukasz Nowak committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  return popen.returncode, result

def isGlobalScopeAddress(a):
  """Returns True if a is global scope IP v4/6 address"""
  ip = netaddr.IPAddress(a)
  return not ip.is_link_local() and not ip.is_loopback() and \
      not ip.is_reserved() and ip.is_unicast()

def netmaskToPrefixIPv4(netmask):
  """Convert string represented netmask to its integer prefix"""
  return netaddr.strategy.ipv4.netmask_to_prefix[
          netaddr.strategy.ipv4.str_to_int(netmask)]

def netmaskToPrefixIPv6(netmask):
  """Convert string represented netmask to its integer prefix"""
  return netaddr.strategy.ipv6.netmask_to_prefix[
          netaddr.strategy.ipv6.str_to_int(netmask)]

def _getDict(instance):
  """
  Serialize an object instance into dictionaries. List and dict will remains
  the same, basic type too. But encapsulated object will be returned as dict.
  Set, collections and other aren't handle for now.

  Args:
    instance: an object of any type.

  Returns:
    A dictionary if the given object wasn't a list, a list otherwise.
  """
  if isinstance(instance, list):
    return [_getDict(item) for item in instance]

  elif isinstance(instance, dict):
    result = {}
145
    for key in instance:
Łukasz Nowak's avatar
Łukasz Nowak committed
146 147 148 149 150
      result[key] = _getDict(instance[key])
    return result

  else:
    try:
151
      dikt = instance.__dict__
Łukasz Nowak's avatar
Łukasz Nowak committed
152 153
    except AttributeError:
      return instance
154 155 156 157
    result = {}
    for key, value in dikt.iteritems():
      result[key] = _getDict(value)
    return result
Łukasz Nowak's avatar
Łukasz Nowak committed
158

Vincent Pelletier's avatar
Vincent Pelletier committed
159
class Computer(object):
Łukasz Nowak's avatar
Łukasz Nowak committed
160
  "Object representing the computer"
161 162
  instance_root = None
  software_root = None
Łukasz Nowak's avatar
Łukasz Nowak committed
163

164
  def __init__(self, reference, bridge=None, addr=None, netmask=None,
165
    ipv6_interface=None, software_user='slapsoft'):
Łukasz Nowak's avatar
Łukasz Nowak committed
166 167 168 169 170 171 172 173 174 175
    """
    Attributes:
      reference: String, the reference of the computer.
      bridge: String, if it has one, the name of the computer's bridge.
    """
    self.reference = str(reference)
    self.bridge = bridge
    self.partition_list = []
    self.address = addr
    self.netmask = netmask
Łukasz Nowak's avatar
Łukasz Nowak committed
176
    self.ipv6_interface = ipv6_interface
177
    self.software_user = software_user
Łukasz Nowak's avatar
Łukasz Nowak committed
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

  def __getinitargs__(self):
    return (self.reference, self.bridge)

  def getAddress(self):
    """
    Return a list of the bridge address not attributed to any partition, (which
    are therefore free for the computer itself).

    Returns:
      False if the bridge isn't available, else the list of the free addresses.
    """
    if self.bridge is None:
      return dict(addr=self.address, netmask=self.netmask)

    computer_partition_address_list = []
    for partition in self.partition_list:
      for address in partition.address_list:
        if netaddr.valid_ipv6(address['addr']):
          computer_partition_address_list.append(address['addr'])
    # Going through addresses of the computer's bridge interface
    for address_dict in self.bridge.getGlobalScopeAddressList():
      # Comparing with computer's partition addresses
      if address_dict['addr'] not in computer_partition_address_list:
        return address_dict

204 205
    # all addresses on interface are for partition, so lets add new one
    computer_tap = Tap('compdummy')
Łukasz Nowak's avatar
Łukasz Nowak committed
206
    computer_tap.createWithOwner(User('root'), attach_to_tap=True)
207 208
    self.bridge.addTap(computer_tap)
    return self.bridge.addAddr()
Łukasz Nowak's avatar
Łukasz Nowak committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

  def send(self, config):
    """
    Send a marshalled dictionary of the computer object serialized via_getDict.
    """

    slap_instance = slap.slap()
    connection_dict = {}
    if config.key_file and config.cert_file:
      connection_dict.update(
        key_file=config.key_file,
        cert_file=config.cert_file)
    slap_instance.initializeConnection(config.master_url,
      **connection_dict)
    slap_computer = slap_instance.registerComputer(self.reference)
224 225
    if config.dry_run:
      return
Łukasz Nowak's avatar
Łukasz Nowak committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    return slap_computer.updateConfiguration(
        xml_marshaller.dumps(_getDict(self)))

  def dump(self, path_to_xml):
    """
    Dump the computer object to an xml file via xml_marshaller.

    Args:
      path_to_xml: String, path to the file to load.
      users: List of User, list of user needed to be add to the dump
          (even if they are not related to any tap interface).
    """

    computer_dict = _getDict(self)
    output_file = open(path_to_xml,'w')
    output_file.write(xml_marshaller.dumps(computer_dict))
    output_file.close()

  @classmethod
Łukasz Nowak's avatar
Łukasz Nowak committed
245
  def load(cls, path_to_xml, reference, ipv6_interface):
Łukasz Nowak's avatar
Łukasz Nowak committed
246 247 248 249 250 251 252 253
    """
    Create a computer object from a valid xml file.

    Arg:
      path_to_xml: String, a path to a valid file containing
          a valid configuration.

    Return:
254
      A Computer object.
Łukasz Nowak's avatar
Łukasz Nowak committed
255 256 257 258 259 260 261 262 263
    """

    dumped_dict = xml_marshaller.loads(open(path_to_xml).read())

    # Reconstructing the computer object from the xml
    computer = Computer(
        reference = reference,
        addr = dumped_dict['address'],
        netmask = dumped_dict['netmask'],
Łukasz Nowak's avatar
Łukasz Nowak committed
264
        ipv6_interface=ipv6_interface,
265
        software_user=dumped_dict.get('software_user', 'slapsoft'),
Łukasz Nowak's avatar
Łukasz Nowak committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    )

    for partition_dict in dumped_dict['partition_list']:

      if partition_dict['user']:
        user = User(partition_dict['user']['name'])
      else:
        user = User('root')

      if partition_dict['tap']:
        tap = Tap(partition_dict['tap']['name'])
      else:
        tap = Tap(partition_dict['reference'])

      address_list = partition_dict['address_list']

      partition = Partition(
          reference = partition_dict['reference'],
          path = partition_dict['path'],
          user = user,
          address_list = address_list,
          tap = tap,
      )

      computer.partition_list.append(partition)

    return computer

  def construct(self, alter_user=True, alter_network=True):
    """
    Construct the computer object as it is.
    """
    if alter_network and self.address is not None:
      self.bridge.addAddr(self.address, self.netmask)

    for path in self.instance_root, self.software_root: 
      if not os.path.exists(path):
        os.makedirs(path, 0755)
      else:
        os.chmod(path, 0755)

307 308
    # own self.software_root by software user
    slapsoft = User(self.software_user)
Łukasz Nowak's avatar
Łukasz Nowak committed
309 310 311
    slapsoft.path = self.software_root
    if alter_user:
      slapsoft.create()
Łukasz Nowak's avatar
Łukasz Nowak committed
312
      slapsoft_pw = pwd.getpwnam(slapsoft.name)
Łukasz Nowak's avatar
Łukasz Nowak committed
313 314 315
      os.chown(self.software_root, slapsoft_pw.pw_uid, slapsoft_pw.pw_gid)
    os.chmod(self.software_root, 0755)

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    try:
      for partition_index, partition in enumerate(self.partition_list):
        # Reconstructing User's
        partition.path = os.path.join(self.instance_root, partition.reference)
        partition.user.setPath(partition.path)
        partition.user.additional_group_list = [slapsoft.name]
        if alter_user:
          partition.user.create()

        # Reconstructing Tap
        if partition.user and partition.user.isAvailable():
          owner = partition.user
        else:
          owner = User('root')

        if alter_network:
          # In case it has to be  attached to the TAP network device, only one
          # is necessary for the bridge to assert carrier
          if self.bridge.attach_to_tap and partition_index == 0:
            partition.tap.createWithOwner(owner, attach_to_tap=True)
Łukasz Nowak's avatar
Łukasz Nowak committed
336
          else:
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
            partition.tap.createWithOwner(owner)

          self.bridge.addTap(partition.tap)

        # Reconstructing partition's directory
        partition.createPath(alter_user)

        # Reconstructing partition's address
        # There should be two addresses on each Computer Partition:
        #  * global IPv6
        #  * local IPv4, took from slapformat:ipv4_local_network
        if len(partition.address_list) == 0:
          # regenerate
          partition.address_list.append(self.bridge.addIPv4LocalAddress())
          partition.address_list.append(self.bridge.addAddr())
        elif alter_network:
          # regenerate list of addresses
          old_partition_address_list = partition.address_list
          partition.address_list = []
          if len(old_partition_address_list) != 2:
            raise ValueError('There should be exactly 2 stored addresses')
Vincent Pelletier's avatar
Vincent Pelletier committed
358 359
          if not any([netaddr.valid_ipv6(q['addr'])
              for q in old_partition_address_list]):
360
            raise ValueError('Not valid ipv6 addresses loaded')
Vincent Pelletier's avatar
Vincent Pelletier committed
361 362
          if not any([netaddr.valid_ipv4(q['addr'])
              for q in old_partition_address_list]):
363 364 365
            raise ValueError('Not valid ipv6 addresses loaded')
          for address in old_partition_address_list:
            if netaddr.valid_ipv6(address['addr']):
Vincent Pelletier's avatar
Vincent Pelletier committed
366 367 368
              partition.address_list.append(self.bridge.addAddr(
                address['addr'],
                address['netmask']))
369
            elif netaddr.valid_ipv4(address['addr']):
Vincent Pelletier's avatar
Vincent Pelletier committed
370 371
              partition.address_list.append(self.bridge.addIPv4LocalAddress(
                address['addr']))
372 373 374
            else:
              raise ValueError('Address %r is incorrect' % address['addr'])
    finally:
375
      if alter_network and self.bridge.attach_to_tap:
376 377 378 379
        try:
          self.partition_list[0].tap.detach()
        except IndexError:
          pass
Łukasz Nowak's avatar
Łukasz Nowak committed
380

Vincent Pelletier's avatar
Vincent Pelletier committed
381
class Partition(object):
Łukasz Nowak's avatar
Łukasz Nowak committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  "Represent a computer partition"

  def __init__(self, reference, path, user, address_list, tap):
    """
    Attributes:
      reference: String, the name of the partition.
      path: String, the path to the partition folder.
      user: User, the user linked to this partition.
      address_list: List of associated IP addresses.
      tap: Tap, the tap interface linked to this partition.
    """

    self.reference = str(reference)
    self.path = str(path)
    self.user = user
    self.address_list = address_list or []
    self.tap = tap

  def __getinitargs__(self):
    return (self.reference, self.path, self.user, self.address_list, self.tap)

  def createPath(self, alter_user=True):
    """
Vincent Pelletier's avatar
Vincent Pelletier committed
405 406
    Create the directory of the partition, assign to the partition user and
    give it the 750 permission. In case if path exists just modifies it.
Łukasz Nowak's avatar
Łukasz Nowak committed
407 408
    """

409
    self.path = os.path.abspath(self.path)
Łukasz Nowak's avatar
Łukasz Nowak committed
410
    owner = self.user if self.user else User('root')
411 412
    if not os.path.exists(self.path):
      os.mkdir(self.path, 0750)
Łukasz Nowak's avatar
Łukasz Nowak committed
413
    if alter_user:
Łukasz Nowak's avatar
Łukasz Nowak committed
414
      owner_pw = pwd.getpwnam(owner.name)
415 416
      os.chown(self.path, owner_pw.pw_uid, owner_pw.pw_gid)
    os.chmod(self.path, 0750)
Łukasz Nowak's avatar
Łukasz Nowak committed
417

Vincent Pelletier's avatar
Vincent Pelletier committed
418
class User(object):
Łukasz Nowak's avatar
Łukasz Nowak committed
419
  "User: represent and manipulate a user on the system."
420
  path = None
Łukasz Nowak's avatar
Łukasz Nowak committed
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

  def __init__(self, user_name, additional_group_list=None):
    """
    Attributes:
        user_name: string, the name of the user, who will have is home in
    """
    self.name = str(user_name)
    self.additional_group_list = additional_group_list

  def __getinitargs__(self):
    return (self.name,)

  def setPath(self, path):
    self.path = path

  def create(self):
    """
    Create a user on the system who will be named after the self.name with its
    own group and directory.

    Returns:
        True: if the user creation went right
    """
    # XXX: This method shall be no-op in case if all is correctly setup
    #      This method shall check if all is correctly done
    #      This method shall not reset groups, just add them
    try:
448
      grp.getgrnam(self.name)
Łukasz Nowak's avatar
Łukasz Nowak committed
449 450 451
    except KeyError:
      callAndRead(['groupadd', self.name])

Vincent Pelletier's avatar
Vincent Pelletier committed
452 453
    user_parameter_list = ['-d', self.path, '-g', self.name, '-s',
      '/bin/false']
Łukasz Nowak's avatar
Łukasz Nowak committed
454 455 456 457
    if self.additional_group_list is not None:
      user_parameter_list.extend(['-G', ','.join(self.additional_group_list)])
    user_parameter_list.append(self.name)
    try:
Łukasz Nowak's avatar
Łukasz Nowak committed
458
      pwd.getpwnam(self.name)
Łukasz Nowak's avatar
Łukasz Nowak committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    except KeyError:
      callAndRead(['useradd'] + user_parameter_list)
    else:
      callAndRead(['usermod'] + user_parameter_list)

    return True

  def isAvailable(self):
    """
    Determine the availability of a user on the system

    Return:
        True: if available
        False: otherwise
    """

    try:
Łukasz Nowak's avatar
Łukasz Nowak committed
476
      pwd.getpwnam(self.name)
Łukasz Nowak's avatar
Łukasz Nowak committed
477 478 479 480 481
      return True

    except KeyError:
      return False

482 483 484 485 486
import struct
import fcntl
import errno
import threading

Vincent Pelletier's avatar
Vincent Pelletier committed
487
class Tap(object):
Łukasz Nowak's avatar
Łukasz Nowak committed
488
  "Tap represent a tap interface on the system"
489 490 491
  IFF_TAP = 0x0002
  TUNSETIFF = 0x400454ca
  KEEP_TAP_ATTACHED_EVENT = threading.Event()
Łukasz Nowak's avatar
Łukasz Nowak committed
492 493 494 495 496 497 498 499 500 501 502 503

  def __init__(self, tap_name):
    """
    Attributes:
        tap_name: String, the name of the tap interface.
    """

    self.name = str(tap_name)

  def __getinitargs__(self):
    return (self.name,)

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
  def attach(self):
    """
    Attach to the TAP interface, meaning  that it just opens the TAP interface
    and wait for the caller to notify that it can be safely detached.

    Linux  distinguishes administrative  and operational  state of  an network
    interface.  The  former can be set  manually by running ``ip  link set dev
    <dev> up|down'', whereas the latter states that the interface can actually
    transmit  data (for  a wired  network interface,  it basically  means that
    there is  carrier, e.g.  the network  cable is plugged  into a  switch for
    example).

    In order  to be able to check  the uniqueness of IPv6  address assigned to
    the bridge, the network interface  must be up from an administrative *and*
    operational point of view.

    However,  from  Linux  2.6.39,  the  bridge  reflects  the  state  of  the
    underlying device (e.g.  the bridge asserts carrier if at least one of its
    ports has carrier) whereas it  always asserted carrier before. This should
    work fine for "real" network interface,  but will not work properly if the
    bridge only binds TAP interfaces, which, from 2.6.36, reports carrier only
    and only if an userspace program is attached.
    """
    tap_fd = os.open("/dev/net/tun", os.O_RDWR)

    try:
      # Attach to the TAP interface which has previously been created
      fcntl.ioctl(tap_fd, self.TUNSETIFF,
                  struct.pack("16sI", self.name, self.IFF_TAP))

    except IOError, error:
      # If  EBUSY, it  means another  program is  already attached,  thus just
      # ignore it...
      if error.errno != errno.EBUSY:
        os.close(tap_fd)
        raise
    else:
      # Block until the  caller send an event stating that  the program can be
      # now detached safely,  thus bringing down the TAP  device (from 2.6.36)
      # and the bridge at the same time (from 2.6.39)
      self.KEEP_TAP_ATTACHED_EVENT.wait()
    finally:
      os.close(tap_fd)

  def detach(self):
    """
    Detach to the  TAP network interface by notifying  the thread which attach
    to the TAP and closing the TAP file descriptor
    """
    self.KEEP_TAP_ATTACHED_EVENT.set()

  def createWithOwner(self, owner, attach_to_tap=False):
Łukasz Nowak's avatar
Łukasz Nowak committed
556 557 558 559 560 561 562 563 564
    """
    Create a tap interface on the system.
    """

    # some systems does not have -p switch for tunctl
    #callAndRead(['tunctl', '-p', '-t', self.name, '-u', owner.name])
    check_file = '/sys/devices/virtual/net/%s/owner' % self.name
    owner_id = None
    if os.path.exists(check_file):
565
      owner_id = open(check_file).read().strip()
Łukasz Nowak's avatar
Łukasz Nowak committed
566
      try:
567 568
        owner_id = int(owner_id)
      except ValueError:
Łukasz Nowak's avatar
Łukasz Nowak committed
569
        pass
570
    if owner_id != pwd.getpwnam(owner.name).pw_uid:
Łukasz Nowak's avatar
Łukasz Nowak committed
571 572 573
      callAndRead(['tunctl', '-t', self.name, '-u', owner.name])
    callAndRead(['ip', 'link', 'set', self.name, 'up'])

574 575 576
    if attach_to_tap:
      threading.Thread(target=self.attach).start()

Vincent Pelletier's avatar
Vincent Pelletier committed
577
class Bridge(object):
Łukasz Nowak's avatar
Łukasz Nowak committed
578 579
  "Bridge represent a bridge on the system"

580
  def __init__(self, name, ipv4_local_network, ipv6_interface=None):
Łukasz Nowak's avatar
Łukasz Nowak committed
581 582 583 584 585 586 587
    """
    Attributes:
        name: String, the name of the bridge
    """

    self.name = str(name)
    self.ipv4_local_network = ipv4_local_network
Łukasz Nowak's avatar
Łukasz Nowak committed
588
    self.ipv6_interface = ipv6_interface
Łukasz Nowak's avatar
Łukasz Nowak committed
589

590 591
    # Attach to TAP  network interface, only if the  bridge interface does not
    # report carrier
592
    _, result = callAndRead(['ip', 'addr', 'list', self.name])
593 594
    self.attach_to_tap = 'DOWN' in result.split('\n', 1)[0]

Łukasz Nowak's avatar
Łukasz Nowak committed
595 596 597 598
  def __getinitargs__(self):
    return (self.name,)

  def getIPv4LocalAddressList(self):
Vincent Pelletier's avatar
Vincent Pelletier committed
599 600 601 602
    """
    Returns currently configured local IPv4 addresses which are in
    ipv4_local_network
    """
Łukasz Nowak's avatar
Łukasz Nowak committed
603 604 605 606 607 608 609 610 611
    if not socket.AF_INET in netifaces.ifaddresses(self.name):
      return []
    return [dict(addr=q['addr'], netmask=q['netmask']) for q in
      netifaces.ifaddresses(self.name)[socket.AF_INET] if netaddr.IPAddress(
        q['addr'], 4) in netaddr.glob_to_iprange(
          netaddr.cidr_to_glob(self.ipv4_local_network))]

  def getGlobalScopeAddressList(self):
    """Returns currently configured global scope IPv6 addresses"""
Łukasz Nowak's avatar
Łukasz Nowak committed
612 613 614 615
    if self.ipv6_interface:
      interface_name = self.ipv6_interface
    else:
      interface_name = self.name
616
    try:
Vincent Pelletier's avatar
Vincent Pelletier committed
617 618 619
      address_list = [q
        for q in netifaces.ifaddresses(interface_name)[socket.AF_INET6]
        if isGlobalScopeAddress(q['addr'].split('%')[0])]
620 621
    except KeyError:
      raise ValueError("%s must have at least one IPv6 address assigned" % \
Łukasz Nowak's avatar
Łukasz Nowak committed
622
                         interface_name)
Łukasz Nowak's avatar
Łukasz Nowak committed
623 624 625 626 627 628 629 630 631 632 633 634
    # XXX: Missing implementation of Unique Local IPv6 Unicast Addresses as
    # defined in http://www.rfc-editor.org/rfc/rfc4193.txt
    # XXX: XXX: XXX: IT IS DISALLOWED TO IMPLEMENT link-local addresses as
    # Linux and BSD are possibly wrongly implementing it -- it is "too local"
    # it is impossible to listen or access it on same node
    # XXX: IT IS DISALLOWED to implement ad hoc solution like inventing node
    # local addresses or anything which does not exists in RFC!
    return address_list

  def getInterfaceList(self):
    """Returns list of interfaces already present on bridge"""
    interface_list = []
635
    _, result = callAndRead(['brctl', 'show'])
Łukasz Nowak's avatar
Łukasz Nowak committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    in_bridge = False
    for line in result.split('\n'):
      if len(line.split()) > 1:
        if self.name in line:
          interface_list.append(line.split()[-1])
          in_bridge = True
          continue
        if in_bridge:
          break
      elif in_bridge:
        if line.strip():
          interface_list.append(line.strip())

    return interface_list

  def addTap(self, tap):
    """
    Add the tap interface tap to the bridge.

    Args:
      tap: Tap, the tap interface.
    """
    if tap.name not in self.getInterfaceList():
      callAndRead(['brctl', 'addif', self.name, tap.name])

  def _addSystemAddress(self, address, netmask, ipv6=True):
    """Adds system address to bridge
    
    Returns True if address was added successfully.

    Returns False if there was issue.
    """
    if ipv6:
      address_string = '%s/%s' % (address, netmaskToPrefixIPv6(netmask))
      af = socket.AF_INET6
Łukasz Nowak's avatar
Łukasz Nowak committed
671 672 673 674
      if self.ipv6_interface:
        interface_name = self.ipv6_interface
      else:
        interface_name = self.name
Łukasz Nowak's avatar
Łukasz Nowak committed
675 676 677
    else:
      af = socket.AF_INET
      address_string = '%s/%s' % (address, netmaskToPrefixIPv4(netmask))
Łukasz Nowak's avatar
Łukasz Nowak committed
678
      interface_name = self.name
Łukasz Nowak's avatar
Łukasz Nowak committed
679 680 681

    # check if address is already took by any other interface
    for interface in netifaces.interfaces():
Łukasz Nowak's avatar
Łukasz Nowak committed
682
      if interface != interface_name:
Łukasz Nowak's avatar
Łukasz Nowak committed
683 684
        address_dict = netifaces.ifaddresses(interface)
        if af in address_dict:
685
          if address in [q['addr'].split('%')[0] for q in address_dict[af]]:
Łukasz Nowak's avatar
Łukasz Nowak committed
686 687
            return False

Vincent Pelletier's avatar
Vincent Pelletier committed
688 689 690
    if not af in netifaces.ifaddresses(interface_name) \
        or not address in [q['addr'].split('%')[0]
          for q in netifaces.ifaddresses(interface_name)[af]]:
Łukasz Nowak's avatar
Łukasz Nowak committed
691
      # add an address
Łukasz Nowak's avatar
Łukasz Nowak committed
692
      callAndRead(['ip', 'addr', 'add', address_string, 'dev', interface_name])
Łukasz Nowak's avatar
Łukasz Nowak committed
693 694 695
      # wait few moments
      time.sleep(2)
    # check existence on interface
696
    _, result = callAndRead(['ip', 'addr', 'list', interface_name])
Łukasz Nowak's avatar
Łukasz Nowak committed
697 698 699 700
    for l in result.split('\n'):
      if address in l:
        if 'tentative' in l:
          # duplicate, remove
Łukasz Nowak's avatar
Łukasz Nowak committed
701 702
          callAndRead(['ip', 'addr', 'del', address_string, 'dev',
            interface_name])
Łukasz Nowak's avatar
Łukasz Nowak committed
703 704 705 706 707 708 709 710 711 712 713 714 715
          return False
        # found and clean
        return True
    # even when added not found, this is bad...
    return False

  def _generateRandomIPv4Address(self, netmask):
    # no addresses found, generate new one
    # Try 10 times to add address, raise in case if not possible
    try_num = 10
    while try_num > 0:
      addr = random.choice([q for q in netaddr.glob_to_iprange(
        netaddr.cidr_to_glob(self.ipv4_local_network))]).format()
Vincent Pelletier's avatar
Vincent Pelletier committed
716 717
      if dict(addr=addr, netmask=netmask) not in \
          self.getIPv4LocalAddressList():
Łukasz Nowak's avatar
Łukasz Nowak committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731
        # Checking the validity of the IPv6 address
        if self._addSystemAddress(addr, netmask, False):
          return dict(addr=addr, netmask=netmask)
        try_num -= 1

    raise AddressGenerationError(addr)

  def addIPv4LocalAddress(self, addr=None):
    """Adds local IPv4 address in ipv4_local_network"""
    netmask = '255.255.255.255'
    local_address_list = self.getIPv4LocalAddressList()
    if addr is None:
      return self._generateRandomIPv4Address(netmask)
    elif dict(addr=addr, netmask=netmask) not in local_address_list:
732 733 734 735
      if self._addSystemAddress(addr, netmask, False):
        return dict(addr=addr, netmask=netmask)
      else:
        return self._generateRandomIPv4Address(netmask)
Łukasz Nowak's avatar
Łukasz Nowak committed
736 737 738 739 740 741 742 743 744 745
    else:
      # confirmed to be configured
      return dict(addr=addr, netmask=netmask)

  def addAddr(self, addr = None, netmask = None):
    """
    Adds IP address to bridge.

    If addr is specified and exists already on bridge does nothing.

Vincent Pelletier's avatar
Vincent Pelletier committed
746 747 748
    If addr is specified and does not exists on bridge, tries to add given
    address. If it is not possible (ex. because network changed) calculates new
    address.
Łukasz Nowak's avatar
Łukasz Nowak committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

    Args:
      addr: Wished address to be added to bridge.
      netmask: Wished netmask to be used.

    Returns:
      Tuple of (address, netmask).

    Raises:
      AddressGenerationError: Couldn't construct valid address with existing
          one's on the bridge.
      NoAddressOnBridge: There's no address on the bridge to construct
          an address with.
    """
    # Getting one address of the bridge as base of the next addresses
Łukasz Nowak's avatar
Łukasz Nowak committed
764 765 766 767
    if self.ipv6_interface:
      interface_name = self.ipv6_interface
    else:
      interface_name = self.name
Łukasz Nowak's avatar
Łukasz Nowak committed
768 769 770 771
    bridge_addr_list = self.getGlobalScopeAddressList()

    # No address found
    if len(bridge_addr_list) == 0:
Łukasz Nowak's avatar
Łukasz Nowak committed
772
      raise NoAddressOnBridge(interface_name)
Łukasz Nowak's avatar
Łukasz Nowak committed
773 774 775 776 777 778 779 780 781 782
    address_dict = bridge_addr_list[0]

    if addr is not None:
      if dict(addr=addr, netmask=netmask) in bridge_addr_list:
        # confirmed to be configured
        return dict(addr=addr, netmask=netmask)
      if netmask == address_dict['netmask']:
        # same netmask, so there is a chance to add good one
        bridge_network = netaddr.ip.IPNetwork('%s/%s' % (address_dict['addr'],
          netmaskToPrefixIPv6(address_dict['netmask'])))
Vincent Pelletier's avatar
Vincent Pelletier committed
783 784
        requested_network = netaddr.ip.IPNetwork('%s/%s' % (addr,
          netmaskToPrefixIPv6(netmask)))
Łukasz Nowak's avatar
Łukasz Nowak committed
785 786 787 788 789 790 791 792 793 794
        if bridge_network.network == requested_network.network:
          # same network, try to add
          if self._addSystemAddress(addr, netmask):
            # succeed, return it
            return dict(addr=addr, netmask=netmask)

    # Try 10 times to add address, raise in case if not possible
    try_num = 10
    netmask = address_dict['netmask']
    while try_num > 0:
Vincent Pelletier's avatar
Vincent Pelletier committed
795 796
      addr = ':'.join(address_dict['addr'].split(':')[:-1] + ['%x' % (
        random.randint(1, 65000), )])
Łukasz Nowak's avatar
Łukasz Nowak committed
797
      socket.inet_pton(socket.AF_INET6, addr)
Vincent Pelletier's avatar
Vincent Pelletier committed
798 799
      if dict(addr=addr, netmask=netmask) not in \
          self.getGlobalScopeAddressList():
Łukasz Nowak's avatar
Łukasz Nowak committed
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
        # Checking the validity of the IPv6 address
        if self._addSystemAddress(addr, netmask):
          return dict(addr=addr, netmask=netmask)
        try_num -= 1

    raise AddressGenerationError(addr)

class Parser(OptionParser):
  """
  Parse all arguments.
  """
  def __init__(self, usage=None, version=None):
    """
    Initialize all options possibles.
    """
    OptionParser.__init__(self, usage=usage, version=version,
                          option_list=[
      Option("-x", "--computer_xml",
             help="Path to file with computer's XML. If does not exists, "
                  "will be created",
             default=None,
             type=str),
      Option("-l", "--log_file",
             help="The path to the log file used by the script.",
             type=str),
      Option("-i", "--input_definition_file",
             help="Path to file to read definition of computer instead of "
             "declaration. Using definition file allows to disable "
             "'discovery' of machine services and allows to define computer "
             "configuration in fully controlled manner.",
             type=str),
      Option("-o", "--output_definition_file",
             help="Path to file to write definition of computer from "
             "declaration.",
             type=str),
      Option("-n", "--dry_run",
836 837 838
             help="Don't actually do anything.",
             default=False,
             action="store_true"),
Łukasz Nowak's avatar
Łukasz Nowak committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852
      Option("-v", "--verbose",
             default=False,
             action="store_true",
             help="Verbose output."),
      Option("-c", "--console",
             default=False,
             action="store_true",
             help="Console output."),
      Option('--alter_user', choices=['True', 'False'],
        help="Shall slapformat alter user database [default: True]"),
      Option('--alter_network', choices=['True', 'False'],
        help="Shall slapformat alter network configuration [default: True]"),
      ])

853
  def check_args(self, args):
Łukasz Nowak's avatar
Łukasz Nowak committed
854 855 856
    """
    Check arguments
    """
857 858 859 860
    if args:
      (options, args) = self.parse_args(list(args))
    else:
      (options, args) = self.parse_args()
Łukasz Nowak's avatar
Łukasz Nowak committed
861 862 863 864 865
    if len(args) != 1:
      self.error("Incorrect number of arguments")
    return options, args[0]

def run(config):
866 867 868 869
  # Define the computer
  if config.input_definition_file:
    filepath = os.path.abspath(config.input_definition_file)
    config.logger.info('Using definition file %r' % filepath)
870 871 872
    computer_definition = ConfigParser.RawConfigParser({
      'software_user': 'slapsoft',
    })
873 874 875 876 877
    computer_definition.read(filepath)
    bridge = None
    address = None
    netmask = None
    if computer_definition.has_option('computer', 'address'):
Vincent Pelletier's avatar
Vincent Pelletier committed
878 879
      address, netmask = computer_definition.get('computer',
        'address').split('/')
880 881 882 883 884 885 886 887 888
    if config.alter_network and config.bridge_name is not None \
        and config.ipv4_local_network is not None:
      bridge = Bridge(config.bridge_name, config.ipv4_local_network,
        config.ipv6_interface)
    computer = Computer(
        reference=config.computer_id,
        bridge=bridge,
        addr=address,
        netmask=netmask,
889 890
        ipv6_interface=config.ipv6_interface,
        software_user=computer_definition.get('computer', 'software_user'),
891 892 893 894 895 896 897 898 899 900
      )
    partition_list = []
    for partition_number in range(int(config.partition_amount)):
      section = 'partition_%s' % partition_number
      user = User(computer_definition.get(section, 'user'))
      address_list = []
      for a in computer_definition.get(section, 'address').split():
        address, netmask = a.split('/')
        address_list.append(dict(addr=address, netmask=netmask))
      tap = Tap(computer_definition.get(section, 'network_interface'))
Vincent Pelletier's avatar
Vincent Pelletier committed
901 902 903 904
      partition_list.append(Partition(reference=computer_definition.get(
        section, 'pathname'),
          path=os.path.join(config.instance_root, computer_definition.get(
            section, 'pathname')),
905 906 907 908 909 910 911 912
          user=user,
          address_list=address_list,
          tap=tap,
          ))
    computer.partition_list = partition_list
  else:
    # no definition file, figure out computer
    if os.path.exists(config.computer_xml):
Vincent Pelletier's avatar
Vincent Pelletier committed
913 914 915 916
      config.logger.info('Loading previous computer data from %r' % (
        config.computer_xml, ))
      computer = Computer.load(config.computer_xml,
        reference=config.computer_id, ipv6_interface=config.ipv6_interface)
917 918
      # Connect to the bridge interface defined by the configuration
      computer.bridge = Bridge(config.bridge_name, config.ipv4_local_network,
Łukasz Nowak's avatar
Łukasz Nowak committed
919
          config.ipv6_interface)
Łukasz Nowak's avatar
Łukasz Nowak committed
920
    else:
921
      # If no pre-existent configuration found, creating a new computer object
Vincent Pelletier's avatar
Vincent Pelletier committed
922 923
      config.logger.warning('Creating new data computer with id %r' % (
        config.computer_id, ))
924 925 926 927 928 929
      computer = Computer(
        reference=config.computer_id,
        bridge=Bridge(config.bridge_name, config.ipv4_local_network,
          config.ipv6_interface),
        addr=None,
        netmask=None,
930 931
        ipv6_interface=config.ipv6_interface,
        software_user=config.software_user,
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
      )

    partition_amount = int(config.partition_amount)
    existing_partition_amount = len(computer.partition_list)
    if existing_partition_amount > partition_amount:
      raise ValueError('Requested amount of computer partitions (%s) is lower '
          'then already configured (%s), cannot continue' % (partition_amount,
            len(computer.partition_list)))

    config.logger.info('Adding %s new partitions' %
        (partition_amount-existing_partition_amount))
    for nb_iter in range(existing_partition_amount, partition_amount):
      # add new ones
      user = User("%s%s" % (config.user_base_name, nb_iter))

      tap = Tap("%s%s" % (config.tap_base_name, nb_iter))

      path = os.path.join(config.instance_root, "%s%s" % (
                           config.partition_base_name, nb_iter))
      computer.partition_list.append(
        Partition(
          reference="%s%s" % (config.partition_base_name, nb_iter),
          path=path,
          user=user,
          address_list=None,
          tap=tap,
          ))

  computer.instance_root = config.instance_root
  computer.software_root = config.software_root
  config.logger.info('Updating computer')
  address = computer.getAddress()
  computer.address = address['addr']
  computer.netmask = address['netmask']

  if config.output_definition_file:
    computer_definition = ConfigParser.RawConfigParser()
    computer_definition.add_section('computer')
    if computer.address is not None and computer.netmask is not None:
Vincent Pelletier's avatar
Vincent Pelletier committed
971 972
      computer_definition.set('computer', 'address', '/'.join(
        [computer.address, computer.netmask]))
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    partition_number = 0
    for partition in computer.partition_list:
      section = 'partition_%s' % partition_number
      computer_definition.add_section(section)
      address_list = []
      for address in partition.address_list:
        address_list.append('/'.join([address['addr'], address['netmask']]))
      computer_definition.set(section, 'address', ' '.join(address_list))
      computer_definition.set(section, 'user', partition.user.name)
      computer_definition.set(section, 'user', partition.user.name)
      computer_definition.set(section, 'network_interface', partition.tap.name)
      computer_definition.set(section, 'pathname', partition.reference)
      partition_number += 1
    filepath = os.path.abspath(config.output_definition_file)
    computer_definition.write(open(filepath, 'w'))
    config.logger.info('Stored computer definition in %r' % filepath)
  computer.construct(alter_user=config.alter_user,
      alter_network=config.alter_network)

  # Dumping and sending to the erp5 the current configuration
  if not config.dry_run:
    computer.dump(config.computer_xml)
  config.logger.info('Posting information to %r' % config.master_url)
  computer.send(config)
Łukasz Nowak's avatar
Łukasz Nowak committed
997

Vincent Pelletier's avatar
Vincent Pelletier committed
998
class Config(object):
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
  key_file = None
  cert_file = None
  alter_network = None
  alter_user = None
  computer_xml = None
  logger = None
  log_file = None
  verbose = None
  dry_run = None
  console = None
1009
  software_user = None
1010 1011 1012

  @staticmethod
  def checkRequiredBinary(binary_list):
Łukasz Nowak's avatar
Łukasz Nowak committed
1013 1014 1015 1016 1017 1018 1019 1020 1021
    missing_binary_list = []
    for b in binary_list:
      try:
        callAndRead([b])
      except ValueError:
        pass
      except OSError:
        missing_binary_list.append(b)
    if missing_binary_list:
Vincent Pelletier's avatar
Vincent Pelletier committed
1022 1023
      raise UsageError('Some required binaries are missing or not '
          'functional: %s' % (','.join(missing_binary_list), ))
Łukasz Nowak's avatar
Łukasz Nowak committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

  def setConfig(self, option_dict, configuration_file_path):
    """
    Set options given by parameters.
    """
    self.key_file = None
    self.cert_file = None
    # Set options parameters
    for option, value in option_dict.__dict__.items():
      setattr(self, option, value)

    # Load configuration file
    configuration_parser = ConfigParser.SafeConfigParser()
    configuration_parser.read(configuration_file_path)
    # Merges the arguments and configuration
    for section in ("slapformat", "slapos"):
      configuration_dict = dict(configuration_parser.items(section))
      for key in configuration_dict:
        if not getattr(self, key, None):
          setattr(self, key, configuration_dict[key])

    # setup some nones
    for parameter in ['bridge_name', 'partition_base_name', 'user_base_name',
Łukasz Nowak's avatar
Łukasz Nowak committed
1047
        'tap_base_name', 'ipv4_local_network', 'ipv6_interface']:
Łukasz Nowak's avatar
Łukasz Nowak committed
1048 1049 1050 1051 1052 1053 1054 1055
      if getattr(self, parameter, None) is None:
        setattr(self, parameter, None)

    # Set defaults lately
    if self.alter_network is None:
      self.alter_network = 'True'
    if self.alter_user is None:
      self.alter_user = 'True'
1056 1057
    if self.software_user is None:
      self.software_user = 'slapsoft'
Łukasz Nowak's avatar
Łukasz Nowak committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

    # set up logging
    self.logger = logging.getLogger("slapformat")
    self.logger.setLevel(logging.INFO)
    if self.console:
      self.logger.addHandler(logging.StreamHandler())

    # Convert strings to booleans
    root_needed = False
    for o in ['alter_network', 'alter_user']:
      if getattr(self, o).lower() == 'true':
        root_needed = True
        setattr(self, o, True)
      elif getattr(self, o).lower() == 'false':
        setattr(self, o, False)
      else:
Vincent Pelletier's avatar
Vincent Pelletier committed
1074 1075
        message = 'Option %r needs to be "True" or "False", wrong value: ' \
            '%r' % (o, getattr(self, o))
Łukasz Nowak's avatar
Łukasz Nowak committed
1076 1077 1078
        self.logger.error(message)
        raise UsageError(message)

1079 1080 1081 1082 1083 1084
    if not self.dry_run:
      if self.alter_user:
        self.checkRequiredBinary(['groupadd', 'useradd', 'usermod'])
      if self.alter_network:
        self.checkRequiredBinary(['ip', 'tunctl'])
    # Required, even for dry run
Łukasz Nowak's avatar
Łukasz Nowak committed
1085
    if self.alter_network:
1086
      self.checkRequiredBinary(['brctl'])
Łukasz Nowak's avatar
Łukasz Nowak committed
1087

1088 1089
    if self.dry_run:
      root_needed = False
Łukasz Nowak's avatar
Łukasz Nowak committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    
    # check root
    if root_needed and os.getuid() != 0:
      message = "Root rights are needed"
      self.logger.error(message)
      raise UsageError(message)

    if self.log_file:
      if not os.path.isdir(os.path.dirname(self.log_file)):
        # fallback to console only if directory for logs does not exists and
        # continue to run
        raise ValueError('Please create directory %r to store %r log file' % (
          os.path.dirname(self.log_file), self.log_file))
      else:
        file_handler = logging.FileHandler(self.log_file)
Vincent Pelletier's avatar
Vincent Pelletier committed
1105 1106
        file_handler.setFormatter(logging.Formatter("%(asctime)s - "
          "%(name)s - %(levelname)s - %(message)s"))
Łukasz Nowak's avatar
Łukasz Nowak committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
        self.logger.addHandler(file_handler)
        self.logger.info('Configured logging to file %r' % self.log_file)
    # Check mandatory options
    for parameter in ('computer_id', 'instance_root', 'master_url',
                      'software_root', 'computer_xml'):
      if not getattr(self, parameter, None):
        raise UsageError("Parameter '%s' is not defined." % parameter)

    self.logger.info("Started.")
    if self.verbose:
      self.logger.setLevel(logging.DEBUG)
      self.logger.debug("Verbose mode enabled.")
1119 1120
    if self.dry_run:
      self.logger.info("Dry-run mode enabled.")
Łukasz Nowak's avatar
Łukasz Nowak committed
1121 1122 1123 1124 1125

    # Calculate path once
    self.computer_xml = os.path.abspath(self.computer_xml)


1126
def main(*args):
Łukasz Nowak's avatar
Łukasz Nowak committed
1127
  "Run default configuration."
Vincent Pelletier's avatar
Vincent Pelletier committed
1128 1129 1130
  global os
  global callAndRead
  real_callAndRead = callAndRead
Łukasz Nowak's avatar
Łukasz Nowak committed
1131 1132
  usage = "usage: %s [options] CONFIGURATION_FILE" % sys.argv[0]

1133 1134 1135
  # Parse arguments
  options, configuration_file_path = Parser(usage=usage).check_args(args)
  config = Config()
Łukasz Nowak's avatar
Łukasz Nowak committed
1136 1137 1138
  try:
    config.setConfig(options, configuration_file_path)
  except UsageError, err:
1139
    print >>sys.stderr, err.message
Łukasz Nowak's avatar
Łukasz Nowak committed
1140
    print >>sys.stderr, "For help use --help"
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    sys.exit(1)
  os = OS(config)
  if config.dry_run:
    def dry_callAndRead(argument_list, raise_on_error=True):
      if argument_list == ['brctl', 'show']:
        return real_callAndRead(argument_list, raise_on_error)
      else:
        return 0, ''
    callAndRead = dry_callAndRead
    real_addSystemAddress = Bridge._addSystemAddress
    def fake_addSystemAddress(*args, **kw):
      real_addSystemAddress(*args, **kw)
      # Fake success
      return True
    Bridge._addSystemAddress = fake_addSystemAddress
    def fake_getpwnam(user):
Vincent Pelletier's avatar
Vincent Pelletier committed
1157
      class result(object):
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
        pw_uid = 12345
        pw_gid = 54321
      return result
    pwd.getpwnam = fake_getpwnam
  else:
    dry_callAndRead = real_callAndRead
  if config.verbose:
    def logging_callAndRead(argument_list, raise_on_error=True):
      config.logger.debug(' '.join(argument_list))
      return dry_callAndRead(argument_list, raise_on_error)
    callAndRead = logging_callAndRead
  try:
    run(config)
  except:
    config.logger.exception('Uncaught exception:')
    raise