Ejemplo n.º 1
0
  def __init__(self, buildout, name, options):
    self.logger = logging.getLogger(name)
    software_url = options['software-url']
    name = options['name']
    return_parameters = options.get('return', '').split()
    if not return_parameters:
      self.logger.debug("No parameter to return to main instance."
        "Be careful about that...")
    software_type = options.get('software-type', DEFAULT_SOFTWARE_TYPE)
    filter_kw = dict(
      (x, options['sla-' + x]) for x in options.get('sla', '').split()
      if options['sla-' + x]
    )
    partition_parameter_kw = self._filterForStorage(dict(
      (x, options['config-' + x])
      for x in options.get('config', '').split()
    ))
    slave = options.get('slave', 'false').lower() in \
      librecipe.GenericBaseRecipe.TRUE_VALUES
    slap = slapmodule.slap()
    slap.initializeConnection(
      options['server-url'],
      options.get('key-file'),
      options.get('cert-file'),
    )
    request = slap.registerComputerPartition(
      options['computer-id'],
      options['partition-id'],
    ).request
    self._raise_request_exception = None
    self._raise_request_exception_formatted = None
    self.instance = None
    # Try to do the request and fetch parameter dict...
    try:
      self.instance = request(software_url, software_type,
          name, partition_parameter_kw=partition_parameter_kw,
          filter_kw=filter_kw, shared=slave)
      return_parameter_dict = self._getReturnParameterDict(self.instance,
          return_parameters)
      if not slave:
        try:
          options['instance-guid'] = self.instance.getInstanceGuid()
          # XXX: deprecated, to be removed
          options['instance_guid'] = self.instance.getInstanceGuid()
        except slapmodule.ResourceNotReady:
          # Backward compatibility. Old SlapOS masters don't know this.
          self.logger.warning("Impossible to fetch instance GUID.")
    except (slapmodule.NotFoundError, slapmodule.ServerError, slapmodule.ResourceNotReady) as exc:
      self._raise_request_exception = exc
      self._raise_request_exception_formatted = traceback.format_exc()
      return_parameter_dict = {}

    # Then try to get all the parameters. In case of problem, put empty string.
    for param in return_parameters:
      options['connection-%s' % param] = ''
      try:
        options['connection-%s' % param] = return_parameter_dict[param]
      except KeyError:
        if self.failed is None:
          self.failed = param
Ejemplo n.º 2
0
  def __init__(self, buildout, name, options):
    """Default initialisation"""
    self.name = name
    options['eggs'] = 'slapos.cookbook'
    self.options = options
    self.logger = logging.getLogger(self.name)
    self.slap = slap.slap()

    # SLAP related information
    slap_connection = buildout['slap-connection']
    self.computer_id = slap_connection['computer-id']
    self.computer_partition_id = slap_connection['partition-id']
    self.server_url = slap_connection['server-url']
    self.software_release_url = slap_connection['software-release-url']
    self.key_file = slap_connection.get('key-file')
    self.cert_file = slap_connection.get('cert-file')

    # setup egg to give possibility to generate scripts
    self.egg = zc.recipe.egg.Egg(buildout, options['recipe'], options)

    # Hook options
    self._options(options)

    # setup auto uninstall/install
    self._setupAutoInstallUninstall()
 def stepBang(self, sequence, **kw):
   self.slap = slap.slap()
   self.slap.initializeConnection(self.server_url, timeout=None)
   slap_computer_partition = self.slap.registerComputerPartition(
       sequence['computer_reference'],
       sequence['computer_partition_reference'])
   slap_computer_partition.bang(self.bang_message)
Ejemplo n.º 4
0
 def stepComputerBang(self, sequence, **kw):
   self.slap = slap.slap()
   self.slap.initializeConnection(self.server_url, timeout=None)
   slap_computer = self.slap.registerComputer(
     sequence['computer_reference'])
   slap_computer.bang(self.bang_message)
   sequence.edit(expected_bang_count=sequence.get('expected_bang_count', 2) + 5)
Ejemplo n.º 5
0
def promise(args):
    def failed_ssh():
        sys.stderr.write("SSH Connection failed\n")
        partition = slap.registerComputerPartition(args["computer_id"], args["partition_id"])
        partition.bang("SSH Connection failed. rdiff-backup is unusable.")

    def sigterm_handler(signum, frame):
        failed_ssh()

    signal.signal(signal.SIGTERM, sigterm_handler)

    slap = slapmodule.slap()
    slap.initializeConnection(args["server_url"], key_file=args.get("key_file"), cert_file=args.get("cert_file"))

    ssh = subprocess.Popen(
        [args["ssh_client"], "%(user)s@%(host)s/%(port)s" % args],
        stdin=subprocess.PIPE,
        stdout=open(os.devnull, "w"),
        stderr=open(os.devnull, "w"),
    )

    # Rdiff Backup protocol quit command
    quitcommand = "q" + chr(255) + chr(0) * 7

    ssh.stdin.write(quitcommand)
    ssh.stdin.flush()
    ssh.stdin.close()
    ssh.wait()

    if ssh.poll() is None:
        return 1
    if ssh.returncode != 0:
        failed_ssh()
    return ssh.returncode
