Class: DeployTool::Command

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

Constant Summary collapse

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

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/deploytool/command.rb', line 114

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


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

def self.print_help
    puts "Deploytool Version #{DeployTool::VERSION} Usage Instructions"
    puts ""
    puts "Add a target:"
    puts "  deploy add production [email protected]"
    puts "  deploy add staging [email protected]"
    puts ""
    puts "Deploy the current directory to the target:"
    puts "  deploy production"
end

.run(command, args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/deploytool/command.rb', line 17

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!

  DeployTool::Config.load(".deployrc")
  
  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 = DeployTool::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
    DeployTool::Config[args[0]] = target.to_h
  elsif command == "list"
    puts "Registered Targets:"
    DeployTool::Config.all.each do |target_name, target|
      target = DeployTool::Target.from_config(target)
      puts "  %s%s" % [target_name.ljust(15), target.to_s]
    end
  else
    args.unshift command unless command == "to"
    target_name = args[0]
    
    unless (target = DeployTool::Config[target_name]) && !target.nil? && target.size > 0
      puts "ERROR: Target \"#{target_name}\" is not configured"
      puts ""
      print_help
      exit
    end
    
    opts = {}
    opts[:timing] = true if args.include?("--timing")

    target = DeployTool::Target.from_config(target)
    begin
      target.push(opts)
    rescue => e
      puts e
      puts "\nPlease contact %s support: %s" % [DeployTool::Target::EfficientCloud.cloud_name, DeployTool::Target::EfficientCloud.support_email]
      exit 2
    end
    DeployTool::Config[args[0]] = target.to_h
  end
  
  DeployTool::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://bit.ly/deploytool-new-issue"
  exit 2
end