Puppet Plan: cd4peadm::upload_license

Defined in:
plans/upload_license.pp

Summary

Upload the license if provided at `/files/license.lic`

Overview

This plan will install a user-provided license into the CD4PE application. If no license is provided, the app will still function, but some features will be locked.

This plan is called automatically by the ‘install_from_config` and `upgrade` plans.

Parameters:

  • config (Cd4peadm::Config) (defaults to: cd4peadm::config())


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'plans/upload_license.pp', line 7

plan cd4peadm::upload_license(
  Cd4peadm::Config $config = cd4peadm::config(),
) {
  run_plan('cd4peadm::check_bolt_version')

  if !file::exists(cd4peadm::license_path()) {
    out::message("No license file found at ${cd4peadm::license_path()}. Some features will be unavailable.")
    out::message("To add your license, place it at ${cd4peadm::license_path()} and run `bolt plan run cd4peadm::upload_license`.")
  } else {
    if(!get_target('localhost').vars['service_will_be_restarted_by_caller']) {
      $backend_hosts = $config['roles']['backend']['targets']
      $runtime = $config['runtime']
      $opts = { '_catch_errors' => true, '_run_as' => 'root' }
      $service_status_result = run_command("systemctl is-active ${runtime}-pipelinesinfra", $backend_hosts, $opts)
      # If the service is running anywhere, fail with a message that it needs to be stopped
      if(!$service_status_result.ok_set.empty) {
          $error = @("ERROR")

            Detected that CD4PE is currently running.
            Please stop CD4PE before uploading a new license.
            To do so, run:

              bolt plan run cd4peadm::ctl action=stop

            Then re-run this plan to upload the license. Once the license
            is uploaded, the server can be started again with:

              bolt plan run cd4peadm::ctl action=start
            | ERROR
          fail_plan($error)
      }
    }
    out::message("License file found at ${cd4peadm::license_path()}.")
    $file_contents = file::read(cd4peadm::license_path()).chomp
    $license_data = cd4peadm::parse_license($file_contents)
    out::message('Local license is valid.')

    $backend_host = $config['roles']['backend']['targets'][0]

    without_default_logging() || {
      out::message('Checking for installed license.')
      $remote_license_check = run_command(
        "cat ${file::join(cd4peadm::license_install_dir(), 'license.lic')}",
        $backend_host,
        { '_run_as' => 'root', '_catch_errors' => true }
      )

      if($remote_license_check.error_set.empty) {
        $remote_license_data = cd4peadm::parse_license($remote_license_check[0].value[stdout].chomp)
        $remote_created_date = $remote_license_data['data']['attributes']['created']
        $remote_ts = Timestamp($remote_created_date)
        $local_created_date = $license_data['data']['attributes']['created']
        $local_ts = Timestamp($local_created_date)
        if($remote_ts > $local_ts) {
          $newer_remote_error = @("ERROR")
            Detected an installed license that is newer than the local license. Aborting upload.

            If you really want to upload an older license, use `bolt plan run cd4peadm::delete_license`
            to remove the installed license first.
          | ERROR
          fail_plan($newer_remote_error)
        } else {
          out::message('Local license is newer than installed license. Uploading.')
        }
      } else {
        $error = $remote_license_check.error_set[0].value[stderr]
        if($error =~ /.*No such file or directory.*/) {
          out::message('No installed license found. Uploading license.')
        } else {
          fail_plan("Error reading installed license: ${error}")
        }
      }

      run_command("mkdir -p ${cd4peadm::license_install_dir()}", $backend_host, { '_run_as' => 'root' })

      upload_file(cd4peadm::license_path(), cd4peadm::license_install_dir(), $backend_host, '_run_as' => 'root')
    }
  }
}