Exemple #1
0
def GenerateHmacKey(file_name):
    """Writes a new HMAC key.

  @type file_name: str
  @param file_name: Path to output file

  """
    utils.WriteFile(file_name,
                    data="%s\n" % utils.GenerateSecret(),
                    mode=0o400,
                    backup=True)
Exemple #2
0
    def testReadFile(self):
        utils.WriteFile(self.sstore.KeyToFilename(constants.SS_CLUSTER_NAME),
                        data="cluster.example.com")

        self.assertEqual(self.sstore._ReadFile(constants.SS_CLUSTER_NAME),
                         "cluster.example.com")

        self.assertEqual(
            self.sstore._ReadFile(constants.SS_CLUSTER_NAME,
                                  default="something.example.com"),
            "cluster.example.com")
Exemple #3
0
    def testNameMismatch(self):
        tmpfile = utils.PathJoin(self.tmpdir, "ssconf_cluster_name")

        for content in [
                "something.example.com", "foobar\n\ncluster.example.com"
        ]:
            utils.WriteFile(tmpfile, data=content)
            self.assertRaises(errors.GenericError,
                              ssconf.VerifyClusterName,
                              "cluster.example.com",
                              _cfg_location=self.tmpdir)
Exemple #4
0
def WriteKnownHostsFile(cfg, file_name):
    """Writes the cluster-wide equally known_hosts file.

  """
    data = ""
    if cfg.GetRsaHostKey():
        data += "%s ssh-rsa %s\n" % (cfg.GetClusterName(), cfg.GetRsaHostKey())
    if cfg.GetDsaHostKey():
        data += "%s ssh-dss %s\n" % (cfg.GetClusterName(), cfg.GetDsaHostKey())

    utils.WriteFile(file_name, mode=0600, data=data)
Exemple #5
0
 def testRunFail(self):
     """Test correct execution, with run failure"""
     fname = os.path.join(self.rundir, "00test")
     utils.WriteFile(fname, data="#!/bin/sh\n\nexit 1")
     os.chmod(fname, stat.S_IREAD | stat.S_IEXEC)
     (relname, status, runresult) = \
       utils.RunParts(self.rundir, reset_env=True)[0]
     self.failUnlessEqual(relname, os.path.basename(fname))
     self.failUnlessEqual(status, constants.RUNPARTS_RUN)
     self.failUnlessEqual(runresult.exit_code, 1)
     self.failUnless(runresult.failed)
Exemple #6
0
  def testRapiUsersDryRun(self):
    self.assertFalse(os.path.exists(self.rapi_users_path))
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))

    utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
    self._TestSimpleUpgrade(version.BuildVersion(2, 3, 0), True)

    self.assertFalse(os.path.isdir(os.path.dirname(self.rapi_users_path)))
    self.assertTrue(os.path.isfile(self.rapi_users_path_pre24) and
                    not os.path.islink(self.rapi_users_path_pre24))
    self.assertFalse(os.path.exists(self.rapi_users_path))
Exemple #7
0
  def testWrongHostname(self):
    self._CreateValidConfigDir()

    utils.WriteFile(self.config_path,
                    data=serializer.DumpJson(GetMinimalConfig()))

    hostname = netutils.GetHostname().name
    assert hostname != utils.ReadOneLineFile(self.ss_master_node_path)

    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True,
                      ignore_hostname=False)
Exemple #8
0
def _CreateBlankFile(path, mode):
    """Create blank file.

  Create a blank file for the path with specified mode.
  An existing file will be overwritten.

  """
    try:
        utils.WriteFile(path, data="", mode=mode)
    except EnvironmentError, err:
        raise HypervisorError("Failed to create file %s: %s" % (path, err))
Exemple #9
0
  def testRapiUsers24AndAboveDryRun(self):
    self.assertFalse(os.path.exists(self.rapi_users_path))
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))

    os.mkdir(os.path.dirname(self.rapi_users_path))
    utils.WriteFile(self.rapi_users_path, data="other user\n")
    self._TestSimpleUpgrade(version.BuildVersion(2, 3, 0), True)

    self.assertTrue(os.path.isfile(self.rapi_users_path) and
                    not os.path.islink(self.rapi_users_path))
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
    self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
Exemple #10
0
    def testNoSigCgt(self):
        sp = utils.PathJoin(self.tmpdir, "status")

        utils.WriteFile(sp, data="\n".join([
            "Name:   bash",
        ]))

        self.assertRaises(RuntimeError,
                          utils.IsProcessHandlingSignal,
                          1234,
                          10,
                          status_path=sp)
Exemple #11
0
    def testNonEmpty(self):
        (name, _) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)

        utils.WriteFile(utils.PathJoin(self.tmpdir, name, "hello-world"),
                        data="Hello World")

        self.assertRaises(backend.RPCFail,
                          backend.RemoveX509Certificate,
                          name,
                          cryptodir=self.tmpdir)

        self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [name])
