#!/usr/bin/env ruby require 'fileutils' require 'sys/filesystem' require 'optparse' require 'methadone' require 'cleanup_recordings.rb' class App include Methadone::Main include Methadone::CLILogging main do # Add args you want: |like,so| # your program code here # You can access CLI options via # the options Hash part = options[:partition] || "/home" puts "Checking #{part}..." # Get the available space in GB for /home partition stat = Sys::Filesystem.stat(part) md_available = stat.block_size * stat.blocks_available / 1024 / 1024 / 1024 space = (options[:space] || 10).to_i puts "\tAvailable: #{md_available}" puts "\tReserve: #{space}" puts "\tThere is not enough space, let's cleanup" if md_available < space files = Dir.glob('/home/hts/*ts').sort_by{|f| File.mtime(f)}.reverse # Check that available space is at least space otherwise remove files. while md_available < space break if files.empty? # Remove file file = files.pop puts "Remove #{file}" FileUtils.rm(file) # Recalculate and print available space. stat = Sys::Filesystem.stat(part) md_available = stat.block_size * stat.blocks_available / 1024 / 1024 / 1024 puts "A: #{md_available} S: #{space}" sleep 0.5 end if md_available >= space puts "\n\nRecordings have been cleaned up" end puts "Available now: #{md_available}" end # supplemental methods here # Declare command-line interface here # description "one line description of your app" # # Accept flags via: # on("--flag VAL","Some flag") # options[flag] will contain VAL on("-s", "--space SPACE", "Space to reserve in GB") on("-p", "--partition PARTITION", "Partition to check (default: /home)") # # Specify switches via: # on("--[no-]switch","Some switch") # # Or, just call OptionParser methods on opts # # Require an argument # arg :some_arg # # # Make an argument optional # arg :optional_arg, :optional version CleanupRecordings::VERSION use_log_level_option :toggle_debug_on_signal => 'USR1' go! end