Class: Pfab::CLI

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/pfab/cli.rb

Instance Method Summary collapse

Instance Method Details

#appsObject



240
241
242
# File 'lib/pfab/cli.rb', line 240

def apps
  @_apps ||= calculate_apps
end

#calculate_appsObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/pfab/cli.rb', line 244

def calculate_apps
  application = @application_yaml["name"]
  apps = {}
  @application_yaml["deployables"].each do |deployable, dep|
    deployable_type = dep["type"]
    app_name = [application, deployable_type, deployable].join("-")
    apps[app_name] = {
      application: application,
      deployable: deployable,
      deployable_type: deployable_type,
      command: dep["command"],
    }
  end
  apps
end

#cmd_applyObject



141
142
143
144
145
146
147
# File 'lib/pfab/cli.rb', line 141

def cmd_apply
  set_kube_context
  get_apps.each do |app_name|
    puts_and_system("kubectl apply -f .application-k8s-#{$env}-#{app_name}.yaml")
    puts_and_system("git tag release-#{$env}-#{app_name}-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")} HEAD")
  end
end

#cmd_build(force: false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pfab/cli.rb', line 149

def cmd_build(force: false)
  rev = get_current_sha
  say "This repo is at rev: #{rev}"
  uncommitted_changes = `git diff-index HEAD --`.empty?
  if uncommitted_changes
    say "FYI! There are uncommitted changes."
    continue = agree("Continue anyway?")
    if continue
      say "carrying on and pushing local code to #{rev}"
    else
      return false
    end
  end

  full_image_name = "#{container_repository}/#{image_name}:#{rev}"

  cmd = "docker images -q #{full_image_name}"
  say "Looking for images with #{cmd}"
  existing = `#{cmd}`

  if !existing.to_s.empty? && !force
    say "Found image #{full_image_name} already, skipping prebuild, build & push"
    return true
  end

  say "No image #{full_image_name} present, building"

  prebuild = @application_yaml["prebuild"] || ""
  if prebuild.empty?
    say "No prebuild task"
  else
    say "Prebuild, running system(#{prebuild})"
    result = system(prebuild)
    if result
      say 'Pfab prebuild success'
    else
      say "Pfab prebuild did not return success. Exiting"
      return false
    end
  end

  puts_and_system "docker build -t #{image_name} ."

  puts_and_system "docker tag #{image_name}:latest #{image_name}:#{rev}"
  puts_and_system "docker tag #{image_name}:#{rev} #{full_image_name}"

  puts_and_system "docker push #{container_repository}/#{image_name}:#{rev}"
  return true
end

#cmd_generate_yamlObject



199
200
201
202
203
204
205
206
207
208
# File 'lib/pfab/cli.rb', line 199

def cmd_generate_yaml
  wrote = Pfab::Yamls.generate_for(apps: @apps,
                                   application_yaml: @application_yaml,
                                   env: $env,
                                   sha: get_current_sha,
                                   image_name: image_name,
                                   config: config
  )
  puts "Generated #{wrote}"
end

#configObject



227
228
229
# File 'lib/pfab/cli.rb', line 227

def config
  @_config ||= YAML.load(File.read(File.join(Dir.home, ".pfab.yaml")))
end

#container_repositoryObject



223
224
225
# File 'lib/pfab/cli.rb', line 223

def container_repository
  @_container_repository ||= config["container.repository"]
end

#get_app_name(all: false) ⇒ Object



274
275
276
277
278
279
# File 'lib/pfab/cli.rb', line 274

def get_app_name(all: false)
  return $app_name unless $app_name.nil?
  apps = @apps.keys
  apps << "all" if all
  $app_name = choose("which app?", *apps)
end

#get_appsObject



281
282
283
284
# File 'lib/pfab/cli.rb', line 281

def get_apps
  name = get_app_name(all: true)
  (name == "all") ? @apps.keys : [name]
end

#get_current_shaObject



210
211
212
# File 'lib/pfab/cli.rb', line 210

def get_current_sha
  `git rev-parse --short --verify HEAD`.chomp
end

#get_first_pod(app) ⇒ Object



260
261
262
263
264
265
# File 'lib/pfab/cli.rb', line 260

def get_first_pod(app)
  if get_pods(app)["items"].empty?
    raise "There are no running pods for #{app}"
  end
  get_pods(app)["items"][0]["metadata"]["name"]
