Class: Puppetserver::Ca::Action::Migrate

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/puppetserver/ca/action/migrate.rb

Constant Summary collapse

PUPPETSERVER_CA_DIR =
'/etc/puppetlabs/puppetserver/ca'
SUMMARY =
"Migrate the existing CA directory to /etc/puppetlabs/puppetserver/ca"
<<-BANNER
Usage:
  puppetserver ca migrate [--help]
  puppetserver ca migrate [--config PATH]

Description:
  Migrate an existing CA directory to /etc/puppetlabs/puppetserver/ca. This is for
  upgrading from Puppet Platform 6.x to Puppet 7. Use the currently configured
  puppet.conf file in your installation, or supply one using the `--config` flag.
Options:
BANNER

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Migrate

Returns a new instance of Migrate.



25
26
27
# File 'lib/puppetserver/ca/action/migrate.rb', line 25

def initialize(logger)
  @logger = logger
end

Class Method Details

.parser(parsed = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/puppetserver/ca/action/migrate.rb', line 80

def self.parser(parsed = {})
  OptionParser.new do |opts|
    opts.banner = BANNER
    opts.on('--help', 'Display this command-specific help output') do |help|
      parsed['help'] = true
    end
    opts.on('--config CONF', 'Path to puppet.conf') do |conf|
      parsed['config'] = conf
    end
  end
end

Instance Method Details

#migrate(old_cadir, new_cadir = PUPPETSERVER_CA_DIR) ⇒ Object



66
67
68
69
# File 'lib/puppetserver/ca/action/migrate.rb', line 66

def migrate(old_cadir, new_cadir=PUPPETSERVER_CA_DIR)
  FileUtils.mv(old_cadir, new_cadir)
  FileUtils.symlink(new_cadir, old_cadir)
end

#parse(args) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/puppetserver/ca/action/migrate.rb', line 71

def parse(args)
  results = {}
  parser = self.class.parser(results)
  errors = CliParsing.parse_with_errors(parser, args)
  errors_were_handled = Errors.handle_with_usage(@logger, errors, parser.help)
  exit_code = errors_were_handled ? 1 : nil
  return results, exit_code
end

#run(input) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppetserver/ca/action/migrate.rb', line 29

def run(input)
  config_path = input['config']
  puppet = Config::Puppet.new(config_path)
  puppet.load({}, @logger)
  return 1 if HttpClient.check_server_online(puppet.settings, @logger)

  errors = FileSystem.check_for_existing_files(PUPPETSERVER_CA_DIR)
  if !errors.empty?
    instructions = <<-ERR
Migration will not overwrite the directory at #{PUPPETSERVER_CA_DIR}. Have you already
run this migration tool? Is this a puppet 7 installation? It is likely that you have
already successfully run the migration or do not need to run it.
ERR
    errors << instructions
    Errors.handle_with_usage(@logger, errors)
    return 1
  end

  current_cadir = puppet.settings[:cadir]
  if FileSystem.check_for_existing_files(current_cadir).empty?
    error_message = <<-ERR
No CA dir found at #{current_cadir}. Please check the configured cadir setting in your
puppet.conf file and verify its contents.
ERR
    Errors.handle_with_usage(@logger, [error_message])
    return 1
  end

  migrate(current_cadir)

  @logger.inform <<-SUCCESS_MESSAGE
CA dir successfully migrated to #{PUPPETSERVER_CA_DIR}. Symlink placed at #{current_cadir}
for backwards compatibility. The puppetserver can be safely restarted now.
SUCCESS_MESSAGE
  return 0
end