Class: GovukConnect::CLI
- Inherits:
-
Object
- Object
- GovukConnect::CLI
- Defined in:
- lib/govuk_connect/cli.rb
Constant Summary collapse
- USAGE_BANNER =
"Usage: gds govuk connect TYPE TARGET [options]".freeze
- EXAMPLES =
<<-EXAMPLES.freeze gds govuk connect ssh --environment integration backend gds govuk connect scp-push --environment integration backend filename.txt /tmp/ gds govuk connect scp-pull --environment integration backend /tmp/filename.txt ~/Downloads/ gds govuk connect app-console --environment staging publishing-api gds govuk connect app-dbconsole -e integration whitehall_backend/whitehall gds govuk connect rabbitmq -e staging aws/rabbitmq EXAMPLES
- MACHINE_TARGET_DESCRIPTION =
<<-DOCS.freeze The ssh, scp-*, rabbitmq connection types target machines. The machine can be specified by name, for example: gds govuk connect ssh -e integration #{bold('backend')} If the hosting provider is ambiguous, you'll need to specify it prior to the name, for example: gds govuk connect ssh -e staging #{bold('aws/')}backend If you want to connect to a specific machine, you can specify a number after the name, for example: gds govuk connect ssh -e integration backend#{bold(':2')} DOCS
- APP_TARGET_DESCRIPTION =
<<-DOCS.freeze The app-console and app-dbconsole connection types target applications. The application is specified by name, for example: gds govuk connect app-console -e integration #{bold('publishing-api')} If the node class is ambiguous, you'll need to specify it prior to the name, for example: gds govuk connect app-console -e integration #{bold('whitehall_backend/')}whitehall If you want to connect to a specific machine, you can specify a number after the name, for example: gds govuk connect app-console -e integration publishing-api#{bold(':2')} DOCS
- CONNECTION_TYPE_DESCRIPTIONS =
{ "ssh" => "Connect to a machine through SSH.", "app-console" => "Launch a console for an application. For example, a rails console when connecting to a Rails application.", "app-dbconsole" => "Launch a console for the database for an application.", "rabbitmq" => "Setup port forwarding to the RabbitMQ admin interface.", }.freeze
- RABBITMQ_PORT =
15_672
- JUMPBOXES =
{ test: { aws: "jumpbox.pink.test.govuk.digital", }, integration: { aws: "jumpbox.integration.publishing.service.gov.uk", }, staging: { aws: "jumpbox.staging.govuk.digital", }, production: { aws: "jumpbox.production.govuk.digital", }, }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #application_names_from_node_class_data(environment, hosting) ⇒ Object
- #bold(string) ⇒ Object
- #check_for_additional_arguments(command, args) ⇒ Object
- #check_for_target(target) ⇒ Object
- #check_ruby_version_greater_than(required_major:, required_minor:) ⇒ Object
- #config_file ⇒ Object
- #error(message) ⇒ Object
- #get_domains_for_node_class(target, environment, hosting) ⇒ Object
- #govuk_app_command(target, environment, command) ⇒ Object
- #govuk_directory ⇒ Object
- #govuk_node_list_classes(environment, hosting) ⇒ Object
- #govuk_puppet_node_class_data(environment, hosting) ⇒ Object
- #hosting_and_environment_from_url(url) ⇒ Object
- #hosting_for_app(app_name, environment) ⇒ Object
- #hosting_for_node_type(node_type, environment) ⇒ Object
- #hosting_for_target_and_environment(target, environment) ⇒ Object
- #hosting_providers ⇒ Object
- #info(message) ⇒ Object
- #jumpbox_for_environment_and_hosting(environment, hosting) ⇒ Object
-
#levenshtein_distance(string1, string2) ⇒ Object
From Rosetta Code: rosettacode.org/wiki/Levenshtein_distance#Ruby.
- #load_yaml(file_path) ⇒ Object
- #log(message) ⇒ Object
- #main(argv) ⇒ Object
- #node_class_for_app(app_name, environment, hosting) ⇒ Object
- #node_classes_for_environment_and_hosting(environment, hosting) ⇒ Object
- #parse_hosting_name_and_number(target) ⇒ Object
- #parse_node_class_app_name_and_number(target) ⇒ Object
- #parse_options(argv) ⇒ Object
- #port_free?(port) ⇒ Boolean
- #print_empty_line ⇒ Object
- #print_ssh_username_configuration_help ⇒ Object
- #rabbitmq_root_password_command(hosting, environment) ⇒ Object
- #random_free_port ⇒ Object
- #scp(target, environment, files, push: false, additional_arguments: []) ⇒ Object
- #single_hosting_provider_for_environment(environment) ⇒ Object
- #ssh(target, environment, command: false, port_forward: false, additional_arguments: []) ⇒ Object
- #ssh_capture(environment, hosting, remote_command) ⇒ Object
- #ssh_identity_arguments ⇒ Object
- #ssh_identity_file ⇒ Object
- #ssh_target(target, environment) ⇒ Object
- #ssh_username ⇒ Object
- #strings_similar_to(target, strings) ⇒ Object
- #target_from_options(target, options) ⇒ Object
- #types ⇒ Object
- #user_at_host(user, host) ⇒ Object
Class Method Details
.bold(string) ⇒ Object
10 11 12 |
# File 'lib/govuk_connect/cli.rb', line 10 def self.bold(string) "\e[1m#{string}\e[0m" end |
Instance Method Details
#application_names_from_node_class_data(environment, hosting) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/govuk_connect/cli.rb', line 353 def application_names_from_node_class_data(environment, hosting) node_class_data = govuk_puppet_node_class_data( environment, hosting, ) all_names = node_class_data.flat_map do |_node_class, data| data["apps"] end all_names.sort.uniq end |
#bold(string) ⇒ Object
14 15 16 |
# File 'lib/govuk_connect/cli.rb', line 14 def bold(string) self.class.bold(string) end |
#check_for_additional_arguments(command, args) ⇒ Object
863 864 865 866 867 868 |
# File 'lib/govuk_connect/cli.rb', line 863 def check_for_additional_arguments(command, args) unless args.empty? error "error: #{command} doesn't support arguments: #{args}" exit 1 end end |
#check_for_target(target) ⇒ Object
853 854 855 856 857 858 859 860 861 |
# File 'lib/govuk_connect/cli.rb', line 853 def check_for_target(target) unless target error "error: you must specify the target\n" warn USAGE_BANNER print_empty_line warn EXAMPLES exit 1 end end |
#check_ruby_version_greater_than(required_major:, required_minor:) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/govuk_connect/cli.rb', line 142 def check_ruby_version_greater_than(required_major:, required_minor:) major, minor = RUBY_VERSION.split "." insufficient_version = ( major.to_i < required_major || ( major.to_i == required_major && minor.to_i < required_minor ) ) if insufficient_version error "insufficient Ruby version: #{RUBY_VERSION}" error "must be at least #{required_major}.#{required_minor}" exit 1 end end |
#config_file ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/govuk_connect/cli.rb', line 219 def config_file @config_file ||= begin directory = ENV.fetch("XDG_CONFIG_HOME", "#{Dir.home}/.config") File.join(directory, "config.yaml") end end |
#error(message) ⇒ Object
107 108 109 |
# File 'lib/govuk_connect/cli.rb', line 107 def error() warn "\e[1m\e[31m#{}\e[0m" end |
#get_domains_for_node_class(target, environment, hosting) ⇒ Object
263 264 265 266 267 268 269 270 |
# File 'lib/govuk_connect/cli.rb', line 263 def get_domains_for_node_class(target, environment, hosting) domains = ssh_capture(environment, hosting, "govuk_node_list -c #{target}") if hosting == :aws domains else domains.sort end end |
#govuk_app_command(target, environment, command) ⇒ Object
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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/govuk_connect/cli.rb', line 474 def govuk_app_command(target, environment, command) node_class, app_name, number = parse_node_class_app_name_and_number(target) info "Connecting to the app #{command} for #{bold(app_name)},\ in the #{bold(environment)} environment" hosting = hosting_for_app(app_name, environment) info "The relevant hosting provider is #{bold(hosting)}" node_class ||= node_class_for_app( app_name, environment, hosting, ) unless node_class error "error: application '#{app_name}' not found." print_empty_line application_names = application_names_from_node_class_data( environment, hosting, ) similar_application_names = strings_similar_to(app_name, application_names) if similar_application_names.any? info "did you mean:" similar_application_names.each { |s| info " - #{s}" } else info "all applications:" print_empty_line info " #{application_names.join(', ')}" print_empty_line end exit 1 end info "The relevant node class is #{bold(node_class)}" ssh( { hosting: hosting, name: node_class, number: number, }, environment, command: "govuk_app_#{command} #{app_name}", ) end |
#govuk_directory ⇒ Object
298 299 300 |
# File 'lib/govuk_connect/cli.rb', line 298 def govuk_directory File.join(ENV["HOME"], "govuk") end |
#govuk_node_list_classes(environment, hosting) ⇒ Object
253 254 255 256 257 258 259 260 261 |
# File 'lib/govuk_connect/cli.rb', line 253 def govuk_node_list_classes(environment, hosting) log "debug: looking up classes in #{hosting}/#{environment}" classes = ssh_capture(environment, hosting, "govuk_node_list --classes").sort log "debug: classes:" classes.each { |c| log " - #{c}" } classes end |
#govuk_puppet_node_class_data(environment, hosting) ⇒ Object
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 |
# File 'lib/govuk_connect/cli.rb', line 302 def govuk_puppet_node_class_data(environment, hosting) log "debug: fetching govuk-puppet node class data for #{hosting} #{environment}" local_hieradata_root = File.join( govuk_directory, "govuk-puppet", { aws: "hieradata_aws", }[hosting], ) hieradata_file = File.join(local_hieradata_root, "#{environment}.yaml") log "debug: reading #{hieradata_file}" environment_specific_hieradata = load_yaml(hieradata_file) if environment_specific_hieradata["node_class"] environment_specific_hieradata["node_class"] else common_hieradata = load_yaml( File.join(local_hieradata_root, "common.yaml"), ) common_hieradata["node_class"] end end |
#hosting_and_environment_from_url(url) ⇒ Object
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/govuk_connect/cli.rb', line 627 def hosting_and_environment_from_url(url) uri = URI(url) host_to_hosting_and_environment = { "alert.integration.publishing.service.gov.uk" => %i[aws integration], "alert.staging.govuk.digital" => %i[aws staging], "alert.blue.staging.govuk.digital" => %i[aws staging], "alert.production.govuk.digital" => %i[aws production], "alert.blue.production.govuk.digital" => %i[aws production], } unless host_to_hosting_and_environment.key? uri.host error "error: unknown hosting and environment for: #{uri.host}" exit 1 end host_to_hosting_and_environment[uri.host] end |
#hosting_for_app(app_name, environment) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/govuk_connect/cli.rb', line 449 def hosting_for_app(app_name, environment) log "debug: finding hosting for #{app_name} in #{environment}" hosting = single_hosting_provider_for_environment(environment) if hosting log "debug: this environment has a single hosting provider: #{hosting}" return hosting end aws_app_names = application_names_from_node_class_data( environment, :aws, ) if aws_app_names.include? app_name log "debug: #{app_name} is hosted in AWS" return :aws end error "error: unknown hosting value '#{hosting}' for #{app_name}" exit 1 end |
#hosting_for_node_type(node_type, environment) ⇒ Object
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 |
# File 'lib/govuk_connect/cli.rb', line 422 def hosting_for_node_type(node_type, environment) log "debug: Looking up hosting for node_type: #{node_type}" hosting = single_hosting_provider_for_environment(environment) return hosting if hosting aws_node_types = govuk_node_list_classes(environment, :aws) if aws_node_types.include?(node_type) :aws else error "error: couldn't find #{node_type} in #{environment}" similar_node_types = strings_similar_to(node_type, aws_node_types) if similar_node_types.any? info "\ndid you mean:" similar_node_types.each { |s| info " - #{s}" } else info "\nall node types:" all_node_types.each { |s| info " - #{s}" } end exit 1 end end |
#hosting_for_target_and_environment(target, environment) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/govuk_connect/cli.rb', line 408 def hosting_for_target_and_environment(target, environment) hosting = single_hosting_provider_for_environment( environment, ) unless hosting hosting, name, _number = parse_hosting_name_and_number(target) hosting ||= hosting_for_node_type(name, environment) end hosting end |
#hosting_providers ⇒ Object
188 189 190 191 192 193 |
# File 'lib/govuk_connect/cli.rb', line 188 def hosting_providers JUMPBOXES .map { |_env, jumpboxes| jumpboxes.keys } .flatten .uniq end |
#info(message) ⇒ Object
103 104 105 |
# File 'lib/govuk_connect/cli.rb', line 103 def info() warn end |
#jumpbox_for_environment_and_hosting(environment, hosting) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/govuk_connect/cli.rb', line 195 def jumpbox_for_environment_and_hosting(environment, hosting) raise "missing environment" unless environment raise "missing hosting" unless hosting jumpbox = JUMPBOXES[environment][hosting] unless jumpbox error "error: couldn't determine jumpbox for #{hosting}/#{environment}" exit 1 end jumpbox end |
#levenshtein_distance(string1, string2) ⇒ Object
From Rosetta Code: rosettacode.org/wiki/Levenshtein_distance#Ruby
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/govuk_connect/cli.rb', line 117 def levenshtein_distance(string1, string2) string1 = string1.downcase string2 = string2.downcase costs = Array(0..string2.length) # i == 0 (1..string1.length).each do |i| costs[0] = i nw = i - 1 # j == 0; nw is lev(i-1, j) (1..string2.length).each do |j| costs[j] = [ costs[j] + 1, costs[j - 1] + 1, string1[i - 1] == string2[j - 1] ? nw : nw + 1, ].min nw = costs[j] end end costs[string2.length] end |
#load_yaml(file_path) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/govuk_connect/cli.rb', line 329 def load_yaml(file_path) # Psych (the gem that provides YAML parsing) introduced a new method, # safe_load_file, in version 3.3 (which shipped with Ruby 3.0) and in # version 4 (shipped with Ruby 3.1) the API for load_file changed and will # error with GOV.UK hieradata. # # Once this gem no longer supports Ruby 2.x (or pins to a version of Psych) # we can remove this conditional and use YAML.safe_load_file only if YAML.respond_to?(:safe_load_file) YAML.safe_load_file(file_path, aliases: true) else YAML.load_file(file_path) end end |
#log(message) ⇒ Object
95 96 97 |
# File 'lib/govuk_connect/cli.rb', line 95 def log() warn if @verbose end |
#main(argv) ⇒ Object
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 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 |
# File 'lib/govuk_connect/cli.rb', line 958 def main(argv) check_ruby_version_greater_than(required_major: 2, required_minor: 0) extra_arguments_after_double_dash = [] double_dash_index = argv.index "--" if double_dash_index # This is used in the case of passing extra options to ssh and # scp, the -- acts as a separator, so to avoid optparse # interpreting those as options, split argv around -- before # parsing the options extra_arguments_after_double_dash = argv[double_dash_index + 1, argv.length] argv = argv[0, double_dash_index] end = (argv) type, target, *extra_arguments_before_double_dash = argv unless type error "error: you must specify the connection type\n" warn @option_parser.help warn "\nValid connection types are:\n" types.each_key do |x| warn " - #{x}" end print_empty_line warn "Example commands:" warn EXAMPLES exit 1 end handler = types[type] unless handler error "error: unknown connection type: #{type}\n" warn "Valid connection types are:\n" types.each_key do |x| warn " - #{x}" end print_empty_line warn "Example commands:" warn EXAMPLES exit 1 end environment = [:environment]&.to_sym unless environment error "error: you must specify the environment\n" warn @option_parser.help exit 1 end unless JUMPBOXES.key? environment error "error: unknown environment '#{environment}'" print_empty_line info "Valid environments are:" JUMPBOXES.each_key { |e| info " - #{e}" } exit 1 end handler.call( target, environment, extra_arguments_before_double_dash, extra_arguments_after_double_dash, , ) rescue Interrupt # Handle SIGTERM without printing a stacktrace exit 1 end |
#node_class_for_app(app_name, environment, hosting) ⇒ Object
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 |
# File 'lib/govuk_connect/cli.rb', line 366 def node_class_for_app(app_name, environment, hosting) log "debug: finding node class for #{app_name} in #{hosting} #{environment}" node_class_data = govuk_puppet_node_class_data( environment, hosting, ) app_lookup_hash = {} node_class_data.each do |node_class, data| data["apps"].each do |app| if app_lookup_hash.key? app app_lookup_hash[app] += [node_class] else app_lookup_hash[app] = [node_class] end end end node_classes = app_lookup_hash[app_name] return if node_classes.nil? if node_classes.length > 1 error "error: ambiguous node class for #{app_name} in #{environment}" print_empty_line info "specify the node class and application mame, for example: " node_classes.each do |node_class| info "\n gds govuk connect app-console -e #{environment} #{node_class}/#{app_name}" end print_empty_line exit 1 else node_class = node_classes.first end log "debug: node class: #{node_class}" node_class end |
#node_classes_for_environment_and_hosting(environment, hosting) ⇒ Object
344 345 346 347 348 349 350 351 |
# File 'lib/govuk_connect/cli.rb', line 344 def node_classes_for_environment_and_hosting(environment, hosting) govuk_puppet_node_class_data( environment, hosting, ).map do |node_class, _data| node_class end end |
#parse_hosting_name_and_number(target) ⇒ Object
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 |
# File 'lib/govuk_connect/cli.rb', line 704 def parse_hosting_name_and_number(target) log "debug: parsing target: #{target}" if target.is_a? Hash return %i[hosting name number].map do |key| target[key] end end if target.include? "/" hosting, name_and_number = target.split "/" hosting = hosting.to_sym unless %i[aws].include? hosting error "error: unknown hosting provider: #{hosting}" print_empty_line info "available hosting providers are:" hosting_providers.each { |x| info " - #{x}" } exit 1 end else name_and_number = target end if name_and_number.include? ":" name, number = name_and_number.split ":" number = number.to_i else name = name_and_number end log "debug: hosting: #{hosting.inspect}, name: #{name.inspect}, number: #{number.inspect}" [hosting, name, number] end |
#parse_node_class_app_name_and_number(target) ⇒ Object
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/govuk_connect/cli.rb', line 742 def parse_node_class_app_name_and_number(target) log "debug: parsing target: #{target}" if target.is_a? Hash return %i[node_class app_name number].map do |key| target[key] end end if target.include? "/" node_class, app_name_and_number = target.split "/" else app_name_and_number = target end if app_name_and_number.include? ":" app_name, number = app_name_and_number.split ":" number = number.to_i else app_name = app_name_and_number end log "debug: node_class: #{node_class.inspect}, app_name: #{app_name.inspect}, number: #{number.inspect}" [node_class, app_name, number] end |
#parse_options(argv) ⇒ Object
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 673 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 |
# File 'lib/govuk_connect/cli.rb', line 646 def (argv) = {} @option_parser = OptionParser.new do |opts| opts. = USAGE_BANNER opts.on( "-e", "--environment ENVIRONMENT", "Select which environment to connect to", ) do |o| [:environment] = o.to_sym end opts.on( "--hosting-and-environment-from-alert-url URL", "Select which environment to connect to based on the URL provided.", ) do |o| hosting, environment = hosting_and_environment_from_url(o) [:hosting] = hosting [:environment] = environment end opts.on("-p", "--port-forward SERVICE", "Connect to a remote port") do |o| [:port_forward] = o end opts.on("-v", "--verbose", "Enable more detailed logging") do @verbose = true end opts.on("-h", "--help", "Prints usage examples and information") do info opts print_empty_line info bold("EXAMPLES") info EXAMPLES print_empty_line info bold("CONNECTION TYPES") types.each_key do |x| info " #{x}" description = CONNECTION_TYPE_DESCRIPTIONS[x] info " #{description}" if description end print_empty_line info bold("MACHINE TARGET") info MACHINE_TARGET_DESCRIPTION print_empty_line info bold("APPLICATION TARGET") info APP_TARGET_DESCRIPTION exit end opts.on("-V", "--version", "Prints version information") do info GovukConnect::VERSION.to_s exit end end @option_parser.parse!(argv) end |
#port_free?(port) ⇒ Boolean
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/govuk_connect/cli.rb', line 160 def port_free?(port) # No idea how well this works, but it's hopefully better than nothing log "debug: checking if port #{port} is free" Socket.tcp("127.0.0.1", port, connect_timeout: 0.1) {} false rescue Errno::ETIMEDOUT log "debug: port #{port} doesn't seem to be free" false rescue Errno::ECONNREFUSED log "debug: port #{port} is free" true end |
#print_empty_line ⇒ Object
99 100 101 |
# File 'lib/govuk_connect/cli.rb', line 99 def print_empty_line warn "" end |
#print_ssh_username_configuration_help ⇒ Object
111 112 113 114 |
# File 'lib/govuk_connect/cli.rb', line 111 def print_ssh_username_configuration_help info "The SSH username used was: #{bold(ssh_username)}" info "Check this is correct, and if it isn't, set the `USER` environment variable to the correct username." end |
#rabbitmq_root_password_command(hosting, environment) ⇒ Object
613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/govuk_connect/cli.rb', line 613 def rabbitmq_root_password_command(hosting, environment) hieradata_directory = { aws: "puppet_aws", }[hosting] directory = File.join( govuk_directory, "govuk-secrets", hieradata_directory, ) "cd #{directory} && rake eyaml:decrypt_value[#{environment},govuk_rabbitmq::root_password]" end |
#random_free_port ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/govuk_connect/cli.rb', line 174 def random_free_port tries = 0 while tries <= 10 port = rand(32_768...61_000) return port if port_free? port tries += 1 end raise "couldn't find open port" end |
#scp(target, environment, files, push: false, additional_arguments: []) ⇒ Object
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/govuk_connect/cli.rb', line 575 def scp( target, environment, files, push: false, additional_arguments: [] ) log "debug: scp #{push ? 'push' : 'pull'} to #{target} in #{environment}" target, hosting = ssh_target(target, environment) sources = files[0, files.length - 1] destination = files[-1] if push destination = "#{target}:#{destination}" else sources = sources.map { |source| "#{target}:#{source}" } end scp_command = [ "scp", *ssh_identity_arguments, "-o", "ProxyJump=#{user_at_host(ssh_username, jumpbox_for_environment_and_hosting(environment, hosting))}", "-o", "User=#{ssh_username}", *additional_arguments, "--", *sources, destination, ] info "\n#{bold('Running command:')} #{scp_command.join(' ')}\n\n" exec(*scp_command) end |
#single_hosting_provider_for_environment(environment) ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/govuk_connect/cli.rb', line 209 def single_hosting_provider_for_environment(environment) jumpboxes = JUMPBOXES[environment] if jumpboxes.size == 1 jumpboxes.keys[0] else false end end |
#ssh(target, environment, command: false, port_forward: false, additional_arguments: []) ⇒ Object
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 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 |
# File 'lib/govuk_connect/cli.rb', line 526 def ssh( target, environment, command: false, port_forward: false, additional_arguments: [] ) log "debug: ssh to #{target} in #{environment}" target, hosting = ssh_target(target, environment) ssh_command = [ "ssh", *ssh_identity_arguments, "-J", user_at_host( ssh_username, jumpbox_for_environment_and_hosting(environment, hosting), ), user_at_host( ssh_username, target, ), ] if command ssh_command += [ "-t", # Force tty allocation so that interactive commands work command, ] elsif port_forward localhost_port = random_free_port ssh_command += [ "-N", "-L", "#{localhost_port}:127.0.0.1:#{port_forward}", ] info "Port forwarding setup, access:\n\n http://127.0.0.1:#{localhost_port}/\n\n" end ssh_command += additional_arguments info "\n#{bold('Running command:')} #{ssh_command.join(' ')}\n\n" exec(*ssh_command) end |
#ssh_capture(environment, hosting, remote_command) ⇒ Object
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 |
# File 'lib/govuk_connect/cli.rb', line 272 def ssh_capture(environment, hosting, remote_command) command = [ "ssh", "-o", "ConnectTimeout=2", # Show a failure quickly *ssh_identity_arguments, user_at_host( ssh_username, jumpbox_for_environment_and_hosting(environment, hosting), ), remote_command, ].join(" ") log "debug: running command: #{command}" output, status = Open3.capture2(command) unless status.success? error "\nerror: command failed: #{command}" print_empty_line print_ssh_username_configuration_help exit 1 end output.split("\n") end |
#ssh_identity_arguments ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/govuk_connect/cli.rb', line 241 def ssh_identity_arguments if ssh_identity_file ["-i", ssh_identity_file] else [] end end |
#ssh_identity_file ⇒ Object
237 238 239 |
# File 'lib/govuk_connect/cli.rb', line 237 def ssh_identity_file @ssh_identity_file ||= (load_yaml(config_file)["ssh_identity_file"] if File.exist? config_file) end |
#ssh_target(target, environment) ⇒ Object
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 812 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 844 845 846 847 848 849 850 851 |
# File 'lib/govuk_connect/cli.rb', line 787 def ssh_target(target, environment) # Split something like aws/backend:2 in to :aws, 'backend', 2 hosting, name, number = parse_hosting_name_and_number(target) if name.end_with? ".internal" target = name hosting = :aws else # The hosting might not have been provided, so check if necessary hosting ||= hosting_for_target_and_environment(target, environment) domains = get_domains_for_node_class( name, environment, hosting, ) if domains.length.zero? error "error: couldn't find #{name} in #{hosting}/#{environment}" node_types = govuk_node_list_classes(environment, hosting) similar_node_types = strings_similar_to(name, node_types) if similar_node_types.any? info "\ndid you mean:" similar_node_types.each { |s| info " - #{s}" } else info "\nall node types:" node_types.each { |s| info " - #{s}" } end exit 1 elsif domains.length == 1 target = domains.first info "There is #{bold('one machine')} to connect to" else n_machines = bold("#{domains.length} machines") info "There are #{n_machines} of this class" if number unless number.positive? print_empty_line error "error: invalid machine number '#{number}', it must be > 0" exit 1 end unless number <= domains.length print_empty_line error "error: cannot connect to machine number: #{number}" exit 1 end target = domains[number - 1] info "Connecting to number #{number}" else target = domains.sample info "Connecting to a random machine (number #{domains.find_index(target) + 1})" end end end [target, hosting] end |
#ssh_username ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/govuk_connect/cli.rb', line 227 def ssh_username @ssh_username ||= begin if File.exist? config_file config_ssh_username = load_yaml(config_file)["ssh_username"] end config_ssh_username || ENV["USER"] end end |
#strings_similar_to(target, strings) ⇒ Object
136 137 138 139 140 |
# File 'lib/govuk_connect/cli.rb', line 136 def strings_similar_to(target, strings) strings.select do |s| levenshtein_distance(s, target) <= 3 # No specific reasoning for this value end end |
#target_from_options(target, options) ⇒ Object
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 |
# File 'lib/govuk_connect/cli.rb', line 769 def (target, ) if .key? :hosting hosting, name, number = parse_hosting_name_and_number(target) if hosting error "error: hosting specified twice" exit 1 end { hosting: [:hosting], name: name, number: number, } else target end end |
#types ⇒ Object
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 895 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 929 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 956 |
# File 'lib/govuk_connect/cli.rb', line 870 def types @types ||= { "app-console" => proc do |target, environment, args, extra_args, | check_for_target(target) check_for_additional_arguments("app-console", args) check_for_additional_arguments("app-console", extra_args) govuk_app_command(target, environment, "console") end, "app-dbconsole" => proc do |target, environment, args, extra_args, | check_for_target(target) check_for_additional_arguments("app-dbconsole", args) check_for_additional_arguments("app-dbconsole", extra_args) govuk_app_command(target, environment, "dbconsole") end, "rabbitmq" => proc do |target, environment, args, extra_args, | check_for_additional_arguments("rabbitmq", args) check_for_additional_arguments("rabbitmq", extra_args) target ||= "rabbitmq" root_password_command = rabbitmq_root_password_command( hosting_for_target_and_environment(target, environment), environment, ) info "You'll need to login as the RabbitMQ #{bold('root')} user." info "Get the password from govuk-secrets, or example:\n\n" info " #{bold(root_password_command)}" print_empty_line ssh( target, environment, port_forward: RABBITMQ_PORT, ) end, "ssh" => proc do |target, environment, args, extra_args, | check_for_target(target) target = (target, ) ssh( target, environment, port_forward: [:port_forward], additional_arguments: [args, extra_args].flatten, ) end, "scp-pull" => proc do |target, environment, args, extra_args, | check_for_target(target) target = (target, ) if args.length < 2 error "error: need at least two filenames" exit 1 end scp( target, environment, args, additional_arguments: extra_args, ) end, "scp-push" => proc do |target, environment, args, extra_args, | check_for_target(target) target = (target, ) if args.length < 2 error "error: need at least two filenames" exit 1 end scp( target, environment, args, push: true, additional_arguments: extra_args, ) end, } end |
#user_at_host(user, host) ⇒ Object
249 250 251 |
# File 'lib/govuk_connect/cli.rb', line 249 def user_at_host(user, host) "#{user}@#{host}" end |