Class: AppBuilder::Server
- Inherits:
-
Object
- Object
- AppBuilder::Server
- Defined in:
- lib/app_builder/server.rb
Constant Summary collapse
- LOCAL_ADDRESSES =
%w(local localhost 127.0.0.1).freeze
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
Returns the value of attribute options.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #execute(*cmds) ⇒ Object
-
#initialize(address = nil, user: nil, options: {}, logger: nil) ⇒ Server
constructor
A new instance of Server.
- #local? ⇒ Boolean
- #ssh_start ⇒ Object
- #upload(src_path, dest_path) ⇒ Object
Constructor Details
#initialize(address = nil, user: nil, options: {}, logger: nil) ⇒ Server
Returns a new instance of Server.
7 8 9 10 11 12 |
# File 'lib/app_builder/server.rb', line 7 def initialize(address = nil, user: nil, options: {}, logger: nil) @address = address @user = user || ENV.fetch("USER", nil) = @logger = logger end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
3 4 5 |
# File 'lib/app_builder/server.rb', line 3 def address @address end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/app_builder/server.rb', line 3 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/app_builder/server.rb', line 3 def end |
#user ⇒ Object
Returns the value of attribute user.
3 4 5 |
# File 'lib/app_builder/server.rb', line 3 def user @user end |
Instance Method Details
#execute(*cmds) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/app_builder/server.rb', line 14 def execute(*cmds) results = [] = cmds.last.is_a?(Hash) ? cmds.pop : {} if local? cmds.each do |cmd| = "Execute command [local]: #{cmd}" += " (with: #{options.inspect})" unless .empty? log(:info, ) stdout, stderr, status = Open3.capture3(cmd, **) log(:error, "Failed [#{status.exitstatus}]: #{stderr}") unless status.success? results << stdout end else ssh_start do |ssh| cmds.each do |cmd| cmd = "cd #{options[:chdir]}; #{cmd}" if .has_key?(:chdir) log(:info, "Execute command [#{address}]: #{cmd}") results << ssh.exec!(cmd).chomp end end end results end |
#local? ⇒ Boolean
74 75 76 |
# File 'lib/app_builder/server.rb', line 74 def local? address.nil? || LOCAL_ADDRESSES.include?(address.to_s) end |
#ssh_start ⇒ Object
69 70 71 72 |
# File 'lib/app_builder/server.rb', line 69 def ssh_start raise ArgumentError unless block_given? Net::SSH.start(address, ssh_user, ) { |ssh| yield ssh } end |
#upload(src_path, dest_path) ⇒ Object
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 |
# File 'lib/app_builder/server.rb', line 41 def upload(src_path, dest_path) erb = File.extname(src_path) == ".erb".freeze if local? if erb log(:info, "Create #{dest_path} from #{src_path}") File.open(dest_path, "w") { |f| f.write(ERB.new(File.read(src_path), nil, "-").result) } else execute("cp #{src_path} #{dest_path}") end else ssh_start do |ssh| log(:info, "Upload: local:#{src_path} to #{address}:#{dest_path}") if erb begin f = Tempfile.open(File.basename(dest_path)) f.write(ERB.new(File.read(src_path), nil, "-").result) f.close ssh.scp.upload!(f.path, dest_path) rescue f.unlink end else ssh.scp.upload!(src_path, dest_path) end end end end |