Class: Shuttle::Runner
- Inherits:
-
Object
- Object
- Shuttle::Runner
- Defined in:
- lib/shuttle/runner.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #execute(command) ⇒ Object
-
#initialize(options) ⇒ Runner
constructor
A new instance of Runner.
- #load_config ⇒ Object
- #parse_toml_data(data) ⇒ Object
- #parse_yaml_data(data) ⇒ Object
- #validate_target(target) ⇒ Object
Constructor Details
#initialize(options) ⇒ Runner
Returns a new instance of Runner.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/shuttle/runner.rb', line 9 def initialize() = @config_path = File.([:path]) @target = [:target] if !File.exists?(config_path) raise ConfigError, "Config file #{config_path} does not exist" end @config_path = config_path @target = target if [:log] ENV["SHUTTLE_DEBUG"] = "1" end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/shuttle/runner.rb', line 7 def config @config end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
6 7 8 |
# File 'lib/shuttle/runner.rb', line 6 def config_path @config_path end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/shuttle/runner.rb', line 5 def end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
7 8 9 |
# File 'lib/shuttle/runner.rb', line 7 def target @target end |
Instance Method Details
#execute(command) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/shuttle/runner.rb', line 62 def execute(command) @config = load_config strategy = config.app.strategy || 'static' if strategy.nil? raise ConfigError, "Invalid strategy: #{strategy}" end if @config.target server = @config.target else if @config.targets.nil? raise ConfigError, "Please define deployment target" end server = @config.targets[target] if server.nil? raise ConfigError, "Target #{target} does not exist" end end validate_target(server) ssh = Net::SSH::Session.new(server.host, server.user, server.password) if [:log] ssh.logger = Logger.new(STDOUT) end begin Timeout::timeout(10) { ssh.open } rescue Timeout::Error STDERR.puts "Unable to establish SSH connection to the server within 10 seconds" exit 1 end klass = Shuttle.const_get(strategy.capitalize) rescue nil command.gsub!(/:/,'_') exit_code = 0 if klass.nil? STDERR.puts "Invalid strategy: #{strategy}" exit 1 end unless %w(setup deploy rollback).include?(command) STDERR.puts "Invalid command: #{command}" exit 1 end integration = klass.new(config, ssh, server, target) puts "\n" puts "Shuttle v#{Shuttle::VERSION}\n" puts "\n" integration.log "Connected to #{server.user}@#{server.host}" if integration.respond_to?(command) time_start = Time.now begin if integration.deploy_running? deployer = ssh.read_file("#{integration.deploy_path}/.lock").strip = "Another deployment is running." << " Deployer: #{deployer}" if deployer.size > 0 integration.error() end integration.disable_history integration.write_lock integration.export_environment integration.send(command.to_sym) integration.write_revision rescue DeployError => err integration.cleanup_release exit_code = 1 rescue SystemExit # NOOP exit_code = 0 rescue Interrupt STDERR.puts "Interrupted by user. Aborting deploy..." exit_code = 1 rescue Exception => err integration.cleanup_release integration.log("Shuttle ERROR: #{err.message}", 'error') integration.log(err.backtrace.join("\n"), 'error') exit_code = 1 ensure integration.release_lock end if exit_code == 0 diff = (Float(Time.now - time_start) * 100).round / 100 duration = ChronicDuration.output(diff, :format => :short) puts "\nExecution time: #{duration}\n" end puts "\n" exit(exit_code) else raise ConfigError, "Invalid command: #{command}" end ssh.close rescue SocketError => err STDERR.puts "Socket error: #{err.message}" exit 1 rescue Net::SSH::AuthenticationFailed STDERR.puts "SSH Authentication failed" exit 1 end |
#load_config ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/shuttle/runner.rb', line 26 def load_config data = File.read(config_path).strip if data.empty? raise ConfigError, "Configuration file is empty" end if config_path =~ /\.toml$/ parse_toml_data(data) else parse_yaml_data(data) end end |
#parse_toml_data(data) ⇒ Object
44 45 46 |
# File 'lib/shuttle/runner.rb', line 44 def parse_toml_data(data) Hashr.new(TOML::Parser.new(data).parsed) end |
#parse_yaml_data(data) ⇒ Object
40 41 42 |
# File 'lib/shuttle/runner.rb', line 40 def parse_yaml_data(data) Hashr.new(YAML.safe_load(data)) end |
#validate_target(target) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/shuttle/runner.rb', line 48 def validate_target(target) if target.host.nil? raise ConfigError, "Target host required" end if target.user.nil? raise ConfigError, "Target user required" end if target.deploy_to.nil? raise ConfigError, "Target deploy path required" end end |