Class: NanoKVM::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/nanokvm/cli.rb

Constant Summary collapse

CONFIG_DIR =
File.expand_path("~/.config/nanokvm")
CONFIG_FILE =
File.join(CONFIG_DIR, "config.yml")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/nanokvm/cli.rb', line 15

def self.exit_on_failure?
  true
end

Instance Method Details

#accountObject



163
164
165
# File 'lib/nanokvm/cli.rb', line 163

def 
  display_as_table("NanoKVM Account Information", :get_account_info)
end

#cdromObject



324
325
326
# File 'lib/nanokvm/cli.rb', line 324

def cdrom
  display_as_table("CD-ROM Information", :get_cdrom_info)
end

#configureObject



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
# File 'lib/nanokvm/cli.rb', line 28

def configure
  unless Dir.exist?(CONFIG_DIR)
    FileUtils.mkdir_p(CONFIG_DIR)
  end

  # Load existing config
  config = if File.exist?(CONFIG_FILE)
            File.open(CONFIG_FILE) { |f| YAML.safe_load(f, symbolize_names: true) } rescue {}
          else
            {}
          end

  # Update with provided options
  config[:host] = options[:host] if options[:host]
  config[:username] = options[:username] if options[:username]
  config[:password] = options[:password] if options[:password]
  config[:token] = options[:token] if options[:token]
  config[:secret_key] = options[:secret_key] if options[:secret_key]

  # Save config
  File.open(CONFIG_FILE, "w") { |f| f.write(config.to_yaml) }

  # Update current configuration
  NanoKVM.configure do |c|
    c.default_host = config[:host]
    c.default_username = config[:username]
    c.default_password = config[:password]
  end

  puts "Configuration saved"
end

#download_enabledObject



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/nanokvm/cli.rb', line 221

def download_enabled
  client = authenticated_client
  begin
    response = client.is_download_image_enabled["data"]
    enabled = response["enabled"] == true
    puts "Image download: #{enabled ? 'ENABLED' : 'DISABLED'}"
  rescue NanoKVM::ApiError => e
    puts "Error checking image download status: #{e.message}"
    puts "This feature may not be available on your NanoKVM device/firmware."
  end
end

#download_image(url) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/nanokvm/cli.rb', line 254

def download_image(url)
  client = authenticated_client
  begin
    puts "Starting download of #{url}..."
    client.download_image(url)
    puts "Download initiated successfully."
    puts "Use 'nanokvm download-status' to check progress."
  rescue NanoKVM::ApiError => e
    puts "Error starting image download: #{e.message}"
    puts "This feature may not be available on your NanoKVM device/firmware."
  end
end

#download_statusObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/nanokvm/cli.rb', line 237

def download_status
  client = authenticated_client
  begin
    if options[:monitor]
      show_download_progress(client, options[:interval], options[:timeout])
    else
      display_download_status(client)
    end
  rescue NanoKVM::ApiError => e
    puts "Error checking image download status: #{e.message}"
    puts "This feature may not be available on your NanoKVM device/firmware."
  rescue Interrupt
    puts "\nMonitoring stopped"
  end
end

#hardwareObject



268
269
270
# File 'lib/nanokvm/cli.rb', line 268

def hardware
  display_as_table("NanoKVM Hardware Information", :get_hardware_info)
end

#hid_modeObject



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/nanokvm/cli.rb', line 330

def hid_mode
  client = authenticated_client
  
  if options[:mode].nil?
    # Get current setting
    display_as_table("HID Mode", :get_hid_mode)
  else
    # Set new mode
    unless ["usb", "ps2"].include?(options[:mode].downcase)
      puts "Error: Mode must be 'usb' or 'ps2'"
      return
    end
    
    begin
      client.set_hid_mode(options[:mode].downcase)
      puts "HID mode updated to #{options[:mode]}"
      puts "Note: This change requires a reboot to take effect"
    rescue NanoKVM::ApiError => e
      puts "Error setting HID mode: #{e.message}"
      puts "This feature may not be available on your NanoKVM device/firmware."
    end
  end
