seira 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5a2029452605046f3d8f1b42deffd44ae3b85be3
4
- data.tar.gz: 26e1918e974a42602e031364b024ada72e4de81a
2
+ SHA256:
3
+ metadata.gz: 48543d46e562c460bb07cd4e5264b381a9f8f290416fc9b49ed72f857c0082d9
4
+ data.tar.gz: 14866aed401685a325be5f8687ac799be8028a2607537ea7c0634331af7f9ad6
5
5
  SHA512:
6
- metadata.gz: ba5fc12eadc708b175e89d56fd835c8b4b13d0ac48415d38c5bedf524bf36ac34cc2114d6d637c1caa9a2819000cb498b742bcdb6680415ede52d7839ea67956
7
- data.tar.gz: ebf13e6d47383f17589af55a17ffb1e096352cfa232b0e9c2486887ca1a3b46750cb65b744ad3407f5db24b9e3d2e8eb1c022d3f334a922a1d11cbe4a7b65c07
6
+ metadata.gz: 867599d8ef0343d263432f3c0faac188864eb0df28bacbb381238669df373c9d9d5c5a9d2ec7dd02a633bbae40b7ba2559db8ee41a094f71c8ceb541d7a8b2d0
7
+ data.tar.gz: 2a8e70e9c427e1f718d85f538b1df7c0909e942e87cec9c575e9ee64a9560b2b7cd3a2bc2b801e5ab47d3a6d18f2245afdd6d0943ea9f2a4a3ac180853b043cf
data/.rubocop.yml CHANGED
@@ -2,7 +2,7 @@ AllCops:
2
2
  Exclude:
3
3
  - bin/**/*
4
4
 
5
- TargetRubyVersion: 2.4.1
5
+ TargetRubyVersion: 2.5.1
6
6
 
7
7
  Rails:
8
8
  Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.4.1
1
+ 2.5.1
data/.travis.yml CHANGED
@@ -3,5 +3,5 @@ language: ruby
3
3
  notifications:
4
4
  email: false
5
5
  rvm:
6
- - 2.4.1
6
+ - 2.5.1
7
7
  before_install: gem install bundler -v 1.14.6
data/lib/helpers.rb CHANGED
@@ -17,6 +17,11 @@ module Seira
17
17
  JSON.parse(output)['items']
18
18
  end
19
19
 
20
+ def fetch_pod(name, context:)
21
+ output = Seira::Commands.kubectl("get pod #{name} -o json", context: context, return_output: true)
22
+ JSON.parse(output) unless output.empty?
23
+ end
24
+
20
25
  def log_link(context:, query:)
21
26
  link = context[:settings].log_link_format
22
27
  return nil if link.nil?
data/lib/seira/pods.rb CHANGED
@@ -82,69 +82,88 @@ module Seira
82
82
  end
83
83
  end
84
84
 
85
- # If a pod name is specified, connect to that pod
86
- # If a tier is specified, connect to a random pod from that tier
87
- # Otherwise connect to a terminal pod
88
- target_pod = pod_name || Helpers.fetch_pods(context: context, filters: { tier: tier || 'terminal' }).sample
89
- if target_pod.nil?
90
- puts 'Could not find pod to connect to'
91
- exit(1)
92
- end
85
+ # If a pod name is specified, find the pod with that name
86
+ existing_pod = Helpers.fetch_pod(pod_name, context: context) unless pod_name.to_s.empty?
87
+
88
+ # Use existing pod if found and not --dedicated
89
+ # Otherwise, connect to a random pod from the specified tier or the 'terminal' tier if unspecified
90
+ target_pod = if existing_pod && dedicated
91
+ puts "Cannot create new dedicated pod with name: #{pod_name}"
92
+ puts "A pod with this name already exists"
93
+ exit(1)
94
+ elsif existing_pod
95
+ existing_pod
96
+ elsif dedicated || pod_name.to_s.empty?
97
+ Helpers.fetch_pods(context: context, filters: { tier: tier || 'terminal' }).sample
98
+ else
99
+ puts 'Could not find pod to connect to'
100
+ exit(1)
101
+ end
93
102
 
94
103
  if dedicated
