class Object

Public Instance Methods

alcapon_message(string) click to toggle source
# File lib/alcapon/utils.rb, line 52
def alcapon_message(string)
  puts( "[alcapon]".green+" #{string}" )
end
capez_puts_done() click to toggle source
# File lib/alcapon/utils.rb, line 6
def capez_puts_done
  puts '✔'.green
end
create_backup_dir( path ) click to toggle source
# File lib/db.rb, line 74
def create_backup_dir( path )
  FileUtils.mkdir_p( path )
end
do_and_retrieve_backup( thisdbserver, thisdbname, thisdbuser, thisdbpass ) click to toggle source
# File lib/db.rb, line 82
def do_and_retrieve_backup( thisdbserver, thisdbname, thisdbuser, thisdbpass )
  filename = generate_backup_name( thisdbname )
  puts "Backing up database ["+thisdbname.green+"] to " + filename.green
  file = File.join( "/tmp", filename )
  backup_dir_for_this_stage = File.join( get_backup_dir, "#{stage}" )
  on_rollback do
    run "rm #{file}"
  end

  dump_result = nil
  run "mysqldump -h#{thisdbserver} -u#{thisdbuser} -p #{thisdbname} | gzip > #{file}" do |ch, stream, out|
    ch.send_data "#{thisdbpass}\n" if out =~ /^Enter password:/
    dump_result = out
  end
  if dump_result =~ /.*error.*1045/i
    puts "Access denied on '#{thisdbserver}' with user '#{thisdbuser}'".red
  else
    create_backup_dir( backup_dir_for_this_stage )
    get( file, File.join( backup_dir_for_this_stage, filename ), :via => :scp )
  end
  run "rm #{file}"
end
ezp5?() click to toggle source
# File lib/alcapon/utils.rb, line 34
def ezp5?
  return ( fetch( :ezpublish_version, nil ) == 5 )
end
ezp_legacy_path(path='') click to toggle source
# File lib/alcapon/utils.rb, line 38
def ezp_legacy_path(path='')
  elements = Array.new

  if( fetch( :ezp_legacy, "" ) != "" )
    elements += [ "#{ezp_legacy}" ]
  end

  if( path != '' )
    elements += [ "#{path}" ]
  end

  return File.join( elements )
end
fix_permissions(path,userid,groupid) click to toggle source
# File lib/capez.rb, line 487
def fix_permissions(path,userid,groupid)
    # If admin_runner is not null then make sure that the command is not run with -u admin_runner
    if fetch( :admin_runner, nil ) != nil
      run( "sudo chown -R #{userid}:#{groupid} #{path}" )
    else
      try_sudo( "chown -R #{userid}:#{groupid} #{path}" )
    end
end
generate_backup_name(dbname) click to toggle source
# File lib/db.rb, line 70
def generate_backup_name(dbname)
  return "#{Time.now.strftime '%Y-%m%d-%H%M%S'}-#{dbname}.sql.gz"
end
get_backup_dir() click to toggle source
# File lib/db.rb, line 78
def get_backup_dir
  return "#{ezp_legacy_path('extension/alcapon/backups/database')}"
end
get_file_changes() click to toggle source
# File lib/capez.rb, line 248
def get_file_changes
  return fetch( :file_changes, false )
end
git_status_result(path) click to toggle source
# File lib/capez.rb, line 460
def git_status_result(path)
  result = Hash.new
  result['has_local_changes'] = false
  result['has_new_files'] = false
  result['tracked_branch'] = nil
  result['tracked_branch_status'] = nil
  result['tracked_branch_commits'] = 0
  cmd_result = `cd #{path} && git status 2> /dev/null`
  result['raw_result'] = cmd_result
  cmd_result_array = cmd_result.split( /\n/ );
  cmd_result_array.each { |value|
    case value
      when /# Changes not staged for commit:/
        result['has_local_changes'] = true
      when /# Untracked files:/
        result['has_new_files'] = true
      when /# On branch (.*)$/
        result['branch'] = $1
      when /# Your branch is (.*) of '(.*)' by (.*) commits?/
        result['tracked_branch_status'] = $1
        result['tracked_branch'] = $2
        result['tracked_branch_commits'] = $3
    end
    }
    return result
