Top Level Namespace

Defined Under Namespace

Classes: ThrushItemBuilder

Instance Method Summary collapse

Instance Method Details

#cmd_vals(key, cmdval) ⇒ Object



60
61
62
63
# File 'lib/thrush.rb', line 60

def cmd_vals(key, cmdval)
  return cmdval[key] if cmdval.has_key? key
  env(key)
end

#config(env, &block) ⇒ Object



44
45
46
47
48
# File 'lib/thrush.rb', line 44

def config(env, &block)
  ev = Docile.dsl_eval(ThrushItemBuilder.new, &block).build
  @current_env = env.to_sym if @current_env == :default && ev.has_key?(:default) && ev[:default] == true
  @env[env] = ev
end

#debug?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/thrush.rb', line 74

def debug?
  flag? :debug
end

#env(key) ⇒ Object



54
55
56
57
58
# File 'lib/thrush.rb', line 54

def env(key)
  return @env[@current_env][key] if @env[@current_env].has_key? key
  return @env[:__global__][key] if @env[:__global__].has_key? key
  nil
end

#env?(env) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/thrush.rb', line 65

def env?(env)
  @current_env == env
end

#flag(key) ⇒ Object



78
79
80
81
82
83
# File 'lib/thrush.rb', line 78

def flag(key)
  k = key.to_sym
  if flag? k
    @flags[k]
  end
end

#flag?(key) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/thrush.rb', line 69

def flag?(key)
  k = key.to_sym
  @flags.has_key?(k) && @flags[k]
end

#flags(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/thrush.rb', line 10

def flags(args)
  param = nil
  args.each do |arg|
    if arg[0..1] == '--'
      param = arg[2..-1].to_sym
      @flags[param] = true unless @flags.has_key? param
    else
      case param
      when :debug
        @flags[param] = true
      when :env
        @flags[param] = arg.to_sym
        @current_env  = arg.to_sym
      else
        @flags[param] = arg
      end
    end
  end
end

#global(&block) ⇒ Object



50
51
52
# File 'lib/thrush.rb', line 50

def global(&block)
  config(:__global__, &block)
end

#header(h) ⇒ Object



109
110
111
112
113
# File 'lib/thrush.rb', line 109

def header(h)
  len = h.gsub(/\e\[(\d+)m/, '').length
  l   = '-' * len
  "\n#{ h }\n#{ l }\n"
end

#local(command, &block) ⇒ Object

TODO: Fix the local, ssh functions so that they will work even if they don’t have a block passed in



89
90
91
92
93
94
# File 'lib/thrush.rb', line 89

def local(command, &block)
  lc = ThrushItemBuilder.new
  lc.command command
  lc.type :local
  @commands << Docile.dsl_eval(lc, &block).build
end

#rsync(&block) ⇒ Object



96
97
98
99
100
# File 'lib/thrush.rb', line 96

def rsync(&block)
  rc = ThrushItemBuilder.new
  rc.type :rsync
  @commands << Docile.dsl_eval(rc, &block).build
end

#run!Object



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/thrush.rb', line 115

def run!
  client = env(:client).nil? ? 'Thrush' : env(:client)
  if @flags.has_key?(:help)
    puts header("#{ client } client help for (ENV = #{ @current_env.upcase })").blue.bold
    puts "#{ env(:help) }\n"
    exit
  end

  puts header("#{ client } client for (ENV = #{ @current_env.upcase })").blue.bold
  puts "** DEBUG MODE **".yellow if debug?
  i = 1

  @commands.each do |cmd_values|
    if cmd_values.has_key?(:exc)
      found = false
      cmd_values[:exc].each { |flag| found = true if flag? flag }
      next if found
    end

    if cmd_values.has_key?(:inc)
      found = false
      cmd_values[:inc].each { |flag| found = true if flag? flag }
      next unless found
    end

    sudo          = cmd_vals(:sudo, cmd_values)
    debug_command = ''

    case cmd_values[:type]
    when :local
      cmd = sudo ? %w(sudo) : []
      cmd << cmd_vals(:command, cmd_values)

      command = cmd.join(' ')

      if debug?
        debug_command = command
        command       = ''
      end
    when :rsync
      debug = debug? ? 'n' : ''
      rsh   = cmd_vals(:rsh, cmd_values)

      # TODO: The default rsync values I've hardcoded including --no-o, --no-g and other defaults should be flags that someone can pass in.
      cmd   = []
      cmd << 'sudo' if rsh.nil? && sudo
      cmd << "rsync -#{ debug }vazcO --no-o --no-g"
      cmd << "--rsh=#{ rsh }" unless rsh.nil?
      cmd << "--exclude '.git' --exclude '.idea' --exclude '.DS_Store'"

      ignore = cmd_vals(:ignore, cmd_values)
      ignore.each { |exclude| cmd << "--exclude '#{ exclude }'" } unless ignore.nil? || ignore.empty?

      if rsh == 'ssh'
        cmd << "--rsync-path='sudo rsync'" if sudo

        path = cmd_vals(:path, cmd_values)
        cmd << "--rsync-path='#{ path }'" unless path.nil? || path.empty?
      end

      cmd << cmd_vals(:src, cmd_values)

      if rsh == 'ssh'
        cmd << "#{ cmd_vals(:hostname, cmd_values) }:#{ cmd_vals(:dest, cmd_values) }"
      else
        cmd << cmd_vals(:dest, cmd_values)
      end

      command = cmd.join(' ')
      debug_command = command if debug?
    when :ssh
      cmd = ["ssh #{ cmd_vals(:hostname, cmd_values) }"]
      cmd << 'sudo' if sudo
      cmd << "#{ cmd_vals(:command, cmd_values) }"

      command = cmd.join(' ')
      if debug?
        debug_command = command
        command       = ''
      end
    else
      command       = ''
      debug_command = ''
    end

    puts "\n#{ i }. #{ cmd_values[:step] }".green if cmd_values[:step]
    i += 1

    unless debug_command.empty?
      puts "Command to run: #{ debug_command }"
    end

    unless command.empty?
      #This code is simpler than requiring open4, but the output is delayed
      #puts `#{ command }`
      #unless $?.to_i == 0
      #  puts "Command failed: #{ command }"
      #  break
      #end

      #status = Open4::popen4('sh') do |pid, stdin, stdout, stderr|
      #  stdin.puts command
      #  stdin.close
      #
      #  while (line = stdout.gets)
      #    puts line
      #  end
      #
      #  while (line = stderr.gets)
      #    puts line.red
      #  end
      #end
      #
      #unless status == 0
      #  puts "Command failed: #{ command }".red
      #  break
      #end

      pid, stdin, stdout, stderr = Open4::popen4('sh')
      stdin.puts command
      stdin.close

      output = false
      while (line = stdout.gets)
        output = true
        puts line
      end

      ignored, status = Process.waitpid2 pid
      if status.to_i == 0
        puts 'OK' unless output
      else
        puts "Command failed (#{ status.exitstatus }): #{ command }".red
        while (line = stderr.gets)
          puts line.red
        end
        break
      end
    end
  end
end

#ssh(command, &block) ⇒ Object



102
103
104
105
106
107
# File 'lib/thrush.rb', line 102

def ssh(command, &block)
  sc = ThrushItemBuilder.new
  sc.command command
  sc.type :ssh
  @commands << Docile.dsl_eval(sc, &block).build
end