-
Notifications
You must be signed in to change notification settings - Fork 982
/
foreman-admin
executable file
·183 lines (154 loc) · 4.92 KB
/
foreman-admin
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
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
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'clamp'
module ForemanAdmin
class Command < ::Clamp::Command
def self.command_name(name = nil)
@command_name = name if name
@command_name
end
def command_name
self.class.command_name
end
def self.description(description = nil)
@description = description if description
@description
end
def description
self.class.description
end
def execute
# override me
end
end
class ExternalCommand < Command
# The base command that will be executed externally.
# Think of this as ARGV[0] of what we're running externally.
def self.external_invocation(invocation = nil)
@external_invocation = invocation if invocation
@external_invocation
end
def external_invocation
self.class.external_invocation
end
# The exact assembled command that will be executed externally.
# Override this in a child class if you want it have logic.
def external_command
"#{external_invocation}"
end
def execute
puts `#{external_command}`
end
end
class BackupCommand < ForemanAdmin::ExternalCommand
command_name 'backup'
description 'Backup your Foreman server'
external_invocation '/usr/sbin/foreman-backup'
# TODO: any options?
#
# or parameters?
end
class RestoreCommand < ForemanAdmin::ExternalCommand
command_name 'restore'
description 'Restore your Foreman server'
external_invocation '/usr/sbin/foreman-restore'
# TODO: any options?
#
# or parameters?
end
class DebugCommand < ForemanAdmin::ExternalCommand
UPLOAD_RESPONSE = 'Archive has been uploaded'
NO_UPLOAD_RESPONSE = 'Skipping archive upload'
command_name 'generate-debug'
description 'Create a foreman-debug tarball for debugging purposes'
external_invocation '/usr/sbin/foreman-debug'
option ['-d', '--directory'],
'DIR',
'Directory to place the tarball in (default: /tmp/foreman-XYZ)'
option ['-g', '--[no-]generic'],
:flag,
'Whether or not to include generic info (CPU, memory, firewall, etc)',
:default => true
option ['-a', '--[no-]tarball'],
:flag,
'Whether to generate a tarball from the resulting directory',
:default => true
option ['-m', '--max-lines'],
'LINES',
'Maximum lines to keep for each file (default: 5000)'
option ['-j', '--filter-program'],
'PROGRAM',
'Filter with provided program when creating a tarball'
option ['-p', '--password-patterns'],
:flag,
'Print password patterns being filtered out'
option ['-q', '--quiet'],
:flag,
'Quiet mode'
option ['-v', '--verbose'],
:flag,
'Verbose mode'
option ['-u', '--[no-]upload'],
:flag,
'Whether to upload archive to rsync://theforeman.org/debug-incoming',
:default => false
def external_command
args = []
args << '-d' << "'#{directory}'" if directory
args << '-g' unless generic?
args << '-a' unless tarball?
args << '-m' << max_lines if max_lines
args << '-j' << "'#{filter_program}'" if filter_program
args << '-p' if password_patterns?
args << '-q' if quiet?
args << '-v' if verbose?
args << '-u' if upload?
args.unshift(external_invocation)
args.join(' ')
end
end
class TaskExportCommand < ForemanAdmin::ExternalCommand
command_name 'export-tasks'
description "Export a file containing Foreman's task data (default: only incomplete tasks are exported)"
external_invocation '/usr/sbin/foreman-rake foreman_tasks:export_tasks'
option ['--all'], :flag, 'Export all tasks (Note: this could take some time)'
option ['--export-file'], 'FILENAME', 'Specify the filename.tar.gz archive to export tasks into'
def external_command
args = []
args << 'tasks=all' if all?
args << "export=#{export_file}" if export_file
args.unshift(external_invocation)
args.join(' ')
end
end
class UpdateCommand < ForemanAdmin::ExternalCommand
command_name 'update'
description 'Update your Foreman server'
external_invocation '/usb/sbin/foreman-update fancy-style'
# TODO: any options?
#
# or parameters?
end
class MainCommand < ForemanAdmin::Command
# TODO: uncomment once these external scripts are in place
# subcommand BackupCommand.command_name,
# BackupCommand.description,
# BackupCommand
# subcommand RestoreCommand.command_name,
# RestoreCommand.description,
# RestoreCommand
subcommand DebugCommand.command_name,
DebugCommand.description,
DebugCommand
subcommand TaskExportCommand.command_name,
TaskExportCommand.description,
TaskExportCommand
# subcommand UpdateCommand.command_name,
# UpdateCommand.description,
# UpdateCommand
end
end
if __FILE__ == $0
ForemanAdmin::MainCommand.run
end