Class: Flapjack::CLI::Maintenance
- Inherits:
-
Object
- Object
- Flapjack::CLI::Maintenance
- Defined in:
- lib/flapjack/cli/maintenance.rb
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(global_options, options) ⇒ Maintenance
constructor
A new instance of Maintenance.
- #show(base_time = Time.now) ⇒ Object
Constructor Details
#initialize(global_options, options) ⇒ Maintenance
Returns a new instance of Maintenance.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/flapjack/cli/maintenance.rb', line 14 def initialize(, ) @global_options = @options = if @global_options[:'force-utf8'] Encoding.default_external = 'UTF-8' Encoding.default_internal = 'UTF-8' end config = Flapjack::Configuration.new config.load([:config]) @config_env = config.all if @config_env.nil? || @config_env.empty? exit_now! "No config data found in '#{[:config]}'" end Flapjack::RedisProxy.config = config.for_redis Zermelo.redis = Flapjack.redis end |
Instance Method Details
#create ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/flapjack/cli/maintenance.rb', line 141 def create errors = {} check_names = @options[:check].is_a?(String) ? @options[:check].split(',') : @options[:check] started = Chronic.parse(@options[:started]) raise "Failed to parse start time #{@options[:started]}" if started.nil? duration = ChronicDuration.parse(@options[:duration]) raise "Failed to parse duration #{@options[:duration]}" if duration.nil? check_names.each do |check_name| check = Flapjack::Data::Check.intersect(:name => check_names).all.first if check.nil? # Create the check if it doesn't exist, so we can schedule maintenance against it check = Flapjack::Data::Check.new(:name => check_name) check.save end success = case @options[:type] when 'scheduled' sched_maint = Flapjack::Data::ScheduledMaintenance.new(:start_time => started, :end_time => started + duration, :summary => @options[:reason]) sched_maint.save check.scheduled_maintenances << sched_maint when 'unscheduled' unsched_maint = Flapjack::Data::UnscheduledMaintenance.new(:start_time => started, :end_time => started + duration, :summary => @options[:reason]) unsched_maint.save check.set_unscheduled_maintenance(unsched_maint) end identifier = "#{check_name}:#{started}" errors[identifier] = "The following check failed to create: #{identifier}" unless success end if errors.empty? puts "The maintenances specified have been created" else puts(errors.map {|k, v| "#{k}: #{v}" }.join("\n")) exit_now!('Failed to create maintenances') end end |
#delete ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/flapjack/cli/maintenance.rb', line 106 def delete base_time = Time.now maintenances = show(base_time) unless @options[:apply] exit_now!('The following maintenances would be deleted. Run this ' + 'command again with --apply true to remove them.') end errors = {} maintenances.each do |maint| check = maint.check identifier = "#{check.name}:#{check.start}" if maint.end_time < base_time errors[identifier] = "Maintenance can't be deleted as it finished in the past" else success = case @options[:type] when 'scheduled' check.end_scheduled_maintenance(maint, base_time) when 'unscheduled' check.clear_unscheduled_maintenance(base_time) end errors[identifier] = "The following maintenance failed to delete: #{entry}" unless success end end if errors.empty? puts "The maintenances above have been deleted" else puts(errors.map {|k, v| "#{k}: #{v}" }.join("\n")) exit_now!('Failed to delete maintenances') end end |
#show(base_time = Time.now) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/flapjack/cli/maintenance.rb', line 35 def show(base_time = Time.now) state = @options[:state] checks = if @options[:check] Flapjack::Data::Check.intersect(:name => Regexp.new(@options[:check])).all else Flapjack::Data::Check.all end start_time_begin, start_time_end = self.class.extract_time_range( @options[:started], base_time) start_time_begin = start_time_begin.to_i unless start_time_begin.nil? start_time_end = start_time_end.to_i unless start_time_end.nil? finish_time_begin, finish_time_end = self.class.extract_time_range( @options[:finishing], base_time) finish_time_begin = finish_time_begin.to_i unless finish_time_begin.nil? finish_time_end = finish_time_end.to_i unless finish_time_end.nil? duration_range = @options[:duration] ? self.class.extract_duration_range( @options[:duration]) : nil start_time_range = Zermelo::Filters::IndexRange.new(start_time_begin, start_time_end, :by_score => true) finish_time_range = Zermelo::Filters::IndexRange.new(finish_time_begin, finish_time_end, :by_score => true) maintenances = case @options[:type].downcase when 'scheduled' checks.inject([]) do |memo, check| maints = check.scheduled_maintenances. intersect(:start_time => start_time_range, :end_time => finish_time_range).all if !maints.empty? filtered = duration_range.nil? ? maints : self.class.filter_duration(maints, duration_range) memo += filtered end memo end when 'unscheduled' checks.inject([]) do |memo, check| maints = check.unscheduled_maintenances. intersect(:start_time => start_time_range, :end_time => finish_time_range).all if !maints.empty? filtered = duration_range.nil? ? maints : self.class.filter_duration(maints, duration_range) memo += filtered end memo end end rows = maintenances.collect do |maint| [maint.check.name, Time.at(maint.start_time), maint.end_time - maint.start_time, maint.summary, Time.at(maint.end_time)] end puts Terminal::Table.new :headings => ['Check', 'Start', 'Duration (s)', 'Reason', 'End'], :rows => rows puts "Found #{rows.count} maintenances" maintenances end |