Skip to content

Commit

Permalink
Merge branch 'master' of github.com:opensvc/opensvc
Browse files Browse the repository at this point in the history
  • Loading branch information
cvaroqui committed Mar 12, 2020
2 parents 9b9925f + 7b4a804 commit 3aba4e6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 97 deletions.
70 changes: 13 additions & 57 deletions lib/data.py
@@ -1,44 +1,33 @@
import os
import sys
import base64
import re
import fnmatch
import shutil
import glob
import tempfile

from rcGlobalEnv import rcEnv
from rcUtilities import lazy, makedirs, split_path, fmt_path, factory, want_context, bencode, which
from svc import BaseSvc
from converters import print_size
from rcUtilities import bencode, create_protected_file, factory, find_editor, makedirs, split_path, want_context
import rcExceptions as ex
import rcStatus

class DataMixin(object):


class DataMixin(object):
def add(self):
self._add(self.options.key, self.options.value_from)


def remove(self):
return self._remove(self.options.key)


def append(self):
self._add(self.options.key, self.options.value_from, append=True)


def _remove(self, key):
if key not in self.data_keys():
return
return self.unset_multi(["data."+key])

return self.unset_multi(["data." + key])

def _add_key(self, key, data):
pass


def add_key(self, key, data, append=False):
if append:
data = self.decode_key(key) + data
Expand All @@ -47,7 +36,6 @@ def add_key(self, key, data, append=False):
else:
self._add_key(key, data)