end

#get_pods(app) ⇒ Object



267
268
269
270
271
272
# File 'lib/pfab/cli.rb', line 267

def get_pods(app)
  get_pods_str = "kubectl get pods -o json -l deployed-name=#{app}"
  puts get_pods_str
  pods_str = `#{get_pods_str}`
  JSON.parse(pods_str)
end

#image_nameObject



219
220
221
# File 'lib/pfab/cli.rb', line 219

def image_name
  @application_yaml["name"]
end

#puts_and_system(cmd) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/pfab/cli.rb', line 231

def puts_and_system cmd
  puts cmd
  if $dryrun
    puts "dry run, didn't run that"
  else
    system cmd
  end
end

#runObject



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pfab/cli.rb', line 11

def run
  program :name, "pfab"
  program :version, "0.0.0"
  program :description, "k8s helper"

  if File.exist? "application.yaml"
    @application_yaml = YAML.load(File.read("application.yaml")).with_indifferent_access
  else
    raise "I need to be run in a directory with a application.yaml"
  end
  global_option("--verbose") { $verbose = true }
  $dryrun = false
  global_option("--dryrun") { $dryrun = true }
  $env = :staging
  global_option("-p") { $env = :production }
  global_option("-a", "--application_name APP_NAME", "run without prompting for app") do |app_name|
    $app_name = app_name
  end

  @apps = apps

  command :build do |c|
    c.syntax = "pfab build"
    c.summary = "build image"
    c.option "--force", "force build and push"
    c.action do |_args, options|
      cmd_build(force: options.force)
    end
  end

  command :generate_yaml do |c|
    c.syntax = "pfab generate_yaml"
    c.summary = "build k8s yaml"
    c.action do
      cmd_generate_yaml
    end
  end
  command :apply do |c|
    c.syntax = "pfab apply"
    c.summary = "kubectl apply"
    c.action do
      cmd_apply
    end
  end

  command :shipit do |c|
    c.syntax = "pfab shipit"
    c.summary = "build, generate, apply"
    c.action do
      app_name = get_app_name(all: true)
      puts "Shipping #{app_name}"
      success = cmd_build
      if success
        cmd_generate_yaml
        cmd_apply
      end
    end
  end

  command :logs do |c|
    c.syntax = "pfab logs"
    c.summary = "tail logs"
    c.description = "show me my logs`"
    c.example "tail logs of the first pod in staging",
              "pfab logs"
    c.example "tail logs of the first pod in production",
              "pfab -p logs"
    c.action do
      set_kube_context
      app_name = get_app_name

      first_pod = get_first_pod(app_name)

      puts_and_system("kubectl logs -f #{first_pod}")
    end
  end

  command :exec do |c|
    c.syntax = "pfab exec"
    c.summary = "kubectl exec into a  pod"
    c.description = "CLI to the Cloud"
    c.example "exec into the first pod in staging",
              "pfab exec"
    c.example "exec into the first pod in production",
              "ezp -p exec"
    c.action do
      set_kube_context
      app_name = get_app_name

      first_pod = get_first_pod app_name

      puts_and_system "kubectl exec -it #{first_pod} -- /bin/bash"
    end
  end

  command :status do |c|
    c.syntax = "pfab status"
    c.summary = "status of an app"
    c.description = "what's happening"
    c.example "what's happening in my app?",
              "pfab status"
    c.example "what's happening in production",
              "pfab status -p status"
    c.example "pick a single app",
              "pfab status --pick "
    c.example "verbose mode",
              "pfab status --verbose "
    c.example "watch deploy",
              "pfab status --watch "
    c.option "--watch", "Watch"
    c.action do |_args, options|
      set_kube_context

      selector = "application=#{@application_yaml['name']}"

      if options.watch
        puts_and_system "kubectl get pods -l #{selector} -w"
      elsif $verbose
        puts_and_system "kubectl describe pods -l #{selector}"
      else
        puts_and_system "kubectl get ingresses,jobs,services,cronjobs,deployments,pods --include-uninitialized=true -l #{selector}"
      end
    end
  end

  default_command :help

  run!
end

#set_kube_contextObject



214
215
216
217
# File 'lib/pfab/cli.rb', line 214

def set_kube_context
  str = "kubectl config use-context #{config["envs"][$env.to_s]["context"]}"
  puts_and_system str
end