Class: Morpheus::Cli::Remote
- Inherits:
-
Object
- Object
- Morpheus::Cli::Remote
- Extended by:
- Term::ANSIColor
- Includes:
- CliCommand, RemoteHelper
- Defined in:
- lib/morpheus/cli/commands/remote.rb
Constant Summary collapse
- @@appliance_config =
for caching the the contents of YAML file $home/appliances it is structured like :appliance_name => => “api.gomorpheus.com”, :active => true not named @@appliances to avoid confusion with the instance variable . This is also a command class…
nil
Instance Attribute Summary
Attributes included from CliCommand
Class Method Summary collapse
-
.active_appliance ⇒ Object
Returns two things, the remote appliance name and url.
- .appliance_config ⇒ Object
- .appliances ⇒ Object
- .appliances_file_path ⇒ Object
- .clear_active_appliance ⇒ Object
- .delete_all_remotes ⇒ Object
- .delete_remote(app_name) ⇒ Object
-
.load_active_remote ⇒ Object
Hash info about the active appliance.
-
.load_all_remotes(params = {}) ⇒ Array
Returns all the appliances in the configuration.
- .load_appliance_file ⇒ Object
-
.load_remote(app_name) ⇒ Hash
Info about the appliance.
- .recalculate_variable_map ⇒ Object
-
.refresh_remote(app_name, params = {}, timeout = 5) ⇒ Object
refresh_remote makes an api request to the configured appliance url and updates the appliance’s build version, status and last_check attributes.
-
.rename_remote(app_name, new_app_name) ⇒ Object
use this to rename, it update appliances file and others.
- .save_appliances(new_config) ⇒ Object
-
.save_remote(app_name, app_map) ⇒ Hash
save_remote updates the appliance info.
-
.save_remote_last_check(appliance, json_response, err = nil, took_sec = nil) ⇒ Object
save the app status and last request information looks for json_response like /setup/check and /ping.
- .set_active_appliance(app_name) ⇒ Object
Instance Method Summary collapse
- #_check_all_appliances(options) ⇒ Object
- #_check_appliance(appliance_name, options) ⇒ Object
- #_get(appliance_name, options) ⇒ Object
- #_remove_all_appliances(options) ⇒ Object
- #_remove_appliance(appliance_name, options) ⇒ Object
- #_view_appliance(appliance_name, path, no_auth, options) ⇒ Object
- #add(args) ⇒ Object
- #check(args) ⇒ Object
- #check_all(args) ⇒ Object
- #clone(args) ⇒ Object
- #connect(opts = {}) ⇒ Object
- #current(args) ⇒ Object
- #format_remote_details(appliance, options = {}) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ Remote
constructor
A new instance of Remote.
- #list(args) ⇒ Object
- #load_remote_by_name(appliance_name) ⇒ Object
- #refresh(args) ⇒ Object
- #remove(args) ⇒ Object
- #remove_all(args) ⇒ Object
- #rename(args) ⇒ Object
-
#setup(args) ⇒ Object
This moved to the SetupCommand.
- #unuse(args) ⇒ Object
- #update(args) ⇒ Object
- #use(args) ⇒ Object
- #version(args) ⇒ Object
- #view(args) ⇒ Object
Methods included from RemoteHelper
#format_appliance_name, #format_appliance_secure, #format_appliance_slug, #format_appliance_status, #format_appliance_status_color, #format_is_current, #get_appliance_session_blurbs, included
Methods included from CliCommand
#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands
Constructor Details
#initialize ⇒ Remote
Returns a new instance of Remote.
23 24 25 |
# File 'lib/morpheus/cli/commands/remote.rb', line 23 def initialize() @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance end |
Class Method Details
.active_appliance ⇒ Object
Returns two things, the remote appliance name and url
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1358 def active_appliance if self.appliances.empty? return nil, nil end app_name, app_map = self.appliances.find {|k,v| v[:active] == true } app_url = app_map ? (app_map[:host] || app_map[:url]).to_s : nil if app_name return app_name, app_url else return nil, nil end end |
.appliance_config ⇒ Object
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1342 def appliance_config if @@appliance_config.nil? @@appliance_config @@appliance_config = load_appliance_file # fix things right away, replace deprecated :host with :url @@appliance_config.each do |app_name, appliance| host = appliance.delete(:host) if host && appliance[:url].to_s.empty? appliance[:url] = host end end end return @@appliance_config end |
.appliances ⇒ Object
1338 1339 1340 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1338 def appliances self.appliance_config end |
.appliances_file_path ⇒ Object
1509 1510 1511 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1509 def appliances_file_path File.join(Morpheus::Cli.home_directory,"appliances") end |
.clear_active_appliance ⇒ Object
1484 1485 1486 1487 1488 1489 1490 1491 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1484 def clear_active_appliance #return set_active_appliance(nil) new_appliances = self.appliances new_appliances.each do |k,v| v.delete(:active) end save_appliances(new_appliances) end |
.delete_all_remotes ⇒ Object
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1661 def delete_all_remotes() deleted_list = [] self.appliances.each do |app_name, appliance| deleted_list << delete_remote(app_name) end # self.appliances = {} # Morpheus::Cli::Remote.save_appliances({}) # recalculate_variable_map() # return deleted_list return self.appliances end |
.delete_remote(app_name) ⇒ Object
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1641 def delete_remote(app_name) app_name = app_name.to_sym cur_appliances = self.appliances #.clone app_map = cur_appliances[app_name] if !app_map return nil end # remove it from config and delete credentials cur_appliances.delete(app_name) ::Morpheus::Cli::Remote.save_appliances(cur_appliances) # this should be a class method too ::Morpheus::Cli::Credentials.new(app_name, nil).clear_saved_credentials(app_name) # delete from groups too.. ::Morpheus::Cli::Groups.clear_active_group(app_name) # recalculate session variables recalculate_variable_map() # return the deleted value return app_map end |
.load_active_remote ⇒ Object
Returns Hash info about the active appliance.
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1438 def load_active_remote() # todo: use this in favor of Remote.active_appliance perhaps? if self.appliances.empty? return nil end result = nil app_name, app_map = self.appliances.find {|k,v| v[:active] == true } if app_map result = app_map result[:name] = app_name # app_name.to_s to be more consistant with other display values end return result end |
.load_all_remotes(params = {}) ⇒ Array
Returns all the appliances in the configuration
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1374 def load_all_remotes(params={}) if self.appliances.empty? return [] end all_appliances = self.appliances.collect do |app_name, app_map| row = app_map.clone # OStruct.new(app_map) tempting row[:name] = app_name row # { # active: v[:active], # name: app_name, # host: v[:host], # || v[:url], # #"LICENSE": v[:licenseIsInstalled] ? "Installed" : "(unknown)" # never return a license key from the server, ever! # status: v[:status], # username: v[:username], # last_check: v[:last_check], # last_whoami: v[:last_whoami], # last_api_request: v[:last_api_request], # last_api_result: v[:last_api_result], # last_command: v[:last_command], # last_command_result: v[:last_command_result] # } end # filter results params[:phrase] = params['phrase'] if params['phrase'] params[:name] = params['name'] if params['name'] params[:url] = params['url'] if params['url'] # params[:insecure] = params['insecure'] if params['insecure'] params[:max] = params['max'] if params['max'] params[:offset] = params['offset'] if params['offset'] params[:sort] = params['sort'] if params['sort'] params[:direction] = params['direction'] if params['direction'] if all_appliances # apply filters if params[:phrase] all_appliances = all_appliances.select do |app| app_name = app[:name] || app['name'] app_url = app[:url] || app['url'] || app[:host] app_name.to_s.include?(params[:phrase]) || app_url.to_s.include?(params[:phrase]) end end # apply sort sort_key = params[:sort] ? params[:sort].to_sym : :name # :url used to be stored as :host if sort_key == :url || sort_key == :host all_appliances = all_appliances.sort {|a,b| (a[:url] || a[:host]).to_s <=> (b[:url] || b[:host]).to_s } elsif sort_key all_appliances = all_appliances.sort {|a,b| a[sort_key].to_s <=> b[sort_key].to_s } end if params['direction'] == 'desc' all_appliances.reverse! end # limit if params[:max] all_appliances = all_appliances.first(params[:max]) end end return all_appliances end |
.load_appliance_file ⇒ Object
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1493 def load_appliance_file fn = appliances_file_path if File.exist? fn #Morpheus::Logging::DarkPrinter.puts "loading appliances file #{fn}" if Morpheus::Logging.debug? return YAML.load_file(fn) else return {} # return { # morpheus: { # host: 'https://api.gomorpheus.com', # active: true # } # } end end |
.load_remote(app_name) ⇒ Hash
Returns info about the appliance.
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1454 def load_remote(app_name) if self.appliances.empty? || app_name.nil? return nil end result = nil app_map = self.appliances[app_name.to_sym] if app_map result = app_map result[:name] = app_name # .to_s probably better end return result end |
.recalculate_variable_map ⇒ Object
1808 1809 1810 1811 1812 1813 1814 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1808 def recalculate_variable_map() Morpheus::Cli::Echo.recalculate_variable_map() # recalculate shell prompt after this change if Morpheus::Cli::Shell.has_instance? Morpheus::Cli::Shell.instance.reinitialize() end end |
.refresh_remote(app_name, params = {}, timeout = 5) ⇒ Object
refresh_remote makes an api request to the configured appliance url and updates the appliance’s build version, status and last_check attributes
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1675 def refresh_remote(app_name, params={}, timeout=5) app_name = app_name.to_sym cur_appliances = self.appliances appliance = cur_appliances[app_name] || {} appliance_url = (appliance[:url] || appliance[:host]).to_s if !appliance_url raise "appliance config is missing url!" # should not need this end # todo: this insecure flag needs to applied everywhere now tho.. if appliance[:insecure] Morpheus::RestClient.enable_ssl_verification = false # Morpheus::RestClient.enable_http = true end err = nil json_response = nil # make request to /api/setup/check # and update appliance :status, :build_version, :last_check{} if appliance_url.to_s.empty? err = "no url specified" # wtf, no url... return appliance, json_response else # access token is needed for buildVersion wallet = Morpheus::Cli::Credentials.new(app_name, appliance_url).load_saved_credentials() setup_interface = Morpheus::SetupInterface.new({url:appliance_url, verify_ssl: !appliance[:insecure], timeout: timeout, access_token: (wallet && wallet['access_token']) ? wallet['access_token'] : nil}) start_time = Time.now begin json_response = setup_interface.check(params) rescue => ex err = ex ensure took_sec = Time.now - start_time end # save and update appliance info return save_remote_last_check(appliance, json_response, err, took_sec) end end |
.rename_remote(app_name, new_app_name) ⇒ Object
use this to rename, it update appliances file and others. oh maybe just put this in the command handler
first check if the requested name exits and that the new name does not exist.
clone it and delete the old one. todo: switch replace symbols with strings please, makes for nicer appliances.yaml
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1579 def rename_remote(app_name, new_app_name) app_name = app_name.to_sym new_app_name = new_app_name.to_sym cur_appliances = self.appliances #.clone app_map = cur_appliances[app_name] if app_map.nil? print_red_alert "Remote not found by the name '#{app_name}', see the command `remote list`" #print "Did you mean one of these commands: #{suggestions.join(', ')?", reset, "\n" return nil end if cur_appliances[new_app_name] print_red_alert "A remote already exists with name '#{new_app_name}', see the command `remote get #{new_app_name}`" puts "First, you must rename or remove the existing remote." return nil end # clone the existing data # copy remote new_appliance_map = app_map.clone() new_appliance_map[:name] = new_app_name # inject name save_remote(new_app_name, new_appliance_map) # clone credentials...just overwrite keys there, f it. old_wallet = ::Morpheus::Cli::Credentials.new(app_name, nil).load_saved_credentials() if old_wallet ::Morpheus::Cli::Credentials.new(new_app_name, nil).save_credentials(new_app_name, old_wallet) #::Morpheus::Cli::Credentials.new(app_name, nil).clear_saved_credentials(app_name) end # clone groups...just overwrite keys there, f it. old_active_group = ::Morpheus::Cli::Groups.active_group(app_name) if old_active_group ::Morpheus::Cli::Groups.set_active_group(new_app_name, old_active_group) #::Morpheus::Cli::Groups.clear_active_group(app_name) end # delete stuff last # delete creds if old_wallet ::Morpheus::Cli::Credentials.new(app_name, nil).clear_saved_credentials(app_name) end # delete groups if old_active_group ::Morpheus::Cli::Groups.clear_active_group(app_name) end # delete remote delete_remote(app_name) # this is all redundant after above # # this should be a class method too # ::Morpheus::Cli::Credentials.new(app_name, nil).clear_saved_credentials(app_name) # # delete from groups too.. # ::Morpheus::Cli::Groups.clear_active_group(app_name) # # recalculate session variables # recalculate_variable_map() # return the deleted value return app_map end |
.save_appliances(new_config) ⇒ Object
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1513 def save_appliances(new_config) fn = appliances_file_path if !Dir.exist?(File.dirname(fn)) FileUtils.mkdir_p(File.dirname(fn)) end File.open(fn, 'w') {|f| f.write new_config.to_yaml } #Store FileUtils.chmod(0600, fn) #@@appliance_config = load_appliance_file @@appliance_config = new_config end |
.save_remote(app_name, app_map) ⇒ Hash
save_remote updates the appliance info
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1528 def save_remote(app_name, app_map) # need an app_name to save it if app_name.to_s.empty? puts "skipped save of remote with a blank name" return nil end # in case a temporary config gets passed in here, do not save it.. this should be avoided though if app_map[:temporary] puts "skipped save of temporary remote '#{app_name}'" return nil end app_name = app_name.to_sym # it's probably better to use load_appliance_file() here instead cur_appliances = self.appliances #.clone cur_appliances[app_name] = app_map #cur_appliances[app_name] ||= {:status => "unknown", :error => "Bad configuration. Missing url. See 'remote update --url'" } cur_appliances[app_name] ||= {:status => "unknown"} # :host is gone, use :url please if cur_appliances[app_name][:host] cur_appliances[app_name][:url] = cur_appliances[app_name].delete(:host) end # this is the new set_active_appliance(), instead just pass :active => true # remove active flag from others if app_map[:active] cur_appliances.each do |k,v| is_match = (app_name ? (k == app_name) : false) if is_match v[:active] = true else v.delete(:active) # v.delete('active') # v[:active] = false end end end # persist all appliances save_appliances(cur_appliances) # recalculate session variables recalculate_variable_map() return app_map end |
.save_remote_last_check(appliance, json_response, err = nil, took_sec = nil) ⇒ Object
save the app status and last request information looks for json_response like /setup/check and /ping
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1717 def save_remote_last_check(appliance, json_response, err=nil, took_sec=nil) #puts "save_remote_last_check: #{appliance}, #{json_response}" app_name = appliance[:name] ? appliance[:name].to_sym : nil cur_appliances = self.appliances app_map = appliance app_url = (app_map[:url] || app_map[:host]).to_s if !app_url raise "appliance config is missing url!" end # only change stuff that was contained in the response # this stores things under the context last_check appliance[:last_check] = {} appliance[:last_check][:success] = json_response.nil? appliance[:last_check][:timestamp] = Time.now.to_i appliance[:last_check][:http_status] = json_response ? 200 : nil if took_sec # store in ms appliance[:last_check][:took] = (took_sec.to_f*1000).round end if json_response # if json_response.key?('applianceUrl') # appliance[:appliance_url] = json_response['applianceUrl'] # end if json_response['success'] == true appliance[:status] = 'ready' appliance[:last_check][:success] = true # consider bumping this after every successful api command appliance[:last_success_at] = Time.now.to_i appliance.delete(:error) end if !json_response['buildVersion'].to_s.empty? appliance[:build_version] = json_response['buildVersion'] end if json_response.key?('setupNeeded') if json_response['setupNeeded'] == true appliance[:setup_needed] = true appliance[:status] = 'fresh' else appliance.delete(:setup_needed) end end else # no response body eh? appliance[:last_check][:success] = false appliance[:last_check][:error] = "Invalid api response" appliance[:status] = 'error' end # handle error if err case(err) when SocketError appliance[:status] = 'unreachable' appliance[:last_check][:http_status] = nil appliance[:last_check][:error] = err. when RestClient::Exceptions::Timeout # print_rest_exception(e, options) # exit 1 appliance[:status] = 'http-timeout' appliance[:last_check][:http_status] = nil when Errno::ECONNREFUSED appliance[:status] = 'net-error' appliance[:last_check][:error] = err. when OpenSSL::SSL::SSLError appliance[:status] = 'ssl-error' appliance[:last_check][:error] = err. when JSON::ParserError appliance[:status] = 'unrecognized' appliance[:last_check][:error] = err. when RestClient::Exception appliance[:status] = 'http-error' # appliance[:http_status] = err.response ? err.response.code : nil appliance[:last_check][:http_status] = err.response ? err.response.code : nil appliance[:last_check][:error] = err. # if err.response.code == 404 # appliance[:status] = 'ready' # 'ok' # Morpheus::Logging::DarkPrinter.puts "ping failed but it is ready" if Morpheus::Logging.debug? # end else appliance[:status] = 'error' # err.class.to_s.dasherize appliance[:last_check][:error] = err. end end # save to disk save_remote(app_name, appliance) # return map and response return appliance, json_response end |
.set_active_appliance(app_name) ⇒ Object
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1467 def set_active_appliance(app_name) app_name = app_name.to_sym new_appliances = self.appliances new_appliances.each do |k,v| is_match = (app_name ? (k == app_name) : false) if is_match v[:active] = true else v.delete(:active) # v.delete('active') # v[:active] = false end end save_appliances(new_appliances) return load_remote(app_name) end |
Instance Method Details
#_check_all_appliances(options) ⇒ Object
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/morpheus/cli/commands/remote.rb', line 600 def _check_all_appliances() start_time = Time.now # reresh all appliances and then display the list view id_list = ::Morpheus::Cli::Remote.appliances.keys # sort ? if id_list.size > 1 print cyan puts "Checking #{id_list.size} remotes" elsif id_list.size == 1 puts "Checking #{Morpheus::Cli::Remote.appliances.keys.first}" end id_list.each do |appliance_name| #print "." appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name) end took_sec = (Time.now - start_time) print_green_success "Completed check of #{id_list.size} #{id_list.size == 1 ? 'remote' : 'remotes'} in #{format_duration_seconds(took_sec)}" if [:quiet] return 0 end list([]) return 0 end |
#_check_appliance(appliance_name, options) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/morpheus/cli/commands/remote.rb', line 543 def _check_appliance(appliance_name, ) exit_code, err = 0, nil begin appliance = load_remote_by_name(appliance_name) appliance_name = appliance[:name] appliance_url = appliance[:url] # found appliance, now refresh it if [:do_offline] json_response = {'appliance' => appliance} # mock payload else appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name.to_sym) json_response = {'appliance' => appliance} # mock payload appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name) # json_response = {'appliance' => appliance} # mock payload end # appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name) if (appliance[:status] != 'ready' && appliance[:status] != 'fresh') exit_code = 1 # err = appliance[:last_check] && appliance[:last_check][:error] ? appliance[:last_check][:error] : nil end render_response(json_response, ) do print_h1 "Morpheus Appliance", [], print cyan print format_remote_details(appliance, ) print reset, "\n" end return exit_code, err rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#_get(appliance_name, options) ⇒ Object
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
# File 'lib/morpheus/cli/commands/remote.rb', line 845 def _get(appliance_name, ) exit_code, err = 0, nil if appliance_name == 'current' current_appliance = ::Morpheus::Cli::Remote.load_active_remote() if current_appliance.nil? #raise_command_error "No current appliance, see the command `remote use`" unless [:quiet] print yellow, "No current appliance, see the command `remote use`", reset, "\n" end return 1, "No current appliance" end end appliance = load_remote_by_name(appliance_name) appliance_name = appliance[:name] appliance_url = appliance[:url] # refresh remote status and version by default # should json just be appliance instead maybe? json_response = nil if [:do_offline] json_response = {'appliance' => appliance} # mock payload else appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name.to_sym) json_response = {'appliance' => appliance} # mock payload end # render render_response(json_response, ) do if [:url_only] if appliance print cyan, (appliance[:url] || appliance[:host]),"\n",reset return exit_code, err else print_error red, err,"\n",reset return exit_code, err end end if exit_code != 0 print_error red, err,"\n",reset return exit_code, err end print_h1 "Morpheus Appliance", [], print cyan print format_remote_details(appliance, ) print reset, "\n" end return exit_code, err end |
#_remove_all_appliances(options) ⇒ Object
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1016 def _remove_all_appliances() exit_code, err = 0, nil all_appliance_names = ::Morpheus::Cli::Remote.appliances.keys.size if all_appliance_names.empty? if [:quiet] != true print_green_success "No remotes found, nothing to remove" return 0, nil end end unless [:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you want to remove all of your remotes (#{all_appliance_names.size})?", ) return 9, "aborted command" end # ok, do it deleted_list = ::Morpheus::Cli::Remote.delete_all_remotes() # render if [:quiet] return exit_code, err end if all_appliance_names.size == 1 print_green_success "Removed 1 remote (#{all_appliance_names.join(', ')})" else print_green_success "Removed 1 remote () " end #list([]) return exit_code, err end |
#_remove_appliance(appliance_name, options) ⇒ Object
984 985 986 987 988 989 990 991 992 993 994 995 996 997 |
# File 'lib/morpheus/cli/commands/remote.rb', line 984 def _remove_appliance(appliance_name, ) if ::Morpheus::Cli::Remote.appliances[appliance_name.to_sym].nil? raise_command_error "Remote does not exist with name '#{appliance_name.to_s}'" end # ok, delete it ::Morpheus::Cli::Remote.delete_remote(appliance_name) # return result if [:quiet] return 0 end print_green_success "Deleted remote #{appliance_name}" # list([]) return 0 end |
#_view_appliance(appliance_name, path, no_auth, options) ⇒ Object
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 |
# File 'lib/morpheus/cli/commands/remote.rb', line 930 def _view_appliance(appliance_name, path, no_auth, ) appliance = load_remote_by_name(appliance_name) appliance_name = appliance[:name] appliance_url = appliance[:url] if appliance_url.to_s.empty? raise_command_error "Remote appliance does not have a url?" end path = path.to_s.empty? ? "/" : path if path[0].chr != "/" path = "/#{path}" end wallet = ::Morpheus::Cli::Credentials.new(appliance_name, nil).load_saved_credentials() # try to auto login if we have a token link = "#{appliance_url}#{path}" if no_auth == false if wallet && wallet['access_token'] link = "#{appliance_url}/login/oauth-redirect?access_token=#{wallet['access_token']}\\&redirectUri=#{path}" end end if [:dry_run] puts Morpheus::Util.open_url_command(link) return 0 end return Morpheus::Util.open_url(link) end |
#add(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/commands/remote.rb', line 181 def add(args) , params, payload = {}, {}, {} new_appliance_map = {} use_it = nil secure = nil optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage('[name] [url]') opts.on(nil, '--use [true|false]', "Start using remote right now. By default this is true if it's the first remote, otherwise false." ) do |val| use_it = (val == 'no' || val == 'off' || val.to_s == 'false') new_appliance_map[:active] = true end opts.on(nil, "--secure", "Prevent insecure HTTPS communication. Default is true.") do secure = true end opts.on(nil, "--insecure", "Allow insecure HTTPS communication. i.e. Ignore SSL errors. Default is false.") do secure = false end # ok, need to be able to pass every option supported by login() and setup() (opts, , [:options, :quiet]) opts. = <<-EOT Add a new remote to your morpheus client configuration. [name] is required. A unique name for your appliance. eg. demo [url] is required. The URL of your appliance eg. https://demo.morpheusdata.com First, this inspects the remote url to check the appliance status and version. If remote is ready, it will prompt to login, see the command `login`. If remote is freshly installed, it will prompt to initialize the appliance, see the command `setup`. The option --use can be included to start using the new remote right away. The remote will be used by default if it is the first remote in the configuration. The --quiet option can be used to to skip prompting. EOT end optparse.parse!(args) verify_args!(args:args, max:2, optparse:optparse) # payload = options[:payload] ? options[:payload] : {} # payload.deep_merge!(parse_passed_options(options)) # # skip prompting when --payload is used # if options[:payload].nil? # end # load current appliances appliances = ::Morpheus::Cli::Remote.appliances new_appliance_name = args[0] ? args[0] : nil url = args[1] ? args[1] : nil # Name still_prompting = true while still_prompting do if args[0] new_appliance_name = args[0] still_prompting = false else if new_appliance_name.to_s.empty? v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'A unique name for the remote Morpheus appliance. Example: local'}], [:options]) new_appliance_name = v_prompt['name'] end end # for the sake of sanity if [:current, :all, :'remote-url'].include?(new_appliance_name.to_sym) raise_command_error "The specified remote appliance name '#{new_appliance_name}' is invalid." new_appliance_name = nil end # unique name existing_appliance = appliances[new_appliance_name.to_sym] if existing_appliance print_error red,"The specified remote appliance name '#{new_appliance_name}' already exists: #{display_appliance(existing_appliance[:name], (existing_appliance[:url]))}",reset,"\n" new_appliance_name = nil end if new_appliance_name still_prompting = false end if new_appliance_name.nil? && still_prompting == false return 1 end end new_appliance_map[:name] = new_appliance_name.to_sym # URL still_prompting = true while still_prompting do if args[1] url = args[1] still_prompting = false else if !url default_url = nil # use Name: dev to get a happy default. # if new_appliance_name == "dev" if new_appliance_name == "local" || new_appliance_name == "localhost" || new_appliance_name == "dev" default_url = "http://localhost:8080" end v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'url', 'fieldLabel' => 'URL', 'type' => 'text', 'required' => true, 'description' => 'The URL of the remote Morpheus appliance. Example: https://10.0.2.2', 'defaultValue' => default_url}], [:options]) url = v_prompt['url'] end end # strip whitespace from URL url = url.to_s.strip if url.to_s !~ /^https?\:\/\/.+/ print_error red,"The specified remote appliance url '#{url}' is invalid.",reset,"\n" #still_prompting = true url = nil else still_prompting = false end if url.nil? && still_prompting == false return 1 end end new_appliance_map[:url] = url # --insecure or --secure # Secure? (Ignore SSL errors) # secure is the default, # try to only store insecure:false in the appliances config if url.to_s =~ /^https\:/ && secure.nil? v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'insecure', 'fieldLabel' => 'Insecure (Ignore SSL Errors)', 'type' => 'checkbox', 'required' => false, 'defaultValue' => false, 'description' => 'Allow insecure HTTPS communication, ignore SSL errors.'}], [:options]) if v_prompt['insecure'].to_s == 'true' || v_prompt['insecure'].to_s == 'on' new_appliance_map[:insecure] = true end elsif secure != nil new_appliance_map[:insecure] = !secure end # --use if use_it != nil if use_it new_appliance_map[:active] = true end else # if ::Morpheus::Cli::OptionTypes::confirm("Would you like to switch to using this remote now?", options.merge({default: appliances.empty?})) # new_appliance_map[:active] = true # end v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'use', 'fieldLabel' => 'Use?', 'type' => 'checkbox', 'defaultValue' => appliances.empty?, 'description' => 'Start using this remote right away'}], [:options]) if v_prompt['use'].to_s == 'true' || v_prompt['use'].to_s == 'on' new_appliance_map[:active] = true end end # save it appliance = ::Morpheus::Cli::Remote.save_remote(new_appliance_name.to_sym, new_appliance_map) # refresh it (does /api/setup/check to refresh status and build # todo: this should happen in save eh? #Morpheus::Logging::DarkPrinter.puts "inspecting remote #{appliance[:name]} #{appliance[:url]}" if Morpheus::Logging.debug? && !options[:quiet] appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(new_appliance_name.to_sym) # hit /setup/check api and update version and status if ![:quiet] # print_green_success "Added remote #{new_appliance_name}, status is #{format_appliance_status(appliance)}" print cyan,"Added remote #{new_appliance_name}, status is #{format_appliance_status(appliance)}",reset,"\n" end #appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(new_appliance_name.to_sym) # if !options[:quiet] # print cyan # puts "Status: #{format_appliance_status(appliance)}" # print reset # end # puts "refreshed appliance #{appliance.inspect}" # determine command exit_code and err exit_code, err = 0, nil if (appliance[:status] != 'ready' && appliance[:status] != 'fresh') exit_code = 1 err = "remote status is #{appliance[:status]}" end # just skip prompting no prompt -q is used. if [:quiet] return exit_code, err end # just skip prompting no prompt -N is used. if [:no_prompt] return exit_code, err end # setup fresh appliance? if appliance[:status] == 'fresh' print cyan puts "It looks like this appliance needs to be setup. Starting setup ..." #return setup([new_appliance_name] + [Morpheus::Logging.debug? ? ["--debug"] : []]) return Morpheus::Cli::Setup.new.handle(["-r", new_appliance_name] + (Morpheus::Logging.debug? ? ["--debug"] : [])) end # only login if you are using this remote # maybe remote use should do the login prompting eh? # if appliance[:active] && appliance[:status] == 'ready' if appliance[:status] == 'ready' print reset if ::Morpheus::Cli::OptionTypes::confirm("Would you like to login now?", .merge({default: true})) login_result = ::Morpheus::Cli::Login.new.handle(["-r", appliance[:name].to_s]) = true if login_result == 0 = false end while do if ::Morpheus::Cli::OptionTypes::confirm("Login attempt failed. Would you like to try again?", .merge({default: true})) login_result = ::Morpheus::Cli::Login.new.handle(["-r", appliance[:name].to_s]) if login_result == 0 = false end else = false end end end # refresh to get buildVersion now that we are logged in appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(new_appliance_name.to_sym) else #puts "Status is #{format_appliance_status(appliance)}" end # print new appliance details print_h1 "Morpheus Appliance", [], print cyan print format_remote_details(appliance, ) print reset, "\n" #_get(appliance[:name], {}) return exit_code, err end |
#check(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/commands/remote.rb', line 413 def check(args) = {} checkall = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:json, :yaml, :csv, :fields, :quiet]) opts.on('-a', '--all', "Check all remotes.") do checkall = true end opts. = <<-EOT Check the status of a remote appliance. [name] is optional. This is the name of a remote. Default is the current remote. Can be passed as 'all'. to perform remote check-all. This makes a request to the remote url and updates the status and version. EOT end optparse.parse!(args) if checkall == true return _check_all_appliances() end if args.count == 0 id_list = ['current'] else id_list = parse_id_list(args) end # trick for remote check all if id_list.length == 1 && id_list[0].to_s.downcase == 'all' return _check_all_appliances() end #connect(options) return run_command_for_each_arg(id_list) do |arg| _check_appliance(arg, ) end end |
#check_all(args) ⇒ Object
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/morpheus/cli/commands/remote.rb', line 580 def check_all(args) = {} checkall = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage() (opts, , [:quiet]) opts. = <<-EOT Refresh all remote appliances. This makes a request to each of the configured appliance urls and updates the status and version. EOT end optparse.parse!(args) if args.count != 0 raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}" end #connect(options) # needed? _check_all_appliances() end |
#clone(args) ⇒ Object
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
# File 'lib/morpheus/cli/commands/remote.rb', line 745 def clone(args) exit_code, err, , params, payload = 0, nil, {}, {}, {} use_it = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[remote] [name]") # opts.on(nil, "--secure", "Prevent insecure HTTPS communication. This is enabled by default") do # params[:secure] = true # end # opts.on(nil, "--insecure", "Allow insecure HTTPS communication. i.e. Ignore SSL errors.") do # params[:insecure] = true # end opts.on(nil, '--use', "Make it the current appliance" ) do use_it = true params[:active] = true end (opts, , [:quiet]) opts. = <<-EOT Clone remote appliance configuration, including any existing credentials. [remote] is required. This is the name of an existing remote. [name] is optional. This is the name of the new remote that will be created. EOT end optparse.parse!(args) verify_args!(args:args, optparse:optparse, count:2) appliance_name = args[0].to_sym appliance = load_remote_by_name(appliance_name) if !appliance raise_command_error "Remote appliance not found by the name '#{appliance_name}', see the command `remote list`" end new_appliance_name = args[1].to_sym matching_appliance = ::Morpheus::Cli::Remote.appliances[new_appliance_name.to_sym] if matching_appliance raise_command_error "Remote already exists with the name '#{matching_appliance[:name]}', see the command `remote get #{matching_appliance[:name]}`" end # ok clone it original_appliance = appliance appliance = original_appliance.clone appliance[:name] = new_appliance_name if params[:insecure] appliance[:insecure] = true elsif params[:secure] appliance.delete(:insecure) end if params[:url] || params[:host] appliance[:url] = params[:url] || params[:host] # appliance.delete(:host) end if use_it appliance[:active] = true end # save the new remote appliance = ::Morpheus::Cli::Remote.save_remote(new_appliance_name, appliance) # refresh it now? appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(new_appliance_name) # render if ![:quiet] print_green_success "Cloned remote #{original_appliance[:name]} to #{appliance[:name]}, status is #{format_appliance_status(appliance)}" # print new appliance details _get(appliance[:name].to_s, {}) end return exit_code, err end |
#connect(opts = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/morpheus/cli/commands/remote.rb', line 27 def connect(opts={}) # no authorization needed, do not verify a token or prompt for login @api_client = establish_remote_appliance_connection({:skip_verify_access_token => true, :skip_login => true}.merge(opts)) @setup_interface = @api_client.setup end |
#current(args) ⇒ Object
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1141 def current(args) = {} name_only = false url_only = false version_only = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage() opts.on( '-n', '--name', "Print only the name." ) do name_only = true end opts.on( '-u', '--url', "Print only the url." ) do url_only = true end opts.on( '-v', '--version', "Print only the build version." ) do version_only = true end opts.on('--offline', '--offline', "Do this offline without an api request to refresh the remote appliance status.") do [:do_offline] = true end (opts, , [:json,:yaml,:csv,:fields, :quiet]) opts. = <<-EOT Print details about the current remote appliance. This behaves the same as `remote get current`. EOT end optparse.parse!(args) verify_args!(args:args, count:0, optparse:optparse) #connect(options) current_appliance = ::Morpheus::Cli::Remote.load_active_remote() if current_appliance.nil? #raise_command_error "No current appliance, see the command `remote use`" unless [:quiet] print yellow, "No current appliance, see the command `remote use`", reset, "\n" end return 1, "No current appliance" end # this does the same thing #return _get("current", options) appliance = current_appliance #appliance = load_remote_by_name("current") #appliance = @remote_appliance exit_code, err = 0, nil if appliance.nil? raise_command_error "no current remote appliance, see command `remote add`." end # ok need to refresh here unless do_offline # refresh status and version # maybe just make json_response = appliance here json_response = nil if [:do_offline] json_response = {'appliance' => appliance} # mock payload else appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance[:name]) json_response = {'appliance' => appliance} # mock payload end # could set exit_code if appliance[:build_version].nil? render_response(json_response, ) do if name_only && url_only #print cyan, display_appliance(appliance[:name], appliance[:url]),"\n",reset print cyan, appliance[:name], " ", appliance[:url],"\n",reset elsif name_only print cyan, appliance[:name],"\n",reset elsif url_only print cyan, appliance[:url],"\n",reset elsif version_only print cyan, appliance[:build_version],"\n",reset else print_h1 "Morpheus Appliance", [], print cyan print format_remote_details(appliance, ) print reset, "\n" end end return exit_code, err end |
#format_remote_details(appliance, options = {}) ⇒ Object
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1271 def format_remote_details(appliance, ={}) columns = { #"Name" => :name, "Name" => lambda {|it| it[:name].to_s }, #"Name" => lambda {|it| it[:active] ? "#{it[:name]} #{bold}(current)#{reset}#{cyan}" : it[:name] }, "URL" => lambda {|it| it[:url] || it[:host] }, #"Status" => lambda {|it| format_appliance_status(it, cyan) }, "Version" => lambda {|it| it[:build_version] ? "#{it[:build_version]}" : '' }, #"Appliance URL" => lambda {|it| it[:appliance_url] ? "#{it[:appliance_url]}" : '' }, "Secure" => lambda {|it| format_appliance_secure(it) }, "Active" => lambda {|it| it[:active] ? "Yes " + format_is_current() : "No" }, # "Active" => lambda {|it| format_boolean(it[:active]) }, #"Authenticated" => lambda {|it| format_boolean it[:authenticated] }, "Username" => :username, # "Activity" => lambda {|it| get_appliance_session_blurbs(it).join("\n" + (' '*15)) }, "Last Login" => lambda {|it| format_duration_ago(it[:last_login_at]) }, #"Last Logout" => lambda {|it| format_duration_ago(it[:last_logout_at]) }, "Last Success" => lambda {|it| format_duration_ago(it[:last_success_at]) }, "Last Check" => lambda {|it| = nil if it[:last_check] && it[:last_check][:timestamp] = it[:last_check][:timestamp] end check_status = nil if it[:last_check] && it[:last_check][:http_status] check_status = it[:last_check][:http_status] end if format_duration_ago() # + (check_status ? " (HTTP #{check_status})" : "") else "" end }, "Response Time" => lambda {|it| format_duration_milliseconds(it[:last_check][:took]) rescue "" }, "Status" => lambda {|it| format_appliance_status(it, cyan) }, "Error" => lambda {|it| error_str = it[:last_check] ? it[:last_check][:error] : "" # meh no need to show http status, :error explains it well enough # check_status = it[:last_check] ? it[:last_check][:http_status] : nil # if check_status && check_status != 200 # #"(HTTP #{check_status}) #{error_str}" # else # error_str # end error_str }, } if appliance[:last_success_at].nil? columns.delete("Last Success") else columns.delete("Last Success") end if appliance[:last_check].nil? || appliance[:last_check][:error].nil? columns.delete("Error") end return as_description_list(appliance, columns, ) end |
#get(args) ⇒ Object
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/morpheus/cli/commands/remote.rb', line 813 def get(args) exit_code, err, , params, payload = 0, nil, {}, {}, {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") opts.on( '-u', '--url', "Print only the url." ) do [:url_only] = true end opts.on('--offline', '--offline', "Do this offline without an api request to refresh the remote appliance status.") do [:do_offline] = true end (opts, , [:json,:yaml,:csv,:fields, :quiet]) opts. = <<-EOT Print details about the a remote appliance. [name] is optional. This is the name of a remote. By default, the current appliance is used. Returns an error if the specified remote is not found, or there is no current remote. EOT end optparse.parse!(args) id_list = nil # verify_args!(args:args, min:1, optparse:optparse) if args.count == 0 id_list = ['current'] else id_list = parse_id_list(args) end #connect(options) return run_command_for_each_arg(id_list) do |arg| _get(arg, ) end end |
#handle(args) ⇒ Object
33 34 35 |
# File 'lib/morpheus/cli/commands/remote.rb', line 33 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/commands/remote.rb', line 37 def list(args) = {} params = {} show_all_activity = false current_only = false do_check = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[search]") opts.on("-a",'--all', "Show all the appliance activity details") do show_all_activity = true [:wrap] = true end opts.on("--current", "--current", "List only the active (current) appliance.") do current_only = true end opts.on("--check", "--check", "Check each appliance in the list to refresh their status, this may take a while.") do do_check = true end (opts, , [:list, :json, :yaml, :csv, :fields]) opts. = <<-EOT List the configured remote appliances. EOT end optparse.parse!(args) # verify_args!(args:args, optparse:optparse, count:0) if args.count > 0 [:phrase] = args.join(" ") end #connect(options) params.merge!(()) appliances = ::Morpheus::Cli::Remote.load_all_remotes(params) # if appliances.empty? # raise_command_error "You have no appliances configured. See the command `remote add`." # end if current_only appliances = appliances.select {|a| a[:active] } end if do_check # return here because it prints a list() too.. return _check_all_appliances() end # mock json json_response = {"appliances" => appliances} # render exit_code, err = 0, nil render_response(json_response, , "appliances") do if appliances.empty? if params[:phrase] print reset,"0 remotes matched '#{params[:phrase]}'", reset, "\n" print reset,"Try `remote add #{params[:phrase]}`", reset, "\n" return 0, nil # maybe exit non-zero when no records are found, could be nicer else warning_msg = "No remote appliances configured. See the command `remote add`." print yellow, warning_msg, reset, "\n" return 2, warning_msg end else title = "Morpheus Appliances" subtitles = parse_list_subtitles() subtitles << "Current" if current_only print_h1 title, subtitles, columns = { "Name" => :name, "URL" => lambda {|it| it[:url] || it[:host] }, "Status" => lambda {|it| format_appliance_status(it, cyan) }, "Version" => lambda {|it| it[:build_version] ? "#{it[:build_version]}" : '' }, #"Appliance URL" => lambda {|it| it[:appliance_url] ? "#{it[:appliance_url]}" : '' }, "Secure" => lambda {|it| format_boolean(it[:insecure] != true && (it[:url] || it[:host]).to_s.include?("https")) }, "Active" => lambda {|it| it[:active] ? "Yes " + format_is_current() : "No" }, #"Authenticated" => lambda {|it| format_boolean it[:authenticated] }, "Username" => :username, # "Activity" => lambda {|it| get_appliance_session_blurbs(it).join("\n" + (' '*15)) }, "Last Login" => lambda {|it| format_duration_ago(it[:last_login_at]) }, #"Last Logout" => lambda {|it| format_duration_ago(it[:last_logout_at]) }, "Last Success" => lambda {|it| format_duration_ago(it[:last_success_at]) }, "Last Check" => lambda {|it| check_str = "" if it[:last_check] = it[:last_check][:timestamp] check_status = it[:last_check][:http_status] if check_status if check_status == 200 # no need to show this else check_status = check_status end end if check_duration_str = format_duration_ago() # check_str = check_status ? "#{check_duration_str} (HTTP #{check_status})" : check_duration_str check_str = check_duration_str end else # check_str = "n/a" end check_str }, "Response Time" => lambda {|it| format_duration_milliseconds(it[:last_check][:took]) rescue "" }, "Error" => {display_method: lambda {|it| error_str = it[:last_check] ? it[:last_check][:error].to_s : "" error_str }, max_width: 30}, } # when an active remote is in the list, add => prefix and padding to keep things aligned. # this is sucky, use arrays has_active_remote = appliances.find {|appliance| appliance[:active] } if has_active_remote columns.delete("Name") columns = {" Name" => lambda {|it| it[:active] ? (bold + "=> #{it[:name]}" + reset + cyan) : " #{it[:name]}" } }.merge(columns) end has_an_error = appliances.find {|appliance| appliance[:last_check][:error] rescue nil } if !has_an_error columns.delete("Error") end if show_all_activity != true columns.delete("Secure") columns.delete("Appliance URL") columns.delete("Active") columns.delete("Authenticated") columns.delete("Last Login") columns.delete("Last Logout") columns.delete("Last Success") #columns.delete("Error") # unless appliances.find {|appliance| appliance[:last_check][:error] rescue nil } else # always remove these columns because they are worthless columns.delete("Authenticated") columns.delete("Active") columns.delete("Last Success") end # oops, table labels are upcase, but description list is not??, make them upcase here new_columns = {} columns.each {|k,v| new_columns[k.to_s.upcase] = v } columns = new_columns print as_pretty_table(appliances, columns, ) print reset print_results_pagination({size:appliances.size,total:appliances.size}) end print reset, "\n" end return exit_code, err end |
#load_remote_by_name(appliance_name) ⇒ Object
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1255 def load_remote_by_name(appliance_name) appliance = nil if appliance_name.to_s == "current" appliance = ::Morpheus::Cli::Remote.load_active_remote() if !appliance raise_command_error "No current appliance, see the command `remote use`" end else appliance = ::Morpheus::Cli::Remote.load_remote(appliance_name) if !appliance raise_command_error "Remote not found by name '#{appliance_name}', see the command `remote list`" end end return appliance end |
#refresh(args) ⇒ Object
409 410 411 |
# File 'lib/morpheus/cli/commands/remote.rb', line 409 def refresh(args) check_appliance(args) end |
#remove(args) ⇒ Object
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 |
# File 'lib/morpheus/cli/commands/remote.rb', line 957 def remove(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:auto_confirm, :quiet]) opts. = <<-EOT This will delete the specified remote appliance(s) from your local configuration. [name] is required. This is the name of a remote. More than one can be passed. EOT end optparse.parse!(args) verify_args!(args:args, min:1, optparse:optparse) id_list = parse_id_list(args) #connect(options) # verify they all exist first, this a cheap lookup and raises and error if not found id_list.each do |remote_id| found_remote = load_remote_by_name(remote_id) return 1, "Remote appliance not found by the name '#{remote_id}', see the command `remote list`" if found_remote.nil? end unless [:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete #{id_list.size == 1 ? 'remote' : id_list.size.to_s + ' remotes'}: #{anded_list(id_list)}?", ) return 9, "aborted command" end return run_command_for_each_arg(id_list) do |arg| _remove_appliance(arg, ) end end |
#remove_all(args) ⇒ Object
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 |
# File 'lib/morpheus/cli/commands/remote.rb', line 999 def remove_all(args) = {} checkall = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage() (opts, , [:auto_confirm, :quiet]) opts. = <<-EOT Remove all remote appliances, clearing the client configuration. This clears all the configured remotes and credentials. EOT end optparse.parse!(args) verify_args!(args:args, count:0, optparse:optparse) connect() # needed? _remove_all_appliances() end |
#rename(args) ⇒ Object
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/morpheus/cli/commands/remote.rb', line 624 def rename(args) exit_code, err, , params, payload = 0, nil, {}, {}, {} use_it = false is_insecure = nil new_name = nil optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name] [new name]") opts.on(nil, "--name NAME", "Update the name of your remote appliance") do |val| new_name = val end # opts.on(nil, '--use', "Make this the current appliance" ) do # use_it = true # end (opts, , [:auto_confirm, :quiet]) opts. = <<-EOT Rename a remote. This changes your client configuration remote name, not the appliance itself. [name] is required. This is the current name of a remote. [new name] is required. This is the new name for the remote. This must not already be in use. EOT end optparse.parse!(args) verify_args!(args:args, count:2, optparse:optparse) appliance_name = args[0].to_sym new_appliance_name = args[1].to_sym appliance = ::Morpheus::Cli::Remote.load_remote(appliance_name) if !appliance raise_command_error "Remote appliance not found by the name '#{appliance_name}', see the command `remote list`" end # don't allow overwrite yet matching_appliance = ::Morpheus::Cli::Remote.load_remote(new_appliance_name) if matching_appliance raise_command_error "Remote appliance already exists with the name '#{new_appliance_name}', see the command `, see the command `remote get #{new_appliance_name}`" end unless [:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you want to rename remote '#{appliance_name}' to '#{new_appliance_name}'?", ) return 9, "aborted command" end # this does all the work ::Morpheus::Cli::Remote.rename_remote(appliance_name, new_appliance_name) print_green_success "Renamed remote #{appliance_name} to #{new_appliance_name}" # todo: just go ahead and refresh it now... # _check(appliance_name, {:quiet => true}) # appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(new_appliance_name) # print new appliance details _get(new_appliance_name, {}) return 0, nil end |
#setup(args) ⇒ Object
This moved to the SetupCommand
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1222 def setup(args) print_error yellow,"[DEPRECATED] The command `remote setup` is deprecated. It has been replaced by `setup`.",reset,"\n" = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage() (opts, , [:payload, :options, :json, :dry_run, :quiet]) opts.on('--hubmode MODE','--hubmode MODE', "Choose an option for hub registration possible values are login, register, skip.") do |val| [:hubmode] = val.to_s.downcase end opts. = <<-EOT Setup a fresh remote appliance, initializing it. First, this checks if setup is available, and returns an error if not. Then it prompts to create the master tenant and admin user. If Morpheus Hub registration is enabled, you may login or register to retrieve a license key, or you can pass `--hubmode skip`. This is only available on a new, freshly installed remote appliance, and it may only be executed successfully one time. EOT end optparse.parse!(args) verify_args!(args:args, optparse:optparse, max:1) # just invoke the setup command. # for this to work, the argument [remote] must be the first argument. cmd_args = [] remote_name = nil if args[0] remote_name = args.shift # cmd_args = cmd_args + ["-r",remote_name, args] cmd_args = args + ["-r",remote_name, args] end return Morpheus::Cli::Setup.new.handle(cmd_args) end |
#unuse(args) ⇒ Object
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1116 def unuse(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage() opts. = "" + "Stop using the current remote appliance.\n" (opts, , []) end optparse.parse!(args) verify_args!(args:args, count:0, optparse:optparse) #connect(options) exit_code, err = 0, nil @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance if !@appliance_name print reset,"You are not using any remote appliance",reset,"\n" return 0 end Morpheus::Cli::Remote.clear_active_appliance() print_green_success "Stopped using remote #{display_appliance(@appliance_name, @appliance_url)}" # recalculate session variables ::Morpheus::Cli::Remote.recalculate_variable_map() # return list([]) return exit_code, err end |
#update(args) ⇒ Object
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/morpheus/cli/commands/remote.rb', line 674 def update(args) exit_code, err, , params, payload = 0, nil, {}, {}, {} use_it = false is_insecure = nil optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") opts.on("--name NAME", String, "Update the name of your remote appliance") do |val| params[:name] = val end opts.on("--url URL", String, "Update the url of your remote appliance") do |val| params[:host] = val end opts.on(nil, "--secure", "Prevent insecure HTTPS communication. This is enabled by default") do params[:secure] = true end opts.on(nil, "--insecure", "Allow insecure HTTPS communication. i.e. Ignore SSL errors.") do params[:insecure] = true end opts.on(nil, '--use', "Make this the current appliance" ) do use_it = true params[:active] = true end (opts, , [:quiet]) opts. = "This can be used to update remote appliance settings.\n" end optparse.parse!(args) verify_args!(args:args, count:1, optparse:optparse) appliance_name = args[0].to_sym appliance = load_remote_by_name(appliance_name) appliance_name = appliance[:name] appliance_url = appliance[:url] # params[:url] = args[1] if args[1] if params.empty? print_error Morpheus::Terminal.angry_prompt puts_error "Specify at least one option to update" puts_error optparse return 1 end if params[:insecure] appliance[:insecure] = true elsif params[:secure] appliance.delete(:insecure) end if params[:url] || params[:host] appliance[:host] = params[:url] || params[:host] end ::Morpheus::Cli::Remote.save_remote(appliance_name, appliance) # rename_remote() should be inside save_remote() if params[:name] && params[:name].to_s != appliance_name.to_s ::Morpheus::Cli::Remote.save_remote(appliance_name, appliance) ::Morpheus::Cli::Remote.rename_remote(appliance_name, params[:name]) print_green_success "Updated remote #{appliance_name} (renamed #{params[:name]})" appliance_name = params[:name] else ::Morpheus::Cli::Remote.save_remote(appliance_name, appliance) print_green_success "Updated remote #{appliance_name}" end # todo: just go ahead and refresh it now... # _check(appliance_name, {:quiet => true}) appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name) # print new appliance details _get(appliance[:name], {}) return exit_code, err end |
#use(args) ⇒ Object
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 |
# File 'lib/morpheus/cli/commands/remote.rb', line 1044 def use(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") opts.on('--offline', '--offline', "Do this offline without an api request to refresh the remote appliance status.") do [:do_offline] = true end (opts, , [:quiet]) opts. = <<-EOT [name] is required. This is the name of a remote to begin using. Start using a remote, making it the active (current) remote appliance. This switches the remote context of your client configuration for all subsequent commands. It is important to always be aware of the context your commands are running in. The command `remote current` will return the current remote information. Instead of using an active remote, the -r option can be specified with each command. It is recommeneded to set a custom prompt to show the current remote name. For example, add the following to your .morpheusrc file: # set your shell prompt to display the current username and remote set-prompt "%green%username%reset@%magenta%remote %cyanmorpheus> %reset" EOT end optparse.parse!(args) verify_args!(args:args, count:1, optparse:optparse) exit_code, err = 0, nil # connect() current_appliance_name, current_appliance_url = @appliance_name, @appliance_url current_appliance = ::Morpheus::Cli::Remote.load_active_remote() if current_appliance current_appliance_name, current_appliance_url = current_appliance[:name], current_appliance[:url] end # current_appliance_name, current_appliance_url = @appliance_name, @appliance_url appliance_name = args[0].to_sym appliance = load_remote_by_name(appliance_name) # appliance = ::Morpheus::Cli::Remote.load_remote(appliance_name) # if !appliance # raise_command_error "Remote not found by the name '#{appliance_name}', see the command `remote list`" # end # appliance = ::Morpheus::Cli::Remote.set_active_appliance(appliance_name) appliance[:active] = true appliance = ::Morpheus::Cli::Remote.save_remote(appliance_name, appliance) if [:quiet] return 0 end if current_appliance_name.to_s == appliance_name.to_s print_green_success "Using remote #{display_appliance(appliance[:name], appliance[:url])}" else print_green_success "Using remote #{display_appliance(appliance[:name], appliance[:url])}" end # recalculate session variables ::Morpheus::Cli::Remote.recalculate_variable_map() # could just do this # return _get(appliance_name, options) # ok need to refresh here unless --offline # refresh status and version # maybe just make json_response = appliance here unless [:do_offline] appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance[:name]) end # return list([]) return exit_code, err end |
#version(args) ⇒ Object
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 500 501 502 503 504 |
# File 'lib/morpheus/cli/commands/remote.rb', line 447 def version(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[remote]") opts.on('--offline', '--offline', "Do this offline without an api request to refresh the remote appliance status.") do [:do_offline] = true end (opts, , [:json, :yaml, :csv, :fields, :quiet]) opts. = <<-EOT Print version of remote appliance. [name] is optional. This is the name of a remote. Default is the current remote. This makes a request to the configured appliance url and updates the status and version. EOT end optparse.parse!(args) verify_args!(args:args, max:1, optparse:optparse) #connect(options) # print version, default is current remote appliance_name = nil if args.count == 0 appliance_name = 'current' elsif args.count == 1 appliance_name = args[0] else raise_command_error "No current appliance, see the command `remote use`" end exit_code, err = 0, nil appliance = load_remote_by_name(appliance_name) appliance_name = appliance[:name] appliance_url = appliance[:url] # found appliance, now refresh it # print "Checking remote url: #{appliance[:url]} ..." json_response = nil if [:do_offline] == true json_response = {'appliance' => appliance} else appliance, json_response = ::Morpheus::Cli::Remote.refresh_remote(appliance_name) end # render render_response(json_response, ) do end # if options[:json] || options[:yml] || options[:csv] || options[:quiet] render_result = render_with_format(json_response, ) return exit_code if render_result build_version = appliance[:build_version] if build_version print cyan,build_version.to_s,reset,"\n" return 0 else print yellow,"version unknown".to_s,reset,"\n" return 1 end end |
#view(args) ⇒ Object
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/morpheus/cli/commands/remote.rb', line 896 def view(args) print_error "#{yellow}DEPRECATION WARNING: `remote view` has been deprecated and replaced with `view`. Please use `view` instead.#{reset}\n" = {} path = "/" no_auth = false optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") opts.on('--path PATH', String, "Specify a path to load. eg '/logs'" ) do |val| path = val end opts.on('--no-auth', "Do not attempt to login with access token." ) do |val| no_auth = true end (opts, , [:dry_run]) opts. = <<-EOT View remote appliance in a web browser. [name] is optional. This is the name of a remote. Default is the current remote. This will automatically login with the current access token. EOT end optparse.parse!(args) # verify_args!(args:args, optparse:optparse) if args.count == 0 id_list = ['current'] #raise_command_error "wrong number of arguments, expected 1-N and got 0\n#{optparse}" else id_list = parse_id_list(args) end #connect(options) return run_command_for_each_arg(id_list) do |arg| _view_appliance(arg, path, no_auth, ) end end |