seira 0.5.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00e2bc628518db4932766efa514535604cffe9983b4d5ebb949fa6c932388da9
4
- data.tar.gz: affbd9c3db26d811993872d7cfabf71606c3e5cf3f2562a1513c55eec15525c8
3
+ metadata.gz: f9cdf6d46992134d607c18e7e695426600599fd71d2ff37ab02b35c560463ff8
4
+ data.tar.gz: 53e6c5b9767ee60231bef3b035c9f4657dcdf642063b1089de62a75cb747d94a
5
5
  SHA512:
6
- metadata.gz: 59e85f167e6d0c2930d90d440d21a5afe23041b09228371c2f4db6726b86da91fe0695e149837439681dc5bcf3a5d9d15071d9d3fa7606955e882bc8e50b1d62
7
- data.tar.gz: df264bd54d20926b5fe00bc3d4059006e4f813ff1a3a318b567e45b413c7cf81688eac79e8f973d300892e55fb34f480b054872da4d1b3b1a54ed8c95ab55aaf
6
+ metadata.gz: 91e9b558f1d18a13803e6be20dfccefbdb1c38d4dd134f8c48e35a24fba274b1760e217ccf92e1de1fcfc5c5512cf377e8f84ed67299e994630ba0cfca1bcec2
7
+ data.tar.gz: 652ed52d764c7e901be54e74a590c459a9205d3385dc5785ec5ba65148ec76eefd6f82976831c648181ee9f765ba3e933b217959abbc0411b9fae826b7e6fe98
@@ -8,6 +8,7 @@ require 'seira/commands'
8
8
  require "seira/version"
9
9
  require 'helpers'
10
10
  require 'seira/app'
11
+ require 'seira/config'
11
12
  require 'seira/cluster'
12
13
  require 'seira/pods'
13
14
  require 'seira/jobs'
@@ -28,6 +29,7 @@ module Seira
28
29
 
