question: what am I doing wrong? #88

Closed
opened 2021-11-20 13:23:43 +00:00 by ungleich-gitea · 15 comments

init manifest:

__foo --foo a --foo b --foo c

type code:

if [ -f "$__object/parameter/foo" ]
then
    while read -r l
    do
        #__file "$l"
        echo "$l" >&2
    done \
        < "$__object/parameter/foo"
fi

cdist-dump -r output after config run:

stderr/manifest:a
stderr/manifest:b
stderr/manifest:c

if i uncomment __file and run again:

stderr/manifest:a
messages:__file/a:create

why b and c is skipped?

same thing works with for, but not with while.

🤷

init manifest: ``` __foo --foo a --foo b --foo c ``` type code: ``` if [ -f "$__object/parameter/foo" ] then while read -r l do #__file "$l" echo "$l" >&2 done \ < "$__object/parameter/foo" fi ``` `cdist-dump -r` output after config run: ``` stderr/manifest:a stderr/manifest:b stderr/manifest:c ``` if i uncomment `__file` and run again: ``` stderr/manifest:a messages:__file/a:create ``` why b and c is skipped? same thing works with `for`, but not with `while`. :shrug:
poljakowski was assigned by ungleich-gitea 2021-11-20 13:23:43 +00:00
Author
Owner

of course i can

of course i can
Author
Owner

@nico Yee, it's not cdist specific, but it's specific for cdist that types always consume stdin.

@nico Yee, it's not cdist specific, but it's specific for cdist that types always consume stdin.
Author
Owner

Today I became aware of something new :)

Today I became aware of something new :)
Author
Owner

mentioned in commit 3c8b470367

mentioned in commit 3c8b470367b755e4011b443426b16128a5e5962a
Author
Owner

closed via merge request !784

closed via merge request !784
Author
Owner

This is actually not cdist specific, but happens the same way with ssh - i.e. anything that likes to read stdion in a loop that you input stuff in

This is actually not cdist specific, but happens the same way with ssh - i.e. anything that likes to read stdion in a loop that you input stuff in
Author
Owner

@steven Thanks! I will update documentation.

@steven Thanks! I will update documentation.
Author
Owner

We should document this. It's known since forever.
The workaround e.g.:


# Generate and configure enabled host keys
comm -23 "$__object/files/enable-host-key" "$__object/files/disable-host-key" | \
while read type; do
   set -- "$type"
   if [ -f "$__object/parameter/$type" ]; then
      set -- "$@" "--source" "$(cat "$__object/parameter/$type")"
   fi
   __ssh_host_key "$@" </dev/null
   export require="$require __ssh_host_key/$type"
done

So make sure your types inside a loop get there stdin from somewhere else.

We should document this. It's known since forever. The workaround e.g.: ``` # Generate and configure enabled host keys comm -23 "$__object/files/enable-host-key" "$__object/files/disable-host-key" | \ while read type; do set -- "$type" if [ -f "$__object/parameter/$type" ]; then set -- "$@" "--source" "$(cat "$__object/parameter/$type")" fi __ssh_host_key "$@" </dev/null export require="$require __ssh_host_key/$type" done ``` So make sure your types inside a loop get there stdin from somewhere else.
Author
Owner

mentioned in merge request !784

mentioned in merge request !784
Author
Owner

@ander Can you write your solution with for loop?

@ander Can you write your solution with `for` loop?
Author
Owner

I think we cannot distinguish such stdin from e.g. heredoc stdin.
Or am I missing something?

@nico @steven We should add this as 'Caveats' to cdist type chapter.

I think we cannot distinguish such stdin from e.g. heredoc stdin. Or am I missing something? @nico @steven We should add this as 'Caveats' to cdist type chapter.
Author
Owner

assigned to @poljakowski

assigned to @poljakowski
Author
Owner

@ander @nico @steven Got it!

$ find ~/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/ -name stdin
/home/darko/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/object/__foo/foo/.cdist-fzov8f2m/stdin
/home/darko/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/object/__file/a/.cdist-fzov8f2m/stdin
$ cat /home/darko/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/object/__file/a/.cdist-fzov8f2m/stdin
b
c

So, parameter foo file content becomes while body's stdin and when executing first __file cdist's emulator reads stdin (the rest of foo param file) and saves it to stdin file (in cdist cache).
For the second read there is no more lines to read.

@ander @nico @steven Got it! ``` $ find ~/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/ -name stdin /home/darko/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/object/__foo/foo/.cdist-fzov8f2m/stdin /home/darko/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/object/__file/a/.cdist-fzov8f2m/stdin $ cat /home/darko/.cdist/cache/90cb04f9d8b4a35c89571b1c6fb9927c/object/__file/a/.cdist-fzov8f2m/stdin b c ``` So, parameter foo file content becomes while body's stdin and when executing first `__file` cdist's emulator reads stdin (the rest of foo param file) and saves it to `stdin` file (in cdist cache). For the second `read` there is no more lines to read.
Author
Owner

@ander Look what seems to work:

if [ -f "$__object/parameter/foo" ]
then
    while read -r line || [ -n "$line" ]
    do
#        __file "$line"
        __file "$line" --source - <<DONE
foo
DONE
        echo "$line" >&2
    done < "$__object/parameter/foo"
fi
@ander Look what seems to work: ``` if [ -f "$__object/parameter/foo" ] then while read -r line || [ -n "$line" ] do # __file "$line" __file "$line" --source - <<DONE foo DONE echo "$line" >&2 done < "$__object/parameter/foo" fi ```
Author
Owner

@ander Interesting.
Currently trying to debug it and not getting it.

@ander Interesting. Currently trying to debug it and not getting it.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: ungleich-public/cdist#88
No description provided.