james_bond-mission_kubernetes 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fa107242fdcb793cb9ba668175949a5df7ebb5c3
4
+ data.tar.gz: d52bb1a1eb642cb23146a884bdab9918ea4f4a22
5
+ SHA512:
6
+ metadata.gz: 1099bba011809c798f515e0c6dec5c2c831f4b723553850bf3385a2885592fc3c065590fc7cf6ef4bdad09fe4dd20e4340a136966e4b74aa96c89bedea4258f0
7
+ data.tar.gz: 664addd41068a4808048fb2d3ec4f2c7acc3e50f30567573ffaadda587a6f5326d42b3fae731c36950fa00e589d18211053a8312b42b98c34f02b762d7318d57
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ ## Kubernetes Mission
2
+
3
+ ### Usage
4
+
5
+ To run the test suit in development mode:
6
+
7
+ ```sh
8
+ bond test
9
+ ```
10
+
11
+ To run a specfic test suit in dev mode:
12
+
13
+ ```sh
14
+ bond test -t /main/java/com/inlocomedia/sdk_config/dao/
15
+ ```
16
+
17
+
18
+ To run tests in production:
19
+
20
+ ```sh
21
+ bond test -t /main/java/com/inlocomedia/sdk_config/dao/ -e production
22
+ ```
data/lib/james_bond.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "james_bond/mission_kubernetes"
2
+
3
+ module JamesBond
4
+ end
@@ -0,0 +1,32 @@
1
+ require "james_bond/core/mission"
2
+ require "james_bond/mission_kubernetes/config"
3
+ require "james_bond/mission_kubernetes/build_handler"
4
+
5
+ module JamesBond
6
+ class KubernetesBuildMission
7
+ include JamesBond::Core::Mission
8
+
9
+ MISSION_NAME = "kubernetes"
10
+ MISSION_MAIN_COMMANDS = ["build"]
11
+ CONFIGURATION_FILE_PATH = File.join(Dir.pwd, "config", "james_bond", "kubernetes.yml")
12
+
13
+ def build(config, params)
14
+ config.name = MISSION_NAME
15
+ config.main_commands = MISSION_MAIN_COMMANDS
16
+ arguments_parser do |parser|
17
+ parser.banner = "bond #{MISSION_MAIN_COMMANDS[0]} [options]"
18
+ parser.string "-t", "--tag",
19
+ "Creates a docker image with the specified tag. If omitted a tag " + \
20
+ "like devel-212121 will be generated automaticaly."
21
+ end
22
+ end
23
+
24
+ def run_build_command(command:, mission_pool:)
25
+ self.class::BuildHandler.new(command, config_file).run
26
+ end
27
+
28
+ def config_file
29
+ @config_file ||= self.class::Config.new(yaml_path: CONFIGURATION_FILE_PATH)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ require "james_bond/core/mission"
2
+ require "james_bond/mission_kubernetes/config"
3
+ require "james_bond/mission_kubernetes/patch_deploy_handler"
4
+
5
+ module JamesBond
6
+ class KubernetesDeployMission
7
+ include JamesBond::Core::Mission
8
+
9
+ MISSION_NAME = "kubernetes_deploy"
10
+ MISSION_MAIN_COMMANDS = ["deploy"]
11
+ CONFIGURATION_FILE_PATH = File.join(
12
+ Dir.pwd, "config", "james_bond", "kubernetes.yml"
13
+ )
14
+
15
+ def build(config, params)
16
+ config.name = MISSION_NAME
17
+ config.main_commands = MISSION_MAIN_COMMANDS
18
+
19
+ arguments_parser do |parser|
20
+ parser.banner = "bond #{MISSION_MAIN_COMMANDS[0]} [options]"
21
+ parser.string "-t", "--tag", "The image tag to deploy."
22
+ parser.string "-e", "--environment",
23
+ "Environment to deploy to. Default: dev.",
24
+ default: "dev"
25
+ end
26
+ end
27
+
28
+ def run_deploy_command(command:, mission_pool:)
29
+ tag = command.options[:tag]
30
+ tag ||= invoke_build(mission_pool)[:tag]
31
+
32
+ command.options[:tag] = tag
33
+ MissionKubernetes::PatchDeployHandler.new(command, config_file).run
34
+ end
35
+
36
+ private
37
+
38
+ def invoke_build(mission_pool)
39
+ build = JamesBond::Core::Command.new( argv: ["build"], env: "dev")
40
+ build_mission = mission_pool.decide_mission(build)
41
+ build_mission.run_command(command: build, mission_pool: mission_pool)
42
+ end
43
+
44
+ def config_file
45
+ @config_file ||= JamesBond::MissionKubernetes::Config.new(yaml_path: CONFIGURATION_FILE_PATH)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,38 @@
1
+ require "james_bond/core/mission"
2
+ require "james_bond/mission_kubernetes/server_handler"
3
+ require "james_bond/mission_kubernetes/config"
4
+
5
+ module JamesBond
6
+ class KubernetesServerMission
7
+ include JamesBond::Core::Mission
8
+
9
+ MISSION_NAME = "kubernetes_server"
10
+ MISSION_MAIN_COMMANDS = ["server"]
11
+ CONFIGURATION_FILE_PATH = File.join(
12
+ Dir.pwd, "config", "james_bond", "kubernetes.yml"
13
+ )
14
+
15
+ def build(config, params)
16
+ config.name = MISSION_NAME
17
+ config.main_commands = MISSION_MAIN_COMMANDS
18
+
19
+ arguments_parser do |parser|
20
+ parser.banner = "bond #{MISSION_MAIN_COMMANDS[0]} [options]"
21
+ parser.string "-t", "--tag",
22
+ "Use this option to specify what image tag to use. " + \
23
+ "Remember the image should be accessible to the docker deamon used "+ \
24
+ "by Kubernetes. If you are using Minikube run " + \
25
+ "`eval $(minikube docker-env)` to share de deamon."
26
+ parser.separator " "
27
+ end
28
+ end
29
+
30
+ def run_server_command(command:)
31
+ MissionKubernetes::ServerHandler.new(command, config_file).run
32
+ end
33
+
34
+ def config_file
35
+ @config_file ||= JamesBond::MissionKubernetes::Config.new(yaml_path: CONFIGURATION_FILE_PATH)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require "james_bond/core/mission"
2
+ require "james_bond/mission_kubernetes/config"
3
+ require "james_bond/mission_kubernetes/test_handler"
4
+
5
+ module JamesBond
6
+ class KubernetesTestMission
7
+ include JamesBond::Core::Mission
8
+
9
+ MISSION_NAME = "kubernetes"
10
+ MISSION_MAIN_COMMANDS = ["test"]
11
+ CONFIGURATION_FILE_PATH = File.join(Dir.pwd, "config", "james_bond", "kubernetes.yml")
12
+
13
+ def build(config, params)
14
+ config.name = MISSION_NAME
15
+ config.main_commands = MISSION_MAIN_COMMANDS
16
+ end
17
+
18
+ def run_test_command(command:, mission_pool: )
19
+ build_output = invoke_build(mission_pool)
20
+
21
+ command.env = "test" #FIXME
22
+ command.options = (command.options || {}).merge("tag" => build_output[:tag])
23
+ JamesBond::MissionKubernetes::TestHandler.new(command, config_file).run
24
+ end
25
+
26
+ private
27
+
28
+ def invoke_build(mission_pool)
29
+ build = JamesBond::Core::Command.new( argv: ["build"], env: "dev")
30
+ build_mission = mission_pool.decide_mission(build)
31
+ build_mission.run_command(command: build, mission_pool: mission_pool)
32
+ end
33
+
34
+ def config_file
35
+ @config_file ||= JamesBond::MissionKubernetes::Config.new(yaml_path: CONFIGURATION_FILE_PATH)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ require "james_bond/core/mission"
2
+ require "james_bond/kubernetes_build_mission"
3
+ require "james_bond/kubernetes_test_mission"
4
+ require "james_bond/kubernetes_server_mission"
5
+ require "james_bond/kubernetes_deploy_mission"
6
+
7
+ module JamesBond
8
+ class MissionKubernetes
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ require "open3"
2
+ require "json"
3
+
4
+ module JamesBond
5
+ class MissionKubernetes
6
+ class BuildHandler
7
+ attr_accessor :config
8
+
9
+ def initialize(command, config)
10
+ @command = command
11
+ @config = config.for(env: command.env, command: command.main_command)
12
+ end
13
+
14
+ def run
15
+ target = "#{config["image_name"]}:#{tag}"
16
+ command = "docker build -t #{target} ."
17
+
18
+ Open3.popen3(command) do |stdin, stdout, stderr, thread|
19
+ Thread.new do
20
+ until (line = stdout.gets).nil? do
21
+ puts line
22
+ end
23
+ end
24
+
25
+ thread.join
26
+ end
27
+
28
+ { tag: tag }
29
+ end
30
+
31
+ def tag
32
+ options = @command.options
33
+ time_tag = "devel-#{Time.now.to_i}"
34
+ @tag ||= if options && options[:tag]
35
+ options[:tag]
36
+ else
37
+ time_tag
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ require "yaml"
2
+
3
+ module JamesBond
4
+ class MissionKubernetes
5
+ class Config
6
+ attr_accessor :yaml_path,
7
+ :raw_hash
8
+
9
+ def initialize(yaml_path:)
10
+ @yaml_path = yaml_path
11
+ extract_config_from_yaml(@yaml_path)
12
+ end
13
+
14
+
15
+ def for(env:,command:)
16
+ env_config = @raw_hash[env]
17
+ raise "No config for '#{env}' environment" if !env_config
18
+
19
+ commands = env_config.fetch("commands", proc { raise_error(command, env)} )
20
+ commands.fetch(command, proc { raise_error(command, env) })
21
+ end
22
+
23
+ private
24
+
25
+ def raise_error(command, env)
26
+ raise "No config for '#{command}' command in '#{env}' environment"
27
+ end
28
+
29
+ def extract_config_from_yaml(yaml_path)
30
+ @raw_hash = YAML.load(File.read(yaml_path)).freeze
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,84 @@
1
+ require "open3"
2
+ require "json"
3
+
4
+ module JamesBond
5
+ class MissionKubernetes
6
+ class PatchDeployHandler
7
+ attr_accessor :config
8
+
9
+ def initialize(command, config)
10
+ @command = command
11
+ @config = config.for(
12
+ env: command.options[:environment],
13
+ command: command.main_command
14
+ )
15
+ end
16
+
17
+ def run
18
+ command = [
19
+ "kubectl",
20
+ "patch",
21
+ "deployment #{app_name}",
22
+ "-p '#{JSON.dump(spec)}'",
23
+ ]
24
+
25
+ command = switch_environment(command).join(' ')
26
+ puts command
27
+
28
+ Open3.popen3(command) do |stdin, stdout, stderr, thread|
29
+ Thread.new do
30
+ until (line = stdout.gets).nil? do
31
+ puts line
32
+ end
33
+ end
34
+
35
+ Thread.new do
36
+ until (line = stderr.gets).nil? do
37
+ puts line
38
+ end
39
+ end
40
+
41
+ thread.join
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def switch_environment(patch_command)
48
+ if @command.options[:environment] == 'production'
49
+ patch_command << ["--kubeconfig=#{kubeconfig}"]
50
+ else
51
+ patch_command
52
+ end
53
+ end
54
+
55
+ def kubeconfig
56
+ File.join(Dir.pwd, @config["kubeconfig_path"])
57
+ end
58
+
59
+ def app_name
60
+ @config["app_name"]
61
+ end
62
+
63
+ def image_name
64
+ @config["image_name"]
65
+ end
66
+
67
+ def target
68
+ "#{image_name}:#{@command.options[:tag]}"
69
+ end
70
+
71
+ def spec
72
+ {
73
+ spec: {
74
+ template: {
75
+ spec: {
76
+ containers: [ { name: app_name, image: target } ]
77
+ }
78
+ }
79
+ }
80
+ }
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,39 @@
1
+ require "open3"
2
+ require "json"
3
+
4
+ module JamesBond
5
+ class MissionKubernetes
6
+ class ServerHandler
7
+ attr_accessor :config
8
+
9
+ def initialize(command, config)
10
+ @command = command
11
+ @config = config.for(env: command.env, command: command.main_command)
12
+ end
13
+
14
+ def run
15
+ tag = @command.options["tag"]
16
+ app_name = @config["app_name"]
17
+ image = @config["image_name"]
18
+ deploy = "#{app_name}-#{Time.now.to_i}"
19
+
20
+ command = [
21
+ "kubectl run #{deploy}",
22
+ "--image=#{image}:#{tag}",
23
+ "--restart=Never",
24
+ "--attach --rm"
25
+ ].join(" ")
26
+
27
+ Open3.popen3(command) do |stdin, stdout, stderr, thread|
28
+ Thread.new do
29
+ until (line = stdout.gets).nil? do
30
+ puts line
31
+ end
32
+ end
33
+
34
+ thread.join
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,47 @@
1
+ require "open3"
2
+ require "json"
3
+
4
+ module JamesBond
5
+ class MissionKubernetes
6
+ class TestHandler
7
+ attr_accessor :config
8
+
9
+ def initialize(command, config)
10
+ @command = command
11
+ @config = config.for(env: command.env, command: command.main_command)
12
+ end
13
+
14
+ def run
15
+ tag = @command.options["tag"]
16
+ app_name = @config["app_name"]
17
+ test_command = @config["command"]
18
+ image = @config["image_name"]
19
+ deploy = "#{app_name}-#{Time.now.to_i}"
20
+
21
+ command = [
22
+ "kubectl run #{deploy}",
23
+ "--image=#{image}:#{tag}",
24
+ "--restart=Never",
25
+ "--command --attach --rm",
26
+ "-- #{test_command}",
27
+ ].join(" ")
28
+
29
+ Open3.popen3(command) do |stdin, stdout, stderr, thread|
30
+ Thread.new do
31
+ until (line = stdout.gets).nil? do
32
+ puts line
33
+ end
34
+ end
35
+
36
+ Thread.new do
37
+ until (line = stderr.gets).nil? do
38
+ puts line
39
+ end
40
+ end
41
+
42
+ thread.join
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: james_bond-mission_kubernetes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Airton Sobral
8
+ - Guilherme Cavalcanti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-09-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: james_bond-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 0.2.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 0.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '='
33
+ - !ruby/object:Gem::Version
34
+ version: 3.4.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '='
40
+ - !ruby/object:Gem::Version
41
+ version: 3.4.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: byebug
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '='
47
+ - !ruby/object:Gem::Version
48
+ version: 9.0.5
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '='
54
+ - !ruby/object:Gem::Version
55
+ version: 9.0.5
56
+ description: Fully kubernetes automation
57
+ email:
58
+ - airtonsobral@gmail.com
59
+ - guiocavalcanti@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files:
63
+ - README.md
64
+ files:
65
+ - README.md
66
+ - lib/james_bond.rb
67
+ - lib/james_bond/kubernetes_build_mission.rb
68
+ - lib/james_bond/kubernetes_deploy_mission.rb
69
+ - lib/james_bond/kubernetes_server_mission.rb
70
+ - lib/james_bond/kubernetes_test_mission.rb
71
+ - lib/james_bond/mission_kubernetes.rb
72
+ - lib/james_bond/mission_kubernetes/build_handler.rb
73
+ - lib/james_bond/mission_kubernetes/config.rb
74
+ - lib/james_bond/mission_kubernetes/patch_deploy_handler.rb
75
+ - lib/james_bond/mission_kubernetes/server_handler.rb
76
+ - lib/james_bond/mission_kubernetes/test_handler.rb
77
+ homepage: http://rubygemgem.org/gems/james_bond-mission_kubernetes
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '2.1'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.8
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: One kube to rule them all!
101
+ test_files: []