end

#imagesObject



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
# File 'lib/nanokvm/cli.rb', line 86

def images
  client = create_client
  ensure_authenticated(client)

  begin
    # Get available images
    available = client.get_available_images["data"]["files"] rescue []
    
    # Get mounted image for comparison
    mounted = begin
      client.get_mounted_image["data"]["file"]
    rescue
      nil
    end

    rows = []
    if available.nil?
      puts "No images available"
      return
    end
    
    available.each do |image|
      rows << [image, image == mounted ? "MOUNTED" : ""]
    end

    if rows.empty?
      puts "No images available"
      return
    end

    table = ::Terminal::Table.new(
      title: "Available Images",
      headings: ["Image Path", "Status"],
      rows: rows
    )

    puts table
  rescue NanoKVM::ApiError => e
    puts "Error listing images: #{e.message}"
    puts "This feature may not be available on your NanoKVM device/firmware."
  end
end

#infoObject



61
62
63
# File 'lib/nanokvm/cli.rb', line 61

def info
  display_as_table("NanoKVM Information", :get_device_info)
end

#interfacesObject



416
417
418
# File 'lib/nanokvm/cli.rb', line 416

def interfaces
  display_as_table("Interface Status", :get_interface_status)
end

#is_password_updatedObject



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/nanokvm/cli.rb', line 203

def is_password_updated
  client = authenticated_client
  begin
    response = client.is_password_updated["data"]
    updated = response["updated"] == true
    puts "Password status: #{updated ? 'Updated from default' : 'Still using default password'}"
  rescue NanoKVM::ApiError => e
    puts "Error checking password status: #{e.message}"
    puts "This feature may not be available on your NanoKVM device/firmware."
  end
end

#ledsObject



81
82
83
# File 'lib/nanokvm/cli.rb', line 81

def leds
  display_as_table("NanoKVM LEDs", :get_gpio_state)
end

#mdnsObject



360
361
362
# File 'lib/nanokvm/cli.rb', line 360

def mdns
  display_as_table("mDNS Status", :get_mdns_status)
end

#memory_limitObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/nanokvm/cli.rb', line 275

def memory_limit
  client = authenticated_client
  
  if options[:enabled].nil? && options[:limit].nil?
    # Get current setting
    display_as_table("Memory Limit Settings", :get_memory_limit)
  else
    # Set new values
    unless options[:enabled] && options[:limit]
      puts "Error: Both --enabled and --limit are required to set memory limit"
      return
    end
    
    begin
      client.set_memory_limit(options[:enabled], options[:limit])
      puts "Memory limit updated successfully"
    rescue NanoKVM::ApiError => e
      puts "Error setting memory limit: #{e.message}"
      puts "This feature may not be available on your NanoKVM device/firmware."
    end
  end
end

#mount(image_path) ⇒ Object



130
131
132
133
134
# File 'lib/nanokvm/cli.rb', line 130

def mount(image_path)
  client = authenticated_client
  client.mount_image(image_path)
  puts "Image #{image_path} mounted successfully"
end

#networkObject



168
169
170
# File 'lib/nanokvm/cli.rb', line 168

def network
  display_as_table("NanoKVM Network Information", :get_network_info)
end

#oledObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/nanokvm/cli.rb', line 300

def oled
  client = authenticated_client
  
  if options[:sleep].nil?
    # Get current setting
    display_as_table("OLED Settings", :get_oled_info)
  else
    # Set new sleep timeout
    begin
      client.set_oled_sleep(options[:sleep])
      puts "OLED sleep timeout updated to #{options[:sleep]} seconds"
    rescue NanoKVM::ApiError => e
      puts "Error setting OLED sleep timeout: #{e.message}"
      puts "This feature may not be available on your NanoKVM device/firmware."
    end
  end
end

#powerObject



67
68
69
70
71
# File 'lib/nanokvm/cli.rb', line 67

def power
  client = authenticated_client
  client.power_button(options[:duration])
  puts "Power button pressed for #{options[:duration]}ms"