Ejemplo n.º 6
0
    def __init__(self, buildout, name, options):
        """Default initialisation"""
        self.name = name
        options["eggs"] = "slapos.cookbook"
        self.options = options
        self.logger = logging.getLogger(self.name)
        self.slap = slap.slap()
        self.work_directory = os.path.abspath(buildout["buildout"]["directory"])
        self.bin_directory = os.path.join(buildout["buildout"]["directory"], "bin")
        self.data_root_directory = os.path.join(self.work_directory, "srv")
        self.backup_directory = os.path.join(self.data_root_directory, "backup")
        self.var_directory = os.path.join(self.work_directory, "var")
        self.log_directory = os.path.join(self.var_directory, "log")
        self.run_directory = os.path.join(self.var_directory, "run")
        self.etc_directory = os.path.join(self.work_directory, "etc")
        self.tmp_directory = os.path.join(self.work_directory, "tmp")
        self.wrapper_directory = os.path.join(self.etc_directory, "run")
        self.wrapper_report_directory = os.path.join(self.etc_directory, "report")
        self.wrapper_xml_report_directory = os.path.join(self.var_directory, "xml_report")
        self.destroy_script_location = os.path.join(self, self.work_directory, "sbin", "destroy")
        self.promise_directory = os.path.join(self.etc_directory, "promise")

        # default directory structure information
        self.default_directory_list = [
            self.bin_directory,  # CP/bin - instance own binaries
            os.path.join(self, self.work_directory, "sbin"),  # CP/sbin - system
            # binaries, not exposed, only CP/sbin/destroy
            self.data_root_directory,  # CP/srv - data container
            self.backup_directory,  # CP/srv/backup - backup container
            self.etc_directory,  # CP/etc - configuration container
            self.wrapper_directory,  # CP/etc/run - for wrappers
            self.wrapper_report_directory,  # CP/etc/report - for report wrappers
            self.promise_directory,  # CP/etc/promise - for promise checking scripts
            self.var_directory,  # CP/var - partition "internal" container for logs,
            # and another metadata
            self.wrapper_xml_report_directory,  # CP/var/xml_report - for xml_report wrappers
            self.log_directory,  # CP/var/log - log container
            self.run_directory,  # CP/var/run - working container - pids, sockets
            self.tmp_directory,  # CP/tmp - temporary files
        ]

        # SLAP related information
        slap_connection = buildout["slap_connection"]
        self.computer_id = slap_connection["computer_id"]
        self.computer_partition_id = slap_connection["partition_id"]
        self.server_url = slap_connection["server_url"]
        self.software_release_url = slap_connection["software_release_url"]
        self.key_file = slap_connection.get("key_file")
        self.cert_file = slap_connection.get("cert_file")

        # setup egg to give possibility to generate scripts
        self.egg = zc.recipe.egg.Egg(buildout, options["recipe"], options)

        # Hook options
        self._options(options)

        # setup auto uninstall/install
        self._setupAutoInstallUninstall()
Ejemplo n.º 7
0
def promise(args):

  def failed_ssh(partition, ssh):
    sys.stderr.write("SSH Connection failed\n")
    try:
      ssh.terminate()
    except:
      pass
    partition.bang("SSH Connection failed. rdiff-backup is unusable.")

  def sigterm_handler(signum, frame):
    # Walk up in the stack to get promise local
    # variables
    ssh = None
    for upper_frame in inspect.getouterframes(frame):
      # Use promise.func_name insteand of 'promise' in order to be
      # detected by editor if promise func name change.
      # Else, it's hard to debug this kind of error.
      if upper_frame[3] == promise.func_name:
        try:
          partition = upper_frame[0].f_locals['partition']
          ssh = upper_frame[0].f_locals['ssh']
        except KeyError:
          raise SystemExit("SIGTERM Send too soon.")
        break
    # If ever promise function wasn't found in the stack.
    if ssh is None:
      raise SystemExit
    failed_ssh(partition, ssh)

  signal.signal(signal.SIGTERM, sigterm_handler)

  slap = slapmodule.slap()
  slap.initializeConnection(args['server_url'],
                            key_file=args.get('key_file'),
                            cert_file=args.get('cert_file'))

  partition = slap.registerComputerPartition(args['computer_id'],
                                             args['partition_id'])

  ssh = subprocess.Popen([args['ssh_client'], '%(user)s@%(host)s/%(port)s' % args],
                         stdin=subprocess.PIPE,
                         stdout=open(os.devnull, 'w'),
                         stderr=open(os.devnull, 'w'))

  # Rdiff Backup protocol quit command
  quitcommand = 'q' + chr(255) + chr(0) * 7

  ssh.stdin.write(quitcommand)
  ssh.stdin.flush()
  ssh.stdin.close()
  ssh.wait()

  if ssh.poll() is None:
    return 1
  if ssh.returncode != 0:
    failed_ssh(partition, ssh)
  return ssh.returncode
Ejemplo n.º 8
0
def runAccords(accords_conf):
  """Launch ACCORDS, parse manifest, broker manifest, send connection
     informations to SlapOS Master. Destroy instance and stops ACCORDS at
     SIGTERM."""
  computer_id = accords_conf['computer_id']
  computer_partition_id = accords_conf['computer_partition_id']
  server_url = accords_conf['server_url']
  software_release_url = accords_conf['software_release_url']
  key_file = accords_conf['key_file']
  cert_file = accords_conf['cert_file']
  accords_lib_directory = accords_conf['accords_lib_directory']
  accords_location = accords_conf['accords_location']
  manifest_name = accords_conf['manifest_name']

  environment = dict(
     LD_LIBRARY_PATH=accords_lib_directory,
     PATH=accords_conf['path'],
     HOME=accords_location,
  )

  # Set handler to stop ACCORDS when end of world comes
  # XXX use subprocess.check_call and add exception handlers
  def sigtermHandler(signum, frame):
    Popen(['./co-command', 'stop', '/service/*'],
        cwd=accords_location, env=environment).communicate()
    Popen(['./co-stop'],
        cwd=accords_location, env=environment).communicate()
    sys.exit(0)

  signal.signal(signal.SIGTERM, sigtermHandler)

  # Launch ACCORDS, parse & broke manifest to deploy instance
  print 'Starting ACCORDS and friends...'
  subprocess.check_call(['./co-start'],cwd=accords_location, env=environment)
  print 'Parsing manifest...'
  subprocess.check_call(['./co-parser', manifest_name],
      cwd=accords_location, env=environment)
  print 'Brokering manifest...'
  subprocess.check_call(['./co-broker', manifest_name],
      cwd=accords_location, env=environment)
  print 'Done.'

  # Parse answer
  # XXX
  connection_dict = dict(connection='hardcoded')

  # Send information about published service to SlapOS Master
  slap_connection = slap.slap()
  slap_connection.initializeConnection(server_url, key_file, cert_file)
  computer_partition = slap_connection.registerComputerPartition(computer_id,
      computer_partition_id)
  computer_partition.setConnectionDict(connection_dict)

  # Go to sleep, wait kill
  while(True):
    time.sleep(60)