29
30
  CATEGORIES = {
30
31
  'secrets' => Seira::Secrets,
32
+ 'config' => Seira::Config,
31
33
  'pods' => Seira::Pods,
32
34
  'jobs' => Seira::Jobs,
33
35
  'db' => Seira::Db,
@@ -0,0 +1,147 @@
1
+ require 'json'
2
+
3
+ # Example usages:
4
+ # seira staging specs config set RAILS_ENV=staging
5
+ # seira demo tracking config unset DISABLE_SOME_FEATURE
6
+ # seira staging importer config list
7
+ # TODO: Can we avoid writing to disk completely and instead pipe in raw json?
8
+ module Seira
9
+ class Config
10
+ include Seira::Commands
11
+
12
+ VALID_ACTIONS = %w[help get set unset list].freeze
13
+ SUMMARY = "Manage your application's environment variables configuration".freeze
14
+
15
+ attr_reader :app, :action, :args, :context
16
+
17
+ def initialize(app:, action:, args:, context:)
18
+ @app = app
19
+ @action = action
20
+ @args = args
21
+ @context = context
22
+ end
23
+
24
+ def run
25
+ case action
26
+ when 'help'
27
+ run_help
28
+ when 'get'
29
+ validate_single_key
30
+ run_get
31
+ when 'set'
32
+ validate_keys_and_values
33
+ run_set
34
+ when 'unset'
35
+ validate_single_key
36
+ run_unset
37
+ when 'list'
38
+ run_list
39
+ else
40
+ fail "Unknown command encountered"
41
+ end
42
+ end
43
+
44
+ def main_config_name
45
+ "#{app}-env-config"
46
+ end
47
+
48
+ def get(key)
49
+ config = fetch_current_config
50
+ value = config.dig('data', key)
51
+ value.nil? ? nil : value
52
+ end
53
+
54
+ private
55
+
56
+ def run_help
57
+ puts SUMMARY
58
+ puts "\n\n"
59
+ puts "Possible actions:\n\n"
60
+ puts "get: fetch the value of a config value: `config get DB_MAX_IDLE_CONNS`"
61
+ puts "set: set one or more config values: `config set DB_MAX_IDLE_CONNS=10 DB_MAX_CONN_LIFETIME=2m`"
62
+ puts " to specify a value with spaces: `config set FOO=\"Lorem ipsum\"`"
63
+ puts "unset: remove a config key: `config unset DB_MAX_IDLE_CONNS`"
64
+ puts "list: list all config keys and values"
65
+ end
66
+
67
+ def validate_single_key
68
+ if key.nil? || key.strip == ""
69
+ puts "Please specify a key in all caps and with underscores"
70
+ exit(1)
71
+ end
72
+ end
73
+
74
+ def validate_keys_and_values
75
+ if args.empty? || !args.all? { |arg| /^[^=]+=.+$/ =~ arg }
76
+ puts "Please list keys and values to set like KEY_ONE=value_one KEY_TWO=value_two"
77
+ exit(1)
78
+ end
79
+ end
80
+
81
+ def run_get
82
+ value = get(key)
83
+ if value.nil?
84
+ puts "Config '#{key}' not found"
85
+ else
86
+ puts "#{key.green}: #{value}"
87
+ end
88
+ end
89
+
90
+ def run_set
91
+ config = fetch_current_config
92
+ config['data'].merge!(key_value_map.transform_values { |value| value })
93
+ write_config(config: config)
94
+ end
95
+
96
+ def run_unset
97
+ config = fetch_current_config
98
+ config['data'].delete(key)
99
+ write_config(config: config)
100
+ end
101
+
102
+ def run_list
103
+ config = fetch_current_config
104
+ puts "Base64 encoded keys for #{app}:"
105
+ config['data'].each do |k, v|
106
+ puts "#{k.green}: #{v}"
107
+ end
108
+ end
109
+
110
+ # In the normal case the config we are updating is just main_config_name,
111
+ # but in special cases we may be doing an operation on a different config
112
+ def write_config(config:, config_name: main_config_name)
113
+ Dir.mktmpdir do |dir|
114
+ file_name = "#{dir}/temp-config-#{Seira::Cluster.current_cluster}-#{config_name}.json"
115
+ File.open(file_name, "w") do |f|
116
+ f.write(config.to_json)
117
+ end
118
+
119
+ # This only works if the config map already exists
120
+ if kubectl("replace -f #{file_name}", context: context)
121
+ puts "Successfully created/replaced #{config_name} config #{key} in cluster #{Seira::Cluster.current_cluster}"
122
+ else
123
+ puts "Failed to update configmap"
124
+ end
125
+ end
126
+ end
127
+
128
+ # Returns the configmap hashmap
129
+ def fetch_current_config
130
+ json_string = kubectl("get configmap #{main_config_name} -o json", context: context, return_output: true)
131
+ json = JSON.parse(json_string)
132
+ fail "Unexpected Kind" unless json['kind'] == 'ConfigMap'
133
+ json
134
+ end
135
+
136
+ def key
137
+ args[0]
138
+ end
139
+
140
+ def key_value_map
141
+ args.map do |arg|
142
+ equals_index = arg.index('=')
143
+ [arg[0..equals_index - 1], arg[equals_index + 1..-1]]
144
+ end.to_h
145
+ end
146
+ end
147
+ end
@@ -1,3 +1,3 @@
1
1
  module Seira
2
- VERSION = "0.5.9".freeze
2
+ VERSION = "0.6.0".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.5.9
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Ringwelski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2019-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -123,6 +123,7 @@ files:
123
123
  - lib/seira/commands.rb
124
124
  - lib/seira/commands/gcloud.rb
125
125
  - lib/seira/commands/kubectl.rb
126
+ - lib/seira/config.rb
126
127
  - lib/seira/db.rb
127
128
  - lib/seira/db/alter_proxyuser_roles.rb
128
129
  - lib/seira/db/create.rb