Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RubyConf 2017: Packing your Ruby application into a single executable

Minqi Pan
November 15, 2017

RubyConf 2017: Packing your Ruby application into a single executable

Recent languages like Go compiles a project into a nice executable, why can't good ol' Ruby? We have built an packer for Ruby to do just that. It is 100% open-source, and can produce executables for Windows, macOS and Linux individually. By packing, distributing Ruby apps are made extremely easy, additionally with intellectual property protection. Auto-updating is also made easy, in that the executable only needs to download and replace itself. So, how we did it? How to use it? What goes under the hood? What future will this bring to Ruby? That's what will be unraveled in this talk!

Minqi Pan

November 15, 2017
Tweet

More Decks by Minqi Pan

Other Decks in Programming

Transcript

  1. Problems • Slow installation. Tons of files to download. Great-

    Wall’d in China. Remembered to use sudo? • Error-prone. Failed compiling native modules? Should I care about post-installations notices? • Ruby runtime version? Wanted to use lonely operator reliably? Coexist with multiple Rubies?
  2. Problems • No version checks. New versions missed without your

    attentions. • Cumbersome to update. Multiple steps needed.
  3. But there are so many API’s • require, load, require_relative

    • File.read, File.open, Dir.open • File.readlink, File.stat, File.lstat • …
  4. SquashFS • a compressed read-only file system • used by

    the Live CD versions of Arch Linux, Debian, Fedora, Gentoo, Mint, Salix, Ubuntu • used on OpenWrt and DD-WRT router firmware
  5. SquashFS • Introduced in 2009 with Linux 2.6.29 • File

    format very stabilized • Unsquashfs and mksquashfs have win32 ports; 7-Zip on win32 also supports SquashFS • Part of kernel; GPL Licensed
  6. https://github.com/pmq20/libsquash libsquash • MIT licensed • 100% User-land Code •

    Embeddable, 1 dep. only • Compiles on 3 platforms, even Windows XP with VC++ 2010 • Introduces VFD - virtual file descriptor, intercepting system calls unobtrusively libsquash
  7. What about Native Extensions? • Libsquash Intercepts dlopen(), LoadLibraryExW() •

    Dynamic library files inside the pack are extracted to temporary files • dlopen / LoadLibraryExW redirects the request to the temporary files • temporary files are deleted on exit
  8. But what about Rails? • SquashFS is read-only, so your

    project root is read- only • Rails creates tmp/ or log/ and writes to it, and tmp/ or log/ is in your project root • Rails has config files in your project root
  9. Solution: writable root • redirect `mkdir()` inside the memfs to

    a temporary directory • redirect `open()` with `O_CREAT` inside the memfs to a temporary directory • redirect `CreateFileW()` with writing inside the memfs to a temporary directory • removes the temporary directory and files at exit
  10. or

  11. Windows • SquashFS Tools 4.3 • Visual Studio 2015 Update

    3, all editions including the Community edition (remember to select
 “Common Tools for Visual C++ 2015" feature during installation). • Ruby
  12. macOS • SquashFS Tools 4.3: brew install squashfs • Xcode,

    You also need to install the Command Line Tools via Xcode. • Ruby
  13. Linux • SquashFS Tools 4.3: sudo yum install squashfs- tools

    or sudo apt-get install squashfs-tools • gcc or clang • GNU Make • Ruby
  14. Use older Linux, e.g. CentOS release 5.8
 with gcc and

    g++ 4.8
 (possibly from devtoolset-2 of slc5-devtoolset)