end

#previewObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/nanokvm/cli.rb', line 371

def preview
  client = authenticated_client
  
  if options[:enable].nil?
    # Get current setting
    display_as_table("Preview State", :get_preview_state)
  else
    # Set new state
    begin
      client.set_preview_state(options[:enable])
      puts "Preview #{options[:enable] ? 'enabled' : 'disabled'}"
    rescue NanoKVM::ApiError => e
      puts "Error setting preview state: #{e.message}"
      puts "This feature may not be available on your NanoKVM device/firmware."
    end
  end
end

#rebootObject



178
179
180
181
182
183
184
# File 'lib/nanokvm/cli.rb', line 178

def reboot
  client = authenticated_client
  puts "Rebooting NanoKVM system..."
  client.system_reboot
  puts "Reboot command sent successfully."
  puts "Note: The device will be unavailable during the reboot process."
end

#resetObject



74
75
76
77
78
# File 'lib/nanokvm/cli.rb', line 74

def reset
  client = authenticated_client
  client.reset_button
  puts "Reset button pressed"
end

#reset_hdmiObject



187
188
189
190
191
192
# File 'lib/nanokvm/cli.rb', line 187

def reset_hdmi
  client = authenticated_client
  puts "Resetting HDMI subsystem..."
  client.reset_hdmi
  puts "HDMI reset command sent successfully."
end

#reset_hidObject



195
196
197
198
199
200
# File 'lib/nanokvm/cli.rb', line 195

def reset_hid
  client = authenticated_client
  puts "Resetting HID subsystem..."
  client.reset_hid
  puts "HID reset command sent successfully."
end

#scriptObject



355
356
357
# File 'lib/nanokvm/cli.rb', line 355

def script
  display_as_table("Script Information", :get_script_info)
end

#ssh_statusObject



319
320
321
# File 'lib/nanokvm/cli.rb', line 319

def ssh_status
  display_as_table("SSH Server Status", :get_ssh_status)
end

#tailscaleObject



216
217
218
# File 'lib/nanokvm/cli.rb', line 216

def tailscale
  display_as_table("NanoKVM Tailscale Status", :get_tailscale_status)
end

#testObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/nanokvm/cli.rb', line 390

def test
  puts "Testing connection to NanoKVM..."
  
  client = create_client
  client.debug = true
  
  begin
    puts "Connecting to #{client.host}..."
    # Try a simple HTTP GET to see if the server is up
    uri = URI.parse("http://#{client.host}")
    response = Net::HTTP.get_response(uri)
    puts "Server responded with status: #{response.code}"
    
    if response.code == "200"
      puts "Connection successful, attempting authentication..."
      ensure_authenticated(client)
      puts "Authentication successful!"
    else
      puts "Server responded, but with an unexpected status code"
    end
  rescue => e
    puts "Connection failed: #{e.message}"
  end
end

#toggle(device) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nanokvm/cli.rb', line 149

def toggle(device)
  unless ["network", "disk"].include?(device)
    puts "Error: Device must be 'network' or 'disk'"
    return
  end

  client = authenticated_client
  result = client.toggle_virtual_device(device)
  state = result["data"]["on"]
  
  puts "#{device.capitalize} is now #{state ? 'ENABLED' : 'DISABLED'}"
end

#type(text) ⇒ Object



137
138
139
140
141
# File 'lib/nanokvm/cli.rb', line 137

def type(text)
  client = authenticated_client
  client.send_text(text)
  puts "Text sent successfully"
end

#versionObject



173
174
175
# File 'lib/nanokvm/cli.rb', line 173

def version
  display_as_table("NanoKVM Application Version", :get_app_version)
end

#virtualObject



144
145
146
# File 'lib/nanokvm/cli.rb', line 144

def virtual
  display_as_table("Virtual Devices", :get_virtual_device_status)
end

#wol_macObject



365
366
367
# File 'lib/nanokvm/cli.rb', line 365

def wol_mac
  display_as_table("Wake-on-LAN MAC Address", :get_wol_mac)
end