95
- # Create a dedicated temp pod to run in
96
- # This is useful if you would like to have a persistent connection that doesn't get killed
97
- # when someone updates the terminal deployment, or if you want to avoid noisy neighbors
98
- # connected to the same pod.
99
- temp_name = "temp-#{Random.unique_name}"
100
-
101
- # Construct a spec for the temp pod
102
- spec = target_pod['spec']
103
- temp_pod = {
104
- apiVersion: target_pod['apiVersion'],
105
- kind: 'Pod',
106
- spec: spec,
107
- metadata: {
108
- name: temp_name,
109
- annotations: {
110
- owner: Helpers.shell_username
111
- }
104
+ new_pod = if pod_name.nil?
105
+ create_dedicated_pod(target_pod)
106
+ else
107
+ create_dedicated_pod(target_pod, pod_name)
108
+ end
109
+
110
+ connect_to_pod(new_pod, command)
111
+ clean_up_pod(new_pod)
112
+ else
113
+ # If we don't need a dedicated pod, it's way easier - just connect to the already running one
114
+ connect_to_pod(target_pod.dig('metadata', 'name'), command)
115
+ end
116
+ end
117
+
118
+ # Create a dedicated temp pod to run in
119
+ # This is useful if you would like to have a persistent connection that doesn't get killed
120
+ # when someone updates the terminal deployment, or if you want to avoid noisy neighbors
121
+ # connected to the same pod.
122
+ def create_dedicated_pod(target_pod, pod_name = "temp-#{Random.unique_name}")
123
+ spec = target_pod['spec']
124
+ temp_pod = {
125
+ apiVersion: target_pod['apiVersion'],
126
+ kind: 'Pod',
127
+ spec: spec,
128
+ metadata: {
129
+ name: pod_name,
130
+ annotations: {
131
+ owner: Helpers.shell_username
112
132
  }
113
133
  }
114
- # Don't restart the pod when it dies
115
- spec['restartPolicy'] = 'Never'
116
- # Overwrite container commands with something that times out, so if the client disconnects
117
- # there's a limited amount of time that the temp pod is still taking up resources
118
- # Note that this will break a pods which depends on containers running real commands, but
119
- # for a simple terminal pod it's fine
120
- spec['containers'].each do |container|
121
- container['command'] = ['sleep', '86400'] # 86400 seconds = 24 hours
122
- end
134
+ }
135
+ # Don't restart the pod when it dies
136
+ spec['restartPolicy'] = 'Never'
137
+ # Overwrite container commands with something that times out, so if the client disconnects
138
+ # there's a limited amount of time that the temp pod is still taking up resources
139
+ # Note that this will break a pods which depends on containers running real commands, but
140
+ # for a simple terminal pod it's fine
141
+ spec['containers'].each do |container|
142
+ container['command'] = %w[sleep 86400] # 86400 seconds = 24 hours
143
+ end
123
144
 
124
- puts 'Creating dedicated pod...'
125
- unless system("kubectl --namespace=#{app} create -f - <<JSON\n#{temp_pod.to_json}\nJSON")
126
- puts 'Failed to create dedicated pod'
127
- exit(1)
128
- end
145
+ puts 'Creating dedicated pod...'
146
+ unless system("kubectl --namespace=#{app} create -f - <<JSON\n#{temp_pod.to_json}\nJSON")
147
+ puts 'Failed to create dedicated pod'
148
+ exit(1)
149
+ end
129
150
 
130
- print 'Waiting for dedicated pod to start...'
131
- loop do
132
- pod = JSON.parse(kubectl("get pods/#{temp_name} -o json", context: context, return_output: true))
133
- break if pod['status']['phase'] == 'Running'
134
- print '.'
135
- sleep 1
136
- end
137
- print "\n"
151
+ print 'Waiting for dedicated pod to start...'
152
+ loop do
153
+ pod = JSON.parse(kubectl("get pods/#{pod_name} -o json", context: context, return_output: true))
154
+ break if pod['status']['phase'] == 'Running'
155
+ print '.'
156
+ sleep 1
157
+ end
158
+ print "\n"
138
159
 
139
- connect_to_pod(temp_name, command)
160
+ pod_name
161
+ end
140
162
 
141
- # Clean up on disconnect so temp pod isn't taking up resources
142
- unless kubectl("delete pods/#{temp_name}", context: context)
143
- puts 'Failed to delete temp pod'
144
- end
145
- else
146
- # If we don't need a dedicated pod, it's way easier - just connect to the already running one
147
- connect_to_pod(target_pod.dig('metadata', 'name'), command)
163
+ # Clean up pod so it isn't taking up resources. Usually only for dedicated pods
164
+ def clean_up_pod(pod_name)
165
+ unless kubectl("delete pods/#{pod_name}", context: context)
166
+ puts 'Failed to delete temp pod'
148
167
  end
149
168
  end
150
169
 
data/lib/seira/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Seira
2
- VERSION = "0.4.7".freeze
2
+ VERSION = "0.4.8".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seira
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Ringwelski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-23 00:00:00.000000000 Z
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
158
  version: '0'
159
159
  requirements: []
160
160
  rubyforge_project:
161
- rubygems_version: 2.6.13
161
+ rubygems_version: 2.7.6
162
162
  signing_key:
163
163
  specification_version: 4
164
164
  summary: An opinionated library for building applications on Kubernetes.