-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathmaint.rb
154 lines (138 loc) · 5.76 KB
/
maint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
module Nexpose
class Connection
# Retrieve a list of all backups currently stored on the Console.
#
# @return [Array[Backup]] List of backups.
#
def list_backups
data = DataTable._get_dyn_table(self, '/data/admin/backups?tableID=BackupSynopsis')
data.map { |b| Backup.parse(b) }
end
# Create a backup of this security console's data.
# A restart will be initiated in order to put the product into maintenance
# mode while the backup is made. It will then restart automatically.
#
# @param [Boolean] platform_independent Whether to make a platform
# independent backup.
# @param [String] description A note about this backup which will be
# visible through the web interface.
# @return [Boolean] Whether a backup is successfully initiated.
#
def backup(platform_independent = false, description = nil)
parameters = { 'backup_desc' => description,
'cmd' => 'backup',
'platform_independent' => platform_independent,
'targetTask' => 'backupRestore' }
xml = AJAX.form_post(self, '/admin/global/maintenance/maintCmd.txml', parameters)
if !!(xml =~ /succeded="true"/)
_maintenance_restart
end
end
# Initiate database maintenance tasks to improve database performance and
# consistency.
# A restart will be initiated in order to put the product into maintenance
# mode while the tasks are run. It will then restart automatically.
#
# @param [Boolean] clean_up Removes any unnecessary data from the database.
# @param [Boolean] compress Compresses the database tables and reclaims
# unused, allocated space.
# @param [Boolean] reindex Drops and recreates the database indexes for
# improved performance.
# @return [Boolean] Whether a maintenance tasks are successfully initiated.
#
def db_maintenance(clean_up = false, compress = false, reindex = false)
return unless compress || clean_up || reindex
parameters = { 'cmd' => 'startMaintenance', 'targetTask' => 'dbMaintenance' }
parameters['cleanup'] = 1 if clean_up
parameters['compress'] = 1 if compress
parameters['reindex'] = 1 if reindex
xml = AJAX.form_post(self, '/admin/global/maintenance/maintCmd.txml', parameters)
if !!(xml =~ /succeded="true"/)
_maintenance_restart
end
end
def _maintenance_restart
parameters = { 'cancelAllTasks' => false,
'cmd' => 'restartServer',
'targetTask' => 'maintModeHandler' }
xml = AJAX.form_post(self, '/admin/global/maintenance/maintCmd.txml', parameters)
!!(xml =~ /succeded="true"/)
end
end
# Details about an existing backup on the security console.
#
class Backup
# Filename
attr_reader :name
# Date the backup was made.
attr_reader :date
# Description of the backup.
attr_reader :description
# Nexpose version the console was on when the backup was made.
attr_reader :version
# Whether the backup is platform-idependent or not.
attr_reader :platform_independent
# Size of backup file on disk, in Bytes. Can be used to estimate the amount
# of time the backup may take to load.
attr_reader :size
def initialize(name, date, description, version, independent, size)
@name = name
@date = date
@description = description
@version = version
@platform_independent = independent
@size = size
end
# Restore this backup to the Nexpose console.
# It will restart the console after acknowledging receiving the request.
#
# @param [Connection] nsc An active connection to a Nexpose console.
# @param [String] (Optional) The password to use when restoring the backup.
# @return [Boolean] Whether the request was received.
#
def restore(nsc, password = nil)
raise "Supplied Password is incorrect for restoring this Backup." if invalid_backup_password?(nsc, password)
parameters = { 'backupid' => @name,
'cmd' => 'restore',
'targetTask' => 'backupRestore',
'password' => password }
xml = AJAX.form_post(nsc, '/admin/global/maintenance/maintCmd.txml', parameters)
if !!(xml =~ /succeded="true"/)
nsc._maintenance_restart
end
end
# Remove this backup file from the security console.
#
# @param [Connection] nsc An active connection to a Nexpose console.
# @return [Boolean] If the backup was removed.
#
def delete(nsc)
parameters = { 'backupid' => @name,
'cmd' => 'deleteBackup',
'targetTask' => 'backupRestore' }
xml = AJAX.form_post(nsc, '/admin/global/maintenance/maintCmd.txml', parameters)
!!(xml =~ /succeded="true"/)
end
def self.parse(hash)
new(hash['Download'],
Time.at(hash['Date'].to_i / 1000),
hash['Description'],
hash['Version'],
hash['Platform-Independent'],
hash['Size'])
end
private
def invalid_backup_password?(nsc, password)
!correct_backup_password?(nsc, password) if backup_need_password?(nsc)
end
def backup_need_password?(nsc)
resp = Nexpose::AJAX.get(nsc, '/data/admin/backups/password', Nexpose::AJAX::CONTENT_TYPE::JSON, 'backupID' => name)
resp == 'true'
end
def correct_backup_password?(nsc, password)
raise 'This Backup files requires a Password. Please include a password during the restore command.' if password.nil?
resp = Nexpose::AJAX.post(nsc, "/data/admin/backups/password?backupID=#{name}&password=#{password}", nil, Nexpose::AJAX::CONTENT_TYPE::JSON)
resp == 'true'
end
end
end