Class: SkadateGems::Dev::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/skadategems/dev/remote.rb,
lib/skadategems/dev/remote/file.rb,
lib/skadategems/dev/remote/configs.rb,
lib/skadategems/dev/remote/directory.rb,
lib/skadategems/dev/remote/clone_logger.rb

Overview

Represents remote Skadate-driven application.

Defined Under Namespace

Classes: CloneLogger, Configs, Directory, File, ScriptBatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_filename) ⇒ Remote

Returns a new instance of Remote.



13
14
15
# File 'lib/skadategems/dev/remote.rb', line 13

def initialize(config_filename)
  @accessor = ExecPHP::RemoteServer.from_file(config_filename)
end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor.



11
12
13
# File 'lib/skadategems/dev/remote.rb', line 11

def accessor
  @accessor
end

Instance Method Details

#configsRemote::Configs

Returns remote application configs accessor.

Returns:



42
43
44
# File 'lib/skadategems/dev/remote.rb', line 42

def configs
  @config ||= Configs.new(self)
end

#create_mysql_dump(&block) ⇒ Object



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

def create_mysql_dump(&block)
  response = exec do |script|
    script << <<-PHPSCRIPT
require_once DIR_INTERNALS . 'system.const.php';

if (strpos(DIR_INTERNAL_C, DIR_SITE_ROOT) !== 0) {
  header('HTTP/1.1 500 Internal Server Error');
  exit('Error: unable to detect path to `internal_c/`.');
}

function program_available($bin) {
  $output = null;
  $result = -1;
  exec("type $bin>/dev/null 2>&1", $output, $result);
  return $result === 0;
}

if (!program_available('mysqldump')) {
  header('HTTP/1.1 500 Internal Server Error');
  exit('Error: `mysqldump` is not available');
}

$tmp_dir = DIR_INTERNAL_C;
$tmp_dir_path = substr($tmp_dir, strlen(DIR_SITE_ROOT));

$uniqid = uniqid();
$sql_filename = "$uniqid.sql";

$db_host = DB_HOST;
$db_user = DB_USER;
$db_pass = DB_PASS;
$db_name = DB_NAME;

$output = null;
$result = -1;
exec("cd '$tmp_dir' && mysqldump --host='$db_host' --user='$db_user' --password='$db_pass' '$db_name' > '$sql_filename'", $output, $result);
if (!file_exists($tmp_dir . $sql_filename)) {
  header('HTTP/1.1 500 Internal Server Error');
  exit('`mysqldump` error: ' . join($output, "\n"));
}

if (program_available('tar')) {
  $tar_filename = "$uniqid.tar.gz";

  $output = null;
  $result = -1;
  exec("cd '$tmp_dir' && tar -czf '$tar_filename' '$sql_filename'", $output, $result);
  if (file_exists($tmp_dir . $tar_filename)) {
    unlink($tmp_dir . $sql_filename);
    exit('["' . $tmp_dir_path . $tar_filename . '", ' . filesize($tmp_dir . $tar_filename) . ']');
  }
}

exit('["' . $tmp_dir_path . $sql_filename . '", ' . filesize($tmp_dir . $sql_filename) . ']');
    PHPSCRIPT
  end

  raise "#{response.code} [#{response.message}]:\n >> #{response.body}" if response.code != '200'

  path, size = JSON.parse(response.body)
  block.call File.new(self, path, size)

  response = exec do |script|
    script << <<-PHPSCRIPT
echo unlink(DIR_SITE_ROOT . '#{path}') ? '1' : '0';
    PHPSCRIPT
  end

  raise "#{response.code} [#{response.message}]:\n >> #{response.body}" if response.code != '200'

  response.body == '1'
end

#directory(path) ⇒ Remote::Directory

Returns remote source directory accessor.

Returns:



32
33
34
# File 'lib/skadategems/dev/remote.rb', line 32

def directory(path)
  Directory.new(self, path, 0)
end

#exec(options = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/skadategems/dev/remote.rb', line 17

def exec(options = {}, &block)
  batch = ScriptBatch.new

  if options[:include]
    [*options[:include]].each do |include_name|
      batch.include_file ::File.join(__dir__, "remote/includes/#{include_name}.php")
    end
  end

  block.call(batch)

  @accessor.exec(batch)
end

#rootRemote::Directory

Returns remote source root directory.

Returns:



37
38
39
# File 'lib/skadategems/dev/remote.rb', line 37

def root
  @root ||= directory '/'
end

#userfilesRemote::Directory

Detect the name of the ‘userfiles` directory requesting a remote application.

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/skadategems/dev/remote.rb', line 121

def userfiles
  response = exec do |script|
    script << <<-PHPSCRIPT
require_once DIR_INTERNALS . 'system.const.php';

if (strpos(DIR_USERFILES, DIR_SITE_ROOT) !== 0) {
  header('HTTP/1.1 500 Internal Server Error');
  exit('Error: unable to detect path to `/userfiles`.');
}

echo substr(DIR_USERFILES, strlen(DIR_SITE_ROOT) - 1, -1);
    PHPSCRIPT
  end

  raise "#{response.code} [#{response.message}]:\n >> #{response.body}" if response.code != '200'

  directory(response.body)
end