Skip to content

Instantly share code, notes, and snippets.

@glurp
Last active December 27, 2018 22:53
Show Gist options
  • Save glurp/760c73246fc0aa3fe8b07177bb53f119 to your computer and use it in GitHub Desktop.
Save glurp/760c73246fc0aa3fe8b07177bb53f119 to your computer and use it in GitHub Desktop.
Create a autoinstaller file as ruby script
#!/usr/bin/ruby
#################################################################################
# autopatch.rb : Create a autoinstallable script
#
# use 'tar czf' and base64 for serialize a directory in ascii data, embedded it in a ruby script.
# Executing this script will untar the data in current directory
#
#--------------------------------------------------------------------------------
# Create autoinstaller :
# > ruby autopatch.rb /opt/foo
#
# Use it :
# > cd other-directory
# > ruby ..../patch-XXXX.rb
# > ls opt
#################################################################################
require "base64"
####################### Script code embedded....
$patchScript=<<'EEND'
#!/usr/bin/ruby
require "base64"
$DATA=<<'REALYENDOFBIGDOCUMENT123435678'
therdataREALYENDOFBIGDOCUMENT123435678
puts "data :\nsize=#{$DATA.size}\nencoding=#{$DATA.encoding}\nroot directory ==> DIRNAME"
print "Ok (y/n) : " ; rep=gets
exit(-1) if rep.strip !~ /[oy]/
puts "ok, install..."
name="patch#{(rand*100000).to_i}.tgz"
File.open(name,"wb") { |f| f.write(Base64.decode64($DATA)) }
%x{tar xf #{name}}
File.delete(name)
puts "Done."
EEND
##################### Code for make the scipt...
dirname=ARGV.first
if ! File.exist?(dirname)
puts "directory '#{dirname}' existe pas !"
exit(-1)
end
puts "tar #{dirname}..."
tarname="newpatch#{(rand*100000).to_i}.tgz"
%x{tar czf #{tarname} #{dirname} }
puts "File tar size= #{File.size(tarname)}"
puts "make patch file..."
namefile="patch-"+Time.now.strftime("%Y-%m-%d-%Hh%M")+".rb"
File.open(namefile,"wb") { |f| f.write($patchScript.sub("DIRNAME",dirname).sub("therdata",Base64.encode64(IO.binread(tarname)))) }
File.delete(tarname)
puts "autoinstaller file is '#{namefile}' , size=#{File.size(namefile)} !"
system("chmod 777 #{namefile}") unless Gem.win_platform?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment