Class: HSDeploy::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/hsdeploy/command.rb

Constant Summary collapse

COMMANDS =
["to", "logs", "import", "export", "config", "run"]

Class Method Summary collapse

Class Method Details

.change_to_toplevel_dir!Object

Tries to figure out if we’re running in a subdirectory of the source, and switches to the top-level if that’s the case



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hsdeploy/command.rb', line 140

def self.change_to_toplevel_dir!
  indicators = [".git", "Gemfile", "LICENSE", "test"]
  
  timeout = 10
  path = Dir.pwd
  begin
    indicators.each do |indicator|
      next unless File.exists?(File.join(path, indicator))
      
      $logger.debug "Found correct top-level directory %s, switching working directory." % [path] unless path == Dir.pwd
      Dir.chdir path
      return
    end
  end until (path = File.dirname(path)) == "/" || (timeout -= 1) == 0

  $logger.debug "DEBUG: Couldn't locate top-level directory (traversed until %s), falling back to %s" % [path, Dir.pwd]
end

.find_target(target_name) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/hsdeploy/command.rb', line 20

def self.find_target(target_name)
  unless (target = HSDeploy::Config[target_name]) && !target.nil? && target.size > 0
    puts "ERROR: Target \"#{target_name}\" is not configured"
    puts ""
    print_help
    exit
  end
  [target_name, HSDeploy::Target.from_config(target)]
end

.handle_target_exception(e) ⇒ Object



30
31
32
33
34
35
# File 'lib/hsdeploy/command.rb', line 30

def self.handle_target_exception(e)
  $logger.debug e.inspect
  $logger.debug e.backtrace
  $logger.info "\nAn Error (%s) occured. Please contact %s support: %s" % [e.inspect, HSDeploy::Target::HostingStack.cloud_name, HSDeploy::Target::HostingStack.support_email]
  exit 2
end


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hsdeploy/command.rb', line 6

def self.print_help
    puts "HostingStack Deploytool Version #{HSDeploy::VERSION} Usage Instructions"
    puts ""
    puts "Add a target:"
    puts "  hsdeploy add production [email protected]"
    puts "  hsdeploy add staging [email protected]"
    puts ""
    puts "Deploy the current directory to the target:"
    puts "  hsdeploy production"
    puts ""
    puts "Run a command on the server:"
    puts "  hsdeploy run production rake db:migrate"
end

.run(command, args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/hsdeploy/command.rb', line 37

def self.run(command, args)
  if args.include?("--debug")
    args.delete("--debug")
    $logger.level = Logger::DEBUG
  elsif args.include?("-d")
    args.delete("-d")
    $logger.level = Logger::DEBUG
  elsif args.include?("-v")
    args.delete("-v")
    $logger.level = Logger::DEBUG
  else
    $logger.level = Logger::INFO
  end

  change_to_toplevel_dir!

  HSDeploy::Config.load(".hsdeployrc")
  
  if command == "help"
    print_help
  elsif command == "add"
    if args[0].nil?
      puts "ERROR: Missing target name."
      puts ""
      puts "Use \"deploy help\" if you're lost."
      exit
    end
    if args[1].nil?
      puts "ERROR: Missing target specification."
      puts ""
      puts "Use \"deploy help\" if you're lost."
      exit
    end
    unless target = HSDeploy::Target.find(args[1])
      puts "ERROR: Couldn't find provider for target \"#{args[1]}\""
      puts ""
      puts "Use \"deploy help\" if you're lost."
      exit
    end
    if target.respond_to?(:verify)
      target.verify
    end
    HSDeploy::Config[args[0]] = target.to_h
  elsif command == "list"
    puts "Registered Targets:"
    HSDeploy::Config.all.each do |target_name, target|
      target = HSDeploy::Target.from_config(target)
      puts "  %s%s" % [target_name.ljust(15), target.to_s]
    end
  elsif command == "run"
    target_name, target = find_target args.shift
    begin
      command = args.join(' ').strip
      if command.empty?
        puts "ERROR: Must specify command to be run.\n\n"
        print_help
        exit 2
      end
      target.exec(command)
    rescue => e
      handle_target_exception e
    end
  else
    args.unshift command unless command == "to"
    target_name, target = find_target args.shift
    
    opts = {}
    opts[:timing] = true if args.include?("--timing")

    begin
      target.push(opts)
    rescue => e
      handle_target_exception e
    end
  end

  if target_name and target
    HSDeploy::Config[target_name] = target.to_h
  end

  HSDeploy::Config.save
rescue Net::HTTPServerException => e
  $logger.info "ERROR: HTTP call returned %s %s" % [e.response.code, e.response.message]
  if target
    $logger.debug "\nTarget:"
    target.to_h.each do |k, v|
      next if k.to_sym == :password
      $logger.debug "  %s = %s" % [k, v]
    end
  end
  $logger.debug "\nBacktrace:"
  $logger.debug "  " + e.backtrace.join("\n  ")
  $logger.debug "\nResponse:"
  e.response.each_header do |k, v|
    $logger.debug "  %s: %s" % [k, v]
  end
  $logger.debug "\n  " + e.response.body.gsub("\n", "\n  ")
  $logger.info "\nPlease run again with \"--debug\" and report the output at http://j.mp/hsdeploy-issue"
  exit 2
end