go-deploy 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2c5e7e9730a45edefb5dca3888e984b9c4c57520be8d488b412325540dd8fbbf
4
+ data.tar.gz: 431652eb8564fb76a5ae0adf1c6db5310853003b1180f2a86e7e33b263b5537e
5
+ SHA512:
6
+ metadata.gz: 8bbff13e4e0c60685b93fdffb2fd9a3d35dc82da49e7aa7e031461dc7a5499d07d23582dbe1d1ac3b70a79fb304128fd2fc672fc3da5747ccb41c413333def5a
7
+ data.tar.gz: cad8f6abc2e2f45103ada5a6465df95320e7c53b655fb1c9309d5b8c72bcb5c9b995242d2ca9003b895b8fc83790e1c428a540c4d3485b0bd67c62880d136a77
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'go_deploy'
3
+
4
+ GoDeploy::Deploy.new.run
@@ -0,0 +1 @@
1
+ require 'go_deploy/deploy'
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GoDeploy
4
+ # Console cli linux
5
+ class Console
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def log(command:, is_show_remote_name: true)
11
+ puts "\t#{command}".yellow
12
+ show_remote_name if is_show_remote_name
13
+ end
14
+
15
+ def exec(ssh:, command:)
16
+ log(command: command)
17
+ ssh.open_channel do |channel|
18
+ channel.exec(command) do |_ch, success|
19
+ show_remote_name(is_success: success)
20
+ end
21
+ end
22
+ end
23
+
24
+ def file_exists?(ssh:, path:)
25
+ results = []
26
+ ssh.exec!("if [ -e '#{path}' ]; then echo -n 'true'; fi") do |_ch, _stream, out|
27
+ results << (out == 'true')
28
+ end
29
+
30
+ results.all?
31
+ end
32
+
33
+ def sudo(ssh:, command:, password: '', is_show_color: true)
34
+ ssh.open_channel do |channel|
35
+ channel.request_pty do |_c, success|
36
+ raise 'Could not request pty' unless success
37
+
38
+ channel.exec(command)
39
+ channel.on_data do |_c, cmd|
40
+ log(command: cmd, is_show_remote_name: false) if is_show_color
41
+
42
+ channel.send_data "#{password}\n" if cmd[/\[sudo\]|Password/i]
43
+ end
44
+ end
45
+ end
46
+
47
+ ssh.loop
48
+ end
49
+
50
+ def show_remote_name(is_success: true)
51
+ if is_success
52
+ puts " ✔ #{@config['user']}@#{@config['host']}".green
53
+ else
54
+ puts " ✘ #{@config['user']}@#{@config['host']}".red
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,131 @@
1
+ require 'yaml'
2
+ require_relative 'string'
3
+ require_relative 'console'
4
+ require 'net/ssh'
5
+ require 'securerandom'
6
+ require 'net/scp'
7
+ # frozen_string_literal: true
8
+
9
+ module GoDeploy
10
+ # App go-deploy
11
+ class Deploy
12
+ TMP_DIR = '/tmp'.freeze
13
+ attr_accessor :config
14
+
15
+ def initialize
16
+ super
17
+ begin
18
+ file_name = ARGV[0] || 'production'
19
+ self.config = YAML.load_file(File.join(__dir__, "#{file_name}.yaml"))
20
+ @console = Console.new config
21
+ @service = config['service']
22
+ rescue StandardError => e
23
+ puts e.message.red
24
+ raise
25
+ end
26
+ end
27
+
28
+ def run
29
+ # Step 1
30
+ wrapper
31
+ # Step 2
32
+ git_check
33
+ # Step 3
34
+ git_clone
35
+ # Step 4
36
+ set_env
37
+ # Step 5
38
+ build_go
39
+ # Stop 6
40
+ stop_go_service_and_copy_files
41
+ # Stop 7
42
+ remove_files
43
+ # Stop 8
44
+ start_go_service
45
+ end
46
+
47
+ private
48
+
49
+ def ssh
50
+ return @ssh if defined? @ssh
51
+
52
+ @ssh = Net::SSH.start(config['host'], config['user'], ssh_options)
53
+ end
54
+
55
+ def ssh_options
56
+ {
57
+ keys: [config['passphrase']],
58
+ forward_agent: true,
59
+ paranoid: true
60
+ }
61
+ end
62
+
63
+ def wrapper
64
+ puts 'Step 1 git:wrapper'.green
65
+ @git_ssh_name = "git-ssh-#{SecureRandom.hex(10)}.sh"
66
+ @git_wrapper_path = "#{TMP_DIR}/#{@git_ssh_name}"
67
+
68
+ @console.exec(ssh: ssh, command: "mkdir -p #{File.dirname(@git_wrapper_path).shellescape}")
69
+ file = StringIO.new("#!/bin/sh -e\nexec /usr/bin/env ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no \"$@\"\n")
70
+ @console.log(command: "Uploading #{@git_wrapper_path}") if @ssh.scp.upload!(file, @git_wrapper_path)
71
+
72
+ @console.exec(ssh: ssh, command: "chmod 700 #{@git_wrapper_path.shellescape}")
73
+ end
74
+
75
+ def git_check
76
+ puts 'Step 2 git:check'.green
77
+ @console.exec(ssh: ssh, command: "git ls-remote #{@service['git_repo_url']} HEAD")
78
+ end
79
+
80
+ def git_clone
81
+ @deploy_to = @service['deploy_to']
82
+ puts 'Step 3 git:clone'.green
83
+ @console.exec(ssh: ssh, command: "mkdir -p #{@deploy_to}")
84
+ @console.exec(ssh: ssh, command: "git clone #{@service['git_repo_url']} #{@deploy_to}/repo")
85
+ # clean up files
86
+ @service['copy_files'].each do |file_name|
87
+ @console.exec(ssh: ssh, command: "rm -rf #{@deploy_to}/#{file_name}")
88
+ end
89
+ end
90
+
91
+ def set_env
92
+ project_env_file = "#{@deploy_to}/repo/.env"
93
+ @env_file = @service['env_file']
94
+ puts 'Step 4 set:env'.green
95
+ show("Uploading #{project_env_file}") if ssh.scp.upload!(@env_file, project_env_file)
96
+ end
97
+
98
+ def build_go
99
+ puts 'Step 5 build:go'.green
100
+ @console.exec(ssh: ssh, command: "cd #{@service['deploy_to']}/repo && go build -o #{@service['name']}")
101
+ end
102
+
103
+ def stop_go_service_and_copy_files
104
+ @service_name = @service['name']
105
+ puts "Step 6 systemctl:stop:#{@service_name}".green
106
+ @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && sudo systemctl stop #{@service_name}.service")
107
+ @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{@env_file} #{@deploy_to}")
108
+
109
+ @service['copy_files'].each do |file_name|
110
+ @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{file_name} #{@deploy_to}")
111
+ end
112
+
113
+ @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{@service_name} #{@deploy_to}")
114
+ end
115
+
116
+ def remove_files
117
+ puts 'Step 7 removed:files'.green
118
+ @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{@service_name} #{@deploy_to}")
119
+ @console.exec(ssh: ssh, command: "rm -rf #{@deploy_to}/repo")
120
+ @console.exec(ssh: ssh, command: "rm -rf #{@git_wrapper_path}")
121
+ @console.exec(ssh: ssh, command: 'rm -rf /tmp/git-ssh*')
122
+ end
123
+
124
+ def start_go_service
125
+ puts "Step 8 systemctl:start#{@service_name}".green
126
+ @console.exec(ssh: ssh, command: "sudo systemctl start #{@service_name}.service")
127
+ end
128
+ end
129
+ end
130
+
131
+ GoDeploy::Deploy.new.run
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # String to color
4
+ class String
5
+ def colorize(color_code)
6
+ "\e[#{color_code}m#{self}\e[0m"
7
+ end
8
+
9
+ def red
10
+ colorize(31)
11
+ end
12
+
13
+ def green
14
+ colorize(32)
15
+ end
16
+
17
+ def yellow
18
+ colorize(33)
19
+ end
20
+
21
+ def blue
22
+ colorize(34)
23
+ end
24
+
25
+ def pink
26
+ colorize(35)
27
+ end
28
+
29
+ def light_blue
30
+ colorize(36)
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: go-deploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Saharak Manoo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-scp
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-ssh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: securerandom
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
69
+ description: Ruby gem for easy to deploy golang to server(ssh)
70
+ email:
71
+ - saharakmanoo@gmail.com
72
+ executables:
73
+ - go-deploy
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - bin/go-deploy
78
+ - lib/go_deploy.rb
79
+ - lib/go_deploy/console.rb
80
+ - lib/go_deploy/deploy.rb
81
+ - lib/go_deploy/string.rb
82
+ homepage: http://rubygems.org/gems/go-deploy
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '2.0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.2.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Ruby gem for easy to deploy golang to server(ssh)
105
+ test_files: []