Module: CapUtils
- Included in:
- CapUtilsClass
- Defined in:
- lib/buzzville/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_folder(aPath, aOwner = nil, aGroup = nil, aMode = nil, aSudo = nil) ⇒ Object
- #ensure_link(aTo, aFrom, aDir = nil, aUserGroup = nil, aSudo = '') ⇒ Object
- #ensure_link_cmd(aTo, aFrom, aDir = nil, aUserGroup = nil, aSudo = nil) ⇒ Object
- #file_exists?(path) ⇒ Boolean
- #force_copy_mode_cmd(aFrom, aTo, aChmod = nil) ⇒ Object
- #get_ip ⇒ Object
- #install_script_from_string(aScriptString, aFilepath) ⇒ Object
- #make_public_cache_dir(aStartPath) ⇒ Object
- #mkdir_permissions(aStartPath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false, aSudo = true) ⇒ Object
-
#permissions_for_deploy(aUser = nil, aGroup = nil, aPath = nil) ⇒ Object
just quickly ensures user can deploy.
-
#permissions_for_web(aPath = nil, aUser = nil, aApacheUser = nil, aHideScm = nil) ⇒ Object
set standard permissions for web sites - readonly for apache user.
-
#permissions_for_web_writable(aPath) ⇒ Object
run this after permissions_for_web() on dirs that need to be writable by group (apache).
-
#preserve_folder(aReleaseFolder, aSharedFolder) ⇒ Object
Used in deployment to maintain folder contents between deployments.
-
#project_name_from_git_commit_url(aGitUrl) ⇒ Object
returns ‘buzzware-logikal’ from ‘[email protected]:buzzware/logikal.git’.
-
#project_name_from_git_public_url(aGitUrl) ⇒ Object
returns from git://github.com/buzzware/spree.git.
-
#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, aExtendedExtension = false) ⇒ Object
- #set_dir_permissions_r(aStartPath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false) ⇒ Object
- #set_permissions(aFilepath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false, aSudo = true) ⇒ Object
- #set_permissions_cmd(aFilepath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false, aSudo = true) ⇒ Object
-
#setup_designer_filesystem_cmd(aBrowserCmsRoot, aSharedDesignPath, aFtpPath, aUser, aApacheUser) ⇒ Object
This means : * designers can ftp in to the server and upload/edit templates and partials * templates are Rails-style eg.
- #shell(aCommandline, &aBlock) ⇒ Object
- #sudo_run(aString) ⇒ Object
- #svn_command(aCommand) ⇒ Object
- #update_remote_git_cache(aGitUrl, aBranchOrTag = nil) ⇒ 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
257 258 259 |
# File 'lib/buzzville/cap_utils.rb', line 257 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
251 252 253 254 255 |
# File 'lib/buzzville/cap_utils.rb', line 251 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
80 81 82 83 84 85 |
# File 'lib/buzzville/cap_utils.rb', line 80 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_folder(aPath, aOwner = nil, aGroup = nil, aMode = nil, aSudo = nil) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/buzzville/cap_utils.rb', line 309 def ensure_folder(aPath,aOwner=nil,aGroup=nil,aMode=nil,aSudo=nil) aSudo ||= '' cmd = [] cmd << "#{aSudo} mkdir -p #{aPath}" if aOwner || aGroup cmd << "#{aSudo} chown #{aOwner} #{aPath}" if !aGroup cmd << "#{aSudo} chgrp #{aGroup} #{aPath}" if !aOwner cmd << "#{aSudo} chown #{aOwner}:#{aGroup} #{aPath}" if aOwner && aGroup end cmd << "chmod #{aMode} #{aPath}" if aMode cmd.join(' && ') end |
#ensure_link(aTo, aFrom, aDir = nil, aUserGroup = nil, aSudo = '') ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/buzzville/cap_utils.rb', line 103 def ensure_link(aTo,aFrom,aDir=nil,aUserGroup=nil,aSudo='') cmd = [] cmd << "cd #{aDir}" if aDir cmd << "#{sudo} rm -rf #{aFrom}" cmd << "#{sudo} ln -sf #{aTo} #{aFrom}" cmd << "#{sudo} chown -h #{aUserGroup} #{aFrom}" if aUserGroup run cmd.join(' && ') end |
#ensure_link_cmd(aTo, aFrom, aDir = nil, aUserGroup = nil, aSudo = nil) ⇒ Object
299 300 301 302 303 304 305 306 307 |
# File 'lib/buzzville/cap_utils.rb', line 299 def ensure_link_cmd(aTo,aFrom,aDir=nil,aUserGroup=nil,aSudo=nil) aSudo ||= '' cmd = [] cmd << "cd #{aDir}" if aDir cmd << "#{aSudo} rm -rf #{aFrom}" cmd << "#{aSudo} ln -sf #{aTo} #{aFrom}" cmd << "#{aSudo} chown -h #{aUserGroup} #{aFrom}" if aUserGroup cmd.join(' && ') end |
#file_exists?(path) ⇒ Boolean
113 114 115 116 117 118 119 |
# File 'lib/buzzville/cap_utils.rb', line 113 def file_exists?(path) result = nil run "if [[ -e #{path} ]]; then echo 'true'; else echo 'false'; fi", :shell => false do |ch,stream,text| result = (text.strip == 'true') end result end |
#force_copy_mode_cmd(aFrom, aTo, aChmod = nil) ⇒ Object
291 292 293 294 295 296 297 |
# File 'lib/buzzville/cap_utils.rb', line 291 def force_copy_mode_cmd(aFrom,aTo,aChmod=nil) cmd = [] cmd << "rm -rf #{aTo}" cmd << "cp -f #{aFrom} #{aTo}" cmd << "chmod #{aChmod.to_s} #{aTo}" if aChmod cmd.join(' && ') end |
#get_ip ⇒ Object
46 47 48 49 50 |
# File 'lib/buzzville/cap_utils.rb', line 46 def get_ip run "ifconfig eth0 |grep \"inet addr\"" do |channel,stream,data| return data.scan(/inet addr:([0-9.]+)/).flatten.pop end end |
#install_script_from_string(aScriptString, aFilepath) ⇒ Object
360 361 362 363 364 365 |
# File 'lib/buzzville/cap_utils.rb', line 360 def install_script_from_string(aScriptString,aFilepath) temp_path = File.join(deploy_to,File.basename(aFilepath)) put(aScriptString,temp_path) run "#{sudo} mv #{temp_path} #{aFilepath}" (aFilepath,'root',user,750,false,true) end |
#make_public_cache_dir(aStartPath) ⇒ Object
244 245 246 247 248 |
# File 'lib/buzzville/cap_utils.rb', line 244 def make_public_cache_dir(aStartPath) run "#{sudo} mkdir -p #{aStartPath}" (aStartPath) (aStartPath) end |
#mkdir_permissions(aStartPath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false, aSudo = true) ⇒ Object
239 240 241 242 |
# File 'lib/buzzville/cap_utils.rb', line 239 def (aStartPath,aUser=nil,aGroup=nil,aMode=nil,aSetGroupId=false,aSudo=true) run "#{sudo} mkdir -p #{aStartPath}" (aStartPath,aUser,aGroup,aMode,aSetGroupId,aSudo) end |
#permissions_for_deploy(aUser = nil, aGroup = nil, aPath = nil) ⇒ Object
just quickly ensures user can deploy. Only for use before deploying, and should be followed by more secure settings
179 180 181 182 183 184 185 186 |
# File 'lib/buzzville/cap_utils.rb', line 179 def (aUser=nil,aGroup=nil,aPath=nil) aUser ||= user aGroup ||= apache_user aPath ||= deploy_to run "#{sudo} chown -R #{aUser}:#{aGroup} #{MiscUtils.append_slash(aPath)}" run "#{sudo} chmod u+rw #{MiscUtils.append_slash(aPath)}" run_for_all("chmod u+x",aPath,:dirs) end |
#permissions_for_web(aPath = nil, aUser = nil, aApacheUser = nil, aHideScm = nil) ⇒ Object
set standard permissions for web sites - readonly for apache user
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/buzzville/cap_utils.rb', line 189 def (aPath=nil,aUser=nil,aApacheUser=nil,aHideScm=nil) aPath ||= deploy_to aUser ||= user aApacheUser ||= apache_user run "#{sudo} chown -R #{aUser}:#{aApacheUser} #{MiscUtils.append_slash(aPath)}" run "#{sudo} chmod -R 644 #{MiscUtils.append_slash(aPath)}" run_for_all("chmod +x",aPath,:dirs) run_for_all("chmod g+s",aPath,:dirs) case aHideScm when :svn then run_for_all("chown -R #{aUser}:#{aUser}",aPath,:dirs,"*/.svn") end end |
#permissions_for_web_writable(aPath) ⇒ Object
run this after permissions_for_web() on dirs that need to be writable by group (apache)
204 205 206 |
# File 'lib/buzzville/cap_utils.rb', line 204 def (aPath) run "#{sudo} chmod -R g+w #{MiscUtils.append_slash(aPath)}" 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”
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/buzzville/cap_utils.rb', line 126 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 |
#project_name_from_git_commit_url(aGitUrl) ⇒ Object
returns ‘buzzware-logikal’ from ‘[email protected]:buzzware/logikal.git’
262 263 264 265 |
# File 'lib/buzzville/cap_utils.rb', line 262 def project_name_from_git_commit_url(aGitUrl) parts = aGitUrl.split(/[@:\/]/) # eg ["git", "git.assembla.com", "buzzware", "logikal.git"] return File.basename(parts[2..-1].join('-'),'.git') end |
#project_name_from_git_public_url(aGitUrl) ⇒ Object
returns from git://github.com/buzzware/spree.git
268 269 270 271 272 273 |
# File 'lib/buzzville/cap_utils.rb', line 268 def project_name_from_git_public_url(aGitUrl) prot,url = aGitUrl.split(/\:\/\//) url_parts = url.split('/') host = url_parts.shift File.basename(url_parts.join('-'),'.git') end |
#remote_file_exists?(aPath) ⇒ Boolean
check if file exists. Relies on remote ruby
53 54 55 |
# File 'lib/buzzville/cap_utils.rb', line 53 def remote_file_exists?(aPath) remote_ruby("puts File.exists?('#{aPath}').to_s")=="true\n" end |
#remote_ruby(aRubyString) ⇒ Object
57 58 59 60 61 |
# File 'lib/buzzville/cap_utils.rb', line 57 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
38 39 40 41 42 43 44 |
# File 'lib/buzzville/cap_utils.rb', line 38 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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/buzzville/cap_utils.rb', line 157 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 #{MiscUtils.append_slash(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
152 153 154 |
# File 'lib/buzzville/cap_utils.rb', line 152 def run_local(aString) `#{aString}` end |
#run_prompt(aCommand) ⇒ Object
pass prompt to user, and return their response
97 98 99 100 101 |
# File 'lib/buzzville/cap_utils.rb', line 97 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
88 89 90 91 92 93 94 |
# File 'lib/buzzville/cap_utils.rb', line 88 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, aExtendedExtension = false) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/buzzville/cap_utils.rb', line 139 def select_target_file(aFile,aExtendedExtension=false) ext = MiscUtils.file_extension(aFile,aExtendedExtension) no_ext = MiscUtils.file_no_extension(aFile,aExtendedExtension) dir = File.dirname(aFile) run "#{sudo} mv -f #{no_ext}.#{target}.#{ext} #{aFile}" run "#{sudo} rm -f #{no_ext}.*.#{ext}" end |
#set_dir_permissions_r(aStartPath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/buzzville/cap_utils.rb', line 208 def (aStartPath,aUser=nil,aGroup=nil,aMode=nil,aSetGroupId=false) cmd = [] if aGroup cmd << (aUser ? "chown #{aUser}:#{aGroup}" : "chgrp #{aGroup}") else cmd << "chown #{aUser}" if aUser end cmd << "chmod #{aMode.to_s}" if aMode cmd << "chmod g+s" if aSetGroupId cmd.each do |c| run_for_all(c,aStartPath,:dirs) end end |
#set_permissions(aFilepath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false, aSudo = true) ⇒ Object
234 235 236 237 |
# File 'lib/buzzville/cap_utils.rb', line 234 def (aFilepath,aUser=nil,aGroup=nil,aMode=nil,aSetGroupId=false,aSudo=true) cmd = (aFilepath,aUser,aGroup,aMode,aSetGroupId,aSudo) run cmd end |
#set_permissions_cmd(aFilepath, aUser = nil, aGroup = nil, aMode = nil, aSetGroupId = false, aSudo = true) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/buzzville/cap_utils.rb', line 222 def (aFilepath,aUser=nil,aGroup=nil,aMode=nil,aSetGroupId=false,aSudo=true) cmd = [] if aGroup cmd << (aUser ? "#{aSudo ? sudo : ''} chown #{aUser}:#{aGroup}" : "#{aSudo ? sudo : ''} chgrp #{aGroup}") + " #{aFilepath}" else cmd << "#{aSudo ? sudo : ''} chown #{aUser} #{aFilepath}" if aUser end cmd << "#{aSudo ? sudo : ''} chmod #{aMode.to_s} #{aFilepath}" if aMode cmd << "#{aSudo ? sudo : ''} chmod g+s #{aFilepath}" if aSetGroupId cmd.join(' && ') end |
#setup_designer_filesystem_cmd(aBrowserCmsRoot, aSharedDesignPath, aFtpPath, aUser, aApacheUser) ⇒ Object
This means :
-
designers can ftp in to the server and upload/edit templates and partials
-
templates are Rails-style eg. erb but can be whatever you have Rails handlers for
-
designers can upload assets into a design folder that will be available publicly under /design
-
designers templates and assets are not affected by redeploys
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/buzzville/cap_utils.rb', line 328 def setup_designer_filesystem_cmd(aBrowserCmsRoot,aSharedDesignPath,aFtpPath,aUser,aApacheUser) cmd = [] cmd << ensure_folder(aSharedDesignPath,aUser,aApacheUser,750) cmd << ensure_folder(aSharedDesignPath+'/design',aUser,aApacheUser,750) design_views = "#{aSharedDesignPath}/views" cmd << ensure_folder(design_views,aUser,aApacheUser,750) # copy files from database to shared/design # if [[ ! -L libtool ]]; then echo 'true'; fi !!! need NOT cmd << "if [ ! -L #{aBrowserCmsRoot}/tmp/views ]; then cp -rf #{aBrowserCmsRoot}/tmp/views/* #{design_views}/ ; fi" cmd << ensure_folder(design_views+'/layouts/templates',aUser,aApacheUser,750) cmd << ensure_folder(design_views+'/partials',aUser,aApacheUser,750) #convert tmp views folder into a link to shared/design cmd << ensure_link_cmd(design_views,'views',aBrowserCmsRoot+'/tmp',"#{aUser}:#{aApacheUser}") # copy files from aBrowserCmsRoot to shared/design and make readonly cmd << "cp -rf #{aBrowserCmsRoot}/app/views/* #{design_views}/" # link design/public into public folder cmd << ensure_link_cmd(aSharedDesignPath+'/design','design',aBrowserCmsRoot+'/public',"#{aUser}:#{aApacheUser}") ## link shared/design/design into ftp folder cmd << ensure_link_cmd(aSharedDesignPath+'/design','design',aFtpPath,"#{aUser}:#{aApacheUser}") ## link templates into ftp folder cmd << ensure_link_cmd(design_views,'views',aFtpPath,"#{aUser}:#{aApacheUser}") #run "#{sudo} chgrp -h www-data #{deploy_to}/current" # this is to change owner of link, not target cmd_s = cmd.join("\n") end |
#shell(aCommandline, &aBlock) ⇒ Object
147 148 149 150 |
# File 'lib/buzzville/cap_utils.rb', line 147 def shell(aCommandline,&aBlock) result = block_given? ? POpen4::shell(aCommandline,nil,nil,&aBlock) : POpen4::shell(aCommandline) return result[:stdout] end |
#sudo_run(aString) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/buzzville/cap_utils.rb', line 63 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 |
#svn_command(aCommand) ⇒ Object
367 368 369 |
# File 'lib/buzzville/cap_utils.rb', line 367 def svn_command(aCommand) run "#{sudo} svn --trust-server-cert --non-interactive --username #{svn_user} --password #{svn_password} #{aCommand}" end |
#update_remote_git_cache(aGitUrl, aBranchOrTag = nil) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/buzzville/cap_utils.rb', line 275 def update_remote_git_cache(aGitUrl,aBranchOrTag=nil) proj = project_name_from_git_public_url(aGitUrl) cache_path = File.join(shared_path,'extra_cache') run "mkdir -p #{cache_path}" dest = File.join(cache_path,proj) revision = '' sub_mods = false run "git clone #{aGitUrl} #{dest}" if !file_exists?(dest) run "cd #{dest} && git checkout -f #{aBranchOrTag ? aBranchOrTag : ''} #{revision} && git reset --hard && git clean -dfqx" if sub_mods run "cd #{dest} && git submodule init && git submodule update" end return dest end |
#upload_file(aLocalFilePath, aRemoteFilePath, aMode = 0644) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/buzzville/cap_utils.rb', line 31 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
74 75 76 77 78 |
# File 'lib/buzzville/cap_utils.rb', line 74 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 |