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

Fix default install as non-root user, #305 #363

Closed
wants to merge 3 commits into from

Conversation

fnoop
Copy link
Contributor

@fnoop fnoop commented May 2, 2018

npm.pp currently sets /root as the environment HOMEDIR. This breaks using nodejs::npm as a non-root user, as per #305. nodejs::npm::home_dir parameter can be set to override this, but is not intuitive, documented or automatic.

Instead, unset $home_dir parameter by default and only use it to set HOMEDIR environment variable if populated. If not set, npm will use the default home directory for the specified user.

@fnoop
Copy link
Contributor Author

fnoop commented May 2, 2018

Hmm, well travis fails because of:
manifests/npm.pp:11:double_quoted_strings:WARNING:double quoted string containing no variables

However if you set default to undef ( String $home_dir = undef,) then you get a syntax error:

Error: Evaluation Error: Error while evaluating a Resource Statement, Nodejs::Npm[vue install as user mav]: parameter 'home_dir' expects a String value, got Undef (file: /srv/maverick/software/maverick/manifests/maverick-modules/maverick_network/manifests/init.pp, line: 16) on node dom-ubuntu.home

The obvious fix is to set $home_dir parameter without a type:
$home_dir = undef
But this doesn't follow good practice of setting parameter types..

@fnoop
Copy link
Contributor Author

fnoop commented May 2, 2018

This is now failing spec tests, which just completely baffle me:

  1) nodejs::npm on centos-6-x86_64  with package set to express and target set to /home/npm/packages the environment variable HOME should be /root
     Failure/Error: is_expected.to contain_exec('npm_install_express').with('environment' => 'HOME=/root')
       expected that the catalogue would contain Exec[npm_install_express] with environment set to "HOME=/root" but it is set to nil
     # ./spec/defines/nodejs_npm_spec.rb:57:in `block (5 levels) in <top (required)>'

@fnoop
Copy link
Contributor Author

fnoop commented May 2, 2018

OK that failing check makes an incorrect assumption, removed

@fnoop
Copy link
Contributor Author

fnoop commented May 2, 2018

Ooh, a green tick from Travis :)

@ekohl
Copy link
Member

ekohl commented May 2, 2018

Does this work? AFAIK HOME is not set in exec so does npm resolve this itself?

@fnoop
Copy link
Contributor Author

fnoop commented May 2, 2018

@ekohl Yes, works fine.

@juniorsysadmin
Copy link
Member

juniorsysadmin commented May 3, 2018

Hmm, can we go a bit further and add environment as a parameter since there may be other environment variables that one may wish to pass onto npm? (See https://docs.npmjs.com/misc/config#environment-variables)

Perhaps something like:

Parameters:
$home_dir => undef,
$environment => undef,

if !environment and !home_dir {
  $real_environment = "HOME=${home_dir}"
} else {
  $real_environment = $environment

@juniorsysadmin juniorsysadmin requested a review from igalic May 3, 2018 01:58
@juniorsysadmin juniorsysadmin added the enhancement New feature or request label May 3, 2018
@fnoop
Copy link
Contributor Author

fnoop commented May 3, 2018

@juniorsysadmin Not convinced that it's a good idea to encourage passing/using environment variables for npm config. Better to write manifests or use .npmrc?:
https://docs.npmjs.com/misc/config#npmrc-files

Copy link
Contributor

@igalic igalic left a comment

Choose a reason for hiding this comment

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

looks good!
here's some of my opinions…

@@ -8,7 +8,7 @@
String $package = $title,
$source = 'registry',
Array $uninstall_options = [],
$home_dir = '/root',
$home_dir = undef,
Copy link
Contributor

Choose a reason for hiding this comment

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

while we're at it, we could just as well give home_dir an Optional[Stdlib::AbsPath] type.

$_homedir = "HOME=${home_dir}"
} else {
$_homedir = undef
}
Copy link
Contributor

Choose a reason for hiding this comment

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

i think a selector is easier to read?

$_homedir = $home_dir? {
  undef => undef,
  default => "HOME=${home_dir}",
}


it 'the environment variable HOME should be /root' do
is_expected.to contain_exec('npm_install_express').with('environment' => 'HOME=/root')
end
Copy link
Member

Choose a reason for hiding this comment

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

Instead of removing the test, why not modify it ?

@Dan33l
Copy link
Member

Dan33l commented Oct 12, 2018

Hi @fnoop do need some help to move forward about this PR ?

@@ -8,7 +8,7 @@
String $package = $title,
$source = 'registry',
Array $uninstall_options = [],
$home_dir = '/root',
$home_dir = undef,
Copy link
Member

Choose a reason for hiding this comment

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

This changes the default behavior of the module and would likely need a major version bump.

@fnoop fnoop closed this Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants