#!/usr/bin/env ruby require 'optparse' require_relative '../lib/hibernate/lambda_setup' require_relative '../lib/hibernate/ec2_manager' class HibernateCLI def self.run command = nil options = {} if ARGV.include?('setup') command = :setup ARGV.delete('setup') elsif ARGV.include?('node') command = :manage_ec2 ARGV.delete('node') elsif ARGV.include?('remove') command = :remove_ec2 ARGV.delete('remove') end parser = OptionParser.new do |parser| parser.banner = "Usage: hibernate [command] [options]" parser.on('--in=', 'Specify the EC2 instance name') do |instance_name| options[:instance_name] = instance_name end parser.on('--start_instance=', 'Specify the cron expression to start the instance') do |start_cron| options[:start_cron] = start_cron end parser.on('--stop_instance=', 'Specify the cron expression to stop the instance') do |stop_cron| options[:stop_cron] = stop_cron end end begin parser.parse!(ARGV) rescue OptionParser::ParseError => e puts e.message puts parser exit 1 end if command.nil? puts "Please provide a command." puts parser exit end case command when :setup create_lambda_function when :manage_ec2 manage_ec2_command(options) when :remove_ec2 remove_ec2_command(options) end end def self.create_lambda_function LambdaSetup.new.run end def self.manage_ec2_command(options) if options[:instance_name].nil? || (options[:start_cron].nil? && options[:stop_cron].nil?) puts "Please provide the instance name, and at least one cron expression (start or stop)." puts "Usage: hibernate node --in= --start_instance= --stop_instance=" exit else ec2_manager = EC2Manager.new(options[:instance_name], options[:start_cron], options[:stop_cron]) ec2_manager.create_event_rule end end def self.remove_ec2_command(options) if options[:instance_name].nil? || (options[:start_cron].nil? && options[:stop_cron].nil?) puts "Please provide the instance name, and at least one cron expression (start or stop) to remove." puts "Usage: hibernate remove --in= --start_instance= --stop_instance=" exit else ec2_manager = EC2Manager.new(options[:instance_name], options[:start_cron], options[:stop_cron]) ec2_manager.remove_event_rule end end end HibernateCLI.run