Class: AvstCloud::DisableRequireTty

Inherits:
SshTask show all
Defined in:
lib/avst-cloud/task.rb

Overview

In case Requiretty is set in sudoers disable it for bootstrapping and provisioning for user that performs it In case user does not have sudo no pass, enable it for bootstrapping and provisioning

Instance Method Summary collapse

Methods inherited from SshTask

#execute

Methods inherited from Task

#execute

Methods included from Logging

included, logger, #logger, logger=, mask_message, show_passwords=

Constructor Details

#initialize(for_user, pass, enable_passwordless_sudo = false) ⇒ DisableRequireTty

Returns a new instance of DisableRequireTty.



107
108
109
110
111
# File 'lib/avst-cloud/task.rb', line 107

def initialize(for_user, pass, enable_passwordless_sudo=false)
    @for_user = for_user
    @user_password = pass
    @enable_passwordless_sudo = enable_passwordless_sudo
end

Instance Method Details

#ssh_command(session) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/avst-cloud/task.rb', line 112

def ssh_command(session)
    commands = []
    if @enable_passwordless_sudo
        commands << "sudo su -c 'echo \"#{@for_user} ALL=(ALL) NOPASSWD: ALL\" > /etc/sudoers.d/zzz_#{@for_user}'"
        redirect_type = ">>"
    else
        redirect_type = ">"
    end

    commands << "sudo su -c 'echo \"Defaults:#{@for_user} !requiretty\" #{redirect_type} /etc/sudoers.d/zzz_#{@for_user}'"
    
    session.open_channel do |channel|
        channel.request_pty do |ch, success|
            raise 'Error requesting pty' unless success
        end
        channel.exec(commands.join(';')) do |ch, success|
            abort "Could not execute commands!" unless success

            channel.on_data do |ch, data|
                if @debug
                    STDOUT.print  "#{data}"
                end
                channel.send_data "#{@user_password}\n" if data =~ /password/
            end
            channel.on_extended_data do |ch, type, data|
                STDOUT.print "stderr: #{data}"
            end

            channel.on_close do |ch|
                if @debug
                    STDOUT.print "Channel is closing!"
                end
            end
        end
    end
    session.loop
end