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 "yaml"
require "./config/config"
require "./config/local" require "./config/local"
require "./config/remote" require "./config/remote"
require "./config/deployment" 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" 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( YAML.mapping(
local: String, local: String,
remote: String, remote: String,
type: String, dokku_mariadb: DokkuMariadbConfig,
) )
end 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
end end
class LocalConfig class LocalFileConfig
YAML.mapping( YAML.mapping(
name: String, name: String,
type: LocalType, # enum ? type: LocalType, # enum ?
docker_image: String | Nil, path: String
path: String | Nil
) )
end 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" require "colorize"
class DockerImageToDokkuApp class DeploymentApp
def self.handler def initialize(@local : LocalDockerConfig, @remote : RemoteConfig, @deployment : DeploymentAppConfig)
"docker_image_to_dokku_app"
end
def initialize(@local : LocalConfig, @remote : RemoteConfig, @deployment : DeploymentConfig)
end end
def run 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_push(@remote.host, image_meta["tag_name_version"])
image_deploy(@remote.host, image_meta["app"], image_meta["version"]) image_deploy(@remote.host, image_meta["app"], image_meta["version"])
end end

View file

@ -1,13 +1,49 @@
class MysqlDumpToDokkuMariadb class DeploymentMariadb
def self.handler def initialize(@local : LocalFileConfig, @remote : RemoteConfig, @deployment : DeploymentMariadbConfig)
"mysql_dump_to_dokku_mariadb"
end
def initialize(@local : LocalConfig, @remote : RemoteConfig, @deployment : DeploymentConfig)
end end
def run 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
end end

View file

@ -14,9 +14,17 @@ module Pushokku
environment: String environment: String
} }
@config : Config?
@options : Options?
def initialize
@options = nil
@config = nil
end
def parse_options(args) : Options def parse_options(args) : Options
config_file = ".pushokku.yml"
docker_compose_yml = "docker-compose.yml" docker_compose_yml = "docker-compose.yml"
config_file = ".pushokku.yml"
environment = "production" environment = "production"
OptionParser.parse(args) do |parser| OptionParser.parse(args) do |parser|
@ -39,11 +47,12 @@ module Pushokku
exit exit
end end
end end
return { @options = {
docker_compose_yml: docker_compose_yml, docker_compose_yml: docker_compose_yml,
config_file: config_file, config_file: config_file,
environment: environment environment: environment
} }
return @options.as(Options)
end end
def load_config(config_file : String) : Config def load_config(config_file : String) : Config
@ -72,33 +81,32 @@ module Pushokku
config = app.load_config(opts["config_file"]) config = app.load_config(opts["config_file"])
# env_config = App.get_config(config, opts["environment"]) # env_config = App.get_config(config, opts["environment"])
deployment_classes = [ config.deployments.each do |deployment_config|
DockerImageToDokkuApp, local = config.locals.select { |l| l.name == deployment_config.local }.first
MysqlDumpToDokkuMariadb remote = config.remotes.select { |r| r.name == deployment_config.remote }.first
]
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
if local.nil? if local.nil?
puts "Unknown local #{deployment.local}. Exiting." puts "Unknown local #{deployment_config.local}. Exiting."
exit 2 exit 2
end end
if remote.nil? if remote.nil?
puts "Unknown remote #{deployment.remote}. Exiting." puts "Unknown remote #{deployment_config.remote}. Exiting."
exit 2 exit 2
end 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 deployment.run
# puts deployment.inspect
end end
exit 2 exit 2