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 rabbitmq -e staging aws/rabbitmq gds govuk connect amazonmq -e integration 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" => "(DEPRECATED; use kubectl instead) Launch a Rails console for an application.", "app-dbconsole" => "(DEPRECATED; use kubectl instead) Launch a Rails database console for an application.", "rabbitmq" => "Setup port forwarding to the RabbitMQ admin interface.", "amazonmq" => "Setup port forwarding to the AmazonMQ admin interface.", }.freeze
- RABBITMQ_PORT =
15_672
- AMAZONMQ_PORT =
443
- 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
- #amazonmq_root_password_command(environment) ⇒ Object
- #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_k8s_setup_info ⇒ Object
- #print_ssh_username_configuration_help ⇒ Object
- #print_vpn_help_info ⇒ Object
- #rabbitmq_root_password_command(hosting, environment) ⇒ Object
- #random_free_port ⇒ Object
- #remote_port_forward(target, environment, port_forward, additional_arguments: []) ⇒ 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
- #target_name_to_k8s_deployment(target_name) ⇒ 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
#amazonmq_root_password_command(environment) ⇒ Object
672 673 674 |
# File 'lib/govuk_connect/cli.rb', line 672 def amazonmq_root_password_command(environment) "sops data/app-amazonmq/#{environment}/common.secret.tfvars" end |
#application_names_from_node_class_data(environment, hosting) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/govuk_connect/cli.rb', line 364 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
912 913 914 915 916 917 |
# File 'lib/govuk_connect/cli.rb', line 912 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
902 903 904 905 906 907 908 909 910 |
# File 'lib/govuk_connect/cli.rb', line 902 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/govuk_connect/cli.rb', line 151 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
228 229 230 231 232 233 234 |
# File 'lib/govuk_connect/cli.rb', line 228 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
272 273 274 275 276 277 278 279 |
# File 'lib/govuk_connect/cli.rb', line 272 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
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 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/govuk_connect/cli.rb', line 485 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
309 310 311 |
# File 'lib/govuk_connect/cli.rb', line 309 def govuk_directory File.join(ENV["HOME"], "govuk") end |
#govuk_node_list_classes(environment, hosting) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/govuk_connect/cli.rb', line 262 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
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 |
# File 'lib/govuk_connect/cli.rb', line 313 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
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
# File 'lib/govuk_connect/cli.rb', line 676 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
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/govuk_connect/cli.rb', line 460 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
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/govuk_connect/cli.rb', line 433 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
419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/govuk_connect/cli.rb', line 419 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
197 198 199 200 201 202 |
# File 'lib/govuk_connect/cli.rb', line 197 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
204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/govuk_connect/cli.rb', line 204 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/govuk_connect/cli.rb', line 126 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
340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/govuk_connect/cli.rb', line 340 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
1039 1040 1041 1042 1043 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 1115 |
# File 'lib/govuk_connect/cli.rb', line 1039 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
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/govuk_connect/cli.rb', line 377 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
355 356 357 358 359 360 361 362 |
# File 'lib/govuk_connect/cli.rb', line 355 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
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 |
# File 'lib/govuk_connect/cli.rb', line 753 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
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 |
# File 'lib/govuk_connect/cli.rb', line 791 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
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 744 745 746 747 748 749 750 751 |
# File 'lib/govuk_connect/cli.rb', line 695 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
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/govuk_connect/cli.rb', line 169 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_k8s_setup_info ⇒ Object
120 121 122 123 |
# File 'lib/govuk_connect/cli.rb', line 120 def print_k8s_setup_info info "If that doesn't work for you, follow the setup instructions at" info "https://govuk-k8s-user-docs.publishing.service.gov.uk/get-started/access-eks-cluster\n" 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 |
#print_vpn_help_info ⇒ Object
116 117 118 |
# File 'lib/govuk_connect/cli.rb', line 116 def print_vpn_help_info info "You may also wish to check that you are connected to the VPN, as attempting to SSH into an instance when NOT on the VPN can result in an operation timed out error" end |
#rabbitmq_root_password_command(hosting, environment) ⇒ Object
658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/govuk_connect/cli.rb', line 658 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
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/govuk_connect/cli.rb', line 183 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 |
#remote_port_forward(target, environment, port_forward, additional_arguments: []) ⇒ Object
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 612 613 614 615 616 617 618 |
# File 'lib/govuk_connect/cli.rb', line 586 def remote_port_forward( target, environment, port_forward, additional_arguments: [] ) log "debug: remote port-forward to #{target} in #{environment}" target, hosting = ssh_target(target, environment) localhost_port = random_free_port ssh_command = [ "ssh", *ssh_identity_arguments, "-N", "-L", "#{localhost_port}:#{target}:#{port_forward}", user_at_host( ssh_username, jumpbox_for_environment_and_hosting(environment, hosting), ), ] info "Port forwarding setup, access:\n\n https://127.0.0.1:#{localhost_port}/\n\n" info "(ignore any warnings about certificate mis-match - this is expected and OK)" ssh_command += additional_arguments info "\n#{bold('Running command:')} #{ssh_command.join(' ')}\n\n" exec(*ssh_command) end |
#scp(target, environment, files, push: false, additional_arguments: []) ⇒ Object
620 621 622 623 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 |
# File 'lib/govuk_connect/cli.rb', line 620 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
218 219 220 221 222 223 224 225 226 |
# File 'lib/govuk_connect/cli.rb', line 218 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
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 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/govuk_connect/cli.rb', line 537 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
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 |
# File 'lib/govuk_connect/cli.rb', line 281 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 print_empty_line print_vpn_help_info exit 1 end output.split("\n") end |
#ssh_identity_arguments ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/govuk_connect/cli.rb', line 250 def ssh_identity_arguments if ssh_identity_file ["-i", ssh_identity_file] else [] end end |
#ssh_identity_file ⇒ Object
246 247 248 |
# File 'lib/govuk_connect/cli.rb', line 246 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
836 837 838 839 840 841 842 843 844 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 895 896 897 898 899 900 |
# File 'lib/govuk_connect/cli.rb', line 836 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") || name.end_with?(".govuk-internal.digital") 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.empty? 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
236 237 238 239 240 241 242 243 244 |
# File 'lib/govuk_connect/cli.rb', line 236 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
145 146 147 148 149 |
# File 'lib/govuk_connect/cli.rb', line 145 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
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
# File 'lib/govuk_connect/cli.rb', line 818 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 |
#target_name_to_k8s_deployment(target_name) ⇒ Object
919 920 921 922 923 924 925 926 927 928 |
# File 'lib/govuk_connect/cli.rb', line 919 def target_name_to_k8s_deployment(target_name) { /draft_[a-z_]+\/([a-z_-]+)/ => "draft-\\1", /.*\// => "", # If not draft, drop the Puppet class prefix. "whitehall_backend/whitehall" => "whitehall-admin", "whitehall_frontend/whitehall" => "whitehall-frontend", }.reduce(target_name) do |result, (pattern, replacement)| result.sub(pattern, replacement) end end |
#types ⇒ 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 956 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 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 1035 1036 1037 |
# File 'lib/govuk_connect/cli.rb', line 930 def types @types ||= { "app-console" => proc do |target, _environment, _args, _extra_args, | target ||= "<app-name>" deployment = target_name_to_k8s_deployment(target) info "#{bold('The command for opening a Rails console has changed. Run this instead:')}\n\n" info " k exec -it deploy/#{deployment} -- rails c\n\n" print_k8s_setup_info end, "app-dbconsole" => proc do |target, _environment, _args, _extra_args, | target ||= "<app-name>" deployment = target_name_to_k8s_deployment(target) info "#{bold('The command for opening a Rails dbconsole has changed. Run this instead:')}\n\n" info " k exec -it deploy/#{deployment} -- rails db\n\n" print_k8s_setup_info 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, "amazonmq" => proc do |target, environment, args, extra_args, | check_for_additional_arguments("amazonmq", args) check_for_additional_arguments("amazonmq", extra_args) target ||= "publishingmq.#{environment}.govuk-internal.digital" info "You'll need to login as the RabbitMQ #{bold('root')} user." info "Get the password from govuk-aws-data, for example:\n\n" info " # from the root directory of govuk-aws-data repository\n\n" info " #{bold(amazonmq_root_password_command(environment))}" print_empty_line remote_port_forward( target, environment, AMAZONMQ_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
258 259 260 |
# File 'lib/govuk_connect/cli.rb', line 258 def user_at_host(user, host) "#{user}@#{host}" end |