From 0c223ea9cb405db6d6a1e75a68fbacda62f3bbeb Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 20 Nov 2008 12:50:54 +0000
Subject: [PATCH] ganeti.ssconf: Add function to write ssconf files

This function will be used to write ssconf files from the node daemon.
By creating a lock file, we synchronize different child processes of
ganeti-noded to not overwrite each other's changes. Also, external
processes could use this lock to make sure they don't read ssconf
files while changes are going on.

The socket module is not used (anymore) in ganeti.ssconf, removing it.

Reviewed-by: iustinp
---
 lib/constants.py |  1 +
 lib/ssconf.py    | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/lib/constants.py b/lib/constants.py
index 1972303a1..6c3e0dd3d 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -88,6 +88,7 @@ DISK_LINKS_DIR = RUN_GANETI_DIR + "/instance-disks"
 # daemon is started (this takes care of RUN_DIR being tmpfs)
 SUB_RUN_DIRS = [ RUN_GANETI_DIR, BDEV_CACHE_DIR, DISK_LINKS_DIR ]
 LOCK_DIR = _autoconf.LOCALSTATEDIR + "/lock"
+SSCONF_LOCK_FILE = LOCK_DIR + "/ganeti-ssconf.lock"
 CLUSTER_CONF_FILE = DATA_DIR + "/config.data"
 SSL_CERT_FILE = DATA_DIR + "/server.pem"
 WATCHER_STATEFILE = DATA_DIR + "/watcher.data"
diff --git a/lib/ssconf.py b/lib/ssconf.py
index b800f2503..b8189d911 100644
--- a/lib/ssconf.py
+++ b/lib/ssconf.py
@@ -26,7 +26,6 @@ configuration data, which is mostly static and available to all nodes.
 
 """
 
-import socket
 import sys
 
 from ganeti import errors
@@ -35,6 +34,9 @@ from ganeti import utils
 from ganeti import serializer
 
 
+SSCONF_LOCK_TIMEOUT = 10
+
+
 class SimpleConfigReader(object):
   """Simple class to read configuration file.
 
@@ -107,6 +109,40 @@ class SimpleConfigWriter(SimpleConfigReader):
                     mode=0600)
 
 
+def _SsconfPath(name):
+  return "%s/ssconf_%s" % (constants.DATA_DIR, name)
+
+
+def WriteSsconfFiles(file_name):
+  """Writes legacy ssconf files to be used by external scripts.
+
+  @type file_name: string
+  @param file_name: Path to configuration file
+
+  """
+  ssconf_lock = utils.FileLock(constants.SSCONF_LOCK_FILE)
+
+  # Read config
+  cfg = SimpleConfigReader(file_name=file_name)
+
+  # Get lock while writing files
+  ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
+  try:
+    utils.WriteFile(_SsconfPath("cluster_name"),
+                    data="%s\n" % cfg.GetClusterName())
+
+    utils.WriteFile(_SsconfPath("master_ip"),
+                    data="%s\n" % cfg.GetMasterIP())
+
+    utils.WriteFile(_SsconfPath("master_netdev"),
+                    data="%s\n" % cfg.GetMasterNetdev())
+
+    utils.WriteFile(_SsconfPath("master_node"),
+                    data="%s\n" % cfg.GetMasterNode())
+  finally:
+    ssconf_lock.Unlock()
+
+
 def GetMasterAndMyself(ss=None):
   """Get the master node and my own hostname.
 
-- 
GitLab