datacamp / courses-introduction-to-version-control-with-git

Introduction to Version Control with Git by DataCamp
Other
57 stars 70 forks source link

Cannot configure Git for a second user in requirements.sh #8

Closed gvwilson closed 6 years ago

gvwilson commented 6 years ago

requirements.sh contains these lines:

sudo useradd -m thunk
THUNK_HOME=/home/thunk
THUNK_REPO=${THUNK_HOME}/repo
THUNK_SUDO='sudo -u thunk'
THUNK_GIT="${THUNK_SUDO} git -C ${THUNK_REPO}"
${THUNK_SUDO} git config --global user.email "thunk@datacamp.com"
${THUNK_SUDO} git config --global user.name "Thun Ka"

echo
echo "Contents of /home are now:"
ls -al /home
echo 'THUNK_REPO: ' ${THUNK_REPO}
echo 'THUNK_SUDO: ' ${THUNK_SUDO}
echo 'THUNK_GIT: ' ${THUNK_GIT}
echo 'sudo -u thunk git config user.name' $(${THUNK_SUDO} git config user.name)
echo 'sudo -u thunk git config user.email' $(${THUNK_SUDO} git config user.email)

The useradd appears to work (as evidenced by the creation of /home/thunk, but the attempt to configure Git for this user fails, so subsequent Git operations sudo'd by this user ID also fail:

[91mwarning: unable to access '/root/.gitconfig': Permission denied
warning: unable to access '/root/.config/git/config': Permission denied
error: could not lock config file /root/.gitconfig: Permission denied
warning: unable to access '/root/.gitconfig': Permission denied
warning: unable to access '/root/.config/git/config': Permission denied
error: could not lock config file /root/.gitconfig: Permission denied

Contents of /home are now:
total 16
drwxr-xr-x  5 root  root  4096 Sep 11 11:40 .
drwxr-xr-x 56 root  root  4096 Sep 11 11:40 ..
drwxr-xr-x  3 repl  repl  4096 Sep 11 11:40 repl
drwxr-xr-x  2 thunk thunk 4096 Sep 11 11:40 thunk
THUNK_REPO:  /home/thunk/repo
THUNK_SUDO:  sudo -u thunk
THUNK_GIT:  sudo -u thunk git -C /home/thunk/repo
sudo -u thunk git config user.name
sudo -u thunk git config user.email
Cloning into '/home/thunk/repo'...
remote: warning: unable to access '/root/.config/git/attributes': Permission denied        
warning: unable to access '/root/.config/git/attributes': Permission denied
warning: unable to access '/root/.config/git/ignore': Permission denied
warning: unable to access '/root/.config/git/attributes': Permission denied
warning: unable to access '/root/.config/git/attributes': Permission denied

*** Please tell me who you are.

cc @machow @ncarchedi

machow commented 6 years ago

@gvwilson I don't think the user thunk was given root privileges, so cannot use the --global flag in git config --global ..., as it changes something for all users.

gvwilson commented 6 years ago

My understanding is that git config --global writes to ~/.gitconfig so that the setting applies across all of the particular user's projects; git config --system writes to the system-wide file (requiring root), and git config --local applies only to the particular project. Want me to try git config --local to see if that will do what I need?

Ref https://git-scm.com/docs/git-config

machow commented 6 years ago

Ah, sorry--you're right. I'm not sure why these lines seem to be trying to change the root config...

[91mwarning: unable to access '/root/.gitconfig': Permission denied
warning: unable to access '/root/.config/git/config': Permission denied
error: could not lock config file /root/.gitconfig: Permission denied
warning: unable to access '/root/.gitconfig': Permission denied
warning: unable to access '/root/.config/git/config': Permission denied
error: could not lock config file /root/.gitconfig: Permission denied

It seems like it could have something to do with the $HOME variable when running sudo -u thunk, which I think remains whatever $HOME was prior to the command. See this stack exchange post. I'll message to try to set up a time to pair, so you can run these builds on your laptop.

gvwilson commented 6 years ago

But shouldn't sudo -u thunk take care of that? sigh

On Mon, Sep 11, 2017 at 11:04 AM, Michael Chow notifications@github.com wrote:

Ah, sorry--you're right. I'm not sure why these lines seem to be trying to change the root config...

[91mwarning: unable to access '/root/.gitconfig': Permission denied warning: unable to access '/root/.config/git/config': Permission denied error: could not lock config file /root/.gitconfig: Permission denied �[0m�[91mwarning: unable to access '/root/.gitconfig': Permission denied warning: unable to access '/root/.config/git/config': Permission denied error: could not lock config file /root/.gitconfig: Permission denied

It seems like it could have something to do with the $HOME variable when running sudo -u thunk, which I think remains whatever $HOME was prior to the command. See this stack exchange post https://unix.stackexchange.com/questions/91384/how-is-sudo-set-to-not-change-home-in-ubuntu-and-how-to-disable-this-behavior. I'll message to try to set up a time to pair, so you can run these builds on your laptop.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/datacamp/courses-intro-to-git/issues/8#issuecomment-328558313, or mute the thread https://github.com/notifications/unsubscribe-auth/AA3ozjrvA72EN_MfgqSO2Pk24L5RbyjUks5shUvsgaJpZM4PTCSs .

machow commented 6 years ago

The easiest way to debug putting things in the course image is to...

git clone https://github.com/datacamp/docker-shell-base.git
cd docker-shell-base
docker build -t testshell .
docker run --rm -it testshell /bin/bash

This will open an interactive shell which should be identical to the one used to run requirements.sh, so you can test different commands in it.

I have it running right now, and will look into why it's not running (give me a couple minutes to get back to you).

machow commented 6 years ago

@gvwilson you need the interactive sudo flag, so this works...

sudo -u thunk -i git config --global user.email "thunk@datacamp.com"

I think the issue is the one mentioned in the stack exchange post, that without -i, $HOME remains set to /root. Also, I think when you set git config at the very top of requirements, it's not setting it for repl, but for root, also.

Fingers crossed :)

Edit: just for illustration, here's the command I used to test

# /home/thunk
sudo -u thunk -i echo '$HOME'
gvwilson commented 6 years ago

requirements.sh updated to sudo to repl to set Git config correctly, but is failing :-(

machow commented 6 years ago

you also need the interactive flag there, too, sudo -u repl -i ...

gvwilson commented 6 years ago

Solved by moving code into its own script and running that with sudo.