Skip to content

Instantly share code, notes, and snippets.

@merqlove
Created July 21, 2015 22:38
Show Gist options
  • Save merqlove/5874b9d1c0b6ca5a0e76 to your computer and use it in GitHub Desktop.
Save merqlove/5874b9d1c0b6ca5a0e76 to your computer and use it in GitHub Desktop.
GLI binary scaffold
#!/usr/bin/env ruby
# -*- encoding : utf-8 -*-
Signal.trap('INT') { exit 1 }
# resolve bin path, ignoring symlinks
require 'pathname'
bin_file = Pathname.new(__FILE__).realpath
# add self to libpath
$LOAD_PATH.unshift File.expand_path('../../lib', bin_file)
require 'gli'
require 'do_snapshot'
require 'do_snapshot/log'
require 'do_snapshot/mail'
require 'do_snapshot/command'
include GLI::App
program_desc '`do_snapshot` able to create and cleanup snapshots on your droplets.'
desc 'Shows the version of the currently installed DoSnapshot gem'
version DoSnapshot::VERSION
subcommand_option_handling :normal
arguments :strict
pre do |global_options, command, options, args|
# $command = DoSnapshot::Command.new(global_options,
# %w( digital_ocean_client_id digital_ocean_api_key digital_ocean_access_token ))
true
end
accept Array do |value|
value.split(/\s/).map(&:strip)
end
accept Hash do |value|
result = {}
value.split(/\s/).each do |pair|
k,v = pair.split(/:/)
result[k] = v
end
result
end
desc 'DEFAULT. Create and cleanup snapshot\'s'
long_desc <<-LONGDESC
`do_snapshot` able to create and cleanup snapshots on your droplets.
You can optionally specify parameters to select or exclude some droplets.
### Examples
Select api version (1, 2):
$ do_snapshot -a 2
Set DigitalOcean keys:
$ do_snapshot --digital_ocean_api_token SOMELONGTOKEN
Keep latest 5 and cleanup older if maximum is reached, verbose:
$ do_snapshot -k 5 -c -v
Keep latest 3 from selected droplet:
$ do_snapshot --only 123456 --keep 3
Working with all except selected droplets:
$ do_snapshot --exclude 123456 123457
Keep latest 5 snapshots, not create new if we have more, send mail notification instead:
$ do_snapshot --keep 10 --stop --mail to:[email protected]
### Cron example
0 4 * * 7 /.../bin/do_snapshot -k 5 -m to:TO from:FROM -t address:HOST user_name:LOGIN password:PASSWORD port:2525 -q -c
### Advanced options example for MAIL feature:
--mail to:[email protected] from:[email protected] --smtp address:smtp.gmail.com port:25 user_name:someuser password:somepassword
For more details look here: https://github.com/benprew/pony
VERSION: #{DoSnapshot::VERSION}
LONGDESC
command [:s, :c, :snap, :create] do |c|
c.desc 'Select api version.'
c.arg_name '1'
c.flag [:p,:protocol], default_value: 2, type: Numeric
c.desc 'Select some droplets.'
c.arg_name '123456 123456 123456'
c.flag [:o,:only], default_value: [], type: Array
c.desc 'Except some droplets.'
c.arg_name '123456 123456 123456'
c.flag [:e,:exclude], default_value: [], type: Array
c.desc 'How much snapshots you want to keep?'
c.arg_name '5'
c.flag [:k,:keep], default_value: 10, type: Numeric
c.desc 'Delay between snapshot operation status requests.'
c.arg_name '5'
c.flag [:d,:delay], default_value: 10, type: Numeric
c.desc 'Timeout in sec\'s for events like Power Off or Create Snapshot.'
c.arg_name '250'
c.flag :timeout, default_value: 3600, type: Numeric
c.desc 'Receive mail if fail or maximum is reached.'
c.arg_name 'to:[email protected]'
c.flag :mail, type: Hash
c.desc 'SMTP options.'
c.arg_name 'user_name:[email protected] password:password'
c.flag :smtp, type: Hash
c.desc 'Log file path. By default logging is disabled.'
c.arg_name '/Users/someone/.do_snapshot/main.log'
c.flag [:l, :log], type: String
c.desc 'Cleanup snapshots after create.'
c.switch [:c,:clean], default_value: true
c.desc 'Stop creating snapshots if maximum is reached.'
c.switch [:s,:stop]
c.desc 'Verbose mode.'
c.switch [:v,:trace]
c.desc 'Quiet mode. If don\'t need any messages and in console.'
c.switch [:q,:quiet]
c.desc 'DIGITAL_OCEAN_ACCESS_TOKEN. if you can\'t use environment.'
c.arg_name 'YOURLONGAPITOKEN'
c.flag :digital_ocean_access_token, type: String
c.action do |global_options,options,args|
# $command.snap
puts global_options
puts options
puts args
end
end
on_error do |exception|
# Error logic here
# return false to skip default error handling
true
end
exit run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment