Statistics
| Branch: | Tag: | Revision:

one / src / onedb / onedb @ 36906272

History | View | Annotate | Download (13.7 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
nk_encoding = nil
20

    
21
if RUBY_VERSION =~ /^1.9/
22
    Encoding.default_external = Encoding::UTF_8
23
    Encoding.default_internal = Encoding::UTF_8
24
    nk_encoding = "UTF-8"
25
end
26

    
27
NOKOGIRI_ENCODING = nk_encoding
28

    
29
ONE_LOCATION = ENV["ONE_LOCATION"]
30

    
31
if !ONE_LOCATION
32
    LIB_LOCATION      = "/usr/lib/one"
33
    RUBY_LIB_LOCATION = LIB_LOCATION + "/ruby"
34
    VAR_LOCATION      = "/var/lib/one"
35
    ETC_LOCATION      = "/etc/one"
36
    LOCK_FILE         = "/var/lock/one/one"
37
else
38
    LIB_LOCATION      = ONE_LOCATION + "/lib"
39
    RUBY_LIB_LOCATION = LIB_LOCATION + "/ruby"
40
    VAR_LOCATION      = ONE_LOCATION + "/var"
41
    ETC_LOCATION      = ONE_LOCATION + "/etc"
42
    LOCK_FILE         = VAR_LOCATION + "/.lock"
43
end
44

    
45
$: << RUBY_LIB_LOCATION
46
$: << RUBY_LIB_LOCATION+'/onedb'
47

    
48
require 'cli/command_parser'
49
require 'onedb'
50
require 'onedb_live'
51
require 'opennebula'
52

    
53
FORCE={
54
    :name => "force",
55
    :short => "-f",
56
    :large => "--force",
57
    :description => "Forces the backup even if the DB exists"
58
}
59

    
60
BACKUP={
61
    :name => "backup",
62
    :short => "-b file",
63
    :large => "--backup file",
64
    :description => "Use this file to store SQL dump",
65
    :format => String
66
}
67

    
68
FEDERATED = {
69
    :name        => "federated",
70
    :large       => "--federated",
71
    :description => "Limit backup/restore to federated tables"
72
}
73

    
74
###############################################################################
75
# SQLite options
76
###############################################################################
77
SQLITE={
78
    :name => "sqlite",
79
    :short => "-s file",
80
    :large => "--sqlite file",
81
    :format => String,
82
    :description => "SQLite DB file",
83
    :proc => lambda { |o, options|
84
        options[:backend] = :sqlite
85
        options[:sqlite]  = o
86
    }
87
}
88

    
89
###############################################################################
90
# MySQL options
91
###############################################################################
92
SERVER={
93
    :name => "server",
94
    :short => "-S host",
95
    :large => "--server host",
96
    :format => String,
97
    :description => "MySQL server hostname or IP. Defaults to localhost",
98
    :proc => lambda { |o, options|
99
        options[:backend] = :mysql
100
        options[:server]  = o
101
    }
102
}
103

    
104
PORT={
105
    :name => "port",
106
    :short => "-P port",
107
    :large => "--port port",
108
    :format => String,
109
    :description => "MySQL server port. Defaults to 3306",
110
    :proc => lambda { |o, options|
111
        options[:backend] = :mysql
112
        options[:port]  = o
113
    }
114
}
115

    
116
USERNAME={
117
    :name => "username",
118
    :short => "-u user",
119
    :large => "--username user",
120
    :format => String,
121
    :description => "MySQL username",
122
    :proc => lambda { |o, options|
123
        options[:backend] = :mysql
124
        options[:user]    = o
125
    }
126
}
127

    
128
PASSWORD={
129
    :name => "password",
130
    :short => "-p pass",
131
    :large => "--password pass",
132
    :format => String,
133
    :description => "MySQL password. Leave unset to be prompted for it",
134
    :proc => lambda { |o, options|
135
        options[:backend] = :mysql
136
        options[:passwd]  = o
137
    }
138
}
139

    
140
DBNAME={
141
    :name => "dbname",
142
    :short => "-d dbname",
143
    :large => "--dbname dbname",
144
    :format => String,
145
    :description => "MySQL DB name for OpenNebula",
146
    :proc => lambda { |o, options|
147
        options[:backend] = :mysql
148
        options[:db_name] = o
149
    }
150
}
151

    
152
###############################################################################
153
# Slave MySQL options
154
###############################################################################
155
SLAVE_SERVER={
156
    :name => "slave-server",
157
    :large => "--slave-server host",
158
    :format => String,
159
    :description => "Slave MySQL server hostname or IP. Defaults to localhost",
160
    :proc => lambda { |o, options|
161
        options[:slave_backend] = :mysql
162
        options[:slave_server]  = o
163
    }
164
}
165

    
166
SLAVE_PORT={
167
    :name => "slave-port",
168
    :large => "--slave-port port",
169
    :format => String,
170
    :description => "Slave MySQL server port. Defaults to 3306",
171
    :proc => lambda { |o, options|
172
        options[:slave_backend] = :mysql
173
        options[:slave_port]  = o
174
    }
175
}
176

    
177
SLAVE_USERNAME={
178
    :name => "slave-username",
179
    :large => "--slave-username user",
180
    :format => String,
181
    :description => "Slave MySQL username",
182
    :proc => lambda { |o, options|
183
        options[:slave_backend] = :mysql
184
        options[:slave_user]    = o
185
    }
186
}
187

    
188
SLAVE_PASSWORD={
189
    :name => "slave-password",
190
    :large => "--slave-password pass",
191
    :format => String,
192
    :description => "Slave MySQL password. Leave unset to be prompted for it",
193
    :proc => lambda { |o, options|
194
        options[:slave_backend] = :mysql
195
        options[:slave_passwd]  = o
196
    }
197
}
198

    
199
SLAVE_DBNAME={
200
    :name => "slave-dbname",
201
    :large => "--slave-dbname dbname",
202
    :format => String,
203
    :description => "Slave MySQL DB name for OpenNebula",
204
    :proc => lambda { |o, options|
205
        options[:slave_backend] = :mysql
206
        options[:slave_db_name] = o
207
    }
208
}
209

    
210
SLAVE_BACKUP={
211
    :name => "slave-backup",
212
    :large => "--slave-backup file",
213
    :description => "Use this file to store SQL dump",
214
    :format => String
215
}
216

    
217
###############################################################################
218
# Extra options
219
###############################################################################
220

    
221
EXTRA={
222
    :name => "extra",
223
    :large => "--extra arg",
224
    :description => "Extra args",
225
    :format => Array
226
}
227

    
228

    
229
cmd=CommandParser::CmdParser.new(ARGV) do
230
    description <<-EOT.unindent
231
        This command enables the user to manage the OpenNebula database. It
232
        provides information about the DB version, means to upgrade it to the
233
        latest version, and backup tools.
234
    EOT
235

    
236
    ###########################################################################
237
    # Global options
238
    ###########################################################################
239
    set :option, CommandParser::OPTIONS
240
    set :option, [SQLITE, SERVER, PORT, USERNAME, PASSWORD, DBNAME]
241

    
242
    ###########################################################################
243
    # Backup
244
    ###########################################################################
245
    backup_desc = <<-EOT.unindent
246
        Dumps the DB to a file specified in the argument
247
    EOT
248

    
249
    command :backup, backup_desc, [:output_file, nil],
250
            :options=>[FORCE, FEDERATED] do
251

    
252
        begin
253
            helper = OneDB.new(options)
254
            helper.backup(args[0], options)
255
        rescue Exception => e
256
            [-1, e.message]
257
        end
258
    end
259

    
260
    ###########################################################################
261
    # Version
262
    ###########################################################################
263
    version_desc = <<-EOT.unindent
264
        Prints the current DB version.
265
        Use -v flag to see also OpenNebula version
266
    EOT
267

    
268
    command :version , version_desc do
269
        begin
270
            helper = OneDB.new(options)
271
            helper.version(options)
272
        rescue Exception => e
273
            [-1, e.message]
274
        end
275
    end
276

    
277
    ###########################################################################
278
    # History
279
    ###########################################################################
280
    history_desc = <<-EOT.unindent
281
        Prints the upgrades history
282
    EOT
283

    
284
    command :history , history_desc do
285
        begin
286
            helper = OneDB.new(options)
287
            helper.history
288
        rescue Exception => e
289
            [-1, e.message]
290
        end
291
    end
292

    
293
    ###########################################################################
294
    # Restore
295
    ###########################################################################
296
    restore_desc = <<-EOT.unindent
297
        Restores the DB from a backup file. Only restores backups generated
298
        from the same backend (SQLite or MySQL)
299
    EOT
300

    
301
    command :restore , restore_desc, [:backup_file, nil],
302
            :options=>[FORCE, FEDERATED] do
303

    
304
        begin
305
            helper = OneDB.new(options)
306
            helper.restore(args[0], options)
307
        rescue Exception => e
308
            [-1, e.message]
309
        end
310
    end
311

    
312
    ###########################################################################
313
    # Upgrade
314
    ###########################################################################
315
    upgrade_desc = <<-EOT.unindent
316
        Upgrades the DB to the latest version
317
        where <version> : DB version (e.g. 1, 3) to upgrade.
318
                          By default the DB is upgraded to the latest version
319
    EOT
320

    
321
    command :upgrade , upgrade_desc, [:version, nil], :options=>[FORCE,BACKUP] do
322
        begin
323
            helper = OneDB.new(options)
324
            helper.upgrade(args[0], options)
325
        rescue Exception => e
326
            [-1, e.message]
327
        end
328
    end
329

    
330
    ###########################################################################
331
    # fsck
332
    ###########################################################################
333
    fsck_desc = <<-EOT.unindent
334
        Checks the consistency of the DB, and fixes the problems found
335
    EOT
336

    
337
    command :fsck, fsck_desc, :options=>[FORCE,BACKUP] do
338
        begin
339
            helper = OneDB.new(options)
340
            helper.fsck(options)
341
        rescue Exception => e
342
            [-1, e.message]
343
        end
344
    end
345

    
346
    ###########################################################################
347
    # Import slave
348
    ###########################################################################
349
    import_slave_desc = <<-EOT.unindent
350
        Imports an existing federation slave into the federation master database
351
    EOT
352

    
353
    command :"import-slave", import_slave_desc, :options=>[FORCE,BACKUP,
354
        SLAVE_SERVER,SLAVE_PORT,SLAVE_USERNAME,SLAVE_PASSWORD,
355
        SLAVE_DBNAME,SLAVE_BACKUP] do
356

    
357
        begin
358
            helper = OneDB.new(options)
359
            helper.import_slave(options)
360
        rescue Exception => e
361
            [-1, e.message]
362
        end
363
    end
364

    
365
    ###########################################################################
366
    # Migrate vcenter 54
367
    ###########################################################################
368
    vcenter_one54_desc = <<-EOT.unindent
369
        Migrate VM and templates so they can be used by OpenNebula 5.4
370
    EOT
371

    
372
    command :"vcenter-one54", vcenter_one54_desc, :options=>[FORCE,BACKUP] do
373

    
374
        begin
375
            helper = OneDB.new(options)
376
            helper.vcenter_one54(options)
377
        rescue Exception => e
378
            [-1, e.message]
379
        end
380
    end
381

    
382
    ###########################################################################
383
    # Patch
384
    ###########################################################################
385
    patch_desc = <<-EOT.unindent
386
        Applies a database patch file
387
    EOT
388

    
389
    command :patch , patch_desc, :file, :options=>[BACKUP, EXTRA] do
390
        begin
391
            helper = OneDB.new(options)
392
            helper.patch(args[0], options)
393
        rescue Exception => e
394
            [-1, e.message]
395
        end
396
    end
397

    
398
    ###########################################################################
399
    # Migrate SQLite to MySQL
400
    ###########################################################################
401
    sqlite2mysql_desc = <<-EOT.unindent
402
        Migrates a SQLite OpenNebula Database to MySQL
403
    EOT
404

    
405
    command :sqlite2mysql , sqlite2mysql_desc, :options=>[BACKUP] do
406
        begin
407
            options[:backend] = :sqlite
408
            sqlite = OneDB.new(options)
409

    
410
            options[:backend] = :mysql
411
            mysql = OneDB.new(options)
412

    
413
            mysql.sqlite2mysql(options, sqlite)
414
        rescue Exception => e
415
            [-1, e.message]
416
        end
417
    end
418

    
419
    ###########################################################################
420
    # Purge history
421
    ###########################################################################
422
    purge_history_desc = <<-EOT.unindent
423
        Deletes all but the last history records from non DONE VMs
424
    EOT
425

    
426
    command :'purge-history' , purge_history_desc, :options=>[BACKUP] do
427
        begin
428
            action = OneDBLive.new
429
            action.purge_history
430
        rescue Exception => e
431
            pp e.backtrace
432
            [-1, e.message]
433
        end
434

    
435
        0
436
    end
437

    
438
    ###########################################################################
439
    # Purge VMs in DONE state
440
    ###########################################################################
441
    purge_done_desc = <<-EOT.unindent
442
        Deletes all VMs in DONE state
443
    EOT
444

    
445
    command :'purge-done' , purge_done_desc, :options=>[BACKUP] do
446
        begin
447
            action = OneDBLive.new
448
            action.purge_done_vm
449
        rescue Exception => e
450
            pp e.backtrace
451
            [-1, e.message]
452
        end
453

    
454
        0 # exit code
455
    end
456
end