diff --git a/src/config.cr b/src/config.cr index 2a6dfc0..f11d5c9 100644 --- a/src/config.cr +++ b/src/config.cr @@ -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 diff --git a/src/config/config.cr b/src/config/config.cr new file mode 100644 index 0000000..c6856a0 --- /dev/null +++ b/src/config/config.cr @@ -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 diff --git a/src/config/deployment.cr b/src/config/deployment.cr index 23c0af5..23f0168 100644 --- a/src/config/deployment.cr +++ b/src/config/deployment.cr @@ -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 + diff --git a/src/config/local.cr b/src/config/local.cr index 6098af9..a5f4856 100644 --- a/src/config/local.cr +++ b/src/config/local.cr @@ -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 + diff --git a/src/deployment/docker_image_to_dokku_app.cr b/src/deployment/docker_image_to_dokku_app.cr index 8ffb905..808d15c 100644 --- a/src/deployment/docker_image_to_dokku_app.cr +++ b/src/deployment/docker_image_to_dokku_app.cr @@ -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 diff --git a/src/deployment/mysql_dump_to_dokku_mariadb.cr b/src/deployment/mysql_dump_to_dokku_mariadb.cr index 538096c..4f6c775 100644 --- a/src/deployment/mysql_dump_to_dokku_mariadb.cr +++ b/src/deployment/mysql_dump_to_dokku_mariadb.cr @@ -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 diff --git a/src/pushokku.cr b/src/pushokku.cr index a0f6b55..fd5eed4 100644 --- a/src/pushokku.cr +++ b/src/pushokku.cr @@ -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