Module: Rigup::Utils::Run

Included in:
Cli, DeployBase, GitRepo
Defined in:
lib/rigup/utils/run.rb

Instance Method Summary collapse

Instance Method Details

#bashObject



5
6
7
# File 'lib/rigup/utils/run.rb', line 5

def bash
	@bash ||= ::Session::Bash.new
end

#cd(aPath, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rigup/utils/run.rb', line 13

def cd(aPath,&block)
	if block_given?
		begin
	    before_path = pwd
			cd(aPath)
			yield aPath,before_path
		rescue Exception => e
			logger.info e.message
		ensure
			cd(before_path)
		end
	else
		aPath = ::File.expand_path(aPath)
		Dir.chdir(aPath)
		bash.execute("cd \"#{aPath}\"")
	end
	aPath
end

#pwdObject



9
10
11
# File 'lib/rigup/utils/run.rb', line 9

def pwd
	bash.execute("pwd", {:stdout => nil}).first.strip
end

#run(aCommand, aOptions = nil) ⇒ Object

execute the given command in the bash session



33
34
35
36
37
38
39
40
41
# File 'lib/rigup/utils/run.rb', line 33

def run(aCommand,aOptions=nil)
	aOptions ||= {}
	logger.debug aCommand
	response,errout = bash.execute(aCommand,:stdout => STDOUT)  #   ::POpen4::shell(aCommand,aDir || @context.pwd)
	logger.debug errout if errout.to_nil
	logger.debug response if response.to_nil
	raise "Command Failed" unless bash.exit_status==0 or aOptions[:raise]==false
	return response
end

#run_for_all(aCommand, aPath, aFilesOrDirs, aPattern = nil, aInvertPattern = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rigup/utils/run.rb', line 70

def run_for_all(aCommand,aPath,aFilesOrDirs,aPattern=nil,aInvertPattern=false)
	#run "#{sudo} find . -wholename '*/.svn' -prune -o -type d -print0 |xargs -0 #{sudo} chmod 750"
	#sudo find . -type f -exec echo {} \;
	cmd = []
	cmd << sudo_s
	cmd << "find #{aPath.ensure_suffix('/')}"
	cmd << "-wholename '#{aPattern}'" if aPattern
	cmd << "-prune -o" if aInvertPattern
	cmd << case aFilesOrDirs.to_s[0,1]
		when 'f' then '-type f'
		when 'd' then '-type d'
		else ''
	end
	cmd << "-exec"
	cmd << aCommand
	cmd << "'{}' \\;"
	cmd = cmd.join(' ')
	run cmd
end

#sudo(aCommand, aOptions = nil) ⇒ Object

execute the command using sudo. Raises an exception if sudo is not available



51
52
53
54
# File 'lib/rigup/utils/run.rb', line 51

def sudo(aCommand,aOptions=nil)
	raise "sudo not available" unless sudo_available?
	run('sudo '+aCommand,aOptions)
end

#sudo?(aCommand, aOptions = nil) ⇒ Boolean

uses sudo if available, otherwise plain run

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/rigup/utils/run.rb', line 57

def sudo?(aCommand,aOptions=nil)
	if sudo_available?
		sudo(aCommand,aOptions)
	else
		run(aCommand,aOptions)
	end
end

#sudo_available?Boolean

returns true if sudo is available for the current user, otherwise false. Default = true

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/rigup/utils/run.rb', line 44

def sudo_available?
	return !!config[:sudo_available] if config.has_key?(:sudo_available)
	return false if !config[:sudo].to_nil   # false if old config item blank
	true
end

#sudo_sObject

for embedding into command line strings



66
67
68
# File 'lib/rigup/utils/run.rb', line 66

def sudo_s
	sudo_available? ? 'sudo ' : ''
end