def InitConfig(version,
               cluster_config,
               master_node_config,
               cfg_file=pathutils.CLUSTER_CONF_FILE):
    """Create the initial cluster configuration.

  It will contain the current node, which will also be the master
  node, and no instances.

  @type version: int
  @param version: configuration version
  @type cluster_config: L{objects.Cluster}
  @param cluster_config: cluster configuration
  @type master_node_config: L{objects.Node}
  @param master_node_config: master node configuration
  @type cfg_file: string
  @param cfg_file: configuration file path

  """
    uuid_generator = config.TemporaryReservationManager()
    cluster_config.uuid = uuid_generator.Generate([], utils.NewUUID,
                                                  _INITCONF_ECID)
    master_node_config.uuid = uuid_generator.Generate([], utils.NewUUID,
                                                      _INITCONF_ECID)
    cluster_config.master_node = master_node_config.uuid
    nodes = {
        master_node_config.uuid: master_node_config,
    }
    default_nodegroup = objects.NodeGroup(
        uuid=uuid_generator.Generate([], utils.NewUUID, _INITCONF_ECID),
        name=constants.INITIAL_NODE_GROUP_NAME,
        members=[master_node_config.uuid],
        diskparams={},
    )
    nodegroups = {
        default_nodegroup.uuid: default_nodegroup,
    }
    now = time.time()
    config_data = objects.ConfigData(version=version,
                                     cluster=cluster_config,
                                     nodegroups=nodegroups,
                                     nodes=nodes,
                                     instances={},
                                     networks={},
                                     disks={},
                                     filters={},
                                     serial_no=1,
                                     ctime=now,
                                     mtime=now)
    utils.WriteFile(cfg_file,
                    data=serializer.Dump(config_data.ToDict()),
                    mode=0600)
Exemple #13
0
    def testLocked(self):
        path = utils.PathJoin(self.tmpdir, "pid")
        utils.WriteFile(path, data="123")

        fl = utils.FileLock.Open(path)
        try:
            fl.Exclusive(blocking=True)

            self.assertEqual(utils.ReadLockedPidFile(path), 123)
        finally:
            fl.Close()

        self.assert_(utils.ReadLockedPidFile(path) is None)
Exemple #14
0
def OverridePubKeyFile(key_map, key_file=pathutils.SSH_PUB_KEYS):
    """Overrides the public key file with a list of given keys.

  @type key_map: dict from str to list of str
  @param key_map: dictionary mapping uuids to lists of SSH keys

  """
    new_lines = []
    for (uuid, keys) in key_map.items():
        for key in keys:
            new_lines.append("%s %s\n" % (uuid, key))
    new_file_content = "".join(new_lines)
    utils.WriteFile(key_file, data=new_file_content)
 def testEnableDisable(self):
   self.ihandler[self.NOTIFIER_TERM].enable()
   self.ihandler[self.NOTIFIER_TERM].disable()
   self.ihandler[self.NOTIFIER_TERM].disable()
   self.ihandler[self.NOTIFIER_TERM].enable()
   self.ihandler[self.NOTIFIER_TERM].disable()
   self.ihandler[self.NOTIFIER_TERM].enable()
   utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy")
   self.mainloop.Run()
   self.assert_(self.notified[self.NOTIFIER_TERM])
   self.assertFalse(self.notified[self.NOTIFIER_NORM])
   self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0)
   self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0)
Exemple #16
0
def Main():
  """Main routine.

  """
  opts = ParseOptions()

  utils.SetupToolLogging(opts.debug, opts.verbose)

  try:
    getent = runtime.GetEnts()

    data = LoadData(sys.stdin.read())

    cluster_name = VerifyClusterName(data)
    cert_pem = VerifyCertificate(data)
    ssdata = VerifySsconf(data, cluster_name)

    logging.info("Writing ssconf files ...")
    ssconf.WriteSsconfFiles(ssdata, dry_run=opts.dry_run)

    logging.info("Writing node daemon certificate ...")
    utils.WriteFile(pathutils.NODED_CERT_FILE, data=cert_pem,
                    mode=pathutils.NODED_CERT_MODE,
                    uid=getent.masterd_uid, gid=getent.masterd_gid,
                    dry_run=opts.dry_run)

    if (data.get(constants.NDS_START_NODE_DAEMON) and # pylint: disable=E1103
        not opts.dry_run):
      logging.info("Restarting node daemon ...")

      stop_cmd = "%s stop-all" % pathutils.DAEMON_UTIL
      noded_cmd = "%s start %s" % (pathutils.DAEMON_UTIL, constants.NODED)
      mond_cmd = ""
      if constants.ENABLE_MOND:
        mond_cmd = "%s start %s" % (pathutils.DAEMON_UTIL, constants.MOND)

      cmd = "; ".join([stop_cmd, noded_cmd, mond_cmd])

      result = utils.RunCmd(cmd, interactive=True)
      if result.failed:
        raise SetupError("Could not start the node daemons, command '%s'"
                         " failed: %s" % (result.cmd, result.fail_reason))

    logging.info("Node daemon successfully configured")
  except Exception, err: # pylint: disable=W0703
    logging.debug("Caught unhandled exception", exc_info=True)

    (retcode, message) = cli.FormatError(err)
    logging.error(message)

    return retcode
