|
| 1 | +#!/usr/bin/ruby |
| 2 | + |
| 3 | +require 'date' |
| 4 | +require 'fileutils' |
| 5 | +require 'open3' |
| 6 | +require 'yaml' |
| 7 | + |
| 8 | +# ------------------------------------------------------------------------------ |
| 9 | +# Helpers |
| 10 | +# ------------------------------------------------------------------------------ |
| 11 | +# Run a command |
| 12 | +# |
| 13 | +# @param cmd [String] Command to run |
| 14 | +def run(cmd) |
| 15 | + rtn = nil |
| 16 | + |
| 17 | + Open3.popen3(cmd) do |_, o, e, t| |
| 18 | + out_reader = Thread.new { o.read } |
| 19 | + err_reader = Thread.new { e.read } |
| 20 | + |
| 21 | + rtn = [out_reader.value, err_reader.value, t.value] |
| 22 | + end |
| 23 | + |
| 24 | + rtn |
| 25 | +end |
| 26 | + |
| 27 | +# Print error and exit |
| 28 | +# |
| 29 | +# @param rc [Array] rc[0] STDOUT rc[1] STDERR |
| 30 | +def error(rc) |
| 31 | + return rc[0].strip if rc[2].success? |
| 32 | + |
| 33 | + puts "ERROR: #{rc[1]}" |
| 34 | + |
| 35 | + exit(-1) |
| 36 | +end |
| 37 | +# ------------------------------------------------------------------------------ |
| 38 | +# ------------------------------------------------------------------------------ |
| 39 | + |
| 40 | +config = YAML.load_file('publish/config.yaml') |
| 41 | + |
| 42 | +unless config['mapping'].include?(ARGV[0]) |
| 43 | + puts "Aborting, branch '#{ARGV[0]}' is not published" |
| 44 | + exit(0) |
| 45 | +end |
| 46 | + |
| 47 | +# ------------------------------------------------------------------------------ |
| 48 | +# Read secrets from ENV |
| 49 | +# ------------------------------------------------------------------------------ |
| 50 | +host = ENV['HOST'] |
| 51 | +host_path = ENV['HOST_PATH'] |
| 52 | + |
| 53 | +# ------------------------------------------------------------------------------ |
| 54 | +# Compile documentation |
| 55 | +# ------------------------------------------------------------------------------ |
| 56 | +error(run('make SPHINXBUILD="python -m sphinx" html')) |
| 57 | + |
| 58 | +# ------------------------------------------------------------------------------ |
| 59 | +# Publish documentation |
| 60 | +# ------------------------------------------------------------------------------ |
| 61 | +date_time = DateTime.now.strftime('%Y%m%d%H%M%S') |
| 62 | +branch_dir = config['mapping'][ARGV[0]] |
| 63 | +branch_symlink_path = "#{host_path}/#{branch_dir}" |
| 64 | +branch_build_path = "#{branch_symlink_path}.#{date_time}" |
| 65 | +ssh_op = '-o StrictHostKeyChecking=no -i /tmp/id_rsa' |
| 66 | + |
| 67 | +error(run("ssh #{ssh_op} #{host} 'test -d #{host_path} && touch #{host_path}'")) |
| 68 | +error(run("ssh #{ssh_op} #{host} 'mkdir #{branch_build_path}'")) |
| 69 | + |
| 70 | +error(run("tar -C 'build/html' --mode='a+r' -cf - . | " \ |
| 71 | + "ssh #{ssh_op} #{host} 'tar -C '#{branch_build_path}' -xvf -'")) |
| 72 | +error( |
| 73 | + run( |
| 74 | + "ssh #{ssh_op} #{host} " \ |
| 75 | + "'ln -nsf #{branch_dir}.#{date_time} #{branch_symlink_path}'" |
| 76 | + ) |
| 77 | +) |
| 78 | + |
| 79 | +# ensure dir exists |
| 80 | +rc = run("ssh #{ssh_op} #{host} 'ls #{host_path} | grep #{branch_dir}. | sort'") |
| 81 | +error(rc) |
| 82 | + |
| 83 | +# ensure branch_symlink_path exists |
| 84 | +rc = run("ssh #{ssh_op} #{host} 'test -e #{branch_symlink_path} || " << |
| 85 | + " echo \"#{branch_symlink_path} does not exists\" >&2'") |
| 86 | +error(rc) |
| 87 | + |
| 88 | +# ensure branch_symlink_path is a symlink |
| 89 | +rc = run("ssh #{ssh_op} #{host} 'test -L #{branch_symlink_path} || " << |
| 90 | + " echo \"#{branch_symlink_path} is not a symlink\" >&2'") |
| 91 | +error(rc) |
| 92 | + |
| 93 | +# cleanup old versions |
| 94 | + |
| 95 | +rc = run("ssh #{ssh_op} #{host} 'find #{host_path} -maxdepth 1 -type d -name \"#{branch_dir}.*\" -printf \"%f\\n\" | sort'") |
| 96 | +error(rc) |
| 97 | + |
| 98 | +builds = rc[0].split |
| 99 | + |
| 100 | +puts "Total builds found: #{builds.length}" |
| 101 | +puts "Builds: #{builds.join("\n")}" |
| 102 | + |
| 103 | +exit(0) if builds.length <= 2 |
| 104 | + |
| 105 | +# List all builds except the two most recent |
| 106 | +old_builds = builds[0...-2] |
| 107 | +old_builds.each do |build| |
| 108 | + puts "Removing old build folder: #{build}" |
| 109 | + run("ssh #{ssh_op} #{host} rm -rf #{host_path}/#{build}") |
| 110 | +end |
0 commit comments