end
make_file_changes( options={} ) click to toggle source
# File lib/capez.rb, line 129
def make_file_changes( options={} )

  default_options = { :locally => false }
  options = default_options.merge( options )

  puts( "\n--> File operations" )

  unless !(file_changes = get_file_changes) then

    path = options[:locally] ? "" : fetch( :latest_release )

    changes = 0
    renames = 0
    errors = []
    messages = []

    print_dotted( "execution", :eol_msg => (options[:locally] ? "local" : "distant" ), :eol => true, :max_length => 25 )
    print_dotted( "files count", :eol_msg => "#{file_changes.count}", :eol => true, :max_length => 25 )

    # process each files
    print( "progress " )
    file_changes.each { |filename,operations|

      print( "." ) unless dry_run

      target_filename = filename
      renamed = false

      # rename operation is caught and executed at first
      if operations.has_key?("rename")
        if( target_filename != operations['rename'] )
          target_filename = operations['rename']
          cmd = "if [ -f #{path}/#{filename} ]; then cp #{path}/#{filename} #{path}/#{target_filename}; fi;"
          options[:locally] ? run_locally( "#{cmd}" ) : run( "#{cmd}" )
          renames += 1
        else
          target_filename = operations['rename']
          errors += ["target and original name are the same (#{target_filename})"]
        end
      end

      operations.each { |operation,value|
        case operation
          when 'rename'
          when 'replace'

            if( value.count > 0 )

              # download file if necessary
              if options[:locally]
                tmp_filename = target_filename
              else
                tmp_filename = target_filename+".tmp"
                tmp_filename = Digest::MD5.hexdigest( tmp_filename )
                if dry_run
                  puts "\n"
                  puts "tmp_filename : #{tmp_filename}"
                  puts "target_filepath : #{path}/#{target_filename}"
                else
                  get( "#{path}/#{target_filename}", tmp_filename, :via => :scp )
                end
              end

              if !dry_run
                text = File.read(tmp_filename)
              end
              value.each { |search,replace|
                changes += 1
                if dry_run
                  puts "replace '#{search}' by '#{replace}'"
                else
                  text = text.gsub( "#{search}", "#{replace}" )
                end
              }

              if !dry_run
                File.open(tmp_filename, "w") {|file| file.write(text) }
              end

              # upload and remove temporary file
              if !options[:locally] && !dry_run
                run( "if [ -f #{target_filename} ]; then rm #{target_filename}; fi;" )
                upload( tmp_filename, "#{path}/#{target_filename}", :via => :scp )
                run_locally( "rm #{tmp_filename}" )
              end
            end
          else
            errors += ( "operation '#{operation}' supported" )
        end
      }
    }
    puts " done".green

    # stats
    print_dotted( "files renamed", :eol_msg => "#{renames}", :eol => true, :max_length => 25 )
    print_dotted( "changes count", :eol_msg => "#{changes}", :eol => true, :max_length => 25 )
    print_dotted( "changes avg / file", :max_length => 25, :eol_msg => ( file_changes.count > 0 ? "#{changes/file_changes.count}" : "" ), :eol => true )
    messages.each { |msg| puts( "#{msg}") }
    puts( "errors : ".red ) unless errors.count == 0
    errors.each { |msg| puts( "- #{msg}".red ) }
  else
    puts( "No file changes needs to be applied. Please set :file_changes".blue )
  end
end
print_dotted( message, options={} ) click to toggle source
start_spinner() click to toggle source
# File lib/ext/spinner.rb, line 17
def start_spinner
  @spinner_running = true
  @spinner.wakeup
end
stop_spinner() click to toggle source

stops the spinner and backspaces over last displayed character

# File lib/ext/spinner.rb, line 23
def stop_spinner
  @spinner_running = false
  print "\b"
end
unindent(string) click to toggle source
# File lib/alcapon/utils.rb, line 1
def unindent(string)
  indentation = string[/\A\s*/]
  string.strip.gsub(/^#{indentation}/, "")
end