Skip to content
Permalink
Browse files

Allow create --template to take env customization via stdin

Command line example:

$ echo '{"network": "default"} | om test/svc/foo create --template=1
$ echo '{"env": {"network": "default"}} | om test/svc/foo create --template=1

or (multi object template)

$ echo '{"test2/svc/foo": {"network": "default"}} | om svc create --template=1 --namepace=test2
$ echo '{"test2/svc/foo": "env": {{"network": "default"}}} | om svc create --template=1 --namepace=test2

Api create handler example:

{
  "provision": false,
  "template": 1,
  "namespace": "test2",
  "data": {
    "test2/svc/foo": {
      "network": "default"
    }
  }
}

{
  "provision": false,
  "template": 1,
  "namespace": "test2",
  "data": {
    "test2/svc/foo": {
      "env": {
         "network": "default"
      }
    }
  }
}

This patch also allow the template data to be a single or multi
object json string. (previously only mono service ini format was
supported).
  • Loading branch information...
cvaroqui committed Jul 10, 2019
1 parent 2e406cb commit fc1713d3710689d2231ed23360d13c7bffc079b4
Showing with 52 additions and 7 deletions.
  1. +2 −0 lib/mgr.py
  2. +45 −6 lib/node.py
  3. +5 −1 lib/osvcd_lsnr.py
@@ -169,6 +169,8 @@ def export_env_from_options(self, options):
if options.get("daemon"):
os.environ["OSVC_DETACHED"] = "1"
for arg in options.get("env", []):
if arg in ("-", "stdin", "/dev/stdin"):
continue
option, value = self.split_env(arg)
option = option.upper()
os.environ[option] = value
@@ -2805,6 +2805,10 @@ def svc_conf_from_templ(self, name, namespace, kind, template):
raise ex.excError("service not found on the collector")
if len(data["data"][0]["tpl_definition"]) == 0:
raise ex.excError("service has an empty configuration")
try:
return json.loads(data["data"][0]["tpl_definition"])
except Exception:
pass
with open(tmpfpath, "w") as ofile:
os.chmod(tmpfpath, 0o0600)
ofile.write(data["data"][0]["tpl_definition"].replace("\\n", "\n").replace("\\t", "\t"))
@@ -2952,7 +2956,7 @@ def svc_conf_setenv(self, args=None, interactive=False, env=None):
* or leave the value as is, considering the default is accepted
"""
if args is None:
args = {}
args = []
if env is None:
env = {}

@@ -3016,6 +3020,7 @@ def install_service(self, path, fpath=None, template=None,

data = None
installed = []
env_to_merge = {}

if path:
name, _namespace, kind = split_path(path)
@@ -3025,8 +3030,29 @@ def install_service(self, path, fpath=None, template=None,
name = "dummy"
kind = "svc"

if sys.stdin and env in (["-"], ["stdin"], ["/dev/stdin"]):
env_to_merge = self.svc_conf_from_stdin()
env = []

if template and want_context():
req = {
"action": "create",
"options": {
"namespace": namespace,
"provision": provision,
"template": template,
"restore": restore,
"data": env_to_merge,
}
}
result = self.daemon_get(req)
status, error, info = self.parse_result(result)
if status:
raise ex.excError(error)
return

# convert to a pivotal dataset: dict of configs, indexed by path
if sys.stdin and fpath in ("-", "/dev/stdin"):
if sys.stdin and fpath in ("-", "stdin", "/dev/stdin"):
data = self.svc_conf_from_stdin()
elif fpath and "://" not in fpath and not os.path.exists(fpath):
data = self.svc_conf_from_selector(fpath)
@@ -3080,14 +3106,27 @@ def install_service(self, path, fpath=None, template=None,
path: __data,
}
data = _data
else:
if path:
data = {path: {}}

if data:
for path in data:
data[path] = self.svc_conf_set(data[path], kw, env, interactive)
elif path:
data = {
path: self.svc_conf_set({}, kw, env, interactive)
}
if path in env_to_merge:
if "env" in env_to_merge[path]:
_env_to_merge = env_to_merge[path]["env"]
else:
_env_to_merge = env_to_merge[path]
elif "env" in env_to_merge:
_env_to_merge = env_to_merge["env"]
else:
_env_to_merge = env_to_merge
if isinstance(_env_to_merge, dict) and _env_to_merge:
if "env" not in data[path]:
data[path]["env"] = _env_to_merge
else:
data[path]["env"].update(_env_to_merge)

if want_context():
req = {
@@ -2405,9 +2405,13 @@ def action_create(self, nodename, **kwargs):
sync = options.get("sync", True)
namespace = options.get("namespace")
provision = options.get("provision")
template = options.get("template")
restore = options.get("restore")
self.log_request("create/update %s" % ",".join([p for p in data]), nodename, **kwargs)
cmd = ["create", "--config=-"]
if template is not None:
cmd = ["create", "--template=%s" % template, "--env=-"]
else:
cmd = ["create", "--config=-"]
if namespace:
cmd.append("--namespace="+namespace)
if restore:

0 comments on commit fc1713d

Please sign in to comment.
You can’t perform that action at this time.