Module: NexusCli::Tasks

Included in:
Cli
Defined in:
lib/nexus_cli/tasks.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/nexus_cli/tasks.rb', line 6

def self.included(base)
  base.send :include, ::Thor::Actions
  base.class_eval do

    map 'pull'          => :pull_artifact
    map 'push'          => :push_artifact
    map 'info'          => :get_artifact_info
    map 'custom'        => :get_artifact_custom_info
    map 'config'        => :get_nexus_configuration
    map 'status'        => :get_nexus_status
    map 'search'        => :search_for_artifacts
    map 'search_custom' => :search_artifacts_custom
    map 'transfer'      => :transfer_artifact

    class_option :overrides,
      :type => :hash,
      :default => nil,
      :desc => "A hashed list of overrides. Available options are 'url', 'repository', 'username', and 'password'."

    class_option :ssl_verify,
      :type => :boolean,
      :default => true,
      :desc => "Set to false to disable SSL Verification."

    method_option :destination,
      :type => :string,
      :default => nil,
      :desc => "A different folder other than the current working directory."
    desc "pull_artifact coordinates", "Pulls an artifact from Nexus and places it on your machine."
    def pull_artifact(coordinates)
      pull_artifact_response = nexus_remote.pull_artifact(coordinates, options[:destination])
      say "Artifact has been retrieved and can be found at path: #{pull_artifact_response[:file_path]}", :green
    end

    desc "push_artifact coordinates file", "Pushes an artifact from your machine onto the Nexus."
    def push_artifact(coordinates, file)
      nexus_remote.push_artifact(coordinates, file)
      say "Artifact #{coordinates} has been successfully pushed to Nexus.", :green
    end

    desc "get_artifact_info coordinates", "Gets and returns the metadata in XML format about a particular artifact."
    def get_artifact_info(coordinates)
      say nexus_remote.get_artifact_info(coordinates), :green
    end

    desc "search_for_artifacts", "Searches for all the versions of a particular artifact and prints it to the screen."
    def search_for_artifacts(coordinates)
      say nexus_remote.search_for_artifacts(coordinates), :green
    end

    desc "get_artifact_custom_info coordinates", "Gets and returns the custom metadata in XML format about a particular artifact."
    def get_artifact_custom_info(coordinates)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say nexus_remote.get_artifact_custom_info(coordinates), :green
    end

    desc "update_artifact_custom_info coordinates param1 param2 ...", "Updates the artifact custom metadata with the given key-value pairs."
    def update_artifact_custom_info(coordinates, *params)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      nexus_remote.update_artifact_custom_info(coordinates, *params)
      say "Custom metadata for artifact #{coordinates} has been successfully pushed to Nexus.", :green
    end

    desc "clear_artifact_custom_info coordinates", "Clears the artifact custom metadata."
    def clear_artifact_custom_info(coordinates)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      nexus_remote.clear_artifact_custom_info(coordinates)
      say "Custom metadata for artifact #{coordinates} has been successfully cleared.", :green
    end

    desc "search_artifacts_custom param1 param2 ... ", "Searches for artifacts using artifact metadata and returns the result as a list with items in XML format."
    def search_artifacts_custom(*params)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say (s = nexus_remote.search_artifacts_custom(*params)) == "" ? "No search results." : s, :green
    end

    desc "get_nexus_configuration", "Prints out configuration from the .nexus_cli file that helps inform where artifacts will be uploaded."
    def get_nexus_configuration
      config = nexus_remote.configuration
      say "********* Reading CLI configuration from #{File.expand_path('~/.nexus_cli')} *********", :blue
      say "Nexus URL: #{config['url']}", :blue
      say "Nexus Repository: #{config['repository']}", :blue
    end

    desc "get_nexus_status", "Prints out information about the Nexus instance."
    def get_nexus_status
      data = nexus_remote.status
      say "********* Getting Nexus status from #{data['base_url']} *********", :blue
      say "Application Name: #{data['app_name']}", :blue
      say "Version: #{data['version']}", :blue
      say "Edition: #{data['edition_long']}", :blue
      say "State: #{data['state']}", :blue
      say "Started At: #{data['started_at']}", :blue
      say "Base URL: #{data['base_url']}", :blue
    end

    desc "get_global_settings", "Prints out your Nexus' current setttings and saves them to a file."
    def get_global_settings
      nexus_remote.get_global_settings
      say "Your current Nexus global settings have been written to the file: ~/.nexus/global_settings.json", :blue
    end

    method_option :json,
      :type => :string,
      :default => nil,
      :desc => "A String of the JSON you wish to upload."
    desc "upload_global_settings", "Uploads a global_settings.json file to your Nexus to update its settings."
    def upload_global_settings
      nexus_remote.upload_global_settings(options[:json])
      say "Your global_settings.json file has been uploaded to Nexus", :blue
    end

    desc "reset_global_settings", "Resets your Nexus global_settings to their out-of-the-box defaults."
    def reset_global_settings
      nexus_remote.reset_global_settings
      say "Your Nexus global settings have been reset to their default values", :blue
    end

    method_option :id,
      :type => :string,
      :desc => "The id of the repository to use."
    method_option :policy,
      :type => :string,
      :desc => "Repo policy [RELEASE|SNAPSHOT], RELEASE by default"
    method_option :provider,
      :type => :string,
      :desc => "Repo provider (maven2 by default)"
    method_option :proxy,
      :type => :boolean,
      :desc => "True if the new repository should be a proxy repository"
    method_option :url,
      :type => :string,
      :desc => "The url of the actual repository for the proxy repository to use."
    desc "create_repository name", "Creates a new Repository with the provided name."
    def create_repository(name)
      if nexus_remote.create_repository(name, options[:proxy], options[:url], options[:id], options[:policy], options[:provider])
        say "A new Repository named #{name} has been created.", :blue
      end
    end

    desc "delete_repository name", "Deletes a Repository with the provided name."
    def delete_repository(name)
      if nexus_remote.delete_repository(name)
        say "The Repository named #{name} has been deleted.", :blue
      end
    end

    desc "get_repository_info name", "Finds and returns information about the provided Repository."
    def get_repository_info(name)
      say nexus_remote.get_repository_info(name), :green
    end

    desc "get_users", "Returns XML representing the users in Nexus."
    def get_users
      say nexus_remote.get_users, :green
    end

    method_option :username,
      :type => :string,
      :default => nil,
      :desc => "The username."
    method_option :first_name,
      :type => :string,
      :default => nil,
      :desc => "The first name."
    method_option :last_name,
      :type => :string,
      :default => nil,
      :desc => "The last name."
    method_option :email,
      :type => :string,
      :default => nil,
      :desc => "The email."
    method_option :password,
      :type => :string,
      :default => nil,
      :desc => "The password."
    method_option :enabled,
      :type => :boolean,
      :default => nil,
      :desc => "Whether this new user is enabled or disabled."
    method_option :roles,
      :type => :array,
      :default => [],
      :require => false,
      :desc => "An array of roles."
    desc "create_user", "Creates a new user"
    def create_user
      params = ask_user(options)

      if nexus_remote.create_user(params)
        say "A user with the ID of #{params[:userId]} has been created.", :blue
      end
    end

    method_option :username,
      :type => :string,
      :default => nil,
      :desc => "The username."
    method_option :first_name,
      :type => :string,
      :default => nil,
      :desc => "The first name."
    method_option :last_name,
      :type => :string,
      :default => nil,
      :desc => "The last name."
    method_option :email,
      :type => :string,
      :default => nil,
      :desc => "The email."
    method_option :enabled,
      :type => :boolean,
      :default => nil,
      :desc => "Whether this new user is enabled or disabled."
    method_option :roles,
      :type => :array,
      :default => [],
      :require => false,
      :desc => "An array of roles."
    desc "update_user user_id", "Updates a user's details. Leave fields blank for them to remain their current values."
    def update_user(user_id)
      params = ask_user(options, false, false)
      params[:userId] = user_id

      if nexus_remote.update_user(params)
        say "User #{user_id} has been updated.", :blue
      end
    end

    desc "delete_user user_id", "Deletes the user with the given id."
    def delete_user(user_id)
      if nexus_remote.delete_user(user_id)
        say "User #{user_id} has been deleted.", :blue
      end
    end

    method_option :oldPassword,
      :type => :string,
      :default => nil,
      :desc => ""
    method_option :newPassword,
      :type => :string,
      :default => nil,
      :desc => ""
    desc "change_password user_id", "Changes the given user's passwords to a new one."
    def change_password(user_id)

      oldPassword = options[:oldPassword]
      newPassword = options[:newPassword]

      if oldPassword.nil?
        oldPassword = ask_password("Please enter your old password:")
      end
      if newPassword.nil?
        newPassword = ask_password("Please enter your new password:")
      end

      params = {:userId => user_id}
      params[:oldPassword] = oldPassword
      params[:newPassword] = newPassword
      if nexus_remote.change_password(params)
        say "The password for user #{user_id} has been updated.", :blue
      end
    end

    desc "get_pub_sub repository_id", "Returns the publish/subscribe status of the given repository."
    def get_pub_sub(repository_id)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say nexus_remote.get_pub_sub(repository_id), :green
    end

    desc "enable_artifact_publish repository_id", "Sets a repository to enable the publishing of updates about its artifacts."
    def enable_artifact_publish(repository_id)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      if nexus_remote.enable_artifact_publish(repository_id)
        say "The repository #{repository_id} will now publish updates.", :blue
      end
    end

    desc "disable_artifact_publish repository_id", "Sets a repository to disable the publishing of updates about its artifacts."
    def disable_artifact_publish(repository_id)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      if nexus_remote.disable_artifact_publish(repository_id)
        say "The repository #{repository_id} is no longer publishing updates.", :blue
      end
    end

    method_option :preemptive_fetch,
      :type => :boolean,
      :default => false,
      :desc => "Subscribing repositories that preemtively fetch will grab artifacts as updates are received."
    desc "enable_artifact_subscribe repository_id", "Sets a repository to subscribe to updates about artifacts."
    def enable_artifact_subscribe(repository_id)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      if nexus_remote.enable_artifact_subscribe(repository_id, options[:preemptive_fetch])
        say "The repository #{repository_id} is now subscribed for artifact updates.", :blue
      end
    end

    desc "disable_artifact_subscribe repository_id", "Sets a repository to stop subscribing to updates about artifacts."
    def disable_artifact_subscribe(repository_id)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      if nexus_remote.disable_artifact_subscribe(repository_id)
        say "The repository #{repository_id} is no longer subscribed for artifact updates.", :blue
      end
    end

    method_option :host,
      :type => :string,
      :desc => "An IP address for the Nexus server at which publishing will be available."
    method_option :port,
      :type => :numeric,
      :desc => "An available port that will be used for Smart Proxy connections."
    desc "enable_smart_proxy", "Enables Smart Proxy on the server."
    def enable_smart_proxy
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say nexus_remote.enable_smart_proxy(options[:host], options[:port])
    end

    desc "disable_smart_proxy", "Disables Smart Proxy on the server."
    def disable_smart_proxy
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say nexus_remote.disable_smart_proxy
    end

    desc "get_smart_proxy_settings", "Returns the Smart Proxy settings of the server."
    def get_smart_proxy_settings
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say JSON.pretty_generate(JSON.parse(nexus_remote.get_smart_proxy_settings)), :green
    end

    method_option :certificate,
      :type => :string,
      :required => :true,
      :desc => "A path to a file containing a certificate."
    method_option :description,
      :type => :string,
      :required => true,
      :desc => "A description to give to the trusted key. It is probably best to make this meaningful."
    desc "add_trusted_key", "Adds a new trusted key to the Smart Proxy configuration."
    def add_trusted_key
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      if nexus_remote.add_trusted_key(options[:certificate], options[:description])
        say "A new trusted key has been added to the nexus.", :blue
      end
    end

    desc "delete_trusted_key key_id", "Deletes a trusted key using the given key_id."
    def delete_trusted_key(key_id)
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      if nexus_remote.delete_trusted_key(key_id)
        say "The trusted key with an id of #{key_id} has been deleted.", :blue
      end
    end

    desc "get_trusted_keys", "Returns the trusted keys of the server."
    def get_trusted_keys
      raise NotNexusProException unless nexus_remote.kind_of? ProRemote
      say JSON.pretty_generate(JSON.parse(nexus_remote.get_trusted_keys)), :green
    end

    desc "get_license_info", "Returns the license information of the server."
    def get_license_info
      say nexus_remote.get_license_info, :green
    end

    desc "install_license license_file", "Installs a license file into the server."
    def install_license(license_file)
      nexus_remote.install_license(license_file)
    end

    desc "get_logging_info", "Gets the log4j Settings of the Nexus server."
    def get_logging_info
      say nexus_remote.get_logging_info, :green
    end

    desc "set_logger_level level", "Updates the log4j logging level to a new value."
    def set_logger_level(level)
      if nexus_remote.set_logger_level(level)
        say "The logging level of Nexus has been set to #{level.upcase}", :blue
      end
    end

    desc "create_group_repository name", "Creates a new repository group with the given name."
    method_option :id,
      :type => :string,
      :desc => "The id of the group repository to use (calculated from name by default)."
    method_option :provider,
      :type => :string,
      :desc => "Group repo provider (maven2 by default)."
    def create_group_repository(name)
      if nexus_remote.create_group_repository(name, options[:id], options[:provider])
        say "A new group repository named #{name} has been created.", :blue
      end
    end

    desc "get_group_repository group_id", "Gets information about the given group repository."
    def get_group_repository(group_id)
      say nexus_remote.get_group_repository(group_id), :green
    end

    desc "add_to_group_repository group_id repository_to_add_id", "Adds a repository with the given id into the group repository."
    def add_to_group_repository(group_id, repository_to_add_id)
      if nexus_remote.add_to_group_repository(group_id, repository_to_add_id)
        say "The repository #{repository_to_add_id} has been added to the repository group #{group_id}", :blue
      end
    end

    desc "remove_from_group_repository group_id repository_to_remove_id", "Remove a repository with the given id from the group repository."
    def remove_from_group_repository(group_id, repository_to_remove_id)
      if nexus_remote.remove_from_group_repository(group_id, repository_to_remove_id)
        say "The repository with an id of #{repository_to_remove_id} has been removed from the group repository, #{group_id}.", :blue
      end
    end

    desc "delete_group_repository group_id","Deletes a group repository based on the given id."
    def delete_group_repository(group_id)
      if nexus_remote.delete_group_repository(group_id)
        say "The group repository, #{group_id} has been deleted.", :blue
      end
    end

    desc "transfer_artifact coordinates from_repository to_repository", "Transfers a given artifact from one repository to another."
    def transfer_artifact(coordinates, from_repository, to_repository)
      if nexus_remote.transfer_artifact(coordinates, from_repository, to_repository)
        say "The artifact #{coordinates} has been transferred from #{from_repository} to #{to_repository}.", :blue
      end
    end

    desc "get_artifact_download_url coordinates", "Gets the Nexus download URL for the given artifact."
    def get_artifact_download_url(coordinates)
      say nexus_remote.get_artifact_download_url(coordinates), :green
    end

    private

      def nexus_remote
        begin
          nexus_remote ||= RemoteFactory.create(options[:overrides], options[:ssl_verify])
        rescue NexusCliError => e
          say e.message, :red
          exit e.status_code
        end
      end

      def ask_user(params, ask_username=true, ask_password=true)
        username = params[:username]
        first_name = params[:first_name]
        last_name = params[:last_name]
        email = params[:email]
        enabled = params[:enabled]
        password = params[:password]
        roles = params[:roles]
        status = enabled

        if username.nil? && ask_username
          username = ask "Please enter the username:"
        end
        if first_name.nil?
          first_name = ask "Please enter the first name:"
        end
        if last_name.nil?
          last_name = ask "Please enter the last name:"
        end
        if email.nil?
          email = ask "Please enter the email:"
        end
        if enabled.nil?
          status = ask "Is this user enabled for use?", :limited_to => ["true", "false"]
        end
        if password.nil? && ask_password
          password = ask_password("Please enter a password:")
        end
        if roles.size == 0
          roles = ask "Please enter the roles:"
        end
        params = {:userId => username}
        params[:firstName] = first_name
        params[:lastName] = last_name
        params[:email] = email
        params[:status] = status == true ? "active" : "disabled"
        params[:password] = password
        params[:roles] = roles.kind_of?(Array) ? roles : roles.split(' ')
        params
      end

      def ask_password(message)
        HighLine.new.ask(message) do |q|
          q.echo = false
        end
      end
  end
end