docker-manager 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a2080a569140d0acf090a655ab112f313c0c13d2662f675d141d734a42f8ed5
4
+ data.tar.gz: 9e3d6c90286d9084e6674de403de8647f441224f2306fb57c2c2b6fd73ffd1bc
5
+ SHA512:
6
+ metadata.gz: 79ffc90c9ea9ee06aca3308bb471586e83046546c92252187a04fb5e97b4c98120003a8027e98e33bdb59a59c51340ed98863dd2e4a7a95a318f864e8e58eaf6
7
+ data.tar.gz: 9673d45e4f8915bd3f8943fc71e3daa3e8cf2776164851ab1127b39e7eabe5ee91f66e189ee285fc4416e1170b37dd1ae880d3981bd7221e210aa26f84df3d42
data/CHANGELOG.md ADDED
File without changes
data/README.md ADDED
File without changes
data/bin/dockermanager ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), *%w(.. lib))
4
+
5
+ require "dockermanager"
6
+
7
+ DockerManager::run
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/dockermanager/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'docker-manager'
7
+ s.version = DockerManager::VERSION
8
+ s.authors = ['Julien Biard']
9
+ s.description = ''
10
+ s.license = ''
11
+ s.email = 'julien.biard@gmail.com'
12
+ s.executables = ['dockermanager']
13
+ s.extra_rdoc_files = %w[CHANGELOG.md README.md]
14
+ s.files = `git ls-files -z`.split("\0")
15
+ s.test_files = `git ls-files -z spec/`.split("\0")
16
+ s.homepage = ''
17
+ s.summary = ''
18
+
19
+ s.add_dependency('sshkit', '1.16.1')
20
+
21
+ s.required_ruby_version = '>= 2.0.0'
22
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/dockermanager/version'
2
+ require File.dirname(__FILE__) + '/dockermanager/config'
3
+ require File.dirname(__FILE__) + '/dockermanager/commands/base'
4
+ require File.dirname(__FILE__) + '/dockermanager/commands/deploy'
5
+ require File.dirname(__FILE__) + '/dockermanager/commands/db_pull'
6
+
7
+ module DockerManager
8
+ extend self
9
+
10
+ def run
11
+ unless File.exist?("./dockermanager.yml")
12
+ puts "dockermanager.yml file not found"
13
+ exit 1
14
+ end
15
+ if ARGV.size != 2
16
+ puts "usage: dockermanager environment [deploy|db_pull]"
17
+ exit 2
18
+ end
19
+ klass = "Commands::#{ARGV[1].split('_').collect(&:capitalize).join}"
20
+ const_get(klass).new(env: ARGV[0], config_file: ARGV[2]).run
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ require 'sshkit'
2
+ require 'sshkit/dsl'
3
+ require 'yaml'
4
+
5
+ module DockerManager
6
+ module Commands
7
+ class Base
8
+ include SSHKit::DSL
9
+
10
+ def initialize(env:, config_file: )
11
+ self.config = Config.new(
12
+ env: env,
13
+ config_file: (config_file || "./dockermanager.yml")
14
+ )
15
+ init_sshkit
16
+ end
17
+
18
+ def run
19
+ raise "must be implemented"
20
+ end
21
+
22
+ private
23
+ attr_accessor :config
24
+
25
+ def init_sshkit
26
+ SSHKit.config.output_verbosity = Logger::DEBUG
27
+ SSHKit::Backend::Netssh.configure do |ssh|
28
+ ssh.ssh_options = {
29
+ user: config.env_docker_user,
30
+ auth_methods: %w[publickey]
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ module DockerManager
2
+ module Commands
3
+ class DbPull < Base
4
+ def run
5
+ remote_dump_file = "#{config.env_remote_directory}/dump.sql.gz"
6
+ local_dump_file = "#{config.project_root_path}/tmp/#{File.basename(remote_dump_file)}"
7
+ remote_postgres_db_container = config.env_remote_postgres_db_container
8
+ local_postgres_db_container = config.env_local_postgres_db_container
9
+ project_root_path = config.project_root_path
10
+ on config.env_host do
11
+ execute("docker exec -t #{remote_postgres_db_container} bash -c 'PGPASSWORD=$POSTGRES_PASSWORD pg_dump -h $POSTGRES_HOST -U $POSTGRES_USER $POSTGRES_DB --no-acl --no-owner' | gzip - -c --stdout > #{remote_dump_file}")
12
+ download!(remote_dump_file, local_dump_file)
13
+ end
14
+ run_locally do
15
+ execute(:rm, "#{project_root_path}/tmp/dump.sql")
16
+ execute("gunzip #{local_dump_file}")
17
+ # condition pg_stat_activity.datname = \'#{local_database}\' removed
18
+ execute("docker exec -i #{local_postgres_db_container} bash -c 'psql -U $POSTGRES_USER -d $POSTGRES_DB -c \"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid()\"'")
19
+ execute("docker exec -i #{local_postgres_db_container} bash -c 'dropdb -U $POSTGRES_USER $POSTGRES_DB'")
20
+ execute("docker exec -i #{local_postgres_db_container} bash -c 'createdb -U $POSTGRES_USER $POSTGRES_DB'")
21
+ execute("docker exec -i #{local_postgres_db_container} bash -c 'psql -U $POSTGRES_USER -d $POSTGRES_DB' < #{project_root_path}/tmp/dump.sql")
22
+ execute(:rm, "#{project_root_path}/tmp/dump.sql")
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ module DockerManager
2
+ module Commands
3
+ class Deploy < Base
4
+ def run
5
+ # to avoid scope issue
6
+ env = config.env
7
+ project_root_path = config.project_root_path
8
+ local_docker_path = config.local_docker_path
9
+ local_deploy_path = config.local_deploy_path
10
+ registry_login = config.registry_login
11
+ registry_password = config.registry_password
12
+ registry_server = config.registry_server
13
+ env_remote_directory = config.env_remote_directory
14
+ containers_to_restart = (config.containers_to_restart || []).join(' ')
15
+ run_locally do
16
+ # within doesn't work
17
+ execute("cd #{project_root_path} && ln -sf #{local_deploy_path}/#{env}/.env .env")
18
+ execute("cd #{project_root_path} && ln -sf #{local_docker_path}/docker-compose.server.yml docker-compose.server.yml")
19
+ execute("cd #{project_root_path} && docker-compose -f docker-compose.server.yml build")
20
+ execute("cd #{project_root_path} && docker login -u #{registry_login} -p #{registry_password} #{registry_server}")
21
+ execute("cd #{project_root_path} && docker-compose -f docker-compose.server.yml push")
22
+ end
23
+ on config.env_host do
24
+ execute(:mkdir, "-p", "#{env_remote_directory}/data")
25
+ upload!("#{local_deploy_path}/#{env}/.env", "#{env_remote_directory}/.env")
26
+ upload!("#{local_docker_path}/docker-compose.server.yml", "#{env_remote_directory}/docker-compose.yml")
27
+ local_ssl_path = "#{local_deploy_path}/#{env}/ssl"
28
+ if File.readable?(local_ssl_path)
29
+ execute(:rm, "-fr", "#{env_remote_directory}/ssl")
30
+ upload!("#{local_deploy_path}/#{env}/ssl", "#{env_remote_directory}/ssl", recursive: true)
31
+ else
32
+ local_renew_cert_script = "docker/deploy/#{env}/renew_cert.sh"
33
+ upload!(local_renew_cert_script, "#{env_remote_directory}/renew_cert.sh") if File.readable?(local_renew_cert_script)
34
+ end
35
+ execute("docker login -u #{registry_login} -p #{registry_password} #{registry_server}")
36
+ execute("cd #{env_remote_directory} && docker-compose pull -q")
37
+ execute("cd #{env_remote_directory} && docker-compose up -d #{containers_to_restart}")
38
+ execute("docker system prune -f")
39
+ end
40
+ ensure
41
+ run_locally do
42
+ execute("cd #{project_root_path} && ln -sf .env.development .env")
43
+ execute("cd #{project_root_path} && rm docker-compose.server.yml")
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,91 @@
1
+ module DockerManager
2
+ class Config
3
+ KEYS = {
4
+ env: {
5
+ required: %i[host docker_user remote_directory remote_postgres_db_container local_postgres_db_container]
6
+ },
7
+ registry: {
8
+ required: %i[login password],
9
+ optional: %i[server]
10
+ },
11
+ required: [],
12
+ optional: %i[containers_to_restart]
13
+ }
14
+
15
+ attr_reader :env, :config
16
+
17
+ def initialize(env:, config_file:)
18
+ self.env = env
19
+ self.config = YAML.load_file(config_file)
20
+ end
21
+
22
+ def project_root_path
23
+ return @project_root_path if defined?(@project_root_path)
24
+ @project_root_path = value(key: :project_root_path, required: true)
25
+ unless Dir.exist?(@project_root_path)
26
+ puts "invalid project_root_path : #{@project_root_path}"
27
+ exit 3
28
+ end
29
+ @project_root_path
30
+ end
31
+
32
+ def local_docker_path
33
+ return @local_docker_path if defined?(@local_docker_path)
34
+ @local_docker_path = "#{project_root_path}/docker"
35
+ unless Dir.exist?(@local_docker_path)
36
+ puts "invalid local docker path : #{@local_docker_path}"
37
+ exit 4
38
+ end
39
+ @local_docker_path
40
+ end
41
+
42
+ def local_deploy_path
43
+ return @local_deploy_path if defined?(@local_deploy_path)
44
+ @local_deploy_path = "#{local_docker_path}/deploy"
45
+ unless Dir.exist?(@local_deploy_path)
46
+ puts "invalid local deploy path : #{@local_deploy_path}"
47
+ exit 5
48
+ end
49
+ @local_deploy_path
50
+ end
51
+
52
+ KEYS.keys.each do |parent_key|
53
+ case parent_key
54
+ when :required, :optional
55
+ KEYS[parent_key].each do |key|
56
+ define_method key do
57
+ value(key: key, required: (parent_key == :required))
58
+ end
59
+ end
60
+ when :env, :registry
61
+ %i[required optional].each do |scope|
62
+ required = (scope == :required)
63
+ (KEYS[parent_key][scope] || []).each do |key|
64
+ define_method "#{parent_key}_#{key}" do
65
+ value(key: key, parent_key: parent_key, required: required)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ private
73
+ attr_writer :env, :config
74
+
75
+ def value(key:, parent_key: nil, required:)
76
+ value_ = if parent_key.nil?
77
+ config[key.to_s]
78
+ else
79
+ if parent_key == :env
80
+ config["environments"][env][key.to_s]
81
+ else
82
+ config[parent_key.to_s][key.to_s]
83
+ end
84
+ end
85
+ return value_ unless value_.nil?
86
+ return unless required
87
+ puts "#{[parent_key, key].compact.join('.')} is missing"
88
+ exit 6
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module DockerManager
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docker-manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Julien Biard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sshkit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.16.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.1
27
+ description: ''
28
+ email: julien.biard@gmail.com
29
+ executables:
30
+ - dockermanager
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - CHANGELOG.md
34
+ - README.md
35
+ files:
36
+ - CHANGELOG.md
37
+ - README.md
38
+ - bin/dockermanager
39
+ - dockermanager.gemspec
40
+ - lib/dockermanager.rb
41
+ - lib/dockermanager/commands/base.rb
42
+ - lib/dockermanager/commands/db_pull.rb
43
+ - lib/dockermanager/commands/deploy.rb
44
+ - lib/dockermanager/config.rb
45
+ - lib/dockermanager/version.rb
46
+ homepage: ''
47
+ licenses:
48
+ - ''
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.0.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.7.6
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: ''
70
+ test_files: []