Search code examples
ruby-on-railsrubyrails-activestorage

Rails: Find if server has libvips or other package installed


I'm moving my application files to an ActiveStorage solution and would like to take advantage of its image interpolation functions (resize, crop, etc).

I have about 100+ application instances that stands on about 30 servers. Not all have the libvips or the imagemagick package installed, so I would like to add the variant method only if that lib is present. For example something like:

if has_my_server_libvips? 
  logo_header = Portal.logo_header.variant({ resize_to_fit: [230, 50] })
else
  logo_header = Portal.logo_header
end

Is there any Ruby / RoR built-in / custom method to check it?


Solution

  • You can check if these libraries have executable command. This is just idea, you can implement it as you wish

    logo_header =
      if %i[vips magick].any? { |command| `which #{command}`.present? } 
        Portal.logo_header.variant({ resize_to_fit: [230, 50] })
      else
        Portal.logo_header
      end
    

    which command returns path of executable bin (with new line character in the end) or empty string if absent


    You can also define such method (for example in Kernel)

    module Kernel
      def command_exists?(command)
        command = command.delete('^a-zA-Z')
    
        !`which #{command}`.empty?
      end
    end
    

    But be careful: sanitize argument before call this method to prevent injection!