Ejemplo n.º 9
0
  def __init__(self, buildout, name, options):
    self.logger = logging.getLogger(name)

    slap = slapmodule.slap()

    self.software_release_url = options['software-url']
    self.name = options['name']

    slap.initializeConnection(options['server-url'],
                              options.get('key-file'),
                              options.get('cert-file'),
                             )
    computer_partition = slap.registerComputerPartition(
      options['computer-id'], options['partition-id'])
    self.request = computer_partition.request

    self.isSlave = False
    if 'slave' in options:
      self.isSlave = options['slave'].lower() in ['y', 'yes', 'true', '1']

    self.return_parameters = []
    if 'return' in options:
      self.return_parameters = [str(parameter).strip()
                               for parameter in options['return'].split()]
    else:
      self.logger.debug("No parameter to return to main instance."
                          "Be careful about that...")

    software_type = 'RootSoftwareInstance'
    if 'software-type' in options:
      software_type = options['software-type']

    filter_kw = {}
    if 'sla' in options:
      for sla_parameter in options['sla'].split():
        filter_kw[sla_parameter] = options['sla-%s' % sla_parameter]

    partition_parameter_kw = {}
    if 'config' in options:
      for config_parameter in options['config'].split():
        partition_parameter_kw[config_parameter] = \
            options['config-%s' % config_parameter]

    self.instance = self.request(options['software-url'], software_type,
      self.name, partition_parameter_kw=partition_parameter_kw,
      filter_kw=filter_kw, shared=self.isSlave)

    self.failed = None
    for param in self.return_parameters:
      try:
        options['connection-%s' % param] = str(
            self.instance.getConnectionParameter(param))
      except slapmodule.NotFoundError:
        options['connection-%s' % param] = ''
        if self.failed is None:
          self.failed = param
Ejemplo n.º 10
0
 def __init__(self,
              software_root,
              instance_root,
              master_url,
              computer_id,
              supervisord_socket,
              supervisord_configuration_path,
              usage_report_periodicity,
              buildout,
              key_file=None,
              cert_file=None,
              signature_private_key_file=None,
              upload_cache_url=None,
              upload_dir_url=None,
              master_ca_file=None,
              certificate_repository_path=None,
              console=False,
              promise_timeout=3,
              shacache_cert_file=None,
              shacache_key_file=None,
              shadir_cert_file=None,
              shadir_key_file=None):
   """Makes easy initialisation of class parameters"""
   # Parses arguments
   self.software_root = os.path.abspath(software_root)
   self.instance_root = os.path.abspath(instance_root)
   self.master_url = master_url
   self.computer_id = computer_id
   self.supervisord_socket = supervisord_socket
   self.supervisord_configuration_path = supervisord_configuration_path
   self.usage_report_periodicity = usage_report_periodicity
   self.key_file = key_file
   self.cert_file = cert_file
   self.master_ca_file = master_ca_file
   self.certificate_repository_path = certificate_repository_path
   self.signature_private_key_file = signature_private_key_file
   self.upload_cache_url = upload_cache_url
   self.upload_dir_url = upload_dir_url
   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
   # Configures logger
   self.logger = logging.getLogger('Slapgrid')
   # Creates objects from slap module
   self.slap = slap.slap()
   self.slap.initializeConnection(self.master_url, key_file=self.key_file,
       cert_file=self.cert_file, master_ca_file=self.master_ca_file)
   self.computer = self.slap.registerComputer(self.computer_id)
   # Defines all needed paths
   self.instance_etc_directory = os.path.join(self.instance_root, 'etc')
   self.supervisord_configuration_directory = \
       os.path.join(self.instance_etc_directory, 'supervisord.conf.d')
   self.console = console
   self.buildout = buildout
   self.promise_timeout = promise_timeout
Ejemplo n.º 11
0
  def __init__(self, buildout, name, options):
    self.logger = logging.getLogger(name)

    slap = slapmodule.slap()

    slap_connection = buildout['slap_connection']
    self.software_release_url = slap_connection['software_release_url']

    # XXX: Dirty network interation stuff
    slap.initializeConnection(slap_connection['server_url'],
                              slap_connection.get('key_file'),
                              slap_connection.get('cert_file'),
                             )
    computer_partition = slap.registerComputerPartition(
      slap_connection['computer_id'], slap_connection['partition_id'])
    self.request = computer_partition.request

    if 'software-url' not in options:
      options['software-url'] = self.software_release_url

    if 'name' not in options:
      options['name'] = name

    self.return_parameters = []
    if 'return' in options:
      self.return_parameters = [str(parameter).strip()
                               for parameter in options['return'].splitlines()]
    else:
      self.logger.warning("No parameter to return to main instance."
                          "Be careful about that...")

    software_type = 'RootInstanceSoftware'
    if 'software-type' in options:
      software_type = options['software-type']

    filter_kw = {}
    if 'sla' in options:
      filter_kw = self.parseMultiValues(options['sla'])

    partition_parameter_kw = {}
    if 'config' in options:
      partition_parameter_kw = self.parseMultiValues(options['config'])

    instance = self.request(options['software-url'], software_type,
      options['name'], partition_parameter_kw=partition_parameter_kw,
        filter_kw=filter_kw)

    result = {}
    for param in self.return_parameters:
      result[param] = instance.getConnectionParameter(param)

    # Return the connections parameters in options dict
    for key, value in result.items():
      options['connection-%s' % key] = value
 def stepPersonRequestSlapSoftwareInstanceWithAnotherSoftwareType(
                                                   self, sequence, **kw):
   software_release = sequence['software_release_uri']
   self.slap = slap.slap()
   self.slap.initializeConnection(self.server_url, timeout=None)
   open_order = self.slap.registerOpenOrder()
   requested_slap_computer_partition = open_order.request(
      software_release=software_release,
      software_type="SecondSoftwareType",
      partition_reference=sequence['requested_reference'],
      partition_parameter_kw=sequence.get('requested_parameter_dict', {}),
      filter_kw=sequence.get('requested_filter_dict', {}))
Ejemplo n.º 13
0
  def __init__(self, buildout, name, options):
    """Default initialisation"""
    GenericBaseRecipe.__init__(self, buildout, name, options)
    self.slap = slap.slap()

    # SLAP related information
    slap_connection = buildout['slap-connection']
    self.computer_id = slap_connection['computer-id']
    self.computer_partition_id = slap_connection['partition-id']
    self.server_url = slap_connection['server-url']
    self.software_release_url = slap_connection['software-release-url']
    self.key_file = slap_connection.get('key-file')
    self.cert_file = slap_connection.get('cert-file')
Ejemplo n.º 14
0
  def __init__(self, buildout, name, options):
    """Default initialisation"""
    GenericBaseRecipe.__init__(self, buildout, name, options)
    self.slap = slap.slap()

    # SLAP related information
    slap_connection = buildout['slap-connection']
    self.computer_id = slap_connection['computer-id']
    self.computer_partition_id = slap_connection['partition-id']
    self.server_url = slap_connection['server-url']
    self.software_release_url = slap_connection['software-release-url']
    self.key_file = slap_connection.get('key-file')
    self.cert_file = slap_connection.get('cert-file')
  def stepRequestComputerPartitionWithAnotherSoftwareType(self, sequence, **kw):
    self.slap = slap.slap()
    self.slap.initializeConnection(self.server_url, timeout=None)
    slap_computer_partition = self.slap.registerComputerPartition(
        sequence['computer_reference'],
        sequence['computer_partition_reference'])
    kw = dict(software_release=sequence['software_release_uri'],
      software_type="SecondSoftwareType",
      partition_reference=sequence.get('requested_reference',
        'requested_reference'),
      partition_parameter_kw=sequence.get('requested_parameter_dict', {}),
      filter_kw=sequence.get('requested_filter_dict', {}),
      state=sequence.get('instance_state'))

    requested_slap_computer_partition = slap_computer_partition.request(**kw)
Ejemplo n.º 16
0
  def __init__(self, buildout, name, options):
    """Default initialisation"""
    self.slap = slap.slap()

    # SLAP related information
    slap_connection = buildout['slap-connection']
    self.computer_id = slap_connection['computer-id']
    self.computer_partition_id = slap_connection['partition-id']
    self.server_url = slap_connection['server-url']
    self.software_release_url = slap_connection['software-release-url']
    self.key_file = slap_connection.get('key-file')
    self.cert_file = slap_connection.get('cert-file')
    self.slave_list = json.loads(options['slave-instance-list'])

    options['slave-amount'] = '%s' % len(self.slave_list)
    return GenericBaseRecipe.__init__(self, buildout, name, options)
    def stepRequestComputerPartitionNoTic(self, sequence, **kw):
        self.slap = slap.slap()
        self.slap.initializeConnection(self.server_url, timeout=None)
        slap_computer_partition = self.slap.registerComputerPartition(
            sequence["computer_reference"], sequence["computer_partition_reference"]
        )
        kw = dict(
            software_release=sequence["software_release_uri"],
            software_type=sequence.get("requested_software_type", "requested_software_type"),
            partition_reference=sequence.get("requested_reference", "requested_reference"),
            partition_parameter_kw=sequence.get("requested_parameter_dict", {}),
            filter_kw=sequence.get("requested_filter_dict", {}),
            state=sequence.get("instance_state"),
        )

        slap_computer_partition.request(**kw)
Ejemplo n.º 18
0
    def __init__(self, buildout, name, options):
        """Default initialisation"""
        self.slap = slap.slap()

        # SLAP related information
        slap_connection = buildout['slap-connection']
        self.computer_id = slap_connection['computer-id']
        self.computer_partition_id = slap_connection['partition-id']
        self.server_url = slap_connection['server-url']
        self.software_release_url = slap_connection['software-release-url']
        self.key_file = slap_connection.get('key-file')
        self.cert_file = slap_connection.get('cert-file')
        self.slave_list = json.loads(options['slave-instance-list'])

        options['slave-amount'] = '%s' % len(self.slave_list)
        return GenericBaseRecipe.__init__(self, buildout, name, options)