Exemple #17
0
    def _SaveInstanceStash(self, instance_name, data):
        """Save data to the instance stash file in serialized format.

    """
        stash_file = self._InstanceStashFilePath(instance_name)
        serialized = serializer.Dump(data)
        try:
            utils.WriteFile(stash_file,
                            data=serialized,
                            mode=constants.SECURE_FILE_MODE)
        except EnvironmentError as err:
            raise HypervisorError(
                "Failed to save instance stash file %s : %s" %
                (stash_file, err))
Exemple #18
0
  def testRapiUsers24AndAbove(self):
    self.assertFalse(os.path.exists(self.rapi_users_path))
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))

    os.mkdir(os.path.dirname(self.rapi_users_path))
    utils.WriteFile(self.rapi_users_path, data="other user\n")
    self._TestSimpleUpgrade(version.BuildVersion(2, 3, 0), False)

    self.assert_(os.path.islink(self.rapi_users_path_pre24))
    self.assert_(os.path.isfile(self.rapi_users_path))
    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
                     self.rapi_users_path)
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
      self.assertEqual(utils.ReadFile(path), "other user\n")
    def testEmpty(self):
        filename = utils.PathJoin(self.tmpdir, "config.data")
        utils.WriteFile(filename, data="")
        bname = utils.CreateBackup(filename)
        self.assertFileContent(bname, "")
        self.assertEqual(len(glob.glob("%s*" % filename)), 2)
        utils.CreateBackup(filename)
        self.assertEqual(len(glob.glob("%s*" % filename)), 3)
        utils.CreateBackup(filename)
        self.assertEqual(len(glob.glob("%s*" % filename)), 4)

        fifoname = utils.PathJoin(self.tmpdir, "fifo")
        os.mkfifo(fifoname)
        self.assertRaises(errors.ProgrammerError, utils.CreateBackup, fifoname)
  def testSorted(self):
    """Test executions are sorted"""
    files = []
    files.append(os.path.join(self.rundir, "64test"))
    files.append(os.path.join(self.rundir, "00test"))
    files.append(os.path.join(self.rundir, "42test"))

    for fname in files:
      utils.WriteFile(fname, data="")

    results = utils.RunParts(self.rundir, reset_env=True)

    for fname in sorted(files):
      self.failUnlessEqual(os.path.basename(fname), results.pop(0)[0])
Exemple #21
0
    def setUp(self):
        super(_TestXenHypervisor, self).setUp()

        self.tmpdir = tempfile.mkdtemp()

        self.vncpw = "".join(random.sample(string.ascii_letters, 10))

        self._xen_delay = self.TARGET._INSTANCE_LIST_DELAYS
        self.TARGET._INSTANCE_LIST_DELAYS = (0.01, 1.0, 0.05)

        self._list_timeout = self.TARGET._INSTANCE_LIST_TIMEOUT
        self.TARGET._INSTANCE_LIST_TIMEOUT = 0.1

        self.vncpw_path = utils.PathJoin(self.tmpdir, "vncpw")
        utils.WriteFile(self.vncpw_path, data=self.vncpw)
  def _SetUpKeys(self):
    """Creates a fake SSH key for each type and with/without suffix."""
    self._key_file_dict = {}
    for key_type in constants.SSHK_ALL:
      for suffix in ["", self._suffix]:
        pub_key_filename = "id_%s%s.pub" % (key_type, suffix)
        priv_key_filename = "id_%s%s" % (key_type, suffix)

        pub_key_path = self._GetKeyPath(pub_key_filename)
        priv_key_path = self._GetKeyPath(priv_key_filename)

        utils.WriteFile(
            priv_key_path,
            data=self._GenerateKey(key_type + suffix, self.VISIBILITY_PRIVATE))

        utils.WriteFile(
            pub_key_path,
            data=self._GenerateKey(key_type + suffix, self.VISIBILITY_PUBLIC))

        # Fill key dict only for non-suffix keys
        # (as this is how it will be in the code)
        if not suffix:
          self._key_file_dict[key_type] = \
            (priv_key_path, pub_key_path)
  def test(self):
    tmpfile = self._CreateTempFile()

    utils.WriteFile(tmpfile, data="""
      # This is a test file
      /tmp
      /srv/storage
      relative/path
      """)

    self.assertEqual(filestorage._LoadAllowedFileStoragePaths(tmpfile), [
      "/tmp",
      "/srv/storage",
      "relative/path",
      ])
