-
-
Save iNecas/262ac10a200f1a004a8f57cd3e4371f9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash | |
function export-csv () { | |
echo "Exporting $2" | |
echo "COPY ($1) TO STDOUT WITH CSV;" | su - postgres -c '/usr/bin/psql -d foreman' > $2 | |
} | |
function import-csv () { | |
echo "Importing $2" | |
su - postgres -c "/usr/bin/psql -d $IMPORT_DATABASE -c 'COPY $1 FROM STDIN WITH CSV'" <$2 | |
} | |
function run-dynflow () { | |
block=${1:-false} | |
cat <<EOF | sudo -u postgres scl enable tfm ruby | |
require 'dynflow' | |
config = Dynflow::Config.new | |
config.persistence_adapter = Dynflow::PersistenceAdapters::Sequel.new "postgres://postgres@/$IMPORT_DATABASE" | |
config.logger_adapter = Dynflow::LoggerAdapters::Simple.new STDERR, 4 | |
config.auto_rescue = false | |
config.auto_execute = false | |
config.auto_validity_check = false | |
config.auto_terminate = false | |
world = Dynflow::World.new(config) | |
if $block | |
require 'dynflow/web' | |
dynflow_console = Dynflow::Web.setup do | |
set :world, world | |
end | |
puts "\n The Dynflow console is starting. To increase the page size, use 'per_page=1000' in web query\n\n" | |
Rack::Server.new(:app => dynflow_console, :Port => 4567).start | |
end | |
EOF | |
} | |
function dynflow-import () { | |
sudo su - postgres -c "createdb $IMPORT_DATABASE" | |
# prepare db schema | |
run-dynflow | |
# load data | |
import-csv dynflow_execution_plans dynflow_execution_plans.csv | |
import-csv dynflow_actions dynflow_actions.csv | |
import-csv dynflow_steps dynflow_steps.csv | |
# run web console | |
run-dynflow true | |
} | |
function dynflow-export () { | |
export-csv "select dynflow_execution_plans.* from foreman_tasks_tasks join dynflow_execution_plans on (foreman_tasks_tasks.external_id = dynflow_execution_plans.uuid) where foreman_tasks_tasks.started_at > 'now'::timestamp - '2 months'::interval" dynflow_execution_plans.csv | |
export-csv "select dynflow_actions.* from foreman_tasks_tasks join dynflow_actions on (foreman_tasks_tasks.external_id = dynflow_actions.execution_plan_uuid) where foreman_tasks_tasks.started_at > 'now'::timestamp - '2 months'::interval" dynflow_actions.csv | |
export-csv "select dynflow_steps.* from foreman_tasks_tasks join dynflow_steps on (foreman_tasks_tasks.external_id = dynflow_steps.execution_plan_uuid) where foreman_tasks_tasks.started_at > 'now'::timestamp - '2 months'::interval" dynflow_steps.csv | |
export-csv "select * from foreman_tasks_tasks" foreman_tasks_tasks.csv | |
export-csv "select * from dynflow_schema_info" dynflow_schema_info.csv | |
} | |
operation=$1 | |
shift | |
case $operation in | |
import ) | |
IMPORT_DATABASE=${1:-dynflow-import} | |
dynflow-import;; | |
export ) | |
dynflow-export ;; | |
* ) | |
cat <<HELP | |
Usage: | |
Export dynflow data to CSV files inside current directory: | |
dynflow-csv export | |
Import CSV data from current directory | |
dynflow-csv import [database-name] | |
* database-name - what database to use for loading the data? "dynflow-import" by default | |
HELP | |
esac |
For now, we don't ened the dynflow_schema_info: we let the dynflow to recreate the schema for now.
"createdb $IMPORT_DATABASE" (why I can't comment individual lines?): I suggest improvement: if the db already exist, drop it first (or make the drop optional) - we will often use the same machine for importing different task "exports" and need some cleanup.
Worth to mention in usage/help or in stdout, that the WebUI is running on $(hostname):4567
Tiny bug in dynflow(?): clicking twice to sort per time shall sort in descending order, but ascending order (from 1st change) remains. But browser knows button "Back" :)
Otherwise great.
Another (monkey) improvement since on my rails (rubygem-rails-4.2.6-1
, for foreman
1.15
/ Satellite 6.3), the app listens on localhost
only - let it listen on its hostname's IP rather:
..
require 'socket'
Rack::Server.new(:app => dynflow_console, :Port => 4567, :Host => Socket.gethostname).start
..
"function dynflow-import": shall not be here also:
?