Class: Optimacms::Appdata::Service
- Inherits:
-
Object
- Object
- Optimacms::Appdata::Service
- Defined in:
- lib/optimacms/appdata/service.rb
Class Method Summary collapse
-
.build_git_cmd(storage, cmd) ⇒ Object
helpers.
-
.content_setup_git(_env, content_name) ⇒ Object
git helpers.
- .git_commit_push(_env, content_name) ⇒ Object
- .git_pull(_env, content_name) ⇒ Object
- .run_rake_task(cmd) ⇒ Object
- .save(_env, content_name) ⇒ Object
- .save_by_git(_env, content_name) ⇒ Object
-
.save_by_local(_env, content_name) ⇒ Object
local storage.
-
.save_by_ssh(_env, content_name) ⇒ Object
ssh.
- .setup(_env, content_name) ⇒ Object
- .setup_git_storage(storage) ⇒ Object
- .setup_storage(content) ⇒ Object
- .update(_env, content_name) ⇒ Object
- .update_by_git(_env, content_name) ⇒ Object
- .update_by_local(_env, content_name) ⇒ Object
- .update_by_ssh(_env, content_name) ⇒ Object
Class Method Details
.build_git_cmd(storage, cmd) ⇒ Object
helpers
422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/optimacms/appdata/service.rb', line 422 def self.build_git_cmd(storage, cmd) key_path = storage['remote_repo_ssh_key'] #ssh if key_path && key_path!='' res= %Q(GIT_SSH_COMMAND='ssh -i #{key_path} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' #{cmd}) else res = %Q(#{cmd}) end res end |
.content_setup_git(_env, content_name) ⇒ Object
git helpers
349 350 351 352 353 354 355 356 357 |
# File 'lib/optimacms/appdata/service.rb', line 349 def self.content_setup_git(_env, content_name) # input content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # setup_git_storage(storage) end |
.git_commit_push(_env, content_name) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/optimacms/appdata/service.rb', line 397 def self.git_commit_push(_env, content_name) # input content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # repo_local_path = Optimacms::Appdata::Settings.storage_repo_local_path(storage) # commit & push to remote repo %x[cd #{repo_local_path} && git add . && git commit -m "server changes #{Time.now.utc}" ] rescue true # git_cmd = build_git_cmd(storage, 'git push origin master') %x[cd #{repo_local_path} && #{git_cmd}] rescue true #git add -A . end |
.git_pull(_env, content_name) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/optimacms/appdata/service.rb', line 376 def self.git_pull(_env, content_name) # input content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # init repo #Rake::Task["appdata:repo:setup"].invoke # repo_local_path = Optimacms::Appdata::Settings.storage_repo_local_path(storage) git_cmd = build_git_cmd(storage, 'git pull origin master') cmd = %Q(cd #{repo_local_path} && #{git_cmd}) puts "#{cmd}" %x[#{cmd}] end |
.run_rake_task(cmd) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/optimacms/appdata/service.rb', line 437 def self.run_rake_task(cmd) cmd = %Q(#{cmd}) puts "cmd: #{cmd}" res_output = %x[#{cmd}] exit_code = $?.exitstatus #output = "exit_code = #{exit_code}; message: #{res_output}" # res {res: (exit_code==0), output: res_output, exit_code: exit_code} end |
.save(_env, content_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/optimacms/appdata/service.rb', line 15 def self.save(_env, content_name) # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # if storage['type']=='git' return save_by_git(_env, content_name) elsif storage['type']=='ssh' return save_by_ssh(_env, content_name) elsif storage['type']=='local' return save_by_local(_env, content_name) end return nil end |
.save_by_git(_env, content_name) ⇒ Object
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/optimacms/appdata/service.rb', line 238 def self.save_by_git(_env, content_name) res_output =[] # input content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # setup setup_git_storage storage # update repo first git_pull(_env, content_name) # repo_local_path = Optimacms::Appdata::Settings.storage_repo_local_path(storage) # rsync to repo-data #rsync -Lavrt --exclude-from '../{{server}}/files/rsync_exclude_list.txt' -e 'ssh -p {{ansible_ssh_port | default(22)}}' {{root_user}}@{{inventory_hostname | quote}}:{{remote_path | quote}} {{backup_dir | quote }} #rsync -Lavrt --exclude-from '../{{server}}/files/rsync_exclude_list.txt' {{root_user}}@{{inventory_hostname}}:{{remote_path}} {{backup_dir}} # ok #execute "rsync -Lavrt --exclude-from '#{release_path}/.rsync_ignore' #{release_path}/ #{p}" # ok - app #execute "rsync -Lavrt --exclude-from '#{release_path}/.rsync_ignore' #{release_path}/app/ #{p}/app" #%x[rsync -Lavrt --exclude-from '#{d}/.rsync_ignore' #{d}/ #{p}/ ] # check if rsync available output = %x(which rsync) res_rsync = output.strip.delete(" \t\r\n") content['dirs'].each do |d| d_from = File.join(Rails.root, d) d_to = File.join(repo_local_path, d) d_to_base = File.dirname(d_to) #puts "copy from #{d_from} to #{d_to}" FileUtils.mkdir_p d_to # rsync or copy if res_rsync!="" cmd = %Q(rsync -Lavrt #{d_from}/ #{d_to}/) #puts "#{cmd}" %x(#{cmd}) else # no rsync #puts "no rsync. copying..." FileUtils.cp_r d_from, d_to_base end end # repo git_commit_push(_env, content_name) # {res: true, output: res_output.join("; ")} rescue => e {res: false, output: "#{e.}, #{e.backtrace.join(",")}"} end |
.save_by_local(_env, content_name) ⇒ Object
local storage
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 |
# File 'lib/optimacms/appdata/service.rb', line 67 def self.save_by_local(_env, content_name) # res_output = [] # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # sync dirs content['dirs'].each do |d| d_remote = File.join(storage['path'], d) d_local = File.join(File.dirname(d), File.basename(d))+"/" d_local_full = File.join(Rails.root, d_local) # create remote dir %x[mkdir -p #{d_remote}] # rsync cmd = %Q(rsync -Lavrt #{d_local_full} #{d_remote} --delete) puts "#{cmd}" %x[#{cmd}] end # {res: true, output: res_output.join("; ")} rescue => e {res: false, output: "#{e.}, #{e.backtrace.join(",")}"} end |
.save_by_ssh(_env, content_name) ⇒ Object
ssh
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 |
# File 'lib/optimacms/appdata/service.rb', line 136 def self.save_by_ssh(_env, content_name) # res_output = [] # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # cmd = SshCommand.new = { port: storage['ssh_port'], user: storage['ssh_user'], password: storage['ssh_password'], key: storage['ssh_key'] } #s_ssh = " #{storage['ssh_user']}@#{storage['host']}" #ssh_opts = "-p #{storage['ssh_port']||22} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" #if storage['ssh_key'] && storage['ssh_key']!='' # ssh_opts << " -i #{storage['ssh_key']}" #end # sync dirs content['dirs'].each do |d| d_remote = File.join(storage['path'], d) d_local = File.join(File.dirname(d), File.basename(d))+"/" d_local_full = File.join(Rails.root, d_local) # create remote dir #%x[ssh -t #{ssh_opts} #{s_ssh} mkdir -p #{d_remote}] cmd_create = %Q(mkdir -p #{d_remote}) res_cmd_create = cmd.run_ssh_cmd(storage['host'], , cmd_create) if res_cmd_create[:res]!=1 raise "Cannot create dir: #{res_cmd_create[:error]}" end # rsync cmd_rsync = RsyncCommand.build_cmd_with_ssh_save(storage, d_local_full, d_remote) #puts "#{cmd_rsync}" output = %x[#{cmd_rsync}] end # {res: true, output: res_output.join("; ")} rescue => e {res: false, output: "#{e.}, #{e.backtrace.join(",")}"} end |
.setup(_env, content_name) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/optimacms/appdata/service.rb', line 5 def self.setup(_env, content_name) # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) # storage setup_storage content end |
.setup_git_storage(storage) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/optimacms/appdata/service.rb', line 359 def self.setup_git_storage(storage) repo_local_path = Optimacms::Appdata::Settings.storage_repo_local_path(storage) # FileUtils.mkdir_p(repo_local_path) unless File.directory?(repo_local_path) # init local git repo repo_url = storage['remote_repo'] # run commands %x[cd #{repo_local_path} && git init ] %x[cd #{repo_local_path} && git remote add origin #{repo_url} ] rescue nil %x[cd #{repo_local_path} && git remote set-url origin #{repo_url} ] end |
.setup_storage(content) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/optimacms/appdata/service.rb', line 52 def self.setup_storage(content) # storage storage = content['storage'] if storage['type']=='git' return setup_git_storage(storage) elsif storage['type']=='ssh' elsif storage['type']=='local' end return nil end |
.update(_env, content_name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/optimacms/appdata/service.rb', line 32 def self.update(_env, content_name) # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # if storage['type']=='git' return update_by_git(_env, content_name) elsif storage['type']=='ssh' return update_by_ssh(_env, content_name) elsif storage['type']=='local' return update_by_local(_env, content_name) end #Optimacms::Appdata::Service.run_rake_task("rake appdata:update 2>&1") end |
.update_by_git(_env, content_name) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/optimacms/appdata/service.rb', line 308 def self.update_by_git(_env, content_name) res_output = [] # input content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # update repo first git_pull _env, content_name # copy to project repo_local_path = Optimacms::Appdata::Settings.storage_repo_local_path(storage) content['dirs'].each do |d| d_from = File.join(repo_local_path, d) d_to = File.join(Rails.root, d) d_to_base = File.dirname(d_to) puts "copy from #{d_from} to #{d_to}" #in #{d_to_parent} FileUtils.mkdir_p d_to # copy dir FileUtils.cp_r d_from, d_to_base #FileUtils.copy_entry d_from, d_to_parent #FileUtils.cp_r d_from, Rails.root end # {res: true, output: res_output.join("; ")} rescue => e {res: false, output: "#{e.}, #{e.backtrace.join(",")}"} end |
.update_by_local(_env, content_name) ⇒ Object
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 |
# File 'lib/optimacms/appdata/service.rb', line 101 def self.update_by_local(_env, content_name) # res_output = [] # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # sync dirs content['dirs'].each do |d| d_remote = File.join(storage['path'], d)+"/" d_local = File.join(File.dirname(d), File.basename(d))+"/" d_local_full = File.join(Rails.root, d_local) # %x[mkdir -p #{d_local_full}] # rsync cmd = %Q(rsync -Lavrt #{d_remote} #{d_local_full} --delete) puts "#{cmd}" %x[#{cmd}] end # {res: true, output: res_output.join("; ")} rescue => e {res: false, output: "#{e.}, #{e.backtrace.join(",")}"} end |
.update_by_ssh(_env, content_name) ⇒ Object
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 |
# File 'lib/optimacms/appdata/service.rb', line 192 def self.update_by_ssh(_env, content_name) # res_output = [] # content = Optimacms::Appdata::Settings.get_content_info(_env, content_name) storage = content['storage'] # #s_ssh = " #{storage['ssh_user']}@#{storage['host']}" #ssh_opts = "-p #{storage['ssh_port']||22} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" #if storage['ssh_key'] && storage['ssh_key']!='' # ssh_opts << " -i #{storage['ssh_key']}" #end # sync dirs content['dirs'].each do |d| d_remote = File.join(storage['path'], d)+"/" d_local = File.join(File.dirname(d), File.basename(d))+"/" d_local_full = File.join(Rails.root, d_local) # create local dir %x[mkdir -p #{d_local_full}] # rsync cmd_rsync = RsyncCommand.build_cmd_with_ssh_update(storage, d_local_full, d_remote) #puts "#{cmd_rsync}" output = %x[#{cmd_rsync}] #cmd = %Q(rsync -Lavrt -e "ssh #{ssh_opts}" #{s_ssh}:#{d_remote} #{d_local_full} --delete) end # {res: true, output: res_output.join("; ")} rescue => e {res: false, output: "#{e.}, #{e.backtrace.join(",")}"} end |