Exemple #24
0
  def _WriteConfigFile(self, instance_name, data):
    """Write the Xen config file for the instance.

    This version of the function just writes the config file from static data.

    """
    # just in case it exists
    utils.RemoveFile(utils.PathJoin(self._cfgdir, "auto", instance_name))

    cfg_file = self._ConfigFileName(instance_name)
    try:
      utils.WriteFile(cfg_file, data=data)
    except EnvironmentError, err:
      raise errors.HypervisorError("Cannot write Xen instance configuration"
                                   " file %s: %s" % (cfg_file, err))
Exemple #25
0
  def testContent(self):
    bkpcount = 0
    for data in ["", "X", "Hello World!\n" * 100, "Binary data\0\x01\x02\n"]:
      for rep in [1, 2, 10, 127]:
        testdata = data * rep

        filename = utils.PathJoin(self.tmpdir, "test.data_")
        utils.WriteFile(filename, data=testdata)
        self.assertFileContent(filename, testdata)

        for _ in range(3):
          bname = utils.CreateBackup(filename)
          bkpcount += 1
          self.assertFileContent(bname, testdata)
          self.assertEqual(len(glob.glob("%s*" % filename)), 1 + bkpcount)
Exemple #26
0
    def testRemovingAutoConfigFile(self):
        name = "inst8206.example.com"
        cfgfile = utils.PathJoin(self.tmpdir, name)
        autodir = utils.PathJoin(self.tmpdir, "auto")
        autocfgfile = utils.PathJoin(autodir, name)

        os.mkdir(autodir)

        utils.WriteFile(autocfgfile, data="")

        hv = self._GetHv()

        self.assertTrue(os.path.isfile(autocfgfile))
        hv._WriteConfigFile(name, "content")
        self.assertFalse(os.path.exists(autocfgfile))
        self.assertEqual(utils.ReadFile(cfgfile), "content")
Exemple #27
0
 def testEmptylines(self):
   myfile = self._CreateTempFile()
   myline = "myline"
   for nl in ["\n", "\r\n"]:
     for ol in ["", "otherline"]:
       dummydata = "%s%s%s%s%s%s" % (nl, nl, myline, nl, ol, nl)
       utils.WriteFile(myfile, data=dummydata)
       self.assert_(set("\r\n") & set(dummydata))
       datalax = utils.ReadOneLineFile(myfile, strict=False)
       self.assertEqual(myline, datalax)
       if ol:
         self.assertRaises(errors.GenericError, utils.ReadOneLineFile,
                           myfile, strict=True)
       else:
         datastrict = utils.ReadOneLineFile(myfile, strict=True)
         self.assertEqual(myline, datastrict)
Exemple #28
0
  def testRapiUsersExistingSymlinkDryRun(self):
    self.assertFalse(os.path.exists(self.rapi_users_path))
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))

    os.mkdir(os.path.dirname(self.rapi_users_path))
    os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
    utils.WriteFile(self.rapi_users_path, data="hello world\n")

    self._TestSimpleUpgrade(version.BuildVersion(2, 2, 0), True)

    self.assertTrue(os.path.islink(self.rapi_users_path_pre24))
    self.assertTrue(os.path.isfile(self.rapi_users_path) and
                    not os.path.islink(self.rapi_users_path))
    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
                     self.rapi_users_path)
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
      self.assertEqual(utils.ReadFile(path), "hello world\n")
  def test(self):
    sp = utils.PathJoin(self.tmpdir, "status")

    utils.WriteFile(sp, data="\n".join([
      "Name:   bash",
      "State:  S (sleeping)",
      "SleepAVG:       98%",
      "Pid:    22250",
      "PPid:   10858",
      "TracerPid:      0",
      "SigBlk: 0000000000010000",
      "SigIgn: 0000000000384004",
      "SigCgt: 000000004b813efb",
      "CapEff: 0000000000000000",
      ]))

    self.assert_(utils.IsProcessHandlingSignal(1234, 10, status_path=sp))
Exemple #30
0
def _WriteInstanceStatus(filename, data):
    """Writes the per-group instance status file.

  The entries are sorted.

  @type filename: string
  @param filename: Path to instance status file
  @type data: list of tuple; (instance name as string, status as string)
  @param data: Instance name and status

  """
    logging.debug("Updating instance status file '%s' with %s instances",
                  filename, len(data))

    utils.WriteFile(filename,
                    data="\n".join("%s %s" % (n, s)
                                   for (n, s) in sorted(data)))