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

Bug: upload! within an as 'root' block will fail. #329

Open
troelskn opened this issue Feb 9, 2016 · 6 comments
Open

Bug: upload! within an as 'root' block will fail. #329

troelskn opened this issue Feb 9, 2016 · 6 comments

Comments

@troelskn
Copy link

troelskn commented Feb 9, 2016

The following will usually fail:

  task :foo do
    on roles(:web) do
      as "root" do
        upload! "/tmp/foo", "/var/bar/cuux"
      end
    end
  end

Since scp will not su to root. Would you accept a PR that allows upload! to upload to /tmp and then mv the file into place, if the current user has been switched?

@leehambley
Copy link
Member

I'd consider a helper function that wrapped both, but not a change to the upload! api.

@mattbrictson
Copy link
Member

I haven't tested this, but maybe it would work?

on roles(:web) do |host|
  host.user = "root"
  upload! "/tmp/foo", "/var/bar/cuux"
end

@troelskn
Copy link
Author

troelskn commented Feb 9, 2016

@mattbrictson won't work if the root user can't login (Which is default for Ubuntu images an aws anyway).

@mattbrictson
Copy link
Member

Ah, then the helper method you described is probably the best bet.

@troelskn
Copy link
Author

troelskn commented Feb 9, 2016

What if the behaviour required an explicit option to be set. E.g.:

  task :foo do
    on roles(:web) do
      as "root", file_transfer: true do
        upload! "/tmp/foo", "/var/bar/cuux"
      end
    end
  end

Not sure if I disagree with your stance btw. - I'm just thinking out loud here.

@mattbrictson
Copy link
Member

Here's a helper that I use in my own projects. Maybe we could adapt it to be useful for both of our cases and add it to core?

# Uploads the given string or file-like object to the current host
# context. Accepts :owner and :mode options that affect the permissions of the
# remote file.
#
def put(string_or_io, remote_path, opts={})
  sudo_exec = ->(*cmd) {
    cmd = [:sudo] + cmd if opts[:sudo]
    execute *cmd
  }

  tmp_path = "/tmp/#{SecureRandom.uuid}"

  owner = opts[:owner]
  mode = opts[:mode]

  source = if string_or_io.respond_to?(:read)
    string_or_io
  else
    StringIO.new(string_or_io.to_s)
  end

  sudo_exec.call :mkdir, "-p", File.dirname(remote_path)

  upload!(source, tmp_path)

  sudo_exec.call(:mv, "-f", tmp_path, remote_path)
  sudo_exec.call(:chown, owner, remote_path) if owner
  sudo_exec.call(:chmod, mode, remote_path) if mode
end

@leehambley leehambley changed the title upload! within an as 'root' block will fail. Bug: upload! within an as 'root' block will fail. Feb 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants