app_builder 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 7f082bfd74839a0da79160de4b4e0d8cc76428c45edba8de0a693c315fdafc89
4
- data.tar.gz: 42854ccc07fe1e5a86f789a724e90dd8c3a88cefeed15c612f858747a898d975
2
+ SHA1:
3
+ metadata.gz: 16e6784f07c5a6ea216a05502d569ead643ef3cc
4
+ data.tar.gz: 6d2fb40d61b2e7329eb4c3a2f528feaea073a013
5
5
  SHA512:
6
- metadata.gz: 39737fe2d6a50f3d006f2fac2580db2cd0b3f95a468e62174327f0bdf1e09a4b5e57f9a33e00ea5364d9d12708d5b79e71e79bfdb982dbd577071bd6163cd4b2
7
- data.tar.gz: 897305cd932a00ff217c2b87adb1f282b829ab939fb9724549eb9ea0544c9d7545216a93b3bfb345d4d4b13dda6be37954efc10cfef76a01560482d093870c04
6
+ metadata.gz: '0865b3c4d763355ff5aa601ea8dd2b098af728efcd92c336ce2674745819e73aee94deb1d243ac67a3d6f460f41d73ca071b935fddd5f19a0d06ec1741f3374f'
7
+ data.tar.gz: 20aa8af30e89a5f56daa96d151acc1864f4995a30799b03c442ab5b6a21825785bd0386947948373889198b028df80c3cd52479e11458e1c88a3c0096f538242
data/app_builder.gemspec CHANGED
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.add_dependency "net-ssh"
24
+ spec.add_dependency "net-scp"
25
+
23
26
  spec.add_development_dependency "bundler", "~> 1.16"
24
27
  spec.add_development_dependency "rake", "~> 10.0"
25
28
  spec.add_development_dependency "minitest", "~> 5.0"
data/lib/app_builder.rb CHANGED
@@ -2,10 +2,16 @@ require "erb"
2
2
  require "forwardable"
3
3
  require "logger"
4
4
  require "open3"
5
+ require "tempfile"
5
6
  require "yaml"
6
7
 
8
+ require "net/ssh"
9
+ require "net/scp"
10
+
7
11
  require "app_builder/version"
8
12
  require "app_builder/config"
13
+ require "app_builder/logger"
14
+ require "app_builder/server.rb"
9
15
  require "app_builder/base"
10
16
  require "app_builder/archiver"
11
17
  require "app_builder/builder"
@@ -1,8 +1,8 @@
1
1
  module AppBuilder
2
2
  class Archiver < Base
3
3
  class << self
4
- def archive(config)
5
- new(config).archive
4
+ def archive(conf)
5
+ new(conf).archive
6
6
  end
7
7
  end
8
8
 
@@ -7,8 +7,8 @@ module AppBuilder
7
7
  def_delegator :config, name
8
8
  end
9
9
 
10
- def initialize(config = nil)
11
- @config = config || ::AppBuilder::Config.new
10
+ def initialize(conf = nil)
11
+ @config = conf || ::AppBuilder::Config.new
12
12
  end
13
13
 
14
14
  private
@@ -1,18 +1,25 @@
1
1
  module AppBuilder
2
2
  class Builder < Base
3
+ attr_accessor :archiver
4
+
3
5
  class << self
4
- def build(config)
5
- new(config).build
6
+ def build(conf)
7
+ new(conf).build
8
+ end
9
+ end
10
+
11
+ def initialize(conf = nil)
12
+ if conf.class <= Archiver
13
+ @archiver = conf
14
+ conf = conf.config
6
15
  end
16
+ super(conf)
17
+ @archiver ||= Archiver.new(config)
7
18
  end
8
19
 
9
20
  def build
10
21
  archiver.archive
11
22
  execute("tar zcf #{builded_src_path} .", chdir: archive_path)
12
23
  end
13
-
14
- def archiver
15
- @archiver ||= Archiver.new(config)
16
- end
17
24
  end
18
25
  end
@@ -0,0 +1,48 @@
1
+ module AppBuilder
2
+ class Logger < ::Logger
3
+ class Formatter < ::Logger::Formatter
4
+ Format = "%s\t%s\t%d\t%s\n".freeze
5
+
6
+ def call(severity, time, progname, msg)
7
+ Format % [severity, format_datetime(time), $$, msg2str(msg)]
8
+ end
9
+
10
+ private
11
+
12
+ def format_datetime(time)
13
+ time.strftime(@datetime_format || "%F %H:%M:%S.%6N".freeze)
14
+ end
15
+ end
16
+
17
+ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
18
+ progname: nil, formatter: nil, datetime_format: nil,
19
+ shift_period_suffix: '%Y%m%d')
20
+ super
21
+ @default_formatter = Formatter.new
22
+ end
23
+
24
+ def format_message(severity, datetime, progname, msg)
25
+ apply_severity_color(
26
+ severity,
27
+ (@formatter || @default_formatter).call(severity, datetime, progname, msg),
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def apply_severity_color(severity, msg)
34
+ case severity
35
+ when "DEBUG".freeze
36
+ "\033[2m#{msg}\033[m"
37
+ when "INFO".freeze
38
+ "\033[32m#{msg}\033[m"
39
+ when "WARN".freeze
40
+ "\033[33m#{msg}\033[m"
41
+ when "ERROR".freeze, "FATAL".freeze
42
+ "\033[31m#{msg}\033[m"
43
+ else
44
+ msg
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,97 @@
1
+ module AppBuilder
2
+ class Server
3
+ attr_accessor :address, :user, :options, :logger
4
+
5
+ LOCAL_ADDRESSES = %w(local 127.0.0.1).freeze
6
+
7
+ def initialize(address = nil, user: nil, options: {}, logger: nil)
8
+ @address = address
9
+ @user = user || ENV.fetch("USER", nil)
10
+ @options = options
11
+ @logger = logger
12
+ end
13
+
14
+ def execute(*cmds)
15
+ results = []
16
+
17
+ if local?
18
+ options = cmds.last.is_a?(Hash) ? cmds.pop : {}
19
+ cmds.each do |cmd|
20
+ message = "Execute command [local]: #{cmd}"
21
+ message += " (with: #{options.inspect})" unless options.empty?
22
+ log(:info, message)
23
+
24
+ stdout, stderr, status = Open3.capture3(cmd, **options)
25
+ log(:error, "Failed [#{status.exitstatus}]: #{stderr}") unless status.success?
26
+ results << stdout
27
+ end
28
+ else
29
+ ssh_start do |ssh|
30
+ cmds.each do |cmd|
31
+ log(:info, "Execute command [#{address}]: #{cmd}")
32
+ results << ssh.exec!(cmd).chomp
33
+ end
34
+ end
35
+ end
36
+
37
+ results
38
+ end
39
+
40
+ def upload(src_path, dest_path)
41
+ erb = File.extname(src_path) == ".erb".freeze
42
+ if local?
43
+ if erb
44
+ log(:info, "Create #{dest_path} from #{src_path}")
45
+ File.open(dest_path, "w") { |f| f.write(ERB.new(File.read(src_path)).result) }
46
+ else
47
+ execute("cp #{src_path} #{dest_path}")
48
+ end
49
+ else
50
+ ssh_start do |ssh|
51
+ log(:info, "Upload: local:#{src_path} to #{address}:#{dest_path}")
52
+ if erb
53
+ begin
54
+ f = Tempfile.open(File.basename(dest_path))
55
+ f.write(ERB.new(File.read(src_path)).result)
56
+ f.close
57
+ ssh.scp.upload!(f.path, dest_path)
58
+ rescue
59
+ f.unlink
60
+ end
61
+ else
62
+ ssh.scp.upload!(src_path, dest_path)
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def ssh_start
69
+ raise ArgumentError unless block_given?
70
+ Net::SSH.start(address, ssh_user, ssh_options) { |ssh| yield ssh }
71
+ end
72
+
73
+ def local?
74
+ address.nil? || LOCAL_ADDRESSES.include?(address.to_s)
75
+ end
76
+
77
+ private
78
+
79
+ def log(level, message)
80
+ return unless logger
81
+ logger.send(level, message)
82
+ end
83
+
84
+ def ssh_options
85
+ return default_ssh_options if options.nil? || options.empty?
86
+ default_ssh_options.merge(options)
87
+ end
88
+
89
+ def ssh_user
90
+ user || ENV["USER"]
91
+ end
92
+
93
+ def default_ssh_options
94
+ { port: 22, keys: File.expand_path("~/.ssh/id_rsa"), forward_agent: true }
95
+ end
96
+ end
97
+ end
@@ -1,9 +1,23 @@
1
1
  module AppBuilder
2
2
  class Uploader < Base
3
+ attr_accessor :builder
4
+
3
5
  class << self
4
- def upload(config)
5
- new(config).upload
6
+ def upload(conf)
7
+ new(conf).upload
8
+ end
9
+ end
10
+
11
+ def initialize(conf = nil)
12
+ case conf
13
+ when AppBuilder::Archiver
14
+ @builder = Builder.new(conf)
15
+ conf = conf.config
16
+ when AppBuilder::Builder
17
+ @builder = conf
18
+ conf = conf.config
6
19
  end
20
+ super(conf)
7
21
  end
8
22
 
9
23
  def upload
@@ -41,9 +55,5 @@ module AppBuilder
41
55
  def s3?(url)
42
56
  url.to_s.start_with?("s3://")
43
57
  end
44
-
45
- def builder
46
- @builder || Builder.new(config)
47
- end
48
58
  end
49
59
  end
@@ -1,3 +1,3 @@
1
1
  module AppBuilder
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-09 00:00:00.000000000 Z
11
+ date: 2018-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-scp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +115,8 @@ files:
87
115
  - lib/app_builder/base.rb
88
116
  - lib/app_builder/builder.rb
89
117
  - lib/app_builder/config.rb
118
+ - lib/app_builder/logger.rb
119
+ - lib/app_builder/server.rb
90
120
  - lib/app_builder/template/manifest.yml.erb
91
121
  - lib/app_builder/uploader.rb
92
122
  - lib/app_builder/version.rb
@@ -109,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
139
  version: '0'
110
140
  requirements: []
111
141
  rubyforge_project:
112
- rubygems_version: 2.7.6
142
+ rubygems_version: 2.6.11
113
143
  signing_key:
114
144
  specification_version: 4
115
145
  summary: Application build utilities.