Skip to content

Commit

Permalink
commands/*2pcscmd: add --set-exec opt-in to make output exec'able
Browse files Browse the repository at this point in the history
This also demonstrates long-anticipated "post-process" provision in
the command-as-a-function design.

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
  • Loading branch information
jnpkrn committed Apr 6, 2017
1 parent db91f77 commit 32f85af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
21 changes: 21 additions & 0 deletions commands/_chains_pcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"""Chains of filters used in *2pcs* commands"""
__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"

from os import fchmod, fstat
import stat

from ..utils import args2tuple, args2unwrapped, tuplist
from ..utils_func import apply_aggregation_preserving_passing_depth

Expand Down Expand Up @@ -49,3 +52,21 @@ def cast_output_inner(*args):
#ccsflat2pcscmd_chain = (ccsflat2cibfinal_chain_exec(cib2pcscmd_chain))
ccsflat2pcscmd_chain = ccsflat2pcscmd_chain_exec()
ccsflat2pcscmd_output = cast_output(ccsflat2pcscmd_chain)


def output_set_exec(cmd_ctxt, output_flt):
"""Common post-processing for commands producing scripts, sets exec bits"""
o = cmd_ctxt.filter(output_flt)['out'].FILE()
if o.startswith('<') and o.endswith('>'):
pass # do not try to manipulate with stdout/stderr
else:
try:
with open(o, 'rb') as f:
fd = f.fileno()
fchmod(fd, fstat(fd).st_mode | stat.S_IXUSR | stat.S_IXGRP)
except IOError:
from sys import stderr
svc_output = cmd_ctxt.get('svc_output',
lambda s, **kw: stderr.write(s + '\n'))
svc_output("Cannot set output file `{0}` executable".format(o),
base="error", urgent=True)
16 changes: 13 additions & 3 deletions commands/ccs2pcscmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..filter import XMLFilter
from ..protocol import protocols
from ..utils_cman import PATH_CLUSTERCONF
from ._chains_pcs import ccsflat2pcscmd_chain_exec
from ._chains_pcs import ccsflat2pcscmd_chain_exec, output_set_exec


@Command.deco(('cmd-annotate',
Expand All @@ -41,6 +41,7 @@ def ccs2pcscmd_flatiron(cmd_ctxt,
enable=False,
start_wait="{ccspcmk2pcscmd.defs[pcscmd_start_wait]}",
noguidance=False,
set_exec=False,
text_width='0',
_common=XMLFilter.command_common):
"""(CMAN,rgmanager) cluster cfg. -> equivalent in pcs commands
Expand All @@ -56,6 +57,7 @@ def ccs2pcscmd_flatiron(cmd_ctxt,
enable enable cluster infrastructure services (autostart on reboot)
start_wait fixed seconds to give cluster to come up initially
noguidance omit extraneous guiding
set_exec make the output file executable (not recommended)
text_width for commands rewrapping (0/-1/neg. ~ auto/disable/hi-limit)
"""
cmd_ctxt['pcscmd_force'] = force
Expand All @@ -75,7 +77,7 @@ def ccs2pcscmd_flatiron(cmd_ctxt,

void_proto = protocols.plugins['void'].ensure_proto
file_proto = protocols.plugins['file'].ensure_proto
return (
yield (
(
void_proto(),
(
Expand Down Expand Up @@ -107,6 +109,9 @@ def ccs2pcscmd_flatiron(cmd_ctxt,
#),
),
)
# post-processing (make resulting file optionally executable)
if set_exec:
output_set_exec(cmd_ctxt, 'cmd-wrap')


@Command.deco(('cmd-annotate',
Expand Down Expand Up @@ -135,6 +140,7 @@ def ccs2pcscmd_needle(cmd_ctxt,
enable=False,
start_wait="{needlexml2pcscmd.defs[pcscmd_start_wait]}",
noguidance=False,
set_exec=False,
text_width='0',
_common=XMLFilter.command_common):
"""(CMAN,rgmanager) cluster cfg. -> equivalent in pcs commands
Expand All @@ -150,6 +156,7 @@ def ccs2pcscmd_needle(cmd_ctxt,
enable enable cluster infrastructure services (autostart on reboot)
start_wait fixed seconds to give cluster to come up initially
noguidance omit extraneous guiding
set_exec make the output file executable (not recommended)
text_width for commands rewrapping (0/-1/neg. ~ auto/disable/hi-limit)
"""
cmd_ctxt['pcscmd_force'] = force
Expand All @@ -169,7 +176,7 @@ def ccs2pcscmd_needle(cmd_ctxt,

void_proto = protocols.plugins['void'].ensure_proto
file_proto = protocols.plugins['file'].ensure_proto
return (
yield (
(
void_proto(),
(
Expand Down Expand Up @@ -204,6 +211,9 @@ def ccs2pcscmd_needle(cmd_ctxt,
#),
),
)
# post-processing (make resulting file optionally executable)
if set_exec:
output_set_exec(cmd_ctxt, 'cmd-wrap')


@CommandAlias.deco
Expand Down
7 changes: 6 additions & 1 deletion commands/cib2pcscmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def cib2pcscmd(cmd_ctxt,
tmp_cib="{cib2pcscmd.defs[pcscmd_tmpcib]}",
dry_run=False,
enable=False,
set_exec=False,
text_width='0',
_common=XMLFilter.command_common):
"""CIB -> equivalent in pcs commands
Expand All @@ -44,6 +45,7 @@ def cib2pcscmd(cmd_ctxt,
tmp_cib file to accumulate the changes (empty ~ direct push, avoid!)
dry_run omit intrusive commands (TMP_CIB reset if empty)
enable enable cluster infrastructure services (autostart on reboot)
set_exec make the output file executable (not recommended)
text_width for commands rewrapping (0/-1/neg. ~ auto/disable/hi-limit)
"""
cmd_ctxt['pcscmd_force'] = force
Expand All @@ -59,7 +61,7 @@ def cib2pcscmd(cmd_ctxt,

void_proto = protocols.plugins['void'].ensure_proto
file_proto = protocols.plugins['file'].ensure_proto
return (
yield (
(
void_proto(),
(
Expand All @@ -77,3 +79,6 @@ def cib2pcscmd(cmd_ctxt,
#),
),
)
# post-processing (make resulting file optionally executable)
if set_exec:
output_set_exec(cmd_ctxt, 'cmd-wrap')
16 changes: 13 additions & 3 deletions commands/pcs2pcscmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..utils_cib import PATH_CIB
from ..utils_cman import PATH_CLUSTERCONF
from ..utils_corosync import PATH_COROCONF
from ._chains_pcs import cib2pcscmd_chain_exec
from ._chains_pcs import cib2pcscmd_chain_exec, output_set_exec


@Command.deco(('cmd-annotate',
Expand All @@ -40,6 +40,7 @@ def pcs2pcscmd_flatiron(cmd_ctxt,
enable=False,
start_wait="{ccspcmk2pcscmd.defs[pcscmd_start_wait]}",
noguidance=False,
set_exec=False,
text_width='0',
_common=XMLFilter.command_common):
"""(Corosync/CMAN,Pacemaker) cluster cfg. -> reinstating pcs commands
Expand All @@ -56,6 +57,7 @@ def pcs2pcscmd_flatiron(cmd_ctxt,
enable enable cluster infrastructure services (autostart on reboot)
start_wait fixed seconds to give cluster to come up initially
noguidance omit extraneous guiding
set_exec make the output file executable (not recommended)
text_width for commands rewrapping (0/-1/neg. ~ auto/disable/hi-limit)
"""
cmd_ctxt['pcscmd_force'] = force
Expand All @@ -76,7 +78,7 @@ def pcs2pcscmd_flatiron(cmd_ctxt,
void_proto = protocols.plugins['void'].ensure_proto
file_proto = protocols.plugins['file'].ensure_proto

return (
yield (
(
void_proto(),
(
Expand All @@ -102,6 +104,9 @@ def pcs2pcscmd_flatiron(cmd_ctxt,
#),
),
)
# post-processing (make resulting file optionally executable)
if set_exec:
output_set_exec(cmd_ctxt, 'cmd-wrap')


@Command.deco(('cmd-annotate',
Expand Down Expand Up @@ -130,6 +135,7 @@ def pcs2pcscmd_needle(cmd_ctxt,
enable=False,
start_wait="{needlexml2pcscmd.defs[pcscmd_start_wait]}",
noguidance=False,
set_exec=False,
text_width='0',
_common=XMLFilter.command_common):
"""(Corosync v2,Pacemaker) cluster cfg. -> reinstating pcs commands
Expand All @@ -146,6 +152,7 @@ def pcs2pcscmd_needle(cmd_ctxt,
enable enable cluster infrastructure services (autostart on reboot)
start_wait fixed seconds to give cluster to come up initially
noguidance omit extraneous guiding
set_exec make the output file executable (not recommended)
text_width for commands rewrapping (0/-1/neg. ~ auto/disable/hi-limit)
"""
cmd_ctxt['pcscmd_force'] = force
Expand All @@ -165,7 +172,7 @@ def pcs2pcscmd_needle(cmd_ctxt,

void_proto = protocols.plugins['void'].ensure_proto
file_proto = protocols.plugins['file'].ensure_proto
return (
yield (
(
void_proto(),
(
Expand Down Expand Up @@ -201,6 +208,9 @@ def pcs2pcscmd_needle(cmd_ctxt,
#),
),
)
# post-processing (make resulting file optionally executable)
if set_exec:
output_set_exec(cmd_ctxt, 'cmd-wrap')


@CommandAlias.deco
Expand Down

0 comments on commit 32f85af

Please sign in to comment.