Class: Installation::SshImporter

Inherits:
Object
  • Object
show all
Includes:
Singleton, Yast::Logger
Defined in:
src/lib/installation/ssh_importer.rb

Overview

Entry point for the SSH keys importing functionality.

This singleton class provides methods to hold a list of configurations found in the hard disk and to copy its files to the target system

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSshImporter

Returns a new instance of SshImporter.



42
43
44
45
# File 'src/lib/installation/ssh_importer.rb', line 42

def initialize
  @configurations = {}
  reset
end

Instance Attribute Details

#configurationsHash{String => SshConfig} (readonly)

name

Returns:

  • (Hash{String => SshConfig})

    found configurations, indexed by device



38
39
40
# File 'src/lib/installation/ssh_importer.rb', line 38

def configurations
  @configurations
end

#copy_configboolean Also known as: copy_config?

keys

Returns:

  • (boolean)

    whether to copy also the config files in addition to the



35
36
37
# File 'src/lib/installation/ssh_importer.rb', line 35

def copy_config
  @copy_config
end

#deviceString

SshConfig to copy the keys from)

Returns:

  • (String)

    device name of the source filesystem (i.e. the



32
33
34
# File 'src/lib/installation/ssh_importer.rb', line 32

def device
  @device
end

Instance Method Details

#add_config(root_dir, device) ⇒ Object

Reads ssh keys and config files from a given root directory, stores the information in #configurations and updates #device according to the default behavior.

Directories without keys in /etc/ssh are ignored.

Parameters:

  • root_dir (String)

    Path where the original "/" is mounted

  • device (String)

    Name of the mounted device



64
65
66
67
68
69
70
# File 'src/lib/installation/ssh_importer.rb', line 64

def add_config(root_dir, device)
  config = SshConfig.from_dir(root_dir)
  return if config.keys.empty?

  configurations[device] = config
  set_device
end

#resetObject

Set default settings (#device and #copy_config?)

To ensure backwards compatibility, the default behavior is to copy the SSH keys, but not other config files, from the most recently accessed config



51
52
53
54
# File 'src/lib/installation/ssh_importer.rb', line 51

def reset
  set_device
  @copy_config = false
end

#set_deviceObject (protected)

Sets #device according to the logic implemented in the old "copy_to_system" feature, to ensure backwards compatibility. That means selecting the device which contains the most recently accessed (atime) key file.

For some background, see fate#300421, fate#305019, fate#319624



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'src/lib/installation/ssh_importer.rb', line 94

def set_device
  if configurations.empty?
    @device = nil
  else
    with_atime = configurations.to_a.select { |_dev, config| config.keys_atime }
    if with_atime.empty?
      @device = configurations.keys.first
    else
      recent = with_atime.max_by { |_dev, config| config.keys_atime }
      @device = recent.first
    end
  end
end

#write(root_dir) ⇒ Object

Writes the SSH keys from the selected device (and also other configuration files if #copy_config? is true) in the target filesystem

Parameters:

  • root_dir (String)

    Path to use as "/" to locate the ssh directory



76
77
78
79
80
81
82
83
84
# File 'src/lib/installation/ssh_importer.rb', line 76

def write(root_dir)
  return unless device

  configurations[device].write_files(
    root_dir,
    write_keys:         true,
    write_config_files: copy_config
  )
end