Ejemplo n.º 19
0
  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)
    if config.dry_run:
      return
    return slap_computer.updateConfiguration(
        xml_marshaller.dumps(_getDict(self)))
  def stepCheckPersonRequestSlapSoftwareInstanceWithAnotherSoftwareType(
                                                    self, sequence, **kw):
    software_release = sequence['software_release_uri']
    self.slap = slap.slap()
    self.slap.initializeConnection(self.server_url, timeout=None)
    open_order = self.slap.registerOpenOrder()
    requested_slap_computer_partition = open_order.request(
       software_release=software_release,
       software_type="SecondSoftwareType",
       partition_reference=sequence['requested_reference'],
       partition_parameter_kw=sequence.get('requested_parameter_dict', {}),
       filter_kw=sequence.get('requested_filter_dict', {}))

    self.assertEquals(sequence.get('requested_computer_partition_reference'),
                      requested_slap_computer_partition.getId())
    self.assertEquals("SecondSoftwareType",
                      requested_slap_computer_partition.getInstanceParameterDict()['slap_software_type'])
    self.assertEquals(1,
                      requested_slap_computer_partition._need_modification)
 def stepCheckConnectionXmlOfSlaveInstanceFromComputerPartition(self, sequence):
   computer_reference = sequence["computer_reference"]
   computer_partition_reference = sequence["computer_partition_reference"]
   slave_reference = sequence["requested_reference"]
   slave_software_release = sequence["software_release_uri"]
   slave_software_type = sequence["requested_software_type"]
   self.slap = slap.slap()
   self.slap.initializeConnection(self.server_url, timeout=None)
   # Get Computer Partition
   computer_partition = self.slap.registerComputerPartition(
       computer_reference, computer_partition_reference)
   # Get slave
   slave_instance = computer_partition.request(
       software_release=slave_software_release,
       software_type=slave_software_type,
       partition_reference=slave_reference,
       shared=True,
   )
   self.assertEquals(sequence["slave_instance_site_url"],
                     slave_instance.getConnectionParameter("site_url"))
Ejemplo n.º 22
0
  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)
    if config.dry_run:
      return
    try:
      slap_computer.updateConfiguration(xml_marshaller.dumps(_getDict(self)))
    except slap.NotFoundError as error:
      raise slap.NotFoundError("%s\nERROR : This SlapOS node is not recognised by "
          "SlapOS Master. Please make sure computer_id of slapos.cfg looks "
          "like 'COMP-123' and is correct.\nError is : 404 Not Found." % error)
    return
Ejemplo n.º 23
0
def promise(args):
    def failed_ssh():
        sys.stderr.write("SSH Connection failed\n")
        partition = slap.registerComputerPartition(args['computer_id'],
                                                   args['partition_id'])
        partition.bang("SSH Connection failed. rdiff-backup is unusable.")

    def sigterm_handler(signum, frame):
        failed_ssh()

    signal.signal(signal.SIGTERM, sigterm_handler)

    slap = slapmodule.slap()
    slap.initializeConnection(args['server_url'],
                              key_file=args.get('key_file'),
                              cert_file=args.get('cert_file'))

    ssh = subprocess.Popen(
        [args['ssh_client'],
         '%(user)s@%(host)s/%(port)s' % args],
        stdin=subprocess.PIPE,
        stdout=open(os.devnull, 'w'),
        stderr=open(os.devnull, 'w'))

    # Rdiff Backup protocol quit command
    quitcommand = 'q' + chr(255) + chr(0) * 7

    ssh.stdin.write(quitcommand)
    ssh.stdin.flush()
    ssh.stdin.close()
    ssh.wait()

    if ssh.poll() is None:
        return 1
    if ssh.returncode != 0:
        failed_ssh()
    return ssh.returncode
Ejemplo n.º 24
0
  def stepSlapReportUsageCall(self, sequence, **kw):
    """
    Checks that slap.reportUsage is successfully called.
    """
    # We check if this is the first time that a node send a usage report
    if sequence['first_call'] == True:
      sequence['second_call'] = True
    else:
      sequence['first_call'] = True

    # We retrieve an example of XML report sent by a node
    vifib_test_folder = self.getPortalObject().portal_skins.vifib_test
    usage_string = \
      vifib_test_folder.ERP5Site_getUsageReportTestSample(
        self.purchase_packing_list_quantity)
    sequence['initial_xml'] = usage_string

    #We send the XML report to the Master
    slap_object = slap.slap()
    slap_object.initializeConnection(self.server_url)
    slap_object._connection_helper.POST(
      '/useComputer',
      {'computer_id': sequence['computer_reference'],
      'use_string': usage_string})
Ejemplo n.º 25
0
  def __init__(self, buildout, name, options):
    """Default initialisation"""
    self.name = name
    options['eggs'] = 'slapos.cookbook'
    self.options = options
    self.logger = logging.getLogger(self.name)
    self.slap = slap.slap()
    self.work_directory = os.path.abspath(buildout['buildout'][
      'directory'])
    self.bin_directory = os.path.join(buildout['buildout'][
      'directory'], 'bin')
    self.data_root_directory = os.path.join(self.work_directory, 'srv')
    self.backup_directory = os.path.join(self.data_root_directory, 'backup')
    self.var_directory = os.path.join(self.work_directory, 'var')
    self.log_directory = os.path.join(self.var_directory, 'log')
    self.run_directory = os.path.join(self.var_directory, 'run')
    self.etc_directory = os.path.join(self.work_directory, 'etc')
    self.tmp_directory = os.path.join(self.work_directory, 'tmp')
    self.wrapper_directory = os.path.join(self.etc_directory, 'run')
    self.wrapper_report_directory = os.path.join(self.etc_directory, 'report')
    self.wrapper_xml_report_directory = os.path.join(self.var_directory,
        'xml_report')
    self.destroy_script_location = os.path.join(self, self.work_directory,
        'sbin', 'destroy')
    self.promise_directory = os.path.join(self.etc_directory, 'promise')

    # default directory structure information
    self.default_directory_list = [
      self.bin_directory, # CP/bin - instance own binaries
      os.path.join(self, self.work_directory, 'sbin'), # CP/sbin - system
       # binaries, not exposed, only CP/sbin/destroy
      self.data_root_directory, # CP/srv - data container
      self.backup_directory, # CP/srv/backup - backup container
      self.etc_directory, # CP/etc - configuration container
      self.wrapper_directory, # CP/etc/run - for wrappers
      self.wrapper_report_directory, # CP/etc/report - for report wrappers
      self.promise_directory, # CP/etc/promise - for promise checking scripts
      self.var_directory, # CP/var - partition "internal" container for logs,
                          # and another metadata
      self.wrapper_xml_report_directory, # CP/var/xml_report - for xml_report wrappers
      self.log_directory, # CP/var/log - log container
      self.run_directory, # CP/var/run - working container - pids, sockets
      self.tmp_directory, # CP/tmp - temporary files
    ]

    # SLAP related information
    try:
      slap_connection = buildout['slap_connection']
      self.computer_id = slap_connection['computer_id']
      self.computer_partition_id = slap_connection['partition_id']
      self.server_url = slap_connection['server_url']
      self.software_release_url = slap_connection['software_release_url']
      self.key_file = slap_connection.get('key_file')
      self.cert_file = slap_connection.get('cert_file')
    except zc.buildout.buildout.MissingSection:
      slap_connection = buildout['slap-connection']
      self.computer_id = slap_connection['computer-id']
      self.computer_partition_id = slap_connection['partition-id']
      self.server_url = slap_connection['server-url']
      self.software_release_url = slap_connection['software-release-url']
      self.key_file = slap_connection.get('key-file')
      self.cert_file = slap_connection.get('cert-file')

    # setup egg to give possibility to generate scripts
    self.egg = zc.recipe.egg.Egg(buildout, options['recipe'], options)

    # Hook options
    self._options(options)

    # setup auto uninstall/install
    self._setupAutoInstallUninstall()
