6
6
require 'puppet/util/execution'
7
7
require 'puppet/util/suidmanager'
8
8
require 'open3'
9
+ require 'pty'
10
+ require 'expect'
11
+ require 'fileutils'
9
12
10
13
# Implementation for the mysql_login_path type using the Resource API.
11
14
class Puppet ::Provider ::MysqlLoginPath ::MysqlLoginPath < Puppet ::ResourceApi ::SimpleProvider
@@ -17,6 +20,7 @@ def get_homedir(_context, uid)
17
20
def mysql_config_editor_set_cmd ( context , uid , password = nil , *args )
18
21
args . unshift ( '/usr/bin/mysql_config_editor' )
19
22
homedir = get_homedir ( context , uid )
23
+ login_file_path = "#{ homedir } /.mylogin.cnf"
20
24
21
25
if args . is_a? ( Array )
22
26
command = args . flatten . map ( &:to_s )
@@ -25,28 +29,27 @@ def mysql_config_editor_set_cmd(context, uid, password = nil, *args)
25
29
command_str = command
26
30
end
27
31
28
- Puppet ::Util ::SUIDManager . asuser ( uid ) do
29
- @exit_status = Open3 . popen3 ( { 'HOME' => homedir } , command_str ) do |stdin , stdout , stderr , wait_thr |
32
+ begin
33
+ Puppet ::Util ::SUIDManager . asuser ( uid ) do
34
+ FileUtils . touch login_file_path
35
+ FileUtils . chmod 0o600 , login_file_path
36
+ end
37
+
38
+ PTY . spawn ( { 'HOME' => homedir } , command_str ) do |input , output , _pid |
30
39
if password
31
- stdin . puts ( password + " \r \n " )
32
- stdin . close
40
+ input . expect ( %r{Enter password:} )
41
+ output . puts password
33
42
end
34
- @captured_stdout = stdout . read
35
- @captured_stderr = stderr . read
36
- wait_thr . value
37
43
end
38
- end
39
-
40
- if @exit_status . success? == false
44
+ rescue => e
41
45
raise Puppet ::ExecutionFailure , _ (
42
46
"Execution of '%{str}' returned %{exit_status}: %{output}" ,
43
47
) % {
44
48
str : command_str ,
45
- exit_status : @exit_status ,
46
- output : @captured_stderr . strip ,
49
+ exit_status : $? . exitstatus ,
50
+ output : e . message ,
47
51
}
48
52
end
49
- @captured_stdout
50
53
end
51
54
52
55
def mysql_config_editor_cmd ( context , uid , *args )
0 commit comments