Module: CapUtils
- Included in:
- CapUtilsClass
- Defined in:
- lib/buzzcore/cap_utils.rb
Instance Method Summary collapse
- #add_user_to_group(aUser, aGroup) ⇒ Object
-
#adduser(aNewUser, aPassword, aGroup = nil) ⇒ Object
if aGroup is given, that will be the users only group.
- #branch_name_from_svn_url(aURL) ⇒ Object
- #ensure_link(aTo, aFrom, aDir = nil, aUserGroup = nil) ⇒ Object
- #file_exists?(path) ⇒ Boolean
- #get_ip ⇒ Object
-
#preserve_folder(aReleaseFolder, aSharedFolder) ⇒ Object
Used in deployment to maintain folder contents between deployments.
-
#remote_file_exists?(aPath) ⇒ Boolean
check if file exists.
- #remote_ruby(aRubyString) ⇒ Object
- #render_template_file(aTemplateFile, aXmlConfig, aOutputFile, aMoreConfig = nil) ⇒ Object
- #run_for_all(aCommand, aPath, aFilesOrDirs, aPattern = nil, aInvertPattern = false, aSudo = true) ⇒ Object
- #run_local(aString) ⇒ Object
-
#run_prompt(aCommand) ⇒ Object
pass prompt to user, and return their response.
-
#run_respond(aCommand) ⇒ Object
give block with |aText,aStream,aState| that returns response or nil.
- #select_target_file(aFile) ⇒ Object
- #shell(aCommandline, &aBlock) ⇒ Object
- #sudo_run(aString) ⇒ Object
-
#upload_file(aLocalFilePath, aRemoteFilePath, aMode = 0644) ⇒ Object
upload the given local file to the remote server and set the given mode.
- #upload_file_anywhere(aSourceFile, aDestHost, aDestUser, aDestPassword, aDestFile, aDestPort = 22) ⇒ Object
Instance Method Details
#add_user_to_group(aUser, aGroup) ⇒ Object
173 174 175 |
# File 'lib/buzzcore/cap_utils.rb', line 173 def add_user_to_group(aUser,aGroup) run "#{sudo} usermod -a -G #{aGroup} #{aUser}" end |
#adduser(aNewUser, aPassword, aGroup = nil) ⇒ Object
if aGroup is given, that will be the users only group
167 168 169 170 171 |
# File 'lib/buzzcore/cap_utils.rb', line 167 def adduser(aNewUser,aPassword,aGroup=nil) run "#{sudo} adduser --gecos '' #{aGroup ? '--ingroup '+aGroup : ''} #{aNewUser}" do |ch, stream, out| ch.send_data aPassword+"\n" if out =~ /UNIX password:/ end end |
#branch_name_from_svn_url(aURL) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/buzzcore/cap_utils.rb', line 68 def branch_name_from_svn_url(aURL) prot_domain = (aURL.scan(/[^:]+:\/\/[^\/]+\//)).first without_domain = aURL[prot_domain.length..-1] return 'trunk' if without_domain =~ /^trunk\// return (without_domain.scan(/branches\/(.+?)(\/|$)/)).flatten.first end |
#ensure_link(aTo, aFrom, aDir = nil, aUserGroup = nil) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/buzzcore/cap_utils.rb', line 91 def ensure_link(aTo,aFrom,aDir=nil,aUserGroup=nil) cmd = [] cmd << "cd #{aDir}" if aDir cmd << "#{sudo} rm -f #{aFrom}" cmd << "#{sudo} ln -sf #{aTo} #{aFrom}" cmd << "#{sudo} chown -h #{aUserGroup} #{aFrom}" if aUserGroup run cmd.join(' && ') end |
#file_exists?(path) ⇒ Boolean
101 102 103 104 105 106 107 108 |
# File 'lib/buzzcore/cap_utils.rb', line 101 def file_exists?(path) begin run "ls #{path}" return true rescue Exception => e return false end end |
#get_ip ⇒ Object
34 35 36 37 38 |
# File 'lib/buzzcore/cap_utils.rb', line 34 def get_ip run "ifconfig eth0 |grep \"inet addr\"" do |channel,stream,data| return data.scan(/inet addr:([0-9.]+)/).flatten.pop end end |
#preserve_folder(aReleaseFolder, aSharedFolder) ⇒ Object
Used in deployment to maintain folder contents between deployments. Normally the shared path exists and will be linked into the release. If it doesn’t exist and the release path does, it will be moved into the shared path aFolder eg. “vendor/extensions/design” aSharedFolder eg. “design”
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/buzzcore/cap_utils.rb', line 115 def preserve_folder(aReleaseFolder,aSharedFolder) aReleaseFolder = File.join(release_path,aReleaseFolder) aSharedFolder = File.join(shared_path,aSharedFolder) release_exists = file_exists?(aReleaseFolder) shared_exists = file_exists?(aSharedFolder) if shared_exists run "rm -rf #{aReleaseFolder}" if release_exists else run "mv #{aReleaseFolder} #{aSharedFolder}" if release_exists end ensure_link("#{aSharedFolder}","#{aReleaseFolder}",nil,"#{user}:#{apache_user}") end |
#remote_file_exists?(aPath) ⇒ Boolean
check if file exists. Relies on remote ruby
41 42 43 |
# File 'lib/buzzcore/cap_utils.rb', line 41 def remote_file_exists?(aPath) remote_ruby("puts File.exists?('#{aPath}').to_s")=="true\n" end |
#remote_ruby(aRubyString) ⇒ Object
45 46 47 48 49 |
# File 'lib/buzzcore/cap_utils.rb', line 45 def remote_ruby(aRubyString) run 'ruby -e "'+aRubyString+'"' do |channel,stream,data| return data end end |
#render_template_file(aTemplateFile, aXmlConfig, aOutputFile, aMoreConfig = nil) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/buzzcore/cap_utils.rb', line 26 def render_template_file(aTemplateFile,aXmlConfig,aOutputFile,aMoreConfig=nil) template = MiscUtils.string_from_file(aTemplateFile) values = XmlUtils.read_config_values(aXmlConfig) values = values ? values.merge(aMoreConfig || {}) : aMoreConfig result = StringUtils.render_template(template, values) MiscUtils.string_to_file(result, aOutputFile) end |
#run_for_all(aCommand, aPath, aFilesOrDirs, aPattern = nil, aInvertPattern = false, aSudo = true) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/buzzcore/cap_utils.rb', line 146 def run_for_all(aCommand,aPath,aFilesOrDirs,aPattern=nil,aInvertPattern=false,aSudo=true) #run "#{sudo} find . -wholename '*/.svn' -prune -o -type d -print0 |xargs -0 #{sudo} chmod 750" #sudo find . -type f -exec echo {} \; cmd = [] cmd << "sudo" if aSudo cmd << "find #{aPath}" 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 |
#run_local(aString) ⇒ Object
141 142 143 |
# File 'lib/buzzcore/cap_utils.rb', line 141 def run_local(aString) `#{aString}` end |
#run_prompt(aCommand) ⇒ Object
pass prompt to user, and return their response
85 86 87 88 89 |
# File 'lib/buzzcore/cap_utils.rb', line 85 def run_prompt(aCommand) run_respond aCommand do |text,stream,state| Capistrano::CLI.password_prompt(text)+"\n" end end |
#run_respond(aCommand) ⇒ Object
give block with |aText,aStream,aState| that returns response or nil
76 77 78 79 80 81 82 |
# File 'lib/buzzcore/cap_utils.rb', line 76 def run_respond(aCommand) run(aCommand) do |ch,stream,text| ch[:state] ||= { :channel => ch } output = yield(text,stream,ch[:state]) ch.send_data(output) if output end end |
#select_target_file(aFile) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/buzzcore/cap_utils.rb', line 128 def select_target_file(aFile) ext = MiscUtils.file_extension(aFile,false) no_ext = MiscUtils.file_no_extension(aFile,false) dir = File.dirname(aFile) run "#{sudo} mv -f #{no_ext}.#{target}.#{ext} #{aFile}" run "#{sudo} rm -f #{no_ext}.*.#{ext}" end |
#shell(aCommandline, &aBlock) ⇒ Object
136 137 138 139 |
# File 'lib/buzzcore/cap_utils.rb', line 136 def shell(aCommandline,&aBlock) result = block_given? ? POpen4::shell(aCommandline,nil,nil,&aBlock) : POpen4::shell(aCommandline) return result[:stdout] end |
#sudo_run(aString) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/buzzcore/cap_utils.rb', line 51 def sudo_run(aString) # as = fetch(:runner, "app") # via = fetch(:run_method, :sudo) # invoke_command(aString, :via => via, :as => as) # if aUseSudo # run "sudo "+aString # else run aString # end end |
#upload_file(aLocalFilePath, aRemoteFilePath, aMode = 0644) ⇒ Object
upload the given local file to the remote server and set the given mode. 0644 is the default mode
Unix file modes : 4: read 2: write 1: execute 0[group][other]
19 20 21 22 23 24 |
# File 'lib/buzzcore/cap_utils.rb', line 19 def upload_file(aLocalFilePath,aRemoteFilePath,aMode = 0644) puts "* uploading #{aLocalFilePath} to #{aRemoteFilePath}" s = nil File.open(aLocalFilePath, "rb") { |f| s = f.read } put(s,aRemoteFilePath,:mode => aMode) end |
#upload_file_anywhere(aSourceFile, aDestHost, aDestUser, aDestPassword, aDestFile, aDestPort = 22) ⇒ Object
62 63 64 65 66 |
# File 'lib/buzzcore/cap_utils.rb', line 62 def upload_file_anywhere(aSourceFile,aDestHost,aDestUser,aDestPassword,aDestFile,aDestPort=22) Net::SSH.start(aDestHost, aDestUser, {:port => aDestPort, :password => aDestPassword, :verbose =>Logger::DEBUG}) do |ssh| File.open(aSourceFile, "rb") { |f| ssh.sftp.upload!(f, aDestFile) } end end |