Class: SkadateGems::Dev::CLI::Remote

Inherits:
Thor
  • Object
show all
Defined in:
lib/skadategems/dev/cli/remote.rb

Constant Summary collapse

IGNORED_DIR_PATHS =
%w[
  /$external_c /$internal_c /$userfiles /$userfiles/tmp
   /external_c  /internal_c  /userfiles  /userfiles/tmp
  /cgi-bin
  /layout/themes
  /m/application/cache/smarty_compile
]
IGNORED_FILE_EXTENSIONS =
%w[.tar .bz2 .gz .zip .rar .iso]
MAX_SOURCE_FILE_SIZE =

default values, rewritable by ENV

1024 * 1024 * 2
MAX_USER_FILE_SIZE =

~2 megabytes

1024 * 1024 * 10
SHOW_PROGRESS_FILE_SIZE =

show progressbar when downloading file size exceeds this value

1024 * 512

Instance Method Summary collapse

Instance Method Details

#cloneObject

~512 kilobytes



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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/skadategems/dev/cli/remote.rb', line 197

def clone
  if options[:schema] || options[:configs]
    say <<-STDOUT, :red
Remote clone options `--schema`, `--configs` are currently not implemented, please use `--database` for now.
    STDOUT
    return
  end

  if options.size == 0
    say 'Error: `remote clone` options are required', :red
    return help('clone')
  end

  remote = current.remote
  layout_theme = options[:layout_theme]
  @max_file_size = MAX_SOURCE_FILE_SIZE

  dev_logs_dir = ::File.join(current.root_dir, '.dev', 'log')
  FileUtils.mkdir_p(dev_logs_dir) unless ::File.exists?(dev_logs_dir)
  log_filename = ::File.join(dev_logs_dir, "remote-clone[#{Time.now.to_i}].log")

  logger.start(log_filename) do |log|
    if options[:sources]
      log.puts '>> Downloading application sources...', :yellow
      download_source_dir(remote.root, log)

      if layout_theme.nil?
        log.puts('>> Getting active theme name...', :yellow)
        config = remote.configs[1]
        raise <<-ERRMSG unless config.name == 'theme'
unexpected remote application config entry `#{config.name}`, (expected `theme` [config_id: 1])
        ERRMSG
        layout_theme = config.value
      end
    end

    if layout_theme
      log.puts ">> Downloading [/layout/themes/#{layout_theme}/*]...", :yellow
      download_source_dir remote.directory("/layout/themes/#{layout_theme}"), log
    end

    if options[:database]
      log.puts '>> Creating remote database dump...', :yellow
      remote.create_mysql_dump do |remote_file|
        local_filename = File.join(current.root_dir,
            "remote-db-clone.#{remote_file.basename =~ /\.tar\.gz$/ ? 'tar.gz' : 'sql'}")

        if log.mute?
          remote_file.save_as(local_filename)
        else
          progressbar = nil
          remote_file.save_as(local_filename) do |chunk_size|
            if progressbar
              progressbar.progress += chunk_size
            else
              progressbar = create_progressbar(remote_file, log.padding, chunk_size)
            end
          end
        end

        log.puts '>> Remote cache cleanup...', :yellow
      end ? log.puts('>> Done!', :green) : log.puts('>> Warning: remote cache cleanup failed', :red)
    end

    if options[:userfiles]
      log.puts '>> Detecting `$userfiles` directory...', :yellow
      @max_file_size = MAX_USER_FILE_SIZE
      download_source_dir(remote.userfiles, log)
    end

    log.puts '>> Completed!', :green
  end
end

#config(key_or_id, value = nil) ⇒ Object



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
# File 'lib/skadategems/dev/cli/remote.rb', line 73

def config(key_or_id, value = nil)
  key_or_id = key_or_id.to_i if numeric? key_or_id
  config = current.remote.configs[key_or_id]

  say '>> ', (:yellow unless value.nil?), false
  say "##{config.id} ", :white, false
  say '| '
  say config.name, :cyan, false
  say ': '
  say config.value.to_json, (color_for config.value), value.nil?

  unless value.nil?
    value =
      case options[:type]
        when nil
          if value == 'true'
            true
          elsif value == 'false'
            false
          elsif numeric? value
            value.to_i
          else
            value
          end
        when 'str'
        when 'string'
          value
        when 'int'
        when 'integer'
        when 'number'
          value.to_i
        when 'bool'
        when 'boolean'
          value != 'false' && value != '0'
        when 'json'
          JSON.parse(value)
        else
          raise "unrecognized config value type '#{options[:type]}'"
      end

    say ' => ', :white, false
    say value.to_json, (color_for value)

    say '>> '
    if yes?('perform this update? [yn]')
      config.value = value

      case config.update_status
        when :updated
          say '[updated]', :green
        when :not_modified
          say '[not modified]', :yellow
        else
          say '[error]', :red
      end
    else
      say '[cancelled]', :red
    end
  end
end

#exec(filename) ⇒ Object



64
65
66
# File 'lib/skadategems/dev/cli/remote.rb', line 64

def exec(filename)
  puts current.remote.exec { |batch| batch.include_file(filename) }.body
end

#pingObject



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
# File 'lib/skadategems/dev/cli/remote.rb', line 25

def ping
  accessor = current.remote.accessor
  script = ExecPHP::ScriptBatch.new { |s| s << 'echo "pong!";' }

  say '=> '
  say accessor.execphp_uri, :cyan

  accessor.exec(script) do |res|
    say '#> '
    say "#{res.code} [#{res.message}]", (res.code == '200' ? :green : :red)

    if res.body.empty?
      say 'Error: empty response body', :red
    elsif res.body != 'pong!'
      say 'Error: wrong response body:', :red
      if res.body.length > 1000
        say res.body[0..984], nil, false
        say ' ... (continued)', :yellow
      else
        say '"' + res.body + '"'
      end
    else
      say '>> '
      say res.body, :white
    end
  end
end