Class: Hetzner::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hetzner/k3s/configuration.rb

Constant Summary collapse

','
/<([^>]+)>; rel="([^"]+)"/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:) ⇒ Configuration

Returns a new instance of Configuration.



10
11
12
13
14
15
# File 'lib/hetzner/k3s/configuration.rb', line 10

def initialize(options:)
  @options = options
  @errors = []

  validate_configuration_file
end

Instance Attribute Details

#hetzner_clientObject (readonly)

Returns the value of attribute hetzner_client.



8
9
10
# File 'lib/hetzner/k3s/configuration.rb', line 8

def hetzner_client
  @hetzner_client
end

Class Method Details

.assign_url_part(meta_part, url_part) ⇒ Object



113
114
115
116
117
118
# File 'lib/hetzner/k3s/configuration.rb', line 113

def self.assign_url_part(meta_part, url_part)
  case meta_part
  when 'next'
    url_part
  end
end

.available_releasesObject



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
# File 'lib/hetzner/k3s/configuration.rb', line 47

def self.available_releases
  @available_releases ||= begin
    releases = []

    response, page_releases = fetch_releases('https://api.github.com/repos/k3s-io/k3s/tags?per_page=100')
    releases = page_releases
    link_header = response.headers['link']

    until link_header.nil?
      next_page_url = extract_next_github_page_url(link_header)

      break if next_page_url.nil?

      response, page_releases = fetch_releases(next_page_url)

      releases += page_releases

      link_header = response.headers['link']
    end

    releases.sort
  end
rescue StandardError
  if defined? errors
    errors << 'Cannot fetch the releases with Github API, please try again later. This may be due to API rate limits.'
  else
    puts 'Cannot fetch the releases with Github API, please try again later. This may be due to API rate limits.'
  end
end

.extract_next_github_page_url(link_header) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hetzner/k3s/configuration.rb', line 100

def self.extract_next_github_page_url(link_header)
  link_header.split(GITHUB_DELIM_LINKS).each do |link|
    GITHUB_LINK_REGEX.match(link.strip) do |match|
      url_part = match[1]
      meta_part = match[2]
      next if !url_part || !meta_part
      return url_part if meta_part == 'next'
    end
  end

  nil
end

.fetch_releases(url) ⇒ Object



95
96
97
98
# File 'lib/hetzner/k3s/configuration.rb', line 95

def self.fetch_releases(url)
  response = HTTParty.get(url)
  [response, JSON.parse(response.body).map { |hash| hash['name'] }]
end

Instance Method Details

#[](key) ⇒ Object



83
84
85
# File 'lib/hetzner/k3s/configuration.rb', line 83

def [](key)
  configuration[key]
end

#fetch(key, default) ⇒ Object



87
88
89
# File 'lib/hetzner/k3s/configuration.rb', line 87

def fetch(key, default)
  configuration.fetch(key, default)
end

#hetzner_tokenObject



77
78
79
80
81
# File 'lib/hetzner/k3s/configuration.rb', line 77

def hetzner_token
  return @token unless @token.nil?

  @token = ENV.fetch('HCLOUD_TOKEN', configuration['hetzner_token'])
end

#rawObject



91
92
93
# File 'lib/hetzner/k3s/configuration.rb', line 91

def raw
  configuration
end

#validate(action:) ⇒ Object



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
# File 'lib/hetzner/k3s/configuration.rb', line 17

def validate(action:)
  validate_token

  if valid_token?
    validate_cluster_name
    validate_kubeconfig_path

    case action
    when :create
      validate_create
    when :delete
      validate_kubeconfig_path_must_exist
    when :upgrade
      validate_upgrade
    end
  end

  errors.flatten!

  return if errors.empty?

  puts 'Some information in the configuration file requires your attention:'

  errors.each do |error|
    puts " - #{error}"
  end

  exit 1
end