Puppet Function: gitlab_ci_runner::unregister_from_file

Defined in:
lib/puppet/functions/gitlab_ci_runner/unregister_from_file.rb
Function type:
Ruby 4.x API

Overview

gitlab_ci_runner::unregister_from_file(String[1] $url, String[1] $runner_name, Optional[Optional[String[1]]] $proxy, Optional[Optional[String[1]]] $ca_file)Any

A function that unregisters a Gitlab runner from a Gitlab instance, if the local token is there. This is meant to be used in conjunction with the gitlab_ci_runner::register_to_file function.

Examples:

Using it as a Deferred function with a file resource

file { '/etc/gitlab-runner/auth-token-testrunner':
  file    => absent,
  content => Deferred('gitlab_ci_runner::unregister_from_file', ['http://gitlab.example.org'])
}

Parameters:

  • url (String[1])

    The url to your Gitlab instance. Please only provide the host part (e.g gitlab.com)

  • runner_name (String[1])

    The name of the runner. Use as identifier for the retrived auth token.

  • proxy (Optional[Optional[String[1]]])

    HTTP proxy to use when unregistering

  • ca_file (Optional[Optional[String[1]]])

    An absolute path to a trusted certificate authority file.

Returns:

  • (Any)


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
# File 'lib/puppet/functions/gitlab_ci_runner/unregister_from_file.rb', line 7

Puppet::Functions.create_function(:'gitlab_ci_runner::unregister_from_file') do
  # @param url The url to your Gitlab instance. Please only provide the host part (e.g https://gitlab.com)
  # @param runner_name The name of the runner. Use as identifier for the retrived auth token.
  # @param proxy HTTP proxy to use when unregistering
  # @param ca_file An absolute path to a trusted certificate authority file.
  # @example Using it as a Deferred function with a file resource
  #   file { '/etc/gitlab-runner/auth-token-testrunner':
  #     file    => absent,
  #     content => Deferred('gitlab_ci_runner::unregister_from_file', ['http://gitlab.example.org'])
  #   }
  #
  dispatch :unregister_from_file do
    # We use only core data types because others aren't synced to the agent.
    param 'String[1]', :url
    param 'String[1]', :runner_name
    optional_param 'Optional[String[1]]', :proxy
    optional_param 'Optional[String[1]]', :ca_file # This function will be deferred so can't use types from stdlib etc.
  end

  def unregister_from_file(url, runner_name, proxy = nil, ca_file = nil)
    filename = "/etc/gitlab-runner/auth-token-#{runner_name}"
    return "#{filename} file doesn't exist" unless File.exist?(filename)

    authtoken = File.read(filename).strip
    if Puppet.settings[:noop]
      message = "Not unregistering gitlab runner #{runner_name} when in noop mode"
      Puppet.debug message
      message
    else
      begin
        if !ca_file.nil? && !File.exist?(ca_file)
          Puppet.warning('Unable to unregister gitlab runner at this time as the specified `ca_file` does not exist. The runner config will be removed from this hosts config only; please remove from gitlab manually.')
          return 'Specified CA file doesn\'t exist, not attempting to create authtoken'
        end
        PuppetX::Gitlab::Runner.unregister(url, { 'token' => authtoken }, proxy, ca_file)
        message = "Successfully unregistered gitlab runner #{runner_name}"
        Puppet.debug message
        message
      rescue Net::HTTPError => e
        # Chances are the runner is already unregistered.  Best to just log a warning message but otherwise exit the function cleanly
        message = "Error whilst unregistering gitlab runner #{runner_name}: #{e.message}"
        Puppet.warning message
        message
      end
    end
  end
end