-
Notifications
You must be signed in to change notification settings - Fork 90
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
Login shell #149
Login shell #149
Changes from all commits
db17269
5058023
b4213f2
388ebe3
0af1653
175c14c
57a47b5
5f5211d
b8859a1
8e9eb42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ def run(_command) | |
class LinuxCommand < CommandWrapperBase | ||
Train::Options.attach(self) | ||
|
||
option :shell, default: false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its a little bit sub-optimal to switch types of a variable, I would prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also lets add a comment to give users an idea of potential values |
||
option :shell_options, default: nil | ||
option :shell_command, default: nil | ||
option :sudo, default: false | ||
option :sudo_options, default: nil | ||
option :sudo_password, default: nil | ||
|
@@ -38,12 +41,14 @@ def initialize(backend, options) | |
@backend = backend | ||
validate_options(options) | ||
|
||
@shell = options[:shell] | ||
@shell_options = options[:shell_options] # e.g. '--login' | ||
@shell_command = options[:shell_command] # e.g. '/bin/sh' | ||
@sudo = options[:sudo] | ||
@sudo_options = options[:sudo_options] | ||
@sudo_password = options[:sudo_password] | ||
@sudo_command = options[:sudo_command] | ||
@user = options[:user] | ||
@prefix = build_prefix | ||
end | ||
|
||
# (see CommandWrapperBase::verify) | ||
|
@@ -71,29 +76,48 @@ def verify | |
|
||
# (see CommandWrapperBase::run) | ||
def run(command) | ||
@prefix + command | ||
shell_wrap(sudo_wrap(command)) | ||
end | ||
|
||
def self.active?(options) | ||
options.is_a?(Hash) && options[:sudo] | ||
options.is_a?(Hash) && ( | ||
options[:sudo] || | ||
options[:shell] | ||
) | ||
end | ||
|
||
private | ||
|
||
def build_prefix | ||
return '' unless @sudo | ||
return '' if @user == 'root' | ||
# wrap the cmd in a sudo command | ||
def sudo_wrap(cmd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets add comments what these function do |
||
return cmd unless @sudo | ||
return cmd if @user == 'root' | ||
|
||
res = (@sudo_command || 'sudo') + ' ' | ||
|
||
unless @sudo_password.nil? | ||
b64pw = Base64.strict_encode64(@sudo_password + "\n") | ||
res = "echo #{b64pw} | base64 --decode | #{res}-S " | ||
end | ||
res = "#{safe_string(@sudo_password + "\n")} | #{res}-S " unless @sudo_password.nil? | ||
|
||
res << @sudo_options.to_s + ' ' unless @sudo_options.nil? | ||
|
||
res | ||
res + cmd | ||
end | ||
|
||
# wrap the cmd in a subshell allowing for options to | ||
# passed to the subshell | ||
def shell_wrap(cmd) | ||
return cmd unless @shell | ||
|
||
shell = @shell_command || '$SHELL' | ||
options = ' ' + @shell_options.to_s unless @shell_options.nil? | ||
|
||
"#{safe_string(cmd)} | #{shell}#{options}" | ||
end | ||
|
||
# encapsulates encoding the string into a safe form, and decoding for use. | ||
# @return [String] A command line snippet that can be used as part of a pipeline. | ||
def safe_string(str) | ||
b64str = Base64.strict_encode64(str) | ||
"echo #{b64str} | base64 --decode" | ||
end | ||
end | ||
|
||
|
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.
Why are we changing the default setting?
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 think it's because we don't yet have frozen string literal comments in our files:
http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/FrozenStringLiteralComment
I think it's ok to disable for now and let's tackle this in a rubocop cleanup session 😄