Class: Hippo::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wd, stage) ⇒ Hippo::CLI

Initialize a new CLI instance

Parameters:



17
18
19
20
# File 'lib/hippo/cli.rb', line 17

def initialize(wd, stage)
  @wd = wd
  @stage = stage
end

Instance Attribute Details

#stageObject (readonly)

Returns the value of attribute stage.



11
12
13
# File 'lib/hippo/cli.rb', line 11

def stage
  @stage
end

#wdObject (readonly)

Returns the value of attribute wd.



10
11
12
# File 'lib/hippo/cli.rb', line 10

def wd
  @wd
end

Class Method Details

.setup(_context) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/hippo/cli.rb', line 235

def setup(_context)
  wd = Hippo::WorkingDirectory.new

  stage = wd.stages[CURRENT_STAGE]
  if stage.nil?
    raise Error, "Invalid stage name `#{CURRENT_STAGE}`. Check this has been defined in in your stages directory with a matching name?"
  end

  new(wd, stage)
end

Instance Method Details

#apply_configvoid

This method returns an undefined value.

Apply all configuration and secrets



78
79
80
# File 'lib/hippo/cli.rb', line 78

def apply_config
  apply(stage.configs, 'configuration')
end

#apply_namespacevoid

This method returns an undefined value.

Apply the namespace configuration



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hippo/cli.rb', line 63

def apply_namespace
  od = Hippo::ObjectDefinition.new(
    {
      'kind' => 'Namespace',
      'apiVersion' => 'v1',
      'metadata' => { 'name' => stage.namespace, 'labels' => { 'name' => stage.namespace } }
    },
    stage
  )
  apply([od], 'namespace')
end

#apply_servicesvoid

This method returns an undefined value.

Apply all services, ingresses and policies



107
108
109
# File 'lib/hippo/cli.rb', line 107

def apply_services
  apply(stage.services, 'service')
end

#deployvoid

This method returns an undefined value.

Run a full deployment



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hippo/cli.rb', line 128

def deploy
  deployment_id = SecureRandom.hex(6)
  deployments = stage.deployments
  if deployments.empty?
    puts 'There are no deployment objects defined'
    return true
  end

  puts "Using deployment ID: #{deployment_id}"

  deployments.each do |deployment|
    deployment.insert_deployment_id!(deployment_id)
  end

  apply(deployments, 'deployment')
  puts 'Waiting for all deployments to roll out...'

  monitor = DeploymentMonitor.new(stage, deployment_id)
  monitor.on_success do |poll|
    if poll.replica_sets.size == 1
      puts "\e[32mDeployment rolled out successfully\e[0m"
    else
      puts "\e[32mAll #{poll.replica_sets.size} deployments all rolled out successfully\e[0m"
    end
  end

  monitor.on_wait do |poll|
    puts "Waiting for #{poll.pending.size} #{poll.pending.size == 1 ? 'deployment' : 'deployments'} (#{poll.pending_names.join(', ')})"
  end

  monitor.on_failure do |poll|
    puts "\e[31mLooks like things aren't going to plan with some deployments.\e[0m"
    puts 'You can review potential issues using the commads below:'

    poll.pending.each do |rs|
      puts
      name = rs.name.split('-').first
      puts "  hippo #{stage.name} kubectl -- describe deployment \e[35m#{name}\e[0m"
      puts "  hippo #{stage.name} kubectl -- logs deployment/\e[35m#{name}\e[0m --all-containers"
    end
    puts
  end

  monitor.wait
end

#install_all_packagesvoid

This method returns an undefined value.

Install all packages



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hippo/cli.rb', line 85

def install_all_packages
  if stage.packages.empty?
    puts 'There are no packages to install'
    return
  end

  stage.packages.values.each do |package|
    if package.installed?
      puts "#{package.name} is already installed. Upgrading..."
      package.upgrade
    else
      puts "Installing #{package.name} using Helm..."
      package.install
    end
  end

  puts "Finished with #{stage.packages.size} #{stage.packages.size == 1 ? 'package' : 'packages'}"
end

#manifestObject



22
23
24
# File 'lib/hippo/cli.rb', line 22

def manifest
  wd.manifest
end

#preflightvoid

This method returns an undefined value.

Run any checks that should be performed before running any optionation which might influence the environment



49
50
51
52
53
54
55
56
57
58
# File 'lib/hippo/cli.rb', line 49

def preflight
  if stage.context.nil?
    puts "\e[33mStage does not specify a context. The current context specified"
    puts "by the kubectl config will be used (#{Hippo.current_kubectl_context}).\e[0m"
    puts
    puts 'This is not recommended. Add a context name to your stage configuration.'
    puts
    exit 0 unless Util.confirm('Do you wish to continue?')
  end
end

#run_deploy_jobsvoid

This method returns an undefined value.

Run all deploy jobs



114
115
116
# File 'lib/hippo/cli.rb', line 114

def run_deploy_jobs
  run_jobs('deploy')
end

#run_install_jobsvoid

This method returns an undefined value.

Run all install jobs



121
122
123
# File 'lib/hippo/cli.rb', line 121

def run_install_jobs
  run_jobs('install')
end

#verify_image_existencevoid

This method returns an undefined value.

Verify image existence



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hippo/cli.rb', line 29

def verify_image_existence
  missing = 0
  stage.images.each do |_, image|
    if image.exists?
      puts "Image for #{image.name} exists at #{image.image_url}"
    else
      missing += 1
      puts "No #{image.name} image at #{image.image_url}"
    end
  end

  if missing > 0
    raise Error, "#{missing} #{missing == 1 ? 'image was' : 'images were'} not available. Cannot continue."
  end
end