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