Class: Flapjack::CLI::Migrate

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/flapjack/cli/migrate.rb

Instance Method Summary collapse

Methods included from Utility

#hashify, #load_template, #local_timezone, #relative_time_ago, #remove_utc_offset, #stringify, #symbolize, #time_period_in_words, #truncate

Constructor Details

#initialize(global_options, options) ⇒ Migrate

Returns a new instance of Migrate.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flapjack/cli/migrate.rb', line 53

def initialize(global_options, options)
  @global_options = global_options
  @options = options

  config = Flapjack::Configuration.new

  if options[:source].nil? || options[:source].strip.empty?
    config.load(global_options[:config])
    config_env = config.all
  end

  @source_redis_options = config.for_redis
end

Instance Method Details

#to_v2Object



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
105
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
140
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
# File 'lib/flapjack/cli/migrate.rb', line 67

def to_v2
  source_addr = (@options[:source].nil? || @options[:source].strip.empty?) ?
    @source_redis_options : @options[:source]

  @source_redis = case source_addr
  when Hash
    Redis.new(source_addr.merge(:driver => :hiredis))
  when String
    Redis.new(:url => source_addr, :driver => :hiredis)
  else
    exit_now! "could not understand source Redis config"
  end

  @source_redis_version = @source_redis.info['redis_version']

  dest_addr = @options[:destination]
  dest_redis = Redis.new(:url => dest_addr, :driver => :hiredis)

  # Zermelo.logger = ::Logger.new('/tmp/zermelo_migrate.log')

  do_rule_migration = @options[:rules]

  Zermelo.redis = dest_redis

  dest_db_size = Zermelo.redis.dbsize
  if dest_db_size > 0
    if @options[:force]
      Zermelo.redis.flushdb
      puts "Cleared #{@options[:destination]} db"
    else
      exit_now! "Destination db #{@options[:destination]} has " \
        "#{dest_db_size} keys, and the --force option was not provided"
    end
  end

  # key is old contact_id, value = new contact id
  @contact_id_cache = {}

  # key is entity_name:check_name, value is zermelo record
  @check_name_cache = {}

  # key is tag name, value is zermelo record
  @tag_name_cache = {}

  # cache old entity/check <-> contact linkage, use as limiting set in
  # rule construction
  @check_ids_by_contact_id_cache = {}

  @current_entity_names = @source_redis.zrange('current_entities', 0, -1)
  @current_check_names = source_keys_matching('current_checks:?*')

  @time = Time.now

  puts "#{Time.now.iso8601} Migrating contacts and media"
  migrate_contacts_and_media

  puts "#{Time.now.iso8601} Migrating checks"
  migrate_checks

  puts "Migrating check states & actions"
  Flapjack::Data::Check.lock(
    Flapjack::Data::State,
    Flapjack::Data::ScheduledMaintenance,
    Flapjack::Data::UnscheduledMaintenance
  ) do

    idx = 0
    check_count = Flapjack::Data::Check.count

    Flapjack::Data::Check.sort(:name).each do |check|
      idx += 1
      puts "#{Time.now.iso8601} #{idx}/#{check_count} #{check.name}"
      migrate_states(check)
      migrate_actions(check)
      migrate_scheduled_maintenances(check)
      migrate_unscheduled_maintenances(check)

      if (idx % 500) == 0
        # reopen connections -- avoid potential timeout for long-running jobs
        @source_redis.quit
        @source_redis = case source_addr
        when Hash
          Redis.new(source_addr.merge(:driver => :hiredis))
        when String
          Redis.new(:url => source_addr, :driver => :hiredis)
        end

        Zermelo.redis.quit
        Zermelo.redis = Redis.new(:url => dest_addr, :driver => :hiredis)
      end
    end
  end

  if do_rule_migration
    puts "#{Time.now.iso8601} Migrating contact/entity linkages"
    migrate_contact_entity_linkages

    puts "#{Time.now.iso8601} Migrating rules"
    migrate_rules
  end

rescue Exception => e
  puts e.message
  trace = e.backtrace.join("\n")
  puts trace
  # TODO output data, set exit status
  exit 1
end