Ejemplo n.º 26
0
    def __init__(self, buildout, name, options):
        self.logger = logging.getLogger(name)
        software_url = options['software-url']
        name = options['name']
        return_parameters = options.get('return', '').split()
        if not return_parameters:
            self.logger.debug("No parameter to return to main instance."
                              "Be careful about that...")
        software_type = options.get('software-type', DEFAULT_SOFTWARE_TYPE)
        if 'config' in options or 'sla' in options:
            raise UserError("'config' & 'sla' options are obsolete."
                            " Clean up your software release.")
        filter_kw = {
            k[4:]: v
            for k, v in options.iteritems() if k.startswith('sla-') and v
        }
        partition_parameter_kw = self._filterForStorage({
            k[7:]: v
            for k, v in options.iteritems() if k.startswith('config-')
        })
        slave = options.get('slave', 'false').lower() in \
          librecipe.GenericBaseRecipe.TRUE_VALUES

        # By default XXXX Way of doing it is ugly and dangerous
        requested_state = options.get(
            'state', buildout['slap-connection'].get('requested', 'started'))
        options['requested-state'] = requested_state

        slap = slapmodule.slap()
        slap.initializeConnection(
            options['server-url'],
            options.get('key-file'),
            options.get('cert-file'),
        )
        request = slap.registerComputerPartition(
            options['computer-id'],
            options['partition-id'],
        ).request

        if software_url is not None and \
          software_url.startswith(SOFTWARE_PRODUCT_NAMESPACE):

            product = SoftwareProductCollection(self.logger, slap)

            try:
                software_url = product.__getattr__(
                    software_url[len(SOFTWARE_PRODUCT_NAMESPACE):])
            except AttributeError as e:
                self.logger.warning('Error on get software release : %s ' %
                                    e.message)

        self._raise_request_exception = None
        self._raise_request_exception_formatted = None
        self.instance = None

        # Try to do the request and fetch parameter dict...
        try:
            self.instance = request(
                software_url,
                software_type,
                name,
                partition_parameter_kw=partition_parameter_kw,
                filter_kw=filter_kw,
                shared=slave,
                state=requested_state)
            return_parameter_dict = self._getReturnParameterDict(
                self.instance, return_parameters)
            # Fetch the instance-guid and the instance-state
            # Note: SlapOS Master does not support it for slave instances
            if not slave:
                try:
                    options['instance-guid'] = self.instance.getInstanceGuid()
                    # XXX: deprecated, to be removed
                    options['instance_guid'] = self.instance.getInstanceGuid()
                    options['instance-state'] = self.instance.getState()
                    options['instance-status'] = self.instance.getStatus()
                except (slapmodule.ResourceNotReady, AttributeError):
                    # Backward compatibility. Old SlapOS master and core don't know this.
                    self.logger.warning(
                        "Impossible to fetch instance GUID nor state.")
        except (slapmodule.NotFoundError, slapmodule.ServerError,
                slapmodule.ResourceNotReady) as exc:
            self._raise_request_exception = exc
            self._raise_request_exception_formatted = traceback.format_exc()
            return_parameter_dict = {}

        # Then try to get all the parameters. In case of problem, put empty string.
        for param in return_parameters:
            options['connection-%s' % param] = ''
            try:
                options['connection-%s' % param] = return_parameter_dict[param]
            except KeyError:
                if self.failed is None:
                    self.failed = param
Ejemplo n.º 27
0
  def __init__(self, buildout, name, options):
    """Default initialisation"""
    self.name = name
    options['eggs'] = 'slapos.cookbook'
    self.options = options
    self.logger = logging.getLogger(self.name)
    self.slap = slap.slap()
    self.work_directory = os.path.abspath(buildout['buildout'][
      'directory'])
    self.bin_directory = os.path.join(buildout['buildout'][
      'directory'], 'bin')
    self.data_root_directory = os.path.join(self.work_directory, 'srv')
    self.backup_directory = os.path.join(self.data_root_directory, 'backup')
    self.var_directory = os.path.join(self.work_directory, 'var')
    self.log_directory = os.path.join(self.var_directory, 'log')
    self.run_directory = os.path.join(self.var_directory, 'run')
    self.etc_directory = os.path.join(self.work_directory, 'etc')
    self.tmp_directory = os.path.join(self.work_directory, 'tmp')
    self.wrapper_directory = os.path.join(self.etc_directory, 'run')
    self.wrapper_report_directory = os.path.join(self.etc_directory, 'report')
    self.wrapper_xml_report_directory = os.path.join(self.var_directory,
        'xml_report')
    self.destroy_script_location = os.path.join(self, self.work_directory,
        'sbin', 'destroy')
    self.promise_directory = os.path.join(self.etc_directory, 'promise')

    # default directory structure information
    self.default_directory_list = [
      self.bin_directory, # CP/bin - instance own binaries
      os.path.join(self, self.work_directory, 'sbin'), # CP/sbin - system
       # binaries, not exposed, only CP/sbin/destroy
      self.data_root_directory, # CP/srv - data container
      self.backup_directory, # CP/srv/backup - backup container
      self.etc_directory, # CP/etc - configuration container
      self.wrapper_directory, # CP/etc/run - for wrappers
      self.wrapper_report_directory, # CP/etc/report - for report wrappers
      self.promise_directory, # CP/etc/promise - for promise checking scripts
      self.var_directory, # CP/var - partition "internal" container for logs,
                          # and another metadata
      self.wrapper_xml_report_directory, # CP/var/xml_report - for xml_report wrappers
      self.log_directory, # CP/var/log - log container
      self.run_directory, # CP/var/run - working container - pids, sockets
      self.tmp_directory, # CP/tmp - temporary files
    ]

    # SLAP related information
    slap_connection = buildout['slap_connection']
    self.computer_id = slap_connection['computer_id']
    self.computer_partition_id = slap_connection['partition_id']
    self.server_url = slap_connection['server_url']
    self.software_release_url = slap_connection['software_release_url']
    self.key_file = slap_connection.get('key_file')
    self.cert_file = slap_connection.get('cert_file')

    # setup egg to give possibility to generate scripts
    self.egg = zc.recipe.egg.Egg(buildout, options['recipe'], options)

    # Hook options
    self._options(options)

    # setup auto uninstall/install
    self._setupAutoInstallUninstall()
Ejemplo n.º 28
0
 def __init__(
     self,
     software_root,
     instance_root,
     master_url,
     computer_id,
     supervisord_socket,
     supervisord_configuration_path,
     buildout,
     key_file=None,
     cert_file=None,
     signature_private_key_file=None,
     signature_certificate_list=None,
     download_binary_cache_url=None,
     upload_binary_cache_url=None,
     binary_cache_url_blacklist=None,
     upload_cache_url=None,
     download_binary_dir_url=None,
     upload_binary_dir_url=None,
     upload_dir_url=None,
     master_ca_file=None,
     certificate_repository_path=None,
     console=False,
     promise_timeout=3,
     shacache_cert_file=None,
     shacache_key_file=None,
     shadir_cert_file=None,
     shadir_key_file=None,
     develop=False,
     software_release_filter_list=None,
     computer_partition_filter_list=None,
 ):
     """Makes easy initialisation of class parameters"""
     # Parses arguments
     self.software_root = os.path.abspath(software_root)
     self.instance_root = os.path.abspath(instance_root)
     self.master_url = master_url
     self.computer_id = computer_id
     self.supervisord_socket = supervisord_socket
     self.supervisord_configuration_path = supervisord_configuration_path
     self.key_file = key_file
     self.cert_file = cert_file
     self.master_ca_file = master_ca_file
     self.certificate_repository_path = certificate_repository_path
     self.signature_private_key_file = signature_private_key_file
     self.signature_certificate_list = signature_certificate_list
     self.download_binary_cache_url = download_binary_cache_url
     self.upload_binary_cache_url = upload_binary_cache_url
     self.binary_cache_url_blacklist = binary_cache_url_blacklist
     self.upload_cache_url = upload_cache_url
     self.download_binary_dir_url = download_binary_dir_url
     self.upload_binary_dir_url = upload_binary_dir_url
     self.upload_dir_url = upload_dir_url
     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
     # Configures logger
     self.logger = logging.getLogger("Slapgrid")
     # Creates objects from slap module
     self.slap = slap.slap()
     self.slap.initializeConnection(
         self.master_url, key_file=self.key_file, cert_file=self.cert_file, master_ca_file=self.master_ca_file
     )
     self.computer = self.slap.registerComputer(self.computer_id)
     # Defines all needed paths
     self.instance_etc_directory = os.path.join(self.instance_root, "etc")
     self.supervisord_configuration_directory = os.path.join(self.instance_etc_directory, "supervisord.conf.d")
     self.console = console
     self.buildout = buildout
     self.promise_timeout = promise_timeout
     self.develop = develop
     if software_release_filter_list is not None:
         self.software_release_filter_list = software_release_filter_list.split(",")
     else:
         self.software_release_filter_list = []
     self.computer_partition_filter_list = []
     if computer_partition_filter_list is not None:
         self.computer_partition_filter_list = computer_partition_filter_list.split(",")
Ejemplo n.º 29
0
 def test_slap_registerSupply(self):
   self.slap = slap.slap()
   self.slap.initializeConnection(self.server_url, timeout=None)
   supply = self.slap.registerSupply()
   self.assertTrue(isinstance(supply, Supply))
Ejemplo n.º 30
0
def runAccords(accords_conf):
    """Launch ACCORDS, parse manifest, broker manifest, send connection
     informations to SlapOS Master. Destroy instance and stops ACCORDS at
     SIGTERM."""
    computer_id = accords_conf['computer_id']
    computer_partition_id = accords_conf['computer_partition_id']
    server_url = accords_conf['server_url']
    software_release_url = accords_conf['software_release_url']
    key_file = accords_conf['key_file']
    cert_file = accords_conf['cert_file']
    accords_lib_directory = accords_conf['accords_lib_directory']
    accords_location = accords_conf['accords_location']
    manifest_name = accords_conf['manifest_name']

    environment = dict(
        LD_LIBRARY_PATH=accords_lib_directory,
        PATH=accords_conf['path'],
        HOME=accords_location,
    )

    # Set handler to stop ACCORDS when end of world comes
    # XXX use subprocess.check_call and add exception handlers
    def sigtermHandler(signum, frame):
        Popen(['./co-command', 'stop', '/service/*'],
              cwd=accords_location,
              env=environment).communicate()
        Popen(['./co-stop'], cwd=accords_location,
              env=environment).communicate()
        sys.exit(0)

    signal.signal(signal.SIGTERM, sigtermHandler)

    # Launch ACCORDS, parse & broke manifest to deploy instance
    print 'Starting ACCORDS and friends...'
    subprocess.check_call(['./co-start'],
                          cwd=accords_location,
                          env=environment)
    print 'Parsing manifest...'
    subprocess.check_call(['./co-parser', manifest_name],
                          cwd=accords_location,
                          env=environment)
    print 'Brokering manifest...'
    subprocess.check_call(['./co-broker', manifest_name],
                          cwd=accords_location,
                          env=environment)
    print 'Done.'

    # Parse answer
    # XXX
    connection_dict = dict(connection='hardcoded')

    # Send information about published service to SlapOS Master
    slap_connection = slap.slap()
    slap_connection.initializeConnection(server_url, key_file, cert_file)
    computer_partition = slap_connection.registerComputerPartition(
        computer_id, computer_partition_id)
    computer_partition.setConnectionDict(connection_dict)

    # Go to sleep, wait kill
    while (True):
        time.sleep(60)
Ejemplo n.º 31
0
 def stepSupplyComputerSoftwareReleaseDestroyed(self, sequence, **kw):
   self.slap = slap.slap()
   self.slap.initializeConnection(self.server_url, timeout=None)
   supply = self.slap.registerSupply()
   supply.supply(sequence['software_release_uri'],
     sequence['computer_reference'], 'destroyed')
Ejemplo n.º 32
0
  def __init__(self, buildout, name, options):
    self.logger = logging.getLogger(name)
    software_url = options['software-url']
    name = options['name']
    return_parameters = options.get('return', '').split()
    if not return_parameters:
      self.logger.debug("No parameter to return to main instance."
        "Be careful about that...")
    software_type = options.get('software-type', DEFAULT_SOFTWARE_TYPE)
    if 'config' in options or 'sla' in options:
      raise UserError("'config' & 'sla' options are obsolete."
                      " Clean up your software release.")
    filter_kw = {k[4:]: v
      for k, v in options.iteritems()
      if k.startswith('sla-') and v}
    partition_parameter_kw = self._filterForStorage({k[7:]: v
      for k, v in options.iteritems()
      if k.startswith('config-')})
    slave = options.get('slave', 'false').lower() in \
      librecipe.GenericBaseRecipe.TRUE_VALUES

    # By default XXXX Way of doing it is ugly and dangerous
    requested_state = options.get('state', buildout['slap-connection'].get('requested','started'))
    options['requested-state'] = requested_state

    slap = slapmodule.slap()
    slap.initializeConnection(
      options['server-url'],
      options.get('key-file'),
      options.get('cert-file'),
    )
    request = slap.registerComputerPartition(
      options['computer-id'],
      options['partition-id'],
    ).request

    if software_url is not None and \
      software_url.startswith(SOFTWARE_PRODUCT_NAMESPACE):
    
      product = SoftwareProductCollection(self.logger, slap)
      
      try:
        software_url = product.__getattr__(
          software_url[len(SOFTWARE_PRODUCT_NAMESPACE):])
      except AttributeError as e:
        self.logger.warning('Error on get software release : %s ' % e.message)
        

    self._raise_request_exception = None
    self._raise_request_exception_formatted = None
    self.instance = None

    # Try to do the request and fetch parameter dict...
    try:
      self.instance = request(software_url, software_type,
          name, partition_parameter_kw=partition_parameter_kw,
          filter_kw=filter_kw, shared=slave, state=requested_state)
      return_parameter_dict = self._getReturnParameterDict(self.instance,
          return_parameters)
      # Fetch the instance-guid and the instance-state
      # Note: SlapOS Master does not support it for slave instances
      if not slave:
        try:
          options['instance-guid'] = self.instance.getInstanceGuid() \
              .encode('UTF-8')
          # XXX: deprecated, to be removed
          options['instance_guid'] = options['instance-guid']
          options['instance-state'] = self.instance.getState()
          options['instance-status'] = self.instance.getStatus()
        except (slapmodule.ResourceNotReady, AttributeError):
          # Backward compatibility. Old SlapOS master and core don't know this.
          self.logger.warning("Impossible to fetch instance GUID nor state.")
    except (slapmodule.NotFoundError, slapmodule.ServerError, slapmodule.ResourceNotReady) as exc:
      self.logger.warning(
        'Request for %(request_name)r with software release '
        '%(software_release)r and software type %(software_type)r failed '
        'with partition_parameter_kw=%(partition_parameter_kw)r, '
        'filter_kw=%(filter_kw)r, shared=%(shared)r, state=%(state)r.', dict(
          software_release=software_url,
          software_type=software_type,
          request_name=name,
          partition_parameter_kw=partition_parameter_kw,
          filter_kw=filter_kw,
          shared=slave,
          state=requested_state
        )
      )
      self._raise_request_exception = exc
      self._raise_request_exception_formatted = traceback.format_exc()
      return_parameter_dict = {}

    # Then try to get all the parameters. In case of problem, put empty string.
    for param in return_parameters:
      value = ''
      try:
        value = return_parameter_dict[param]
      except KeyError:
        if self.failed is None:
          self.failed = param
      if isinstance(value, unicode):
        value = value.encode('UTF-8')
      options['connection-%s' % param] = value