Class: Jenkins2::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins2/command_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CommandLine

Returns a new instance of CommandLine.



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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/jenkins2/command_line.rb', line 11

def initialize( args )
  @global_options = OptionParser::OptionMap.new
  @log_options = { verbose: 0, log: STDOUT }
  @command_options = OptionParser::OptionMap.new
  global = CommandParser.new 'Usage: jenkins [global-options] <command> [options]' do |opts|
    opts.separator ''
    opts.separator "Global options (accepted by all commands):"
    opts.on '-s', '--server URL', ::URI, 'Jenkins Server Url' do |opt|
      @global_options[:server] = opt
    end
    opts.on '-u', '--user USER', 'Jenkins API user' do |opt|
      @global_options[:user] = opt
    end
    opts.on '-k', '--key KEY', 'Jenkins API key' do |opt|
      @global_options[:key] = opt
    end
    opts.on '-c', '--config [PATH]', 'Use configuration file. Instead of providing '\
      'server, user and key through command line, you can do that with configuration file. '\
      'File format is json: { "server": "http://jenkins.example.com", "user": "admin", '\
      '"key": "123456" }. Options provided in command line will overwrite ones from '\
      'configuration file. Program looks for ~/.jenkins2.json if no PATH is provided.' do |opt|
      @global_options[:config] = opt || ::File.join( ENV['HOME'], '.jenkins2.json' )
    end
    opts.on '-l', '--log FILE', 'Log file. Prints to standard out, if not provided' do |opt|
      @log_options[:log] = opt
    end
    opts.on '-v', '--verbose', 'Print more info. Up to -vvv. Prints only errors by default.' do
      @log_options[:verbose] += 1
    end
    opts.on '-h', '--help', 'Show help' do
      @global_options[:help] = true
    end
    opts.on '-V', '--version', 'Show version' do
      puts VERSION
      exit
    end

    opts.separator ''
    opts.separator 'For command specific options run: jenkins2 --help <command>'
    opts.separator ''
    opts.command 'version', 'Outputs the current version of Jenkins'
    opts.command 'prepare-for-shutdown', 'Stop executing new builds, so that the system can '\
      'be eventually shut down safely.'
    opts.command 'cancel-shutdown', 'Cancel the effect of "prepare-for-shutshow" command.'
    opts.command 'wait-nodes-idle', 'Wait for all nodes to become idle. Is expected to be '\
      'called after "prepare_for_shutdown", otherwise new builds will still be run.' do |cmd|
      cmd.on '-m', '--max-wait-minutes INT', Integer, 'Wait for INT minutes at most. '\
        'Default 60' do |opt|
        @command_options[:max_wait_minutes] = opt
      end
    end
    opts.command 'offline-node', 'Stop using a node for performing builds temporarily, until '\
      'the next "online-node" command.' do |cmd|
      cmd.on '-n', '--node NAME', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
      cmd.on '-m', '--message MESSAGE', 'Record the note about why you are '\
        'disconnecting this node' do |opt|
        @command_options[:message] = opt
      end
    end
    opts.command 'online-node', 'Resume using a node for performing builds, to cancel out '\
      'the earlier "offline-node" command.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
    end
    opts.command 'connect-node', 'Connects a node, i.e. starts Jenkins slave on a node.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
    end
    opts.command 'create-node', 'Creates a new node from XML' do |cmd|
      cmd.on '-n', '--node NAME', 'Name of the new node' do |opt|
        @command_options[:node] = opt
      end
      cmd.on '-x', '--xml FILE', 'Path to XML configuration file' do |opt|
        @command_options[:xml] = IO.read opt
      end
    end
    opts.command 'delete-node', 'Deletes a node' do |cmd|
      cmd.on '-n', '--node NAME', 'Node name' do |opt|
        @command_options[:node] = opt
      end
    end
    opts.command 'create-credential', 'Creates credential.' do |cmd|
      cmd.on '-S', '--scope SCOPE', 'GLOBAL or SYSTEM scope' do |opt|
        @command_options[:scope] = opt
      end
      cmd.on '-i', '--id ID', 'Unique Id of credential. Will be generated automactically, if '\
        'not provided' do |opt|
        @command_options[:id] = opt
      end
      cmd.on '-d', '--description DESC', 'Human readable text, what this credential is used for.' do |opt|
        @command_options[:description] = opt
      end
      cmd.on '-n', '--username NAME', 'Username for Username-Password or SSH credential' do |opt|
        @command_options[:username] = opt
      end
      cmd.on '-p', '--password PASS', 'Password in plain text for Username-Password credential' do |opt|
        @command_options[:password] = opt
      end
      cmd.on '-f', '--private-key FILE', 'Path to private key file for SSH credential' do |opt|
        @command_options[:private_key] = IO.read( opt ).gsub( "\n", "\\n" )
      end
      cmd.on '-P', '--passphrase PHRASE', 'Passphrase for the private key for SSH credential' do |opt|
        @command_options[:passphrase] = opt
      end
      cmd.on '-e', '--secret SECRET', 'Some secret text for Secret credential' do |opt|
        @command_options[:secret] = opt
      end
      cmd.on '-F', '--secret-file FILE', 'Path to secret file for Secret File credential' do |opt|
        @command_options[:filename] = File.basename opt
        @command_options[:content] = IO.read opt
      end
    end
    opts.command 'delete-credential', 'Deletes credential.' do |cmd|
      cmd.on '-i', '--id ID', 'Credential id' do |opt|
        @command_options[:id] = opt
      end
    end
    opts.command 'get-credential', 'Returns credential as json.' do |cmd|
      cmd.on '-i', '--id ID', 'Credential id' do |opt|
        @command_options[:id] = opt
      end
    end
    opts.command 'list-credentials', 'Lists all credentials.'
    opts.command 'disconnect-node', 'Disconnects a node.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
      cmd.on '-m', '--message MESSAGE', 'Reason, why the node is being disconnected.' do |opt|
        @command_options[:message] = opt
      end
    end
    opts.command 'wait-node-idle', 'Wait for the node to become idle. Make sure you run '\
      '"offline-node" first.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
      cmd.on '-m', '--max-wait-minutes INT', Integer, 'Wait for INT minutes at most. '\
        'Default 60' do |opt|
        @command_options[:max_wait_minutes] = opt
      end
    end
    opts.command 'get-node-xml', 'Returns the node definition XML.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
    end
    opts.command 'get-node', 'Returns the node state.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
    end
    opts.command 'update-node', 'Updates the node definition XML from stdin or file.' do |cmd|
      cmd.on '-n', '--node [NAME]', 'Name of the node or empty for master' do |opt|
        @command_options[:node] = opt
      end
      cmd.on '-x', '--xml-config FILE', 'File to read definition from. Omit this to read from stdin' do |opt|
        @command_options[:xml_config] = IO.read( opt )
      end
    end
    opts.command 'build', 'Starts a build.' do |cmd|
      cmd.on '-j', '--job NAME', 'Name of the job' do |opt|
        @command_options[:job] = opt
      end
      cmd.on '-p', '--params KEY=VALUE[,KEY=VALUE...]', Array, 'Build parameters, where keys are'\
        ' names of variables' do |opt|
        @command_options[:params] = opt.collect{|i| i.split( '=', 2 ) }.to_h
      end
    end
    opts.command 'install-plugin', 'Installs a plugin from url or by short name. '\
      'Provide either --url or --name.' do |cmd|
      cmd.on '-u', '--uri URI', ::URI, 'Uri to install plugin from.' do |opt|
        @command_options[:uri] = opt
      end
      cmd.on '-n', '--name SHORTNAME', 'Plugin short name (like thinBackup).' do |opt|
        @command_options[:name] = opt
      end
    end
    opts.command 'list-plugins', 'Lists installed plugins'
  end
  begin
    global.parse!( args )
    @global_options[:command] = global.command_name
    if @global_options[:config]
      from_file = JSON.parse( IO.read( @global_options[:config] ), symbolize_names: true )
      @global_options = from_file.merge( @global_options )
    end
    Log.init @log_options
    if @global_options[:help]
      Log.unknown { global.help }
      exit
    end
    raise OptionParser::MissingArgument, :command unless @global_options[:command]
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
    Log.fatal { e.message }
    Log.fatal { global.help }
    exit 1
  end
  Log.debug { "Options: #{@global_options}\nUnparsed args: #{ARGV}" }
end

Instance Attribute Details

#command_optionsObject

Returns the value of attribute command_options.



9
10
11
# File 'lib/jenkins2/command_line.rb', line 9

def command_options
  @command_options
end

#global_optionsObject

Returns the value of attribute global_options.



8
9
10
# File 'lib/jenkins2/command_line.rb', line 8

def global_options
  @global_options
end

Instance Method Details

#runObject



215
216
217
218
# File 'lib/jenkins2/command_line.rb', line 215

def run
  jc = Client.new( @global_options )
  jc.send( @global_options[:command].gsub( '-', '_' ), @command_options )
end