def remote_add_key(self, key, data):
req = {
"action": "set_key",
Expand All @@ -65,7 +53,6 @@ def remote_add_key(self, key, data):
if status:
raise ex.excError(error)


def _add(self, key=None, value_from=None, append=False):
if key and sys.stdin and value_from in ("-", "/dev/stdin"):
self.add_stdin(key, append=append)
Expand All @@ -80,7 +67,6 @@ def _add(self, key=None, value_from=None, append=False):
else:
raise ex.excError("missing arguments")


def add_stdin(self, key, append=False):
if append:
data = self.decode_key(key)
Expand All @@ -90,7 +76,6 @@ def add_stdin(self, key, append=False):
data += line
self.add_key(key, data)


def add_file(self, key, path, append=None):
if key is None:
key = os.path.basename(path)
Expand All @@ -102,7 +87,6 @@ def add_file(self, key, path, append=None):
data += ofile.read()
self.add_key(key, data)


def add_glob(self, key, path, append=False):
if key is None:
key = ""
Expand All @@ -111,7 +95,6 @@ def add_glob(self, key, path, append=False):
_key = os.path.join(key, os.path.basename(path))
self.add_file(_key, path, append=append)


def add_directory(self, key, path, append=False):
if key is None:
key = ""
Expand All @@ -122,7 +105,6 @@ def add_directory(self, key, path, append=False):
_key = os.path.join(key, fpath[plen:])
self.add_file(_key, fpath, append=append)


@staticmethod
def tempfilename():
tmpf = tempfile.NamedTemporaryFile()
Expand All @@ -131,7 +113,6 @@ def tempfilename():
finally:
tmpf.close()


def edit(self):
if self.options.key is None:
self.edit_config()
Expand All @@ -140,21 +121,12 @@ def edit(self):
no_newline = buff.count(os.linesep) == 0
if buff is None:
raise ex.excError("could not decode the secret key '%s'" % self.options.key)
if "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
elif os.name == "nt":
editor = "notepad"
else:
editor = "vi"
if not which(editor):
raise ex.excError("%s not found" % editor)
editor = find_editor()
fpath = self.tempfilename()
try:
with open(fpath, "wb") as f:
f.write(buff)
except TypeError as exc:
with open(fpath, "w") as f:
f.write(buff)
create_protected_file(fpath, buff, "wb")
except TypeError:
create_protected_file(fpath, buff, "w")
try:
os.system(' '.join((editor, fpath)))
with open(fpath, "r") as f:
Expand All @@ -168,7 +140,6 @@ def edit(self):
finally:
os.unlink(fpath)


def decode(self):
buff = self.decode_key(self.options.key)
if buff is None:
Expand All @@ -179,27 +150,23 @@ def decode(self):
# buff is not binary, .buffer is not supported
sys.stdout.write(buff)


def keys(self):
data = sorted(self.data_keys())
if self.options.format in ("json", "flat_json"):
return data
for key in data:
print(key)


def has_key(self, key):
return key in self.data_keys()


def data_keys(self):
"""
Return the list of keys in the data section.
"""
config = self.print_config_data()
return [key for key in config.get("data", {}).keys()]


def data_dirs(self):
dirs = set()
keys = self.data_keys()
Expand All @@ -214,20 +181,18 @@ def data_dirs(self):
dirs.add(path)
return sorted(list(dirs))


def resolve_key(self, key):
if key is None:
def resolve_key(self, key_to_resolve):
if key_to_resolve is None:
return []
keys = self.data_keys()
dirs = self.data_dirs()
done = set()

def recurse(key, done):
data = []
for path in dirs:
if path != key and not fnmatch.fnmatch(path, key):
continue
rkeys, rdone = recurse(path+"/*", done)
rkeys, rdone = recurse(path + "/*", done)
done |= rdone
data.append({
"type": "dir",
Expand All @@ -246,8 +211,7 @@ def recurse(key, done):
})
return data, done

return recurse(key, done)[0]

return recurse(key_to_resolve, set())[0]

def install_key(self, key, path):
if key["type"] == "file":
Expand All @@ -256,7 +220,6 @@ def install_key(self, key, path):
elif key["type"] == "dir":
self.install_dir_key(key, path)


def install_dir_key(self, data, path):
"""
Install a key decoded data in the host's volatile storage.
Expand All @@ -265,13 +228,11 @@ def install_dir_key(self, data, path):
dirname = os.path.basename(data["path"])
dirpath = os.path.join(path.rstrip("/"), dirname, "")
else:
dirname = os.path.basename(path)
dirpath = path + "/"
makedirs(dirpath)
for key in data["keys"]:
self.install_key(key, dirpath)


def install_file_key(self, key, vpath):
"""
Install a key decoded data in the host's volatile storage.
Expand All @@ -292,8 +253,8 @@ def install_file_key(self, key, vpath):
makedirs(vdir)
self.write_key(vpath, data, key=key)


def key_path(self, key, path):
@staticmethod
def key_path(key, path):
"""
The full path to host's volatile storage file containing the key decoded data.
"""
Expand All @@ -304,7 +265,6 @@ def key_path(self, key, path):
npath = path
return npath


def write_key(self, vpath, data, key=None):
mtime = os.path.getmtime(self.paths.cf)
try:
Expand All @@ -326,7 +286,6 @@ def write_key(self, vpath, data, key=None):
ofile.write(data)
os.utime(vpath, (mtime, mtime))


def _install(self, key, path):
"""
Install the <key> decoded data in the host's volatile storage.
Expand All @@ -337,11 +296,9 @@ def _install(self, key, path):
for _key in keys:
self.install_key(_key, path)


def install(self):
self.postinstall(self.options.key)


def postinstall(self, key=None):
"""
Refresh installed keys
Expand All @@ -352,4 +309,3 @@ def postinstall(self, key=None):
for vol in svc.get_resources("volume"):
if vol.has_data(self.kind, self.path, key) and vol._status() == rcStatus.UP:
vol._install_data(self.kind)

33 changes: 6 additions & 27 deletions lib/node.py
Expand Up @@ -51,7 +51,7 @@
glob_services_config, split_path, validate_name, \
validate_ns_name, unset_all_lazy, \
factory, resolve_path, strip_path, normalize_paths, \
daemon_process_running, validate_kind
daemon_process_running, validate_kind, find_editor
from contexts import want_context
from converters import *
from comm import Crypt
Expand Down Expand Up @@ -1000,16 +1000,12 @@ def edit_config(self):
user the --recover or --discard choices for its next edit
config action.
"""
if "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
elif os.name == "nt":
editor = "notepad"
else:
editor = "vi"
from rcUtilities import which, fsum
if not which(editor):
print("%s not found" % editor, file=sys.stderr)
try:
editor = find_editor()
except ex.excError as err:
print(err, file=sys.stderr)
return 1
from rcUtilities import fsum
path = self.make_temp_config()
os.system(' '.join((editor, path)))
if fsum(path) == fsum(rcEnv.paths.nodeconf):
Expand All @@ -1027,23 +1023,6 @@ def edit_config(self):
"--discard to restart from the live config")
return results["errors"] + results["warnings"]

@staticmethod
def edit_cf(fpath):
"""
Choose an editor, setup the LANG, and exec the editor on the
file passed as argument.
"""
if "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
elif os.name == "nt":
editor = "notepad"
else:
editor = "vi"
if not which(editor):
print("%s not found" % editor, file=sys.stderr)
return 1
return os.system(' '.join((editor, fpath)))

def purge_status_last(self):
"""
Purge the cached status of each and every services and resources.
Expand Down
4 changes: 2 additions & 2 deletions lib/rcCollectorCli.py
Expand Up @@ -26,7 +26,7 @@
import rcExceptions as ex
from six.moves import input
from storage import Storage
from rcUtilities import bdecode, is_glob
from rcUtilities import bdecode, is_glob, find_editor
from rcColor import formatter

try:
Expand Down Expand Up @@ -1695,7 +1695,7 @@ def set_variable_value_edit(self, options):
with open(fname, "w") as f:
f.write(json.dumps(text_data, indent=4))

os.system(os.environ.get("EDITOR", "vi")+" "+fname)
os.system(find_editor()+" "+fname)
with open(fname, "r") as f:
buff = f.read()
new_text_data = json.loads(buff)
Expand Down
19 changes: 19 additions & 0 deletions lib/rcUtilities.py
Expand Up @@ -1643,3 +1643,22 @@ def is_glob(text):
if len(set(text) & set("?*[")) > 0:
return True
return False


def find_editor():
if "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
elif os.name == "nt":
editor = "notepad"
else:
editor = "vi"
if not which(editor):
raise ex.excError("%s not found" % editor)
return editor


def create_protected_file(filepath, buff, mode):
with open(filepath, mode) as f:
if os.name == 'posix':
os.chmod(filepath, 0o0600)
f.write(buff)

0 comments on commit 3aba4e6

Please sign in to comment.