Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhauled 900_clone_users_and_groups.sh #1471

Conversation

jsmeix
Copy link
Member

@jsmeix jsmeix commented Sep 4, 2017

Now rescue/default/900_clone_users_and_groups.sh
should match https://github.com/rear/rear/wiki/Coding-Style
plus some possible bug fixes (more fail safe - hopefully)
and a better description in default.conf

@jsmeix jsmeix added cleanup enhancement Adaptions and new features labels Sep 4, 2017
@jsmeix jsmeix added this to the ReaR v2.3 milestone Sep 4, 2017
@jsmeix jsmeix self-assigned this Sep 4, 2017
@jsmeix
Copy link
Member Author

jsmeix commented Sep 4, 2017

Please wait - it is not yet tested...

@jsmeix
Copy link
Member Author

jsmeix commented Sep 4, 2017

@N3WWN
have a look if you think it is also o.k. this way
for your #1464
(I cannot add you as an official reviewer here).

@jsmeix
Copy link
Member Author

jsmeix commented Sep 5, 2017

I tested it and it seems to work at least for me.

@jsmeix jsmeix requested a review from a team September 5, 2017 09:35
@jsmeix
Copy link
Member Author

jsmeix commented Sep 5, 2017

If there are no furious objections ;-)
I like to merge it soon...

Copy link
Member

@schlomo schlomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good of you to refactor this code - thanks!

I think that this will work but I am afraid that there is a fundamental change compared to the previous code.

If CLONE_ALL_USERS_GROUPS is set then this code actually replaces the CLONE_USERS and CLONE_GROUPS from the config. IMHO it should instead add the entries to the arrays set by the user.

The use case is actually adding users and groups that are not defined in the local files but can be resolved through NSS. The later code uses getent to retrieve the details so that this also works for remote users and groups.

I think that we should rename the CLONE_ALL_USERS_AND_GROUPS parameter to be called CLONE_ALL_LOCAL_USERS_AND_GROUPS to make it really explicit that this wildcard cloning is only about local users.

test "${CLONE_USERS[*]}" && Log "Cloning users: ${CLONE_USERS[@]}"
for user in "${CLONE_USERS[@]}" ; do
# Skip if the user exists already in the ReaR recovery system:
grep "^$user:" $ROOTFS_DIR/etc/passwd 1>&2 && continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better grep -q

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No "grep -q" because that deletes information.
With "1>&2" I have the info in the log in any case
even if redirection of STDOUT gets again postponed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If grep fails then there is no output in STDOUT in any case. STDERR, where grep would complain about actual errors, is in any case already in the log. So I don't understand what you gain by this.

If grep found the line then I see no value in printing it because it is just the line that this script added there previously. If you want to dump the content of it then better do it with a suitable heading.

fi
# Prepare to also add the user's group to the CLONE_GROUPS array:
# passwd_entry="user:password:UID:GID:description:HOMEdirectory:shell
groupID="$( echo "$passwd_entry" | cut -d ':' -f '4' )"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use cut ... <<<"$passwd_entry" to manage without the pipe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks!
I am not yet used to use '<<<'.

# group_entry="group:passwd:GID:userlist"
group="$( echo "$group_entry" | cut -d ':' -f '1' )"
# Skip if the group is already in the CLONE_GROUPS array:
IsInArray "$group" "${CLONE_GROUPS[@]}" && continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer here

if !  IsInArray "$group" "${CLONE_GROUPS[@]}" ; then
    CLONE_GROUPS=( "${CLONE_GROUPS[@]}" "$group" )
fi

to make it easier to read. I would use continue only to denote skipping the entire while block for a certain item.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer "return early return often", cf.
https://github.com/rear/rear/wiki/Coding-Style

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me "continue" means "continue with the next item"
and that is exactly what it should do in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does that. However, the next developer who will add more stuff below will be easily fooled by that. That's why I find the use of continue in this case not optimum. With the if clause it is clear that the array check is related to the array addition. A developer can add code before or after that without any worries.

In other words: I prefer locally understandable logic over logic that jumps far away. I agree with the coding style piece you refer to but I don't agree that the generalization for while loops is a conclusion from that.

test "${CLONE_GROUPS[*]}" && Log "Cloning groups: ${CLONE_GROUPS[@]}"
for group in "${CLONE_GROUPS[@]}" ; do
# Skip if the group exists already in the ReaR recovery system:
grep "^$group:" $ROOTFS_DIR/etc/group 1>&2 && continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep -q

@jsmeix jsmeix merged commit df6db15 into rear:master Sep 5, 2017
@jsmeix
Copy link
Member Author

jsmeix commented Sep 5, 2017

@schlomo
whoops!
I clicked in my browser "reload page"
but it did not show your comments
so that I have merged it now "as is".

@jsmeix
Copy link
Member Author

jsmeix commented Sep 5, 2017

That CLONE_ALL_USERS_GROUPS
replaces the CLONE_USERS and CLONE_GROUPS
is what was and still is documented in default.conf
and what was and still is implemented.
My changes should not change the behaviour
compared to wat it was directly before my changes.

@jsmeix
Copy link
Member Author

jsmeix commented Sep 5, 2017

It continues with #1472
but only minor adaptions there - no change in behaviour.
Because it still works as it worked before
#1471
(at least according to what I see)
I close this one as "fixed".
Of course if I implemented new bugs
with my pull requests, then I will fix them.

@jsmeix jsmeix deleted the overhaul_900_clone_users_and_groups_related_to_pull_request_1464 branch September 5, 2017 11:55
@N3WWN
Copy link
Contributor

N3WWN commented Sep 5, 2017

@N3WWN
have a look if you think it is also o.k. this way
for your #1464
(I cannot add you as an official reviewer here).

Yup, looks good to me, @jsmeix ! The changes that I suggested for issues I ran into when work on the YUM backup method are taken into account, so I'm happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants