Handle deployment for different sources

This commit is contained in:
Glenn Y. Rolland 2020-03-25 16:09:20 +01:00
parent 481404b0b9
commit 2be893eb1d
7 changed files with 133 additions and 45 deletions

View file

@ -1,15 +1,9 @@
require "yaml"
require "./config/config"
require "./config/local"
require "./config/remote"
require "./config/deployment"
class Config
YAML.mapping(
locals: Array(LocalConfig),
remotes: Array(RemoteConfig),
deployments: Array(DeploymentConfig)
)
end

14
src/config/config.cr Normal file
View file

@ -0,0 +1,14 @@
require "yaml"
require "./local"
require "./remote"
require "./deployment"
class Config
YAML.mapping(
version: String,
locals: Array(LocalConfig),
remotes: Array(RemoteConfig),
deployments: Array(DeploymentConfig)
)
end

View file

@ -1,10 +1,37 @@
require "yaml"
class DeploymentConfig
class DokkuMariadbConfig
YAML.mapping(
name: String,
options: YAML::Any | Nil
)
end
class DokkuAppConfig
YAML.mapping(
name: String,
options: YAML::Any | Nil
)
end
class DeploymentMariadbConfig
YAML.mapping(
local: String,
remote: String,
type: String,
dokku_mariadb: DokkuMariadbConfig,
)
end
class DeploymentAppConfig
YAML.mapping(
local: String,
remote: String,
dokku_app: DokkuAppConfig,
)
end
alias DeploymentConfig =
DeploymentMariadbConfig |
DeploymentAppConfig

View file

@ -10,12 +10,23 @@ enum LocalType
end
end
class LocalConfig
class LocalFileConfig
YAML.mapping(
name: String,
type: LocalType, # enum ?
docker_image: String | Nil,
path: String | Nil
path: String
)
end
class LocalDockerConfig
YAML.mapping(
name: String,
type: LocalType, # enum ?
docker_image: String
)
end
alias LocalConfig =
LocalFileConfig |
LocalDockerConfig

View file

@ -1,16 +1,14 @@
require "colorize"
class DockerImageToDokkuApp
def self.handler
"docker_image_to_dokku_app"
end
def initialize(@local : LocalConfig, @remote : RemoteConfig, @deployment : DeploymentConfig)
class DeploymentApp
def initialize(@local : LocalDockerConfig, @remote : RemoteConfig, @deployment : DeploymentAppConfig)
end
def run
image_meta = image_tag(@local.docker_image, config["app"])
dokku_app = @deployment.as(DeploymentAppConfig).dokku_app
app = dokku_app.name
image_meta = image_tag(@local.docker_image, app)
image_push(@remote.host, image_meta["tag_name_version"])
image_deploy(@remote.host, image_meta["app"], image_meta["version"])
end

View file

@ -1,13 +1,49 @@
class MysqlDumpToDokkuMariadb
def self.handler
"mysql_dump_to_dokku_mariadb"
end
def initialize(@local : LocalConfig, @remote : RemoteConfig, @deployment : DeploymentConfig)
class DeploymentMariadb
def initialize(@local : LocalFileConfig, @remote : RemoteConfig, @deployment : DeploymentMariadbConfig)
end
def run
dokku_mariadb = @deployment.dokku_mariadb.as(DokkuMariadbConfig)
local_path = @local.path
# puts @local.inspect
file_push(@remote.host, local_path, dokku_mariadb.name)
end
private def file_push(host, local_path, dokku_mariadb_name)
# cat database.sql \
# | ssh SERVER 'dokku mariadb:import DATABASE'
pipe1_reader, pipe1_writer = IO.pipe(true)
proc2_out = IO::Memory.new
puts "Pushing data...".colorize(:yellow)
p2 = Process.new "ssh", [host, "dokku mariadb:import #{dokku_mariadb_name}"],
input: pipe1_reader, output: proc2_out, error: STDERR
p1 = Process.new "cat", [local_path.to_s],
output: pipe1_writer,
error: STDERR
status = p1.wait
pipe1_writer.close
if status.success?
puts "-----> Database file successfully sent"
else
STDERR.puts "Error (code #{status.exit_status}) when deploying docker image!"
exit 1
end
status = p2.wait
pipe1_reader.close
if status.success?
puts "-----> Database file successfully deployed"
else
STDERR.puts "Error (code #{status.exit_status}) when deploying docker image!"
exit 1
end
puts "Image pushed successfully!".colorize(:green)
end
end

View file

@ -14,9 +14,17 @@ module Pushokku
environment: String
}
@config : Config?
@options : Options?
def initialize
@options = nil
@config = nil
end
def parse_options(args) : Options
config_file = ".pushokku.yml"
docker_compose_yml = "docker-compose.yml"
config_file = ".pushokku.yml"
environment = "production"
OptionParser.parse(args) do |parser|
@ -39,11 +47,12 @@ module Pushokku
exit
end
end
return {
@options = {
docker_compose_yml: docker_compose_yml,
config_file: config_file,
environment: environment
}
return @options.as(Options)
end
def load_config(config_file : String) : Config
@ -72,33 +81,32 @@ module Pushokku
config = app.load_config(opts["config_file"])
# env_config = App.get_config(config, opts["environment"])
deployment_classes = [
DockerImageToDokkuApp,
MysqlDumpToDokkuMariadb
]
config.deployments.each do |deployment|
local = config.locals.select { |l| l.name == deployment.local }.first
remote = config.remotes.select { |r| r.name == deployment.remote }.first
config.deployments.each do |deployment_config|
local = config.locals.select { |l| l.name == deployment_config.local }.first
remote = config.remotes.select { |r| r.name == deployment_config.remote }.first
if local.nil?
puts "Unknown local #{deployment.local}. Exiting."
puts "Unknown local #{deployment_config.local}. Exiting."
exit 2
end
if remote.nil?
puts "Unknown remote #{deployment.remote}. Exiting."
puts "Unknown remote #{deployment_config.remote}. Exiting."
exit 2
end
deployment_handler = "#{local.type}_to_#{deployment.type}"
deployment_class = deployment_classes.select {|c| c.handler == deployment_handler }.first
if deployment_class.nil?
puts "Unknown deloyment class for #{deployment_handler}. Exiting."
exit 2
end
deployment = deployment_class.new(local, remote, deployment)
deployment =
case deployment_config
when DeploymentAppConfig then
DeploymentApp.new(local.as(LocalDockerConfig), remote, deployment_config.as(DeploymentAppConfig))
when DeploymentMariadbConfig then
DeploymentMariadb.new(local.as(LocalFileConfig), remote, deployment_config.as(DeploymentMariadbConfig))
when Nil
nil
end
next if deployment.nil?
deployment.run
# puts deployment.inspect
end
exit 2