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
[MSYS-726] Allow setting environment variables at the user level #6612
Conversation
edab568
to
42ee06a
Compare
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 |
8506445
to
6d77c3a
Compare
@coderanger What would a 'platform-specific resource' be? Just renaming it
|
Yes, |
41f013a
to
e31e3c3
Compare
e31e3c3
to
696746f
Compare
There was a problem hiding this 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.
spec/functional/resource/env_spec.rb
Outdated
test_resource.value(env_value1) | ||
test_resource.run_action(:create) | ||
env_obj | ||
expect(@env_obj.username.upcase).to eq(default_env_user) |
There was a problem hiding this comment.
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
?
spec/functional/resource/env_spec.rb
Outdated
return @env_obj if @env_obj.username.split('\\').last.casecmp(test_resource.user) == 0 | ||
end | ||
end | ||
@env_obj = nil |
There was a problem hiding this comment.
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.
spec/functional/resource/env_spec.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
spec/functional/resource/env_spec.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
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"
spec/functional/resource/env_spec.rb
Outdated
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) |
There was a problem hiding this comment.
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.
spec/functional/resource/env_spec.rb
Outdated
expect(@env_obj).to eq(nil) | ||
end | ||
|
||
it "should not delete an user environment variable if user are not passed" do |
There was a problem hiding this comment.
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.
spec/functional/resource/env_spec.rb
Outdated
test_resource.run_action(:delete) | ||
test_resource.user(env_user) | ||
env_obj | ||
expect(@env_obj).not_to be_nil |
There was a problem hiding this comment.
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.
spec/unit/provider/env_spec.rb
Outdated
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 |
There was a problem hiding this comment.
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
"
spec/unit/provider/env_spec.rb
Outdated
@@ -152,14 +158,20 @@ | |||
@provider.action_modify | |||
end | |||
|
|||
it "should call modify_group if the key exists and user are not equal" do |
There was a problem hiding this comment.
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"
debdb22
to
dbfa838
Compare
dbfa838
to
10577c9
Compare
lib/chef/resources.rb
Outdated
@@ -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" |
There was a problem hiding this comment.
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.
lib/chef/resource/windows_env.rb
Outdated
provides :env, os: "windows" | ||
class WindowsEnv < Chef::Resource | ||
resource_name :windows_env | ||
provides :windows_env, os: "windows" |
There was a problem hiding this comment.
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.
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'. |
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. the resource should add a |
@@ -0,0 +1,285 @@ | |||
# |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
caf9c6e
to
4401519
Compare
4401519
to
0b10937
Compare
lib/chef/provider/windows_env.rb
Outdated
attr_accessor :key_exists | ||
|
||
provides :env, os: "!windows" | ||
provides :windows_env, os: "windows" | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this 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!
lib/chef/providers.rb
Outdated
@@ -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" |
There was a problem hiding this comment.
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.
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>
ba8e126
to
fcbc5a2
Compare
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
fcbc5a2
to
ed4b10b
Compare
@thommay merge if you think its good |
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. |
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:
Check List