Class: KubeDeployTools::Deploy::Optparser::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_deploy_tools/deploy/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



24
25
26
27
28
29
# File 'lib/kube_deploy_tools/deploy/options.rb', line 24

def initialize
  self.project = File.basename(`git config remote.origin.url`.chomp, '.git')
  self.flavor = 'default'
  self.dry_run = 'client'
  self.glob_files = []
end

Instance Attribute Details

#artifactObject

Returns the value of attribute artifact.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def artifact
  @artifact
end

#build_numberObject

Returns the value of attribute build_number.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def build_number
  @build_number
end

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def context
  @context
end

#dry_runObject

Returns the value of attribute dry_run.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def dry_run
  @dry_run
end

#flavorObject

Returns the value of attribute flavor.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def flavor
  @flavor
end

#from_filesObject

Returns the value of attribute from_files.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def from_files
  @from_files
end

#glob_filesObject

Returns the value of attribute glob_files.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def glob_files
  @glob_files
end

#kubeconfigObject

Returns the value of attribute kubeconfig.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def kubeconfig
  @kubeconfig
end

#max_retriesObject

Returns the value of attribute max_retries.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def max_retries
  @max_retries
end

#pre_apply_hookObject

Returns the value of attribute pre_apply_hook.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def pre_apply_hook
  @pre_apply_hook
end

#projectObject

Returns the value of attribute project.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def project
  @project
end

#retry_delayObject

Returns the value of attribute retry_delay.



11
12
13
# File 'lib/kube_deploy_tools/deploy/options.rb', line 11

def retry_delay
  @retry_delay
end

Instance Method Details

#define_options(parser) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kube_deploy_tools/deploy/options.rb', line 31

def define_options(parser)
  parser.on('-fPATH', '--from-files FILEPATH', 'Filename, directory, or artifact URL that contains the Kubernetes manifests to apply') do |p|
    self.from_files = p
  end

  parser.on('--kubeconfig FILEPATH', 'Path to the kubconfig file to use for kubectl requests') do |p|
    self.kubeconfig = p
  end

  parser.on('--context CONTEXT', 'The kubeconfig context to use') do |p|
    self.context = p
  end

  parser.on('--project PROJECT', "The project to deploy. Default is '#{project}'.") do |p|
    self.project = p
  end

  parser.on('--flavor FLAVOR', "The flavor to deploy. Default is '#{flavor}'") do |p|
    self.flavor = p
  end

  parser.on('--artifact ARTIFACT', 'The artifact name to deploy') do |p|
    self.artifact = p
  end

  parser.on('--build BUILD', 'The Jenkins build number to deploy') do |p|
    self.build_number = p
  end

  # As of kubernetes 1.23 valid dry-run options are: none, client, server.
  # Legacy values map accordingly: true => client, false => none
  parser.on('--dry-run DRY_RUN', "Will only dry-run apply Kubernetes manifests without sending them to the apiserver. Default is dry-run mode: #{dry_run}. Must be '#{VALID_DRY_RUN_VALUES}'") do |p|
    legacy_mapping = { 'true' => 'client', 'false' => 'none' }

    if legacy_mapping.include?(p) then
      self.dry_run = legacy_mapping[p]
      Logger.warn("#{p} is no longer a supported dry-run value. Setting to value '#{self.dry_run}'.")
    elsif VALID_DRY_RUN_VALUES.include?(p)
      self.dry_run = p
    else
      raise ArgumentError, "#{p} is not a valid dry-run value. Expect one of '#{VALID_DRY_RUN_VALUES.join(', ')}'"
    end
  end

  parser.on('--include INCLUDE', "Include glob pattern. Example: --include=**/* will include every file. Default is ''.") do |p|
    self.glob_files.push(["include_files", p])
  end

  parser.on('--exclude EXCLUDE', "Exclude glob pattern. Example: --exclude=**/gazette/* will exclude every file in gazette folder. Default is ''.") do |p|
    self.glob_files.push(["exclude_files", p])
  end

  parser.on('--include-dir INCLUDE', "Recursively include all files in a directory and its subdirectories. Example: --include-dir=gazette/ (equivalent of --include=**/gazette/**/*)") do |p|
    self.glob_files.push(["include_dir", p])
  end

  parser.on('--exclude-dir EXCLUDE', "Recursively exclude all files in a directory and its subdirectories. Example: --exclude-dir=gazette/ (equivalent of --exclude=**/gazette/**/*)") do |p|
    self.glob_files.push(["exclude_dir", p])
  end

  parser.on("--pre-apply-hook CMD", "Shell command to run with the output directory before applying files") do |p|
    self.pre_apply_hook = p
  end

  parser.on('--retry NUM', 'Maximum number of times to retry') do |p|
    self.max_retries = p
  end

  parser.on('--retry-delay NUM', 'Delay in seconds between retries') do |p|
    self.retry_delay = p
  end

  parser.on('-')
end

#require_optionsObject

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
# File 'lib/kube_deploy_tools/deploy/options.rb', line 106

def require_options
  raise ArgumentError, 'Expect --context to be provided' if context.blank?

  files_mode = from_files.present? && (artifact.blank? && build_number.blank?)
  deploy_artifact_mode = from_files.blank? && (artifact.present? && flavor.present? && build_number.present?)

  if !files_mode && !deploy_artifact_mode
    raise ArgumentError, 'Expect either --from-files or all of [--artifact, --flavor, --build] to be provided'
  end
end