Class: SwitchTower::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/switchtower/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ CLI

Returns a new instance of CLI.



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
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
# File 'lib/switchtower/cli.rb', line 37

def initialize(args = ARGV)
  @args = args
  @options = { :verbose => 0, :recipes => [], :actions => [], :vars => {},
    :pre_vars => {} }

  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options] [args]"

    opts.separator ""
    opts.separator "Recipe Options -----------------------"
    opts.separator ""

    opts.on("-a", "--action ACTION",
      "An action to execute. Multiple actions may",
      "be specified, and are loaded in the given order."
    ) { |value| @options[:actions] << value }

    opts.on("-p", "--password [PASSWORD]",
      "The password to use when connecting. If the switch",
      "is given without a password, the password will be",
      "prompted for immediately. (Default: prompt for password",
      "the first time it is needed.)"
    ) { |value| @options[:password] = value }

    opts.on("-r", "--recipe RECIPE",
      "A recipe file to load. Multiple recipes may",
      "be specified, and are loaded in the given order."
    ) { |value| @options[:recipes] << value }

    opts.on("-s", "--set NAME=VALUE",
      "Specify a variable and it's value to set. This",
      "will be set after loading all recipe files."
    ) do |pair|
      name, value = pair.split(/=/, 2)
      @options[:vars][name.to_sym] = value
    end

    opts.on("-S", "--set-before NAME=VALUE",
      "Specify a variable and it's value to set. This",
      "will be set BEFORE loading all recipe files."
    ) do |pair|
      name, value = pair.split(/=/, 2)
      @options[:pre_vars][name.to_sym] = value
    end

    opts.separator ""
    opts.separator "Framework Integration Options --------"
    opts.separator ""

    opts.on("-A", "--apply-to DIRECTORY",
      "Create a minimal set of scripts and recipes to use",
      "switchtower with the application at the given",
      "directory. (Currently only works with Rails apps.)"
    ) { |value| @options[:apply_to] = value }

    opts.separator ""
    opts.separator "Miscellaneous Options ----------------"
    opts.separator ""

    opts.on("-h", "--help", "Display this help message") do
      puts opts
      exit
    end

    opts.on("-P", "--[no-]pretend",
      "Run the task(s), but don't actually connect to or",
      "execute anything on the servers. (For various reasons",
      "this will not necessarily be an accurate depiction",
      "of the work that will actually be performed.",
      "Default: don't pretend.)"
    ) { |value| @options[:pretend] = value }

    opts.on("-v", "--verbose",
      "Specify the verbosity of the output.",
      "May be given multiple times. (Default: silent)"
    ) { @options[:verbose] += 1 }

    opts.on("-V", "--version",
      "Display the version info for this utility"
    ) do
      require 'switchtower/version'
      puts "SwitchTower v#{SwitchTower::Version::STRING}"
      exit
    end

    opts.separator ""
    opts.separator <<DETAIL.split(/\n/)
You can use the --apply-to switch to generate a minimal set of switchtower
scripts and recipes for an application. Just specify the path to the application
as the argument to --apply-to, like this:

  switchtower --apply-to ~/projects/myapp

You'll wind up with a sample deployment recipe in config/deploy.rb, some new
rake tasks in config/tasks, and a switchtower script in your script directory.

(Currently, --apply-to only works with Rails applications.)
DETAIL

    if args.empty?
      puts opts
      exit
    else
      opts.parse!(args)
    end
  end

  check_options!

  password_proc = Proc.new do
    sync = STDOUT.sync
    begin
      echo false
      STDOUT.sync = true
      print "Password: "
      STDIN.gets.chomp
    ensure
      echo true
      STDOUT.sync = sync
      puts
    end
  end

  if !@options.has_key?(:password)
    @options[:password] = password_proc
  elsif !@options[:password]
    @options[:password] = password_proc.call
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



35
36
37
# File 'lib/switchtower/cli.rb', line 35

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/switchtower/cli.rb', line 34

def options
  @options
end

Class Method Details

.execute!Object



6
7
8
# File 'lib/switchtower/cli.rb', line 6

def self.execute!
  new.execute!
end

Instance Method Details

#execute!Object



167
168
169
170
171
172
173
# File 'lib/switchtower/cli.rb', line 167

def execute!
  if !@options[:recipes].empty?
    execute_recipes!
  elsif @options[:apply_to]
    execute_apply_to!
  end
end