Class: MoSQL::CLI

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mosql/cli.rb

Constant Summary collapse

BATCH =
1000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



19
20
21
22
23
24
# File 'lib/mosql/cli.rb', line 19

def initialize(args)
  @args    = args
  @options = []
  @done    = false
  setup_signal_handlers
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



12
13
14
# File 'lib/mosql/cli.rb', line 12

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/mosql/cli.rb', line 12

def options
  @options
end

#tailerObject (readonly)

Returns the value of attribute tailer.



12
13
14
# File 'lib/mosql/cli.rb', line 12

def tailer
  @tailer
end

Class Method Details

.run(args) ⇒ Object



14
15
16
17
# File 'lib/mosql/cli.rb', line 14

def self.run(args)
  cli = CLI.new(args)
  cli.run
end

Instance Method Details

#connect_mongoObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mosql/cli.rb', line 114

def connect_mongo
  @mongo = Mongo::MongoClient.from_uri(options[:mongo])
  config = @mongo['admin'].command(:ismaster => 1)
  if !config['setName'] && !options[:skip_tail]
    log.warn("`#{options[:mongo]}' is not a replset.")
    log.warn("Will run the initial import, then stop.")
    log.warn("Pass `--skip-tail' to suppress this warning.")
    options[:skip_tail] = true
  end
  options[:service] ||= config['setName']
end

#connect_sqlObject



126
127
128
129
130
131
132
# File 'lib/mosql/cli.rb', line 126

def connect_sql
  @sql = MoSQL::SQLAdapter.new(@schema, options[:sql], options[:schema])
  if options[:verbose] >= 2
    @sql.db.sql_log_level = :debug
    @sql.db.loggers << Logger.new($stderr)
  end
end

#load_collectionsObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/mosql/cli.rb', line 134

def load_collections
  collections = YAML.load_file(@options[:collections])
  begin
    @schema = MoSQL::Schema.new(collections)
  rescue MoSQL::SchemaError => e
    log.error("Error parsing collection map `#{@options[:collections]}':")
    log.error(e.to_s)
    exit(1)
  end
end

#parse_argsObject



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
105
106
107
108
109
110
111
112
# File 'lib/mosql/cli.rb', line 35

def parse_args
  @options = {
    :collections => 'collections.yml',
    :sql    => 'postgres:///',
    :mongo  => 'mongodb://localhost',
    :verbose => 0
  }
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options] "

    opts.on('-h', '--help', "Display this message") do
      puts opts
      exit(0)
    end

    opts.on('-v', "Increase verbosity") do
      @options[:verbose] += 1
    end

    opts.on("-c", "--collections [collections.yml]", "Collection map YAML file") do |file|
      @options[:collections] = file
    end

    opts.on("--sql [sqluri]", "SQL server to connect to") do |uri|
      @options[:sql] = uri
    end

    opts.on("--mongo [mongouri]", "Mongo connection string") do |uri|
      @options[:mongo] = uri
    end

    opts.on("--schema [schema]", "PostgreSQL 'schema' to namespace tables") do |schema|
      @options[:schema] = schema
    end

    opts.on("--ignore-delete", "Ignore delete operations when tailing") do
      @options[:ignore_delete] = true
    end

    opts.on("--only-db [dbname]", "Don't scan for mongo dbs, just use the one specified") do |dbname|
      @options[:dbname] = dbname
    end

    opts.on("--tail-from [timestamp]", "Start tailing from the specified UNIX timestamp") do |ts|
      @options[:tail_from] = ts
    end

    opts.on("--service [service]", "Service name to use when storing tailing state") do |service|
      @options[:service] = service
    end

    opts.on("--skip-tail", "Don't tail the oplog, just do the initial import") do
      @options[:skip_tail] = true
    end

    opts.on("--reimport", "Force a data re-import") do
      @options[:reimport] = true
    end

    opts.on("--no-drop-tables", "Don't drop the table if it exists during the initial import") do
      @options[:no_drop_tables] = true
    end

    opts.on("--unsafe", "Ignore rows that cause errors on insert") do
      @options[:unsafe] = true
    end
  end

  optparse.parse!(@args)

  log = Log4r::Logger.new('Stripe')
  log.outputters = Log4r::StdoutOutputter.new(STDERR)
  if options[:verbose] >= 1
    log.level = Log4r::DEBUG
  else
    log.level = Log4r::INFO
  end
end

#runObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mosql/cli.rb', line 145

def run
  parse_args
  load_collections
  connect_sql
  connect_mongo

   = MoSQL::Tailer.create_table(@sql.db, 'mosql_tailers')

  @tailer = MoSQL::Tailer.new([@mongo], :existing, ,
                              :service => options[:service])

  @streamer = Streamer.new(:options => @options,
                           :tailer  => @tailer,
                           :mongo   => @mongo,
                           :sql     => @sql,
                           :schema  => @schema)

  @streamer.import

  unless options[:skip_tail]
    @streamer.optail
  end
end

#setup_signal_handlersObject



26
27
28
29
30
31
32
33
# File 'lib/mosql/cli.rb', line 26

def setup_signal_handlers
  %w[TERM INT USR2].each do |sig|
    Signal.trap(sig) do
      log.info("Got SIG#{sig}. Preparing to exit...")
      @streamer.stop
    end
  end
end