k-tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0226a35539e64679a9aea7413be04b860ddba08fde82448467fb0bcb3222b97d
4
+ data.tar.gz: dc63a32c9eba1d110eca92bd1face84cee59243d049e4d17c7e3f9d7c0754c8b
5
+ SHA512:
6
+ metadata.gz: 994b074e26c3d39ee9134438dea60b6e6085b1567d07169d7e9874c87f964b6ec92f04c1140689eacecef25c8fc3810096d184aaf58e0eeb6b619494b229dbce
7
+ data.tar.gz: 5a9a1746418a318674f6ecb1980ab670144972e39459223a7724ff654f5461acc2fe1b8a541577ea99336efe55ccf1b2f03c6bc30ed19311bebd2609a54a63b9
data/bin/kt ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ktools'
4
+
5
+ ktools = KTools::Application.new(ARGV)
6
+ ktools.start
@@ -0,0 +1,34 @@
1
+ require 'colorize'
2
+ require 'pry'
3
+ require 'fileutils'
4
+ require 'oj'
5
+
6
+ require 'ktools/application'
7
+ require 'ktools/configuration'
8
+ require 'ktools/kdb'
9
+ require 'ktools/setup'
10
+ require 'ktools/sh'
11
+ require 'ktools/version'
12
+
13
+ require 'ktools/tools/deliver'
14
+ require 'ktools/tools/help'
15
+ require 'ktools/tools/spy'
16
+ require 'ktools/tools/swap'
17
+
18
+ module KTools
19
+ class << self
20
+ attr_writer :configuration
21
+ end
22
+
23
+ def self.configuration
24
+ @configuration ||= Configuration.new
25
+ end
26
+
27
+ def self.reset
28
+ @configuration = Configuration.new
29
+ end
30
+
31
+ def self.configure
32
+ yield configuration
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ module KTools
2
+ class Application
3
+ def initialize(args)
4
+ Setup.perform unless Setup.done?
5
+
6
+ @config = Setup.load
7
+ @command = args[0]
8
+ @tail = drop_first_arg(args)
9
+ end
10
+
11
+ def start
12
+ case @command
13
+ when 'spy'
14
+ Tools::Spy.start(@tail)
15
+ when 'deliver'
16
+ Tools::Deliver.start(@tail)
17
+ when 'swap'
18
+ Tools::Swap.start(@tail)
19
+ when 'setup'
20
+ Setup.perform
21
+ else
22
+ Tools::Help.display
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def drop_first_arg(args)
29
+ args.drop(1)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ module KTools
2
+ LIB_PATH = File.expand_path('../../', __FILE__)
3
+
4
+ class Configuration
5
+ attr_accessor :kpath, :config_file, :kube_path
6
+
7
+ def initialize
8
+ @kpath = "#{Dir.home}/.ktools"
9
+ @kube_path = "#{Dir.home}/.kube"
10
+ @config_file = "#{@kpath}/config.json"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ module KTools
2
+ class KDB
3
+ attr_accessor :config_file, :kpath
4
+
5
+ def self.read
6
+ config_file = self.new.config_file
7
+ return false unless File.exist?(config_file)
8
+
9
+ data = File.read(config_file)
10
+ Oj.load(data)
11
+ end
12
+
13
+ def self.write(data)
14
+ kdb = self.new
15
+ kdb.create_path
16
+
17
+ File.open(kdb.config_file, 'w') do |f|
18
+ f.write(Oj.dump(data))
19
+ end
20
+ end
21
+
22
+ def self.update(current, entry)
23
+ self.write(current.merge(entry))
24
+ end
25
+
26
+ def initialize
27
+ @config_file = KTools.configuration.config_file
28
+ @kpath = KTools.configuration.kpath
29
+ end
30
+
31
+ def create_path
32
+ return true if File.exist?(@config_file)
33
+ FileUtils.mkdir_p(@kpath)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,52 @@
1
+ module KTools
2
+ class Setup
3
+ def self.done?
4
+ KDB.read ? true : false
5
+ end
6
+
7
+ def self.load
8
+ KDB.read
9
+ end
10
+
11
+ def self.perform
12
+ secrets_path = self.new.get_secrets_path
13
+ KDB.write({"secrets" => secrets_path})
14
+
15
+ puts "Your 'secrets' path was saved as:"
16
+ puts secrets_path
17
+
18
+ puts ""
19
+ puts "For redefining it you can run:"
20
+ puts "$ kt setup"
21
+ puts ""
22
+
23
+ exit
24
+ end
25
+
26
+ def initialize; end
27
+
28
+ def get_secrets_path
29
+ path = String.new
30
+
31
+ loop do
32
+ puts "Insert the path for your 'secrets' repository:"
33
+ puts ""
34
+ print "#{Dir.home}/"
35
+
36
+ input = STDIN.gets.chomp.chomp('/')
37
+ path = "#{Dir.home}/#{input}"
38
+ ima_secret_path = "#{path}/.ima_secret"
39
+
40
+ puts ""
41
+
42
+ if File.file?(ima_secret_path)
43
+ break
44
+ else
45
+ puts "That's a invalid path, try again."
46
+ end
47
+ end
48
+
49
+ path
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,84 @@
1
+ module KTools
2
+ class Sh
3
+ # Secure mode, asks for confirmation.
4
+ # Shows command.
5
+ # Shows output.
6
+ def self.ell(cmd)
7
+ puts "Confirmation required."
8
+ puts ""
9
+ puts "Command:"
10
+ puts cmd
11
+ puts ""
12
+
13
+ print "Y/n: "
14
+ if "Y" == STDIN.gets.chomp
15
+ puts ""
16
+ puts "Running..."
17
+ puts `#{cmd}`
18
+ else
19
+ puts ""
20
+ puts "Aborted."
21
+ end
22
+ end
23
+
24
+ # Forced mode, won't ask confirmation.
25
+ # Shows command.
26
+ # Shows output.
27
+ def self.ell!(cmd)
28
+ puts cmd
29
+ puts `#{cmd}`
30
+ end
31
+
32
+ # Forced mode, won't ask confirmation.
33
+ # Won't show command.
34
+ # Won't show output.
35
+ # Returns output.
36
+ def self.elld!(cmd)
37
+ `#{cmd}`
38
+ end
39
+
40
+ # Forced mode, won't ask confirmation.
41
+ # Won't show command.
42
+ # Shows the output.
43
+ def self.ellb!(cmd)
44
+ puts `#{cmd}`
45
+ end
46
+
47
+ # Forced mode, won't ask confirmation.
48
+ # Shows command.
49
+ # Shows output.
50
+ # Loops into the output.
51
+ def self.ell_in!(cmd)
52
+ puts cmd
53
+ IO.popen(cmd) do |io|
54
+ while (line = io.gets) do
55
+ puts line
56
+ end
57
+ end
58
+ end
59
+
60
+ # Forced mode, won't ask confirmation.
61
+ # It's the meta-shell, or kt-shell mode.
62
+ # It emulates a shell over Ruby's $stdin/$stdout.
63
+ def self.ell_meta(cmd)
64
+ $stdout.print 'Press enter...'
65
+ started = false
66
+
67
+ $stdin.each_line do |line|
68
+ if started
69
+ pid = fork {
70
+ exec line
71
+ }
72
+ else
73
+ started = true
74
+ pid = fork {
75
+ exec cmd
76
+ }
77
+ end
78
+
79
+ Process.wait pid
80
+ $stdout.print 'kt:shell$ '
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,84 @@
1
+ module KTools
2
+ module Tools
3
+ class Deliver
4
+ def self.start(args)
5
+ self.new(args).start
6
+ end
7
+
8
+ def initialize(args)
9
+ @action = "#{args[0]} #{args[1]}"
10
+ @subject = args[2]
11
+ @argument = args[3]
12
+ end
13
+
14
+ def start
15
+ case @action
16
+ when 'force deploy'
17
+ dockerfile = "./Dockerfile"
18
+ do_fail("Dockerfile?") unless File.exist?(dockerfile)
19
+
20
+ registry = "registry.gitlab.com/"
21
+ origin = Sh.elld!("git config --get remote.origin.url")
22
+ do_fail("It is a Git repo?") unless origin
23
+
24
+ project = origin[/(?<=:)([^.]*)/]
25
+ do_fail("Can't find your project name.") if project.empty?
26
+
27
+ image_tag = "forcedAt#{Time.now.to_i}"
28
+ image = "#{registry}#{project}:#{image_tag}"
29
+
30
+ puts "Forcing deployment..."
31
+ puts "Project: #{project}"
32
+ puts "Deployment Name: #{@subject}"
33
+ puts "Docker Image: #{image}"
34
+
35
+ puts ""
36
+ puts "Ctrl-C to cancel in 5 seconds..."
37
+ sleep 5
38
+ puts "Starting..."
39
+
40
+ Sh.ell_in!("docker build -t #{image} .")
41
+ Sh.ell_in!("docker push #{image}")
42
+
43
+ Sh.ell_in!("./kdeliver force deploy #{@subject} #{image}")
44
+ when 'get bash'
45
+ puts "Opening live Bash..."
46
+ puts ""
47
+
48
+ pod = get_pod
49
+ container = "#{@subject}-container"
50
+
51
+ pod_cmd = "kubectl exec -ti -n default #{pod}"
52
+ bash_cmd = "-c #{container} /bin/bash"
53
+
54
+ Sh.ell_meta("#{pod_cmd} #{bash_cmd}")
55
+ when 'get logs'
56
+ puts "Opening live logs..."
57
+ puts ""
58
+
59
+ pod = get_pod
60
+
61
+ if @argument == "--tail"
62
+ Sh.ell_in!("kubectl logs -f #{pod}")
63
+ else
64
+ Sh.ell_in!("kubectl logs #{pod}")
65
+ end
66
+ else
67
+ Help.display
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def do_fail(cause)
74
+ puts cause
75
+ exit 1
76
+ end
77
+
78
+ def get_pod
79
+ pods = Sh.elld!("kubectl get pods | grep #{@subject}")
80
+ pods[/(^\S*)/]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,38 @@
1
+ module KTools
2
+ module Tools
3
+ class Help
4
+ def self.display
5
+ puts logo
6
+ end
7
+
8
+ def self.logo
9
+ ascii = <<~HEREDOC
10
+ .----.....``..----...:/``````
11
+ .----.....``..-----:/+++:....
12
+ -----.....``..::/++++++o+----
13
+ `````:+---...-:.`-/++++++++oo/`````
14
+ ````:++///-:++-./+++++++++oo:......
15
+ ::::+++++++++-```/o+++++oo+.```````
16
+ .../+/:/++++o+-.`:+++ooo+:-````````
17
+ ..:++/+//++oooo+++++++++++.........
18
+ `:++////++oooooooo+++++++o:........
19
+ ///:////++oooooooo++++++++o........
20
+ -:/-./++++++++oo++++++++++o/-------
21
+ :::..-+++++++++++++++++++++o-......
22
+ -:..../++++++++++++++++++++o+......
23
+ `......+++++++++++++++++++++o/:::::
24
+ .......:++++++++++++++++++++o/.....
25
+ ......../++++++++++++++++++++s:....
26
+ ........-++++++++++++//::++++-o:```
27
+ ........./++++//+/--.-----.:/+/+-..
28
+ `````````.:++o-.o---.-----.``-+/``-
29
+ ----------:/+/-:o:-------.````.:+/:
30
+ :::::::::::/o/-:/.``````.-------+//
31
+ :::::::::::+/..--........``````./::
32
+ ----------:+::::-........``````./::
33
+ ..........++:------------.`````./::
34
+ HEREDOC
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,109 @@
1
+ module KTools
2
+ module Tools
3
+ class Spy
4
+ def self.start(args)
5
+ self.new(args).start
6
+ end
7
+
8
+ def initialize(args)
9
+ @args = args
10
+ @action = "#{args[0]} #{args[1]}"
11
+ @subject = args[2]
12
+ @env = args[3]
13
+ @cfg = KTools::KDB.read
14
+ end
15
+
16
+ def start
17
+ case @action
18
+ when 'drop registry'
19
+ puts "Deleting Docker Registry for #{@subject}..."
20
+ puts ""
21
+
22
+ registry_name = "#{@subject}-registry"
23
+
24
+ Sh.ell("kubectl delete secret #{registry_name}")
25
+ when 'create registry'
26
+ puts "Creating Docker Registry for #{@subject}..."
27
+ puts ""
28
+
29
+ registry_name = "#{@subject}-registry"
30
+ env_path = "#{@cfg["secrets"]}/#{@env}/#{@subject}"
31
+ env_file = "#{env_path}/registry.json"
32
+ envd = Oj.load(File.read(env_file))
33
+
34
+ kube_cmd = <<~HEREDOC
35
+ kubectl create secret docker-registry #{registry_name} \
36
+ --docker-server=#{envd["KSPY_DOCKER_SERVER"]} \
37
+ --docker-username=#{envd["KSPY_DOCKER_USER"]} \
38
+ --docker-password=#{envd["KSPY_DOCKER_PASSWORD"]}
39
+ HEREDOC
40
+
41
+ puts Sh.ellb!(kube_cmd)
42
+ when 'drop config'
43
+ puts "Deleting configMap for #{@subject}..."
44
+ puts ""
45
+
46
+ config_name = "#{@subject}-config-map"
47
+
48
+ Sh.ell("kubectl delete configMap #{config_name}")
49
+ when 'apply config'
50
+ puts "Updating/Creating configMap for #{@subject}..."
51
+
52
+ unless @env
53
+ puts ""
54
+ puts "Please, express the environment."
55
+ puts "It can be 'production' or 'staging', like:"
56
+ puts "$ kt spy apply config appslug staging"
57
+ exit 1
58
+ end
59
+
60
+ env_path = "#{@cfg["secrets"]}/#{@env}/#{@subject}"
61
+ config_map_file = "#{env_path}/config_map.yml"
62
+
63
+ Sh.ell("kubectl apply -f #{config_map_file}")
64
+ when 'drop ingress'
65
+ puts "Deleting ingress for #{@subject}..."
66
+ puts ""
67
+
68
+ ingress_name = "#{@subject}-ingress"
69
+
70
+ Sh.ell("kubectl delete ingress #{ingress_name}")
71
+ when 'apply ingress'
72
+ puts "Updating/Creating ingress for #{@subject}..."
73
+
74
+ unless @env
75
+ puts ""
76
+ puts "Please, express the environment."
77
+ puts "It can be 'production' or 'staging', like:"
78
+ puts "$ kt spy apply ingress appslug staging"
79
+ exit 1
80
+ end
81
+
82
+ env_path = "#{@cfg["secrets"]}/#{@env}/#{@subject}"
83
+ ingress_file = "#{env_path}/ingress.yml"
84
+
85
+ Sh.ell("kubectl apply -f #{ingress_file}")
86
+ when 'drop all'
87
+ puts "Deleting ALL Kubernetes resources of #{@subject}..."
88
+
89
+ puts ""
90
+ puts "Ctrl-C to cancel in 5 seconds..."
91
+ sleep 5
92
+ puts "Starting..."
93
+
94
+ drop_cmd = <<~HEREDOC
95
+ kubectl delete deployment #{@subject}-deployment &&
96
+ kubectl delete configMap #{@subject}-config-map &&
97
+ kubectl delete ingress #{@subject}-ingress &&
98
+ kubectl delete service #{@subject}-service &&
99
+ kubectl delete secret #{@subject}-registry
100
+ HEREDOC
101
+
102
+ Sh.ellb!(drop_cmd)
103
+ else
104
+ Help.display
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,58 @@
1
+ module KTools
2
+ module Tools
3
+ class Swap
4
+ def self.start(args)
5
+ self.new(args).start
6
+ end
7
+
8
+ def initialize(args)
9
+ @subject = args[0]
10
+ @kube_path = KTools.configuration.kube_path
11
+ @cfg = KTools::KDB.read
12
+ end
13
+
14
+ def start
15
+ unless @subject
16
+ files = Dir["#{@kube_path}/*.yml", "#{@kube_path}/*.yaml"]
17
+
18
+ files.each do |file|
19
+ name = File.basename(file).chomp(".yml").chomp(".yaml")
20
+ if name == @cfg["cluster"]
21
+ puts "-> #{name}".colorize(:yellow)
22
+ else
23
+ puts "- #{name}"
24
+ end
25
+ end
26
+ else
27
+ c_file = "#{@kube_path}/config"
28
+ File.delete(c_file) if File.exist?(c_file)
29
+
30
+ subject_file = check_subject_file
31
+ do_fail unless subject_file
32
+
33
+ FileUtils.cp(subject_file, c_file)
34
+ KTools::KDB.update(@cfg, {"cluster" => @subject})
35
+
36
+ puts "Swapped to #{@subject}."
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def do_fail
43
+ puts "Can't find this config file."
44
+ exit 1
45
+ end
46
+
47
+ def check_subject_file
48
+ yml_file = "#{@kube_path}/#{@subject}.yml"
49
+ yaml_file = "#{@kube_path}/#{@subject}.yaml"
50
+
51
+ return yml_file if File.exist?(yml_file)
52
+ return yaml_file if File.exist?(yaml_file)
53
+
54
+ false
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module KTools
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: k-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Schuindt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.12.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.12.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: colorize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: oj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.7'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.7'
97
+ description: KTools is used to help manage, deploy and debug applications on Kubernetes
98
+ environments.
99
+ email:
100
+ - fernando@foxbox.co
101
+ executables:
102
+ - kt
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - bin/kt
107
+ - lib/ktools.rb
108
+ - lib/ktools/application.rb
109
+ - lib/ktools/configuration.rb
110
+ - lib/ktools/kdb.rb
111
+ - lib/ktools/setup.rb
112
+ - lib/ktools/sh.rb
113
+ - lib/ktools/tools/deliver.rb
114
+ - lib/ktools/tools/help.rb
115
+ - lib/ktools/tools/spy.rb
116
+ - lib/ktools/tools/swap.rb
117
+ - lib/ktools/version.rb
118
+ homepage: https://github.com/foxbox-studios/ktools
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.7.6
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: A set of Bash-like tools to manage DO K8s clusters.
142
+ test_files: []