Method: Pfab::CLI#run

Defined in:
lib/pfab/cli.rb

#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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/pfab/cli.rb', line 11

def run
  program :name, "pfab"
  program :version, Pfab::Version::STRING
  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

  command :build do |c|
    c.syntax = "pfab build"
    c.summary = "build image"
    c.option "--force", "force build and push"
    c.option "--check", "just check if built"
    c.action do |_args, options|
      cmd_build(force: options.force, checkonly: options.check)
    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 :restart do |c|
    c.syntax = "pfab restart"
    c.summary = "rolling restart of a deployment"
    c.description = "rolling restart of a deployment"
    c.action do
      set_kube_context
      app_name = get_app_name

      puts_and_system "kubectl rollout restart deployment.apps/#{app_name}"
    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 -l #{selector}"
      end
    end
  end

  command :run_local do |c|
    c.syntax = "pfab run_local"
    c.summary = "run an app LOCALLY"
    c.description = "run the application command with all env vars set"
    c.example "run a command locally",
              "pfab run_local"
    c.option "-c", "--command COMMAND", "Run a command with the ENV vars of the selected app"
    c.action do |_args, options|
      $env = :development
      app_name = get_app_name(include_run_locals: true)
      puts "RUNNING THE FOLLOWING LOCALLY"

      env_vars = yy.env_vars(app_name).
        reject { |v| v.has_key? :valueFrom }

      env_var_string = env_vars.map { |item| "#{item[:name]}=\"#{item[:value]}\"" }.join(" ")
      options.default command: all_runnables[app_name][:command]

      puts_and_system "#{env_var_string} #{options.command}"
    end
  end
  alias_command :rl, :run_local

  command :clean do |c|
    c.syntax = "pfab clean"
    c.summary = "clean up pods"
    c.description = "clean up old pods"
    c.example "clean up",
              "pfab clean"
    c.action do |_args, options|
      set_kube_context
      puts "THIS APPLIES TO THE ENTIRE NAMESPACE"
      types = %w(Failed Pending Succeeded)
      types.each do |type|
        system("kubectl get pods --field-selector status.phase=#{type}")
        if agree("Delete those?")
          `kubectl delete pods --field-selector status.phase=#{type}`
          puts "Deleted"
        end
      end
    end
  end
  alias_command :rl, :run_local

  default_command :help

  run!
end