applicationsonline / librarian

Librarian - A Framework for Bundlers. Librarian-Chef is at: https://github.com/applicationsonline/librarian-chef.
http://applicationsonline.com/
MIT License
655 stars 71 forks source link

Librarian database fails when no USER or HOME env is set #173

Closed owenbendavies closed 8 years ago

owenbendavies commented 10 years ago

Summary

I am trying to run librarian-puppet install on an Amazon EC2 instance with a shell script in the user data.

It is failing because librarian is not picking up the user, this is because when the script is run, neither the USER or HOME environment variables are set.

I was wondering if anyone has any suggestions of how I could make the Environment.default_home method work? I can just add a HOME= to my script, but it would be nice to fix the method.

Versions

librarian (0.1.2)

Stacktrace

`getpwnam': can't convert nil into String (TypeError)
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/environment.rb:206:in `default_home'
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/environment.rb:33:in `block in initialize'
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/environment.rb:33:in `fetch'
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/environment.rb:33:in `initialize'
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/cli.rb:45:in `new'
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/cli.rb:45:in `with_environment'
/var/lib/gems/1.9.1/gems/librarian-0.1.2/lib/librarian/cli.rb:26:in `bin!'
/var/lib/gems/1.9.1/gems/librarian-puppet-1.1.3/bin/librarian-puppet:7:in `<top (required)>'
/usr/local/bin/librarian-puppet:23:in `load'
/usr/local/bin/librarian-puppet:23:in `<main>'

Environment

TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
RUNLEVEL=2
JOB=cloud-config
PWD=/
RESULT=ok
PREVLEVEL=N
SHLVL=1
UPSTART_INSTANCE=
UPSTART_EVENTS=stopped stopped
UPSTART_JOB=cloud-final
INSTANCE=
_=/usr/bin/env
owenbendavies commented 10 years ago

This method was improved with https://github.com/applicationsonline/librarian/pull/147 however the Etc.getlogin relies on USER env.

robinbowes commented 9 years ago

I've just hit exactly the same issue (running librarian in cloudinit on EC2).

Would it work better if you use something like Etc::getpwuid(Process.euid).name, ie. don't assume that someone is logged on?

R.

owenbendavies commented 9 years ago

I had forgotten about this ticket and no longer have this issue. I would guess it's because I am now using sudo in the script, but unfortunately I can't remember. See:

https://github.com/obduk/setup_server/blob/master/bin/install

robinbowes commented 9 years ago

I've worked round it with this code in the script:

[[ -v HOME ]] || export HOME=/root

ie. set (and export - this is important) $HOME if it's not already set.

I'm sure it would be possible to fix this in the librarian code by using the approach I suggested.

R.

optiz0r commented 8 years ago

Working version here: https://github.com/ecwws/librarian/commit/c6cc53e7e435ce9b9c871c7eadcd8ed4928bdad0