|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'thor' |
| 4 | +require 'yaml' |
| 5 | + |
| 6 | +require 'dory' |
| 7 | + |
| 8 | +class DoryBin < Thor |
| 9 | + class_option :verbose, type: :boolean, aliases: 'v', default: false |
| 10 | + |
| 11 | + desc 'up', 'Bring up dory services (nginx-proxy, dnsmasq, resolv)' |
| 12 | + long_desc <<-LONGDESC |
| 13 | + Bring up dory services (nginx-proxy, dnsmasq, resolv) |
| 14 | +
|
| 15 | + When run, the docker container for the nginx proxy is started, |
| 16 | + along with a local dnsmasq instance to resolve DNS requests |
| 17 | + for your custom domain to the nginx proxy. The local resolver |
| 18 | + will also be configured to use the dnsmasq instance as a nameserver |
| 19 | +
|
| 20 | + You can optionally pass in [-s|--server] or [-c|--client] if doing |
| 21 | + development on a cloud server. This will tell dory to only bring |
| 22 | + up the nginx proxy container if passed --server or only the |
| 23 | + dnsmasq/resolv if passed --client. If both server and client |
| 24 | + are passed, it is equivalent to a naked 'up' |
| 25 | +
|
| 26 | + > $ dory up [-s|--server] [-c|--client] |
| 27 | + LONGDESC |
| 28 | + option :client, type: :boolean, aliases: 'c', default: false |
| 29 | + option :server, type: :boolean, aliases: 's', default: false |
| 30 | + def up |
| 31 | + exec_up(options) |
| 32 | + end |
| 33 | + |
| 34 | + desc 'down', 'Stop all dory services' |
| 35 | + long_desc <<-LONGDESC |
| 36 | + Stops all dory services. Can optionally pass [-d|--destroy] |
| 37 | + to destroy the containers when they stop. |
| 38 | +
|
| 39 | + > $ dory down [-d|--destroy] |
| 40 | + LONGDESC |
| 41 | + option :destroy, type: :boolean, aliases: 'd', default: true |
| 42 | + def down |
| 43 | + exec_down(options) |
| 44 | + end |
| 45 | + |
| 46 | + desc 'version', 'Check current installed version of dory' |
| 47 | + def version |
| 48 | + puts "Dory - Version: #{Dory::VERSION}" |
| 49 | + end |
| 50 | + |
| 51 | + desc 'restart', 'Stop and restart all dory services' |
| 52 | + long_desc <<-LONGDESC |
| 53 | + Stop and restart dory services (nginx-proxy, dnsmasq, resolv) |
| 54 | +
|
| 55 | + > $ dory restart [-d|--destroy] [-s|--server] [-c|--client] |
| 56 | + LONGDESC |
| 57 | + option :client, type: :boolean, aliases: 'c', default: false |
| 58 | + option :server, type: :boolean, aliases: 's', default: false |
| 59 | + option :destroy, type: :boolean, aliases: 'd', default: true |
| 60 | + def restart |
| 61 | + exec_down(options) |
| 62 | + exec_up(options) |
| 63 | + end |
| 64 | + |
| 65 | + desc 'status', 'Report status of the dory services' |
| 66 | + long_desc <<-LONGDESC |
| 67 | + Checks the current status of the services managed by dory. |
| 68 | + This includes nginx-proxy, dnsmasq, and resolv |
| 69 | +
|
| 70 | + > $ dory status |
| 71 | + LONGDESC |
| 72 | + def status |
| 73 | + exec_status(options) |
| 74 | + end |
| 75 | + |
| 76 | + desc 'config-file', 'Write a default config file' |
| 77 | + long_desc <<-LONGDESC |
| 78 | + Writes a dory config file to #{Dory::Config.filename} |
| 79 | + containing the default settings. This can then be configured |
| 80 | + as preferred. |
| 81 | + LONGDESC |
| 82 | + def config_file |
| 83 | + exec_config_file(options) |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def exec_config_file(options) |
| 89 | + if File.exist?(Dory::Config.filename) |
| 90 | + print "A config file already exists at #{Dory::Config.filename}. Overwrite with default settings? (Y/N): ".yellow |
| 91 | + conf = STDIN.gets.chomp |
| 92 | + unless conf =~ /y/i |
| 93 | + puts "User declined over-writing. Not writing config file".red |
| 94 | + return |
| 95 | + end |
| 96 | + end |
| 97 | + puts "Writing config file to #{Dory::Config.filename}".green |
| 98 | + Dory::Config.write_default_settings_file |
| 99 | + end |
| 100 | + |
| 101 | + def exec_up(options) |
| 102 | + full = (!options[:server] && !options[:client]) || |
| 103 | + ( options[:server] && options[:client]) |
| 104 | + |
| 105 | + puts "Reading settings file at '#{Dory::Config.filename}'".green if options[:verbose] |
| 106 | + settings = Dory::Config.settings |
| 107 | + if settings[:dory][:nginx_proxy][:enabled] |
| 108 | + puts "nginx_proxy enabled in config file".green if options[:verbose] |
| 109 | + if Dory::Proxy.start |
| 110 | + puts "Successfully started nginx proxy".green |
| 111 | + else |
| 112 | + puts "Error starting nginx proxy".red |
| 113 | + end |
| 114 | + else |
| 115 | + puts "nginx_proxy disabled in config file".yellow if options[:verbose] |
| 116 | + end |
| 117 | + |
| 118 | + if settings[:dory][:dnsmasq][:enabled] |
| 119 | + puts "dnsmasq enabled in config file".green if options[:verbose] |
| 120 | + if Dory::Dnsmasq.start |
| 121 | + puts "Successfully started dnsmasq".green |
| 122 | + else |
| 123 | + puts "Error starting dnsmasq".red |
| 124 | + end |
| 125 | + else |
| 126 | + puts "dnsmasq disabled in config file".yellow if options[:verbose] |
| 127 | + end |
| 128 | + |
| 129 | + if settings[:dory][:resolv][:enabled] |
| 130 | + if Dory::Resolv.configure |
| 131 | + puts "Successfully configured local resolver".green |
| 132 | + else |
| 133 | + puts "Error configuring local resolver".red |
| 134 | + end |
| 135 | + puts "resolv enabled in config file".green if options[:verbose] |
| 136 | + else |
| 137 | + puts "resolv disabled in config file".yellow if options[:verbose] |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + def exec_status(options) |
| 142 | + settings = Dory::Config.settings |
| 143 | + |
| 144 | + if Dory::Proxy.running? |
| 145 | + puts "[*] Nginx proxy: Running as docker container #{Dory::Proxy.container_name}".green |
| 146 | + elsif !settings[:dory][:nginx_proxy][:enabled] |
| 147 | + puts "[*] Nginx proxy is disabled in config file".yellow |
| 148 | + else |
| 149 | + puts "[*] Nginx proxy is not running".red |
| 150 | + end |
| 151 | + |
| 152 | + if Dory::Dnsmasq.running? |
| 153 | + puts "[*] Dnsmasq: Running as docker container #{Dory::Dnsmasq.container_name}".green |
| 154 | + elsif !settings[:dory][:dnsmasq][:enabled] |
| 155 | + puts "[*] Dnsmasq is disabled in config file".yellow |
| 156 | + else |
| 157 | + puts "[*] Dnsmasq is not running".red |
| 158 | + end |
| 159 | + |
| 160 | + if Dory::Resolv.has_our_nameserver? |
| 161 | + puts "[*] Resolv: configured with nameserver #{}".green |
| 162 | + elsif !settings[:dory][:resolv][:enabled] |
| 163 | + puts "[*] Resolv is disabled in config file".yellow |
| 164 | + else |
| 165 | + puts "[*] Resolv is not configured".red |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + def exec_down(options) |
| 170 | + destroy = options[:destroy] |
| 171 | + |
| 172 | + if Dory::Resolv.clean |
| 173 | + puts "nameserver removed from resolv file".green |
| 174 | + else |
| 175 | + puts "Unable to remove nameserver from resolv file".red |
| 176 | + end |
| 177 | + |
| 178 | + if Dory::Dnsmasq.stop |
| 179 | + puts "Dnsmasq container stopped".green |
| 180 | + if options[:destroy] |
| 181 | + if Dory::Dnsmasq.delete |
| 182 | + puts "Dnsmasq container successfully deleted".green |
| 183 | + else |
| 184 | + puts "Dnsmasq container failed to delete".red |
| 185 | + end |
| 186 | + end |
| 187 | + else |
| 188 | + puts "Dnsmasq container failed to stop".red |
| 189 | + end |
| 190 | + |
| 191 | + if Dory::Proxy.stop |
| 192 | + puts "Nginx proxy stopped".green |
| 193 | + if options[:destroy] |
| 194 | + if Dory::Proxy.delete |
| 195 | + puts "Nginx proxy container successfully deleted".green |
| 196 | + else |
| 197 | + puts "Nginx proxy container failed to delete".red |
| 198 | + end |
| 199 | + end |
| 200 | + else |
| 201 | + puts "Nginx proxy failed to stop".red |
| 202 | + end |
| 203 | + end |
| 204 | +end |
| 205 | + |
| 206 | +if !ARGV.empty? && %w[-v --version].include?(ARGV.first) |
| 207 | + puts "Dory - Version: #{Dory::VERSION}" |
| 208 | +else |
| 209 | + DoryBin.start(ARGV) |
| 210 | +end |
0 commit comments