k8s-deploy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/LICENSE.md +21 -0
- data/README.md +122 -0
- data/k8s-deploy.gemspec +19 -0
- data/lib/k8s-deploy/tasks.rb +12 -0
- data/lib/k8s-deploy/tasks/check.rb +47 -0
- data/lib/k8s-deploy/tasks/configuration.rb +27 -0
- data/lib/k8s-deploy/tasks/console_formatters.rb +38 -0
- data/lib/k8s-deploy/tasks/deploy.rb +125 -0
- data/lib/k8s-deploy/tasks/k8s-deploy.rake +128 -0
- data/lib/k8s-deploy/tasks/status.rb +67 -0
- data/lib/k8s-deploy/version.rb +7 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5cc28d2d236622fd2d05a1c0a540cd11697925d9
|
4
|
+
data.tar.gz: fedeec7ff5b0d64dad150a03110346d277f480d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3b194e0303dd938998a15abb66280d863db5c1835b97c34eb1b58d10c46001b48133567d9f2bde7589909d50ab7e2fbf7cb128e950a65de1e3d7b9fa6618165
|
7
|
+
data.tar.gz: d0b361eb5e000e7f40ede347fd0ba9ab6afc2b504bf6de76be4322bcf653d2127f6992e052004b7bf22c2ed78d7d8296455864bdaf52d04673fcc10586edadc0
|
data/.gitignore
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
===============
|
3
|
+
|
4
|
+
Copyright © 2016 Oleksii Leonov.
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
7
|
+
this software and associated documentation files (the 'Software'), to deal in
|
8
|
+
the Software without restriction, including without limitation the rights to
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
10
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
11
|
+
subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
18
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
19
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
20
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
21
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
K8s deploy
|
2
|
+
=============
|
3
|
+
|
4
|
+
Introduction
|
5
|
+
------------
|
6
|
+
|
7
|
+
:bomb: **Important — this gem is not production ready.**
|
8
|
+
|
9
|
+
This gem provides bunch of useful **Rake** tasks to automate deploy to Kubernetes cluster in Google Container Engine.
|
10
|
+
|
11
|
+
Installation
|
12
|
+
------------
|
13
|
+
|
14
|
+
### Bundler
|
15
|
+
|
16
|
+
Add gem to your Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'k8s-deploy'
|
20
|
+
```
|
21
|
+
|
22
|
+
### Manual
|
23
|
+
|
24
|
+
Invoke the following command from your terminal:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
gem install k8s-deploy
|
28
|
+
```
|
29
|
+
|
30
|
+
Configuration
|
31
|
+
-------------
|
32
|
+
|
33
|
+
1. You need to put `k8s-deploy.yml` file into your project root.
|
34
|
+
2. Add `require 'k8s-deploy/tasks'` to your Rakefile.
|
35
|
+
3. Check available tasks with `rake -T` command.
|
36
|
+
|
37
|
+
##### Example of k8s-deploy.yml
|
38
|
+
|
39
|
+
```yml
|
40
|
+
production: # environment name
|
41
|
+
git_branch: master
|
42
|
+
dockerfile: ./Dockerfile
|
43
|
+
container_registry: gcr.io
|
44
|
+
gcloud_project_name: your-gcloud-project-name
|
45
|
+
kubernetes_context: your-cluster-context
|
46
|
+
kubernetes_deployment_name: your-deployment-name
|
47
|
+
kubernetes_template_name: your-template-name
|
48
|
+
kubernetes_docker_image_name: your-dcoker-image-name
|
49
|
+
```
|
50
|
+
|
51
|
+
You could add as many environments as you need (production, staging, test, etc.) to `k8s-deploy.yml`.
|
52
|
+
|
53
|
+
##### Example of Rakefile
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
require 'k8s-deploy/tasks'
|
57
|
+
```
|
58
|
+
|
59
|
+
##### Example of `rake -T` output
|
60
|
+
|
61
|
+
```bash
|
62
|
+
rake k8s:production:check # Check production ready for deploy
|
63
|
+
rake k8s:production:check:gcloud # Check production GCloud
|
64
|
+
rake k8s:production:check:git # Check production GIT
|
65
|
+
rake k8s:production:configuration # Show production configuration
|
66
|
+
rake k8s:production:deploy # Deploy to production
|
67
|
+
rake k8s:production:deploy:build # Build container for production
|
68
|
+
rake k8s:production:deploy:deployment_patch # Deployment patch for production
|
69
|
+
rake k8s:production:deploy:push # Push container for production
|
70
|
+
rake k8s:production:deploy:rollback # Rollback last deployment to production
|
71
|
+
rake k8s:production:deploy:scale[replicas_count] # Scale deployment production
|
72
|
+
rake k8s:production:status # Show production K8s status
|
73
|
+
rake k8s:production:status:deployment # Show production K8s Deployment status
|
74
|
+
rake k8s:production:status:docker_image # Show production K8s Deployment status
|
75
|
+
rake k8s:production:status:pods # Show production K8s Pods status
|
76
|
+
```
|
77
|
+
|
78
|
+
Tasks
|
79
|
+
-------------
|
80
|
+
|
81
|
+
All tasks have next structure:
|
82
|
+
|
83
|
+
```bash
|
84
|
+
rake k8s:<environment>:<main-task>:<sub-task>
|
85
|
+
```
|
86
|
+
|
87
|
+
### Main tasks
|
88
|
+
|
89
|
+
Show configuration for environment:
|
90
|
+
```bash
|
91
|
+
rake k8s:<environment>:configuration
|
92
|
+
```
|
93
|
+
|
94
|
+
Show status of running Deployment & Pods:
|
95
|
+
```bash
|
96
|
+
rake k8s:<environment>:status
|
97
|
+
```
|
98
|
+
|
99
|
+
Check GCloud/K8s configuration and GIT (branch, uncommitted changes, etc.):
|
100
|
+
```bash
|
101
|
+
rake k8s:<environment>:check
|
102
|
+
```
|
103
|
+
|
104
|
+
Build new Docker image, push to registry, patch Deployment:
|
105
|
+
```bash
|
106
|
+
rake k8s:<environment>:deploy
|
107
|
+
```
|
108
|
+
|
109
|
+
Rollback Deployment to previous state.
|
110
|
+
```bash
|
111
|
+
rake k8s:<environment>:rollback
|
112
|
+
```
|
113
|
+
|
114
|
+
Scale Deployment to desired number of replicas.
|
115
|
+
```bash
|
116
|
+
rake k8s:<environment>:scale[1]
|
117
|
+
```
|
118
|
+
|
119
|
+
License
|
120
|
+
-------
|
121
|
+
|
122
|
+
The project uses the MIT License. See LICENSE.md for details.
|
data/k8s-deploy.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path('../lib/k8s-deploy/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'k8s-deploy'
|
5
|
+
s.version = K8sDeploy::VERSION.dup
|
6
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.author = 'Oleksii Leonov'
|
9
|
+
s.email = 'mail@aleksejleonov.com'
|
10
|
+
s.homepage = 'https://github.com/cropio/k8s-deploy'
|
11
|
+
s.summary = 'automate deploy to Kubernetes.'
|
12
|
+
s.description = 'automate deploy to Kubernetes.'
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.required_ruby_version = '>= 1.9.1'
|
16
|
+
s.add_dependency 'rake', '>= 0.8.2'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
def print_git_check_branch(configuration)
|
2
|
+
conf_branch = configuration['git_branch']
|
3
|
+
local_branch = `git rev-parse --abbrev-ref HEAD`
|
4
|
+
check_result = local_branch == conf_branch
|
5
|
+
|
6
|
+
puts 'Your local branch must be ' \
|
7
|
+
"#{color_output(:green) { conf_branch }}:\t\t" \
|
8
|
+
"#{check_result_output(check_result)}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def print_git_check_uncommitted
|
12
|
+
check_result = `git status --porcelain`.empty?
|
13
|
+
|
14
|
+
puts "Your local branch must be clear:\t\t" \
|
15
|
+
"#{check_result_output(check_result)}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def print_git_check_remote_branch(configuration)
|
19
|
+
conf_branch = configuration['git_branch']
|
20
|
+
|
21
|
+
local_git_hash = `git rev-parse #{conf_branch}`
|
22
|
+
remote_git_hash = `git rev-parse origin/#{conf_branch}`
|
23
|
+
check_result = local_git_hash == remote_git_hash
|
24
|
+
|
25
|
+
puts "Your local version must be same as GitHub:\t" \
|
26
|
+
"#{check_result_output(check_result)}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def print_gcloud_check_project(configuration)
|
30
|
+
conf_gcloud_project = configuration['gcloud_project_name']
|
31
|
+
current_gcloud_project = `gcloud config get-value project`
|
32
|
+
check_result = current_gcloud_project.include?(conf_gcloud_project)
|
33
|
+
|
34
|
+
puts 'Your GCloud project should be ' \
|
35
|
+
"#{color_output(:green) { conf_gcloud_project }}:\t" \
|
36
|
+
"#{check_result_output(check_result)}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def print_gcloud_check_kubectl_context(configuration)
|
40
|
+
conf_k8s_context = configuration['kubernetes_context']
|
41
|
+
current_k8s_context = `kubectl config view -o jsonpath='{.current-context}'`
|
42
|
+
check_result = current_k8s_context == conf_k8s_context
|
43
|
+
|
44
|
+
puts 'Your K8s context should be ' \
|
45
|
+
"#{color_output(:green) { conf_k8s_context }}:\t" \
|
46
|
+
"#{check_result_output(check_result)}"
|
47
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
CONFIGURATION_OPTIONS = {
|
2
|
+
'git_branch' => 'GIT branch',
|
3
|
+
'dockerfile' => 'Path to Dockerfile',
|
4
|
+
'container_registry' => 'Docker container registry',
|
5
|
+
'gcloud_project_name' => 'GCloud project name',
|
6
|
+
'kubernetes_context' => 'K8s context',
|
7
|
+
'kubernetes_deployment_name' => 'K8s deployment name',
|
8
|
+
'kubernetes_template_name' => 'K8s deployment -> template name',
|
9
|
+
'kubernetes_docker_image_name' => 'K8s deployment -> template -> docker image'
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def validate_configuration(configuration)
|
13
|
+
missed_parameters = CONFIGURATION_OPTIONS.keys - configuration.keys
|
14
|
+
|
15
|
+
unless missed_parameters.empty?
|
16
|
+
params_string = missed_parameters.join(', ')
|
17
|
+
raise "Missed parameters in configuration: #{params_string}."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def print_configuration(configuration)
|
22
|
+
max_name_length = CONFIGURATION_OPTIONS.values.map(&:length).max
|
23
|
+
CONFIGURATION_OPTIONS.each do |key, name|
|
24
|
+
value = configuration.fetch(key, '—')
|
25
|
+
puts "#{name.rjust(max_name_length)}: #{color_output(:green) { value }}"
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
DELIMITER_WIDTH = 80
|
2
|
+
|
3
|
+
CONSOLE_COLORS = {
|
4
|
+
red: "\x1b[31;01m",
|
5
|
+
yellow: "\x1b[33;01m",
|
6
|
+
green: "\x1b[32;01m",
|
7
|
+
no_color: "\x1b[0m"
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
# colorizing output
|
11
|
+
def color_output(color)
|
12
|
+
raise 'Wrong color name' unless CONSOLE_COLORS.key?(color)
|
13
|
+
CONSOLE_COLORS[color] + yield.to_s + CONSOLE_COLORS[:no_color]
|
14
|
+
end
|
15
|
+
|
16
|
+
def command_output(text)
|
17
|
+
puts "#> #{text}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def block_output(header)
|
21
|
+
prepared_header = color_output(:yellow) { " #{header} " }
|
22
|
+
|
23
|
+
# adding 12 to fix size of non-visible color symbols
|
24
|
+
puts
|
25
|
+
puts prepared_header.center(DELIMITER_WIDTH + 12, '=')
|
26
|
+
|
27
|
+
puts
|
28
|
+
yield
|
29
|
+
puts
|
30
|
+
end
|
31
|
+
|
32
|
+
def check_result_output(result)
|
33
|
+
if result
|
34
|
+
color_output(:green) { 'Ok' }
|
35
|
+
else
|
36
|
+
color_output(:red) { 'Fail' }
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
def build_timestamp
|
2
|
+
CURRENT_TIME.strftime('%Y%m%d%H%M%S')
|
3
|
+
end
|
4
|
+
|
5
|
+
def build_tag_name(git_hash)
|
6
|
+
build_timestamp + '-' + git_hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_git_hash(configuration)
|
10
|
+
conf_branch = configuration['git_branch']
|
11
|
+
`git rev-parse #{conf_branch}`
|
12
|
+
end
|
13
|
+
|
14
|
+
def build_full_image_name(configuration)
|
15
|
+
registry = configuration['container_registry']
|
16
|
+
gcloud_project_name = configuration['gcloud_project_name']
|
17
|
+
docker_image_name = configuration['kubernetes_docker_image_name']
|
18
|
+
local_git_hash = get_git_hash(configuration)[0..6]
|
19
|
+
tag = build_tag_name(local_git_hash)
|
20
|
+
|
21
|
+
"#{registry}/#{gcloud_project_name}/#{docker_image_name}:#{tag}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def print_deploy_build(configuration)
|
25
|
+
conf_branch = configuration['git_branch']
|
26
|
+
new_image_name = build_full_image_name(configuration)
|
27
|
+
|
28
|
+
puts "New name:\t#{color_output(:green) { new_image_name }}"
|
29
|
+
puts
|
30
|
+
new_git_hash = get_git_hash(configuration)[0..6]
|
31
|
+
puts "New GIT hash:\t#{color_output(:green) { new_git_hash }}"
|
32
|
+
|
33
|
+
git_hash = `git rev-parse #{conf_branch}`
|
34
|
+
git_info = `git log -1 --format=full #{git_hash}`
|
35
|
+
|
36
|
+
puts
|
37
|
+
puts "\t\t--- GIT commit info ---"
|
38
|
+
git_info.each_line do |line|
|
39
|
+
puts "\t\t#{line}"
|
40
|
+
end
|
41
|
+
puts "\t\t-----------------------"
|
42
|
+
puts
|
43
|
+
puts "New Timestamp:\t#{color_output(:green) { build_timestamp }}"
|
44
|
+
puts
|
45
|
+
|
46
|
+
dockerfile_path = configuration['dockerfile']
|
47
|
+
command = "docker build -t #{new_image_name} -f #{dockerfile_path} ."
|
48
|
+
command_output(command)
|
49
|
+
puts
|
50
|
+
|
51
|
+
system command
|
52
|
+
end
|
53
|
+
|
54
|
+
def print_deploy_push(configuration)
|
55
|
+
new_image_name = build_full_image_name(configuration)
|
56
|
+
command = "gcloud docker push #{new_image_name}"
|
57
|
+
command_output(command)
|
58
|
+
puts
|
59
|
+
system command
|
60
|
+
puts
|
61
|
+
puts 'Sleeping for 5 seconds...'
|
62
|
+
sleep 5
|
63
|
+
end
|
64
|
+
|
65
|
+
def print_deploy_deployment_patch(configuration)
|
66
|
+
deployment_name = configuration['kubernetes_deployment_name']
|
67
|
+
template_name = configuration['kubernetes_template_name']
|
68
|
+
new_image_name = build_full_image_name(configuration)
|
69
|
+
|
70
|
+
json_template = {
|
71
|
+
spec: {
|
72
|
+
template: {
|
73
|
+
spec: {
|
74
|
+
containers: [{
|
75
|
+
name: template_name,
|
76
|
+
image: new_image_name
|
77
|
+
}]
|
78
|
+
}
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
template_string = JSON.dump(json_template)
|
84
|
+
|
85
|
+
command = "kubectl patch deployment #{deployment_name} -p " \
|
86
|
+
"'#{template_string}' --record"
|
87
|
+
|
88
|
+
command_output(command)
|
89
|
+
puts
|
90
|
+
|
91
|
+
print color_output(:yellow) { 'Confirm deploy (y/N): ' }
|
92
|
+
|
93
|
+
case STDIN.gets.strip
|
94
|
+
when 'Y', 'y', 'Yes', 'yes'
|
95
|
+
puts
|
96
|
+
puts color_output(:green) { 'Starting deploy...' }
|
97
|
+
system command
|
98
|
+
puts
|
99
|
+
puts 'Now you can check deploy status with ' \
|
100
|
+
"#{color_output(:yellow) { 'status' }} command."
|
101
|
+
else
|
102
|
+
puts
|
103
|
+
puts color_output(:red) { 'Deploy terminated.' }
|
104
|
+
exit 1
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def print_deploy_rollback(configuration)
|
109
|
+
deployment_name = configuration['kubernetes_deployment_name']
|
110
|
+
command = "kubectl rollout undo deployment #{deployment_name}"
|
111
|
+
|
112
|
+
command_output(command)
|
113
|
+
puts
|
114
|
+
system command
|
115
|
+
end
|
116
|
+
|
117
|
+
def print_deploy_scale(configuration, replicas_count)
|
118
|
+
deployment_name = configuration['kubernetes_deployment_name']
|
119
|
+
command = "kubectl scale deployment #{deployment_name} " \
|
120
|
+
"--replicas=#{replicas_count}"
|
121
|
+
|
122
|
+
command_output(command)
|
123
|
+
puts
|
124
|
+
system command
|
125
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
require_relative 'console_formatters.rb'
|
6
|
+
require_relative 'configuration.rb'
|
7
|
+
require_relative 'status.rb'
|
8
|
+
require_relative 'check.rb'
|
9
|
+
require_relative 'deploy.rb'
|
10
|
+
|
11
|
+
# Configuration file path
|
12
|
+
CONFIGURATION_FILE = 'k8s-deploy.yml'.freeze
|
13
|
+
|
14
|
+
# Get start time
|
15
|
+
CURRENT_TIME = Time.now.utc
|
16
|
+
|
17
|
+
# Read configuration file
|
18
|
+
begin
|
19
|
+
CONFIGURATION = YAML.load_file(CONFIGURATION_FILE)
|
20
|
+
rescue Errno::ENOENT
|
21
|
+
CONFIGURATION = [].freeze
|
22
|
+
end
|
23
|
+
|
24
|
+
namespace :k8s do
|
25
|
+
CONFIGURATION.each do |environment, configuration|
|
26
|
+
validate_configuration(configuration)
|
27
|
+
|
28
|
+
namespace environment do
|
29
|
+
desc "Show #{environment} configuration"
|
30
|
+
task :configuration do
|
31
|
+
block_output("Configuration for #{environment}") do
|
32
|
+
print_configuration(configuration)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Show #{environment} K8s status"
|
37
|
+
task status: ['status:deployment', 'status:pods', 'status:docker_image']
|
38
|
+
|
39
|
+
namespace :status do
|
40
|
+
desc "Show #{environment} K8s Pods status"
|
41
|
+
task :pods do
|
42
|
+
block_output("K8s Pods status for #{environment}") do
|
43
|
+
print_pods_status(configuration)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "Show #{environment} K8s Deployment status"
|
48
|
+
task :deployment do
|
49
|
+
block_output("K8s Deployment status for #{environment}") do
|
50
|
+
print_deployment_status(configuration)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
desc "Show #{environment} K8s Deployment status"
|
55
|
+
task :docker_image do
|
56
|
+
block_output("K8s Docker image status for #{environment}") do
|
57
|
+
print_docker_image_status(configuration)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
desc "Check #{environment} ready for deploy"
|
63
|
+
task check: ['check:git', 'check:gcloud']
|
64
|
+
|
65
|
+
namespace :check do
|
66
|
+
desc "Check #{environment} GIT"
|
67
|
+
task :git do
|
68
|
+
block_output("Check GIT for #{environment}") do
|
69
|
+
print_git_check_branch(configuration)
|
70
|
+
print_git_check_uncommitted
|
71
|
+
print_git_check_remote_branch(configuration)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
desc "Check #{environment} GCloud"
|
76
|
+
task :gcloud do
|
77
|
+
block_output("Check GCloud for #{environment}") do
|
78
|
+
print_gcloud_check_project(configuration)
|
79
|
+
print_gcloud_check_kubectl_context(configuration)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
desc "Deploy to #{environment}"
|
85
|
+
task deploy: [:configuration, :check, :status,
|
86
|
+
'deploy:build',
|
87
|
+
'deploy:push',
|
88
|
+
'deploy:deployment_patch']
|
89
|
+
|
90
|
+
namespace :deploy do
|
91
|
+
desc "Build container for #{environment}"
|
92
|
+
task :build do
|
93
|
+
block_output("Build container for #{environment}") do
|
94
|
+
print_deploy_build(configuration)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
desc "Push container for #{environment}"
|
99
|
+
task :push do
|
100
|
+
block_output("Push container for #{environment}") do
|
101
|
+
print_deploy_push(configuration)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
desc "Deployment patch for #{environment}"
|
106
|
+
task :deployment_patch do
|
107
|
+
block_output("Deployment patch for #{environment}") do
|
108
|
+
print_deploy_deployment_patch(configuration)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
desc "Rollback last deployment to #{environment}"
|
113
|
+
task :rollback do
|
114
|
+
block_output("Rollback for #{environment}") do
|
115
|
+
print_deploy_rollback(configuration)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
desc "Scale deployment #{environment}"
|
120
|
+
task :scale, [:replicas_count] do |_task, args|
|
121
|
+
block_output("Scale for #{environment}") do
|
122
|
+
print_deploy_scale(configuration, args[:replicas_count])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
def print_deployment_status(configuration)
|
2
|
+
deployment_name = configuration['kubernetes_deployment_name']
|
3
|
+
|
4
|
+
basic_info_command = "kubectl get deployment #{deployment_name} --show-labels"
|
5
|
+
command_output(basic_info_command)
|
6
|
+
puts
|
7
|
+
system basic_info_command
|
8
|
+
puts
|
9
|
+
|
10
|
+
describe_command = "kubectl describe deployment #{deployment_name}"
|
11
|
+
command_output(describe_command)
|
12
|
+
puts
|
13
|
+
system describe_command
|
14
|
+
end
|
15
|
+
|
16
|
+
def print_pods_status(configuration)
|
17
|
+
template_name = configuration['kubernetes_template_name']
|
18
|
+
|
19
|
+
command = "kubectl get pods --show-labels -l name=#{template_name}"
|
20
|
+
command_output(command)
|
21
|
+
puts
|
22
|
+
system command
|
23
|
+
end
|
24
|
+
|
25
|
+
def print_docker_image_status(configuration)
|
26
|
+
deployment_name = configuration['kubernetes_deployment_name']
|
27
|
+
|
28
|
+
command = "kubectl get deployment #{deployment_name} -o template " \
|
29
|
+
"'--template={{(index .spec.template.spec.containers 0).image}}'"
|
30
|
+
|
31
|
+
command_output(command)
|
32
|
+
puts
|
33
|
+
|
34
|
+
docker_image = `#{command}`
|
35
|
+
puts "Full name:\t#{color_output(:green) { docker_image }}"
|
36
|
+
|
37
|
+
time_hash = docker_image.split(':').last
|
38
|
+
git_hash = time_hash.split('-').last
|
39
|
+
timestamp = time_hash.split('-').first
|
40
|
+
|
41
|
+
puts
|
42
|
+
|
43
|
+
# check git_hash is valid
|
44
|
+
if git_hash && /^[a-f0-9]+$/.match(git_hash)
|
45
|
+
puts "GIT hash:\t#{color_output(:green) { git_hash }}"
|
46
|
+
git_info = `git log -1 --format=full #{git_hash}`
|
47
|
+
|
48
|
+
puts
|
49
|
+
puts "\t\t--- GIT commit info ---"
|
50
|
+
git_info.each_line do |line|
|
51
|
+
puts "\t\t#{line}"
|
52
|
+
end
|
53
|
+
puts "\t\t-----------------------"
|
54
|
+
else
|
55
|
+
puts color_output(:red) { "ERROR! Can't parse GIT hash from tag!" }
|
56
|
+
end
|
57
|
+
|
58
|
+
puts
|
59
|
+
begin
|
60
|
+
time_utc = Time.strptime(timestamp + ' UTC', '%Y%m%d%H%M%S %Z')
|
61
|
+
puts "Timestamp:\t#{color_output(:green) { timestamp }}"
|
62
|
+
puts "Time (UTC):\t#{time_utc}"
|
63
|
+
puts "Time (local):\t#{time_utc.getlocal}"
|
64
|
+
rescue ArgumentError
|
65
|
+
puts color_output(:red) { "ERROR! Can't parse timestamp from tag!" }
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: k8s-deploy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oleksii Leonov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.2
|
27
|
+
description: automate deploy to Kubernetes.
|
28
|
+
email: mail@aleksejleonov.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- LICENSE.md
|
35
|
+
- README.md
|
36
|
+
- k8s-deploy.gemspec
|
37
|
+
- lib/k8s-deploy/tasks.rb
|
38
|
+
- lib/k8s-deploy/tasks/check.rb
|
39
|
+
- lib/k8s-deploy/tasks/configuration.rb
|
40
|
+
- lib/k8s-deploy/tasks/console_formatters.rb
|
41
|
+
- lib/k8s-deploy/tasks/deploy.rb
|
42
|
+
- lib/k8s-deploy/tasks/k8s-deploy.rake
|
43
|
+
- lib/k8s-deploy/tasks/status.rb
|
44
|
+
- lib/k8s-deploy/version.rb
|
45
|
+
homepage: https://github.com/cropio/k8s-deploy
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.9.1
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.6.4
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: automate deploy to Kubernetes.
|
69
|
+
test_files: []
|