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

[MSYS-726] Allow setting environment variables at the user level #6612

Conversation

harikesh-kolekar
Copy link

@harikesh-kolekar harikesh-kolekar commented Dec 1, 2017

Description

The env resource allows you to set a system environment variable, but windows also allow per-user environment variables.

https://blogs.technet.microsoft.com/heyscriptingguy/2005/03/18/how-can-i-create-an-environment-variable-using-a-script/

It looks like we could simply add a user property and pass it in obj.username in the existing provider instead of "".

Acceptance criteria:

  • User property for env resource

Check List

@harikesh-kolekar harikesh-kolekar requested a review from a team December 1, 2017 09:38
@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch 3 times, most recently from edab568 to 42ee06a Compare December 1, 2017 12:26
@coderanger
Copy link
Contributor

This is not part of the abstract interface for "global env vars" so we are probably at (really: long past, but whatevs) the point that this resource needs to be renamed windows_path or this property should be moved into a platform-specific resource :-/ If we want to keep it in the generic one, we should make sure all other providers raise an exception if it is used so it doesn't silently Do The Wrong Thing.

@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch 2 times, most recently from 8506445 to 6d77c3a Compare December 5, 2017 12:09
@btm
Copy link
Contributor

btm commented Dec 5, 2017

this property should be moved into a platform-specific resource :-/ If we want to keep it in the generic one, we should make sure all other providers raise an exception if it is used so it doesn't silently Do The Wrong Thing.

@coderanger What would a 'platform-specific resource' be? Just renaming it windows_env? The resource is currently limited to the windows platform here and thus we do raise exceptions elsewhere, e.g.:

2017-12-05T12:08:08-05:00] FATAL: Chef::Exceptions::NoSuchResourceType: Cannot find a resource for env on mac_os_x version 10.11.6

@coderanger
Copy link
Contributor

Yes, windows_env would be a better name if this is going to be a specific model for Windows environment variables.

@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch 3 times, most recently from 41f013a to e31e3c3 Compare December 11, 2017 10:11
@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch from e31e3c3 to 696746f Compare December 13, 2017 09:12
Copy link
Contributor

@btm btm left a comment

Choose a reason for hiding this comment

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

thanks @harikesh-kolekar

@tas50, did you look into deprecating env => windows_env ? We should probably do that as a separate change anyway since I think it'll take some time for @harikesh-kolekar to understand how we're doing deprecations and talk about foodcritic rules.

@btm btm requested review from thommay and tas50 December 13, 2017 14:05
@harikesh-kolekar harikesh-kolekar changed the title [MSYS-726][In-Progress] Allow setting environment variables at the user level [MSYS-726] Allow setting environment variables at the user level Dec 13, 2017
test_resource.value(env_value1)
test_resource.run_action(:create)
env_obj
expect(@env_obj.username.upcase).to eq(default_env_user)

Choose a reason for hiding this comment

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

Can we assign the return value of env_obj to some variable instead of using the instance variable @env_obj?

return @env_obj if @env_obj.username.split('\\').last.casecmp(test_resource.user) == 0
end
end
@env_obj = nil
Copy link

@NimishaS NimishaS Dec 21, 2017

Choose a reason for hiding this comment

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

Please return a value from this, instead of setting the instance variable. Probably use a method if required.

@@ -55,6 +79,25 @@
expect(ENV[chef_env_test_lower_case]).to eq(env_value1)
end

it "should create an environment variable with default user System for action create" do

Choose a reason for hiding this comment

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

It would be good to have a description "<#create_method_name>" block and club all the test cases related to create method in it.
We can avoid appending for action create in each spec.

@@ -65,6 +108,20 @@
expect(ENV[chef_env_test_lower_case]).to eq(env_value2)
end

it "should not modify an existing variable's value to a new value if the user are diff" do

Choose a reason for hiding this comment

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

"should not modify an existing variable's value to a new value if the users are different"

test_resource.key_name(chef_env_test_lower_case)
test_resource.user(default_env_user)
env_obj
expect(@env_obj.variablevalue).to eq(env_value1)

Choose a reason for hiding this comment

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

We can probably use a before block here for creating the existing variable.

expect(@env_obj).to eq(nil)
end

it "should not delete an user environment variable if user are not passed" do

Choose a reason for hiding this comment

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

should not delete an user environment variable if user is not passed.

test_resource.run_action(:delete)
test_resource.user(env_user)
env_obj
expect(@env_obj).not_to be_nil

Choose a reason for hiding this comment

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

Please use before block or let for creating the existing environment variable.

it "should set the key_name to the key name of the new resource" do
@provider.load_current_resource
expect(@provider.current_resource.key_name).to eq("FOO")
end

it "should check if the key_name exists" do
it "should check if the key_name and user exists" do

Choose a reason for hiding this comment

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

"should check if the key_name and user exist"

@@ -152,14 +158,20 @@
@provider.action_modify
end

it "should call modify_group if the key exists and user are not equal" do

Choose a reason for hiding this comment

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

"and users are not equal"

@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch 2 times, most recently from debdb22 to dbfa838 Compare December 22, 2017 13:18
@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch from dbfa838 to 10577c9 Compare January 8, 2018 12:10
@@ -35,7 +35,7 @@
require "chef/resource/dnf_package"
require "chef/resource/dsc_script"
require "chef/resource/dsc_resource"
require "chef/resource/env"
require "chef/resource/windows_env"
Copy link
Contributor

Choose a reason for hiding this comment

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

this should go in the right place alphabetically.

provides :env, os: "windows"
class WindowsEnv < Chef::Resource
resource_name :windows_env
provides :windows_env, os: "windows"
Copy link
Contributor

Choose a reason for hiding this comment

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

We want to make sure to also include provides :env here so that any windows users with previously cookbooks will continue to work.

@lamont-granquist
Copy link
Contributor

after some poking around i am satisfied that we never had a unix env resource before. on unix previously it blew up with NoSuchResourceType because the resource was windows-only, then the provider that was wired up on unix was a stub that raised notimplementederror. so busted all around, nobody could have ever used it.

want the back compat though so people on windows that were using 'env' can continue using 'env'.

@thommay
Copy link
Contributor

thommay commented Jan 22, 2018

Right, so this is going in the right direction, and clearly on non-Windows this has raised out of the box so there's no concern about deprecation there.
What we should do:

the resource should add a provides :env os: "windows".
There's no real need for the superclass any more, so just merge providers/env/windows.rb into providers/env.rb and then rename that to providers/windows_env.rb.
Delete the providers/env/ directory.

@@ -0,0 +1,285 @@
#
Copy link

Choose a reason for hiding this comment

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

I recommend doing a git mv here and then make the appropriate changes.

Copy link
Author

Choose a reason for hiding this comment

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

I have tried by git mv but still, in git file changes it shows file has been deleted you can see commit at 85ad3ee#diff-5903ffd7bafff40eae05e9c21380c6a0

@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch from caf9c6e to 4401519 Compare January 29, 2018 12:11
@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch from 4401519 to 0b10937 Compare January 29, 2018 12:56
attr_accessor :key_exists

provides :env, os: "!windows"
provides :windows_env, os: "windows"

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add

provides :env, os: "windows"

here too please?

Copy link
Contributor

@thommay thommay left a comment

Choose a reason for hiding this comment

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

We're pretty close here thank you!

@@ -28,7 +28,7 @@
require "chef/provider/directory"
require "chef/provider/dsc_script"
require "chef/provider/dsc_resource"
require "chef/provider/env"
require "chef/provider/windows_env"
Copy link
Contributor

Choose a reason for hiding this comment

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

this file should be sorted alphabetically.

harikesh-kolekar added 6 commits January 30, 2018 07:31
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch 5 times, most recently from ba8e126 to fcbc5a2 Compare January 31, 2018 10:31
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
@harikesh-kolekar harikesh-kolekar force-pushed the harry/allow_setting_environment_variables_at_the_user branch from fcbc5a2 to ed4b10b Compare January 31, 2018 11:26
@lamont-granquist
Copy link
Contributor

@thommay merge if you think its good

@thommay thommay merged commit 2d38372 into chef:master Feb 12, 2018
@lock
Copy link

lock bot commented Apr 14, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked and limited conversation to collaborators Apr 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants