app_builder 0.1.3 → 0.1.4

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
- SHA1:
3
- metadata.gz: 16e6784f07c5a6ea216a05502d569ead643ef3cc
4
- data.tar.gz: 6d2fb40d61b2e7329eb4c3a2f528feaea073a013
2
+ SHA256:
3
+ metadata.gz: 17ce5dd23729388a0087da20a784ff3b816f8c57fba31ca57f3e721e1a7ffdcf
4
+ data.tar.gz: a88df3a875b6a397cc1c1035628bea75ae59712130f6b952c57a173408c92387
5
5
  SHA512:
6
- metadata.gz: '0865b3c4d763355ff5aa601ea8dd2b098af728efcd92c336ce2674745819e73aee94deb1d243ac67a3d6f460f41d73ca071b935fddd5f19a0d06ec1741f3374f'
7
- data.tar.gz: 20aa8af30e89a5f56daa96d151acc1864f4995a30799b03c442ab5b6a21825785bd0386947948373889198b028df80c3cd52479e11458e1c88a3c0096f538242
6
+ metadata.gz: 7bfc464fb1677988dc93858de377d3497fdf4cb470e6adfbf4ca20ba8d0586100f162e70c4b28579a35280215b9cf5f78cfdd96c6c3a4c584dbecdecf02bd121
7
+ data.tar.gz: 211e70acaf02d62d8e4077f158f2952b57a89a3e6a4d66fa10e9f0ab763d6bc18b522442c65bef511dc6d5c1e3365764b20b5076369303352fd8738953fde193
data/app_builder.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_dependency "net-ssh"
24
24
  spec.add_dependency "net-scp"
25
+ spec.add_dependency "aws-sdk-s3"
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.16"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
data/lib/app_builder.rb CHANGED
@@ -7,6 +7,7 @@ require "yaml"
7
7
 
8
8
  require "net/ssh"
9
9
  require "net/scp"
10
+ require "aws-sdk-s3"
10
11
 
11
12
  require "app_builder/version"
12
13
  require "app_builder/config"
@@ -3,25 +3,26 @@ module AppBuilder
3
3
  extend Forwardable
4
4
 
5
5
  attr_accessor :config
6
- ::AppBuilder::Config::PARAMETERS.each do |name|
6
+ Config::PARAMETERS.each do |name|
7
7
  def_delegator :config, name
8
8
  end
9
9
 
10
10
  def initialize(conf = nil)
11
- @config = conf || ::AppBuilder::Config.new
11
+ @config = conf || Config.new
12
12
  end
13
13
 
14
14
  private
15
15
 
16
+ def log(level, message)
17
+ logger&.send(level, message)
18
+ end
19
+
16
20
  def execute(cmd, options = {})
17
- log(:info, "Execute command [local]: #{cmd}")
18
- stdout, stderr, status = Open3.capture3(cmd, **options)
19
- log(:error, "Failed [#{status.exitstatus}]: #{stderr}") unless status.success?
20
- stdout.chomp
21
+ build_server.execute(cmd, options).first
21
22
  end
22
23
 
23
- def log(level, message)
24
- logger&.send(level, message)
24
+ def build_server
25
+ @build_server ||= Server.new(:localhost, logger: logger)
25
26
  end
26
27
  end
27
28
  end
@@ -1,20 +1,38 @@
1
1
  module AppBuilder
2
2
  class Config
3
- VALID_OPTIONS = [
3
+ CHANGEABLE_PARAMETERS = [
4
4
  :build_id,
5
5
  :project_name,
6
6
  :remote_repository,
7
7
  :branch,
8
8
  :revision,
9
- :src_base_url,
10
- :manifest_base_url,
9
+ :upload_type,
10
+ :upload_id, # bucket name or remote host
11
+ :logger,
12
+
13
+ # source
14
+ :builded_src_ext,
11
15
  :remote_src_path,
12
- :remote_manifest_path,
16
+
17
+ # manifest
13
18
  :manifest_template_path,
19
+ :remote_manifest_path,
20
+ :manifest_ext,
21
+
22
+ # Only use when upload to S3
23
+ :region,
24
+ :access_key_id,
25
+ :secret_access_key,
26
+
27
+ # Only use when upload with scp
14
28
  :resource_host,
15
- :ssh_user,
16
- :identity_file,
17
- :logger,
29
+ :resource_user,
30
+ :resource_ssh_options,
31
+
32
+ # Only use when remote build
33
+ # :build_host,
34
+ # :build_user,
35
+ # :build_ssh_options,
18
36
  ].freeze
19
37
 
20
38
  PARAMETERS = [
@@ -25,12 +43,13 @@ module AppBuilder
25
43
  :builded_src_path,
26
44
  :builded_manifest_path,
27
45
  :revision_path,
46
+ :remote_src_file,
47
+ :remote_manifest_file,
28
48
  :src_url,
29
- :manifest_url,
30
49
  :remote_app_home,
31
- ].concat(VALID_OPTIONS).freeze
50
+ ].concat(CHANGEABLE_PARAMETERS).freeze
32
51
 
33
- attr_accessor *VALID_OPTIONS
52
+ attr_accessor *CHANGEABLE_PARAMETERS
34
53
 
35
54
  def initialize(options = {})
36
55
  reset
@@ -45,11 +64,11 @@ module AppBuilder
45
64
  end
46
65
 
47
66
  def build_name
48
- "#{build_id}.tar.gz"
67
+ [build_id, builded_src_ext].join(".")
49
68
  end
50
69
 
51
70
  def manifest_name
52
- "#{build_id}.yml"
71
+ [build_id, manifest_ext].join(".")
53
72
  end
54
73
 
55
74
  def working_path
@@ -80,12 +99,16 @@ module AppBuilder
80
99
  File.join(archive_path, "revision.yml")
81
100
  end
82
101
 
83
- def src_url
84
- File.join(src_base_url, build_name)
102
+ def remote_src_file
103
+ File.join(remote_src_path, build_name)
85
104
  end
86
105
 
87
- def manifest_url
88
- File.join(manifest_base_url, manifest_name)
106
+ def remote_manifest_file
107
+ File.join(remote_manifest_path, manifest_name)
108
+ end
109
+
110
+ def src_url
111
+ "#{upload_type.to_s}://#{File.join(upload_id, remote_src_path, build_name)}"
89
112
  end
90
113
 
91
114
  def remote_app_home
@@ -93,15 +116,49 @@ module AppBuilder
93
116
  end
94
117
 
95
118
  def reset
119
+ @build_host = "localhost"
120
+ @build_user = ENV.fetch("USER", nil)
121
+ @build_ssh_options = {}
96
122
  @build_id = Time.now.strftime("%Y%m%d%H%M%S")
97
123
  @project_name = File.basename(`git rev-parse --show-toplevel`.chomp)
98
124
  @remote_repository = `git remote get-url origin`.chomp
99
125
  @branch = ENV.fetch("TARGET_BRANCH", "master")
100
126
  @revision = `git rev-parse #{branch}`.chomp
127
+ @builded_src_ext = "tar.gz"
128
+ @manifest_ext = "yml"
101
129
  @manifest_template_path = File.expand_path("template/manifest.yml.erb", __dir__)
102
- @ssh_user = ENV.fetch("USER", nil)
103
- @identity_file = "~/.ssh/id_rsa"
130
+ @resource_user = @build_user
131
+ @resource_ssh_options = {}
104
132
  @logger = Logger.new(STDOUT)
133
+ @upload_type = :s3
134
+
135
+ # for upload to S3
136
+ @region = ENV.fetch("AWS_DEFAULT_REGION", aws_config("region") || "ap-northeast-1")
137
+ @access_key_id = ENV.fetch("AWS_ACCESS_KEY_ID", aws_credential("aws_access_key_id"))
138
+ @secret_access_key = ENV.fetch("AWS_SECRET_ACCESS_KEY", aws_credential("aws_secret_access_key"))
105
139
  end
140
+
141
+ private
142
+
143
+ def aws_config(key)
144
+ find_aws_setting_by(
145
+ ENV.fetch("AWS_CONFIG_FILE", File.expand_path("~/.aws/config")),
146
+ key,
147
+ )
148
+ end
149
+
150
+ def aws_credential(key)
151
+ find_aws_setting_by(
152
+ ENV.fetch("AWS_CREDENTIAL_FILE", File.expand_path("~/.aws/credentials")),
153
+ key,
154
+ )
155
+ end
156
+
157
+ def find_aws_setting_by(path, key)
158
+ return nil unless File.exist?(path)
159
+ File.readlines(path).detect { |line|
160
+ line.start_with?(/\A\s*#{key}/)
161
+ }&.split("=")&.last&.strip
162
+ end
106
163
  end
107
164
  end
@@ -2,7 +2,7 @@ module AppBuilder
2
2
  class Server
3
3
  attr_accessor :address, :user, :options, :logger
4
4
 
5
- LOCAL_ADDRESSES = %w(local 127.0.0.1).freeze
5
+ LOCAL_ADDRESSES = %w(local localhost 127.0.0.1).freeze
6
6
 
7
7
  def initialize(address = nil, user: nil, options: {}, logger: nil)
8
8
  @address = address
@@ -14,8 +14,8 @@ module AppBuilder
14
14
  def execute(*cmds)
15
15
  results = []
16
16
 
17
+ options = cmds.last.is_a?(Hash) ? cmds.pop : {}
17
18
  if local?
18
- options = cmds.last.is_a?(Hash) ? cmds.pop : {}
19
19
  cmds.each do |cmd|
20
20
  message = "Execute command [local]: #{cmd}"
21
21
  message += " (with: #{options.inspect})" unless options.empty?
@@ -28,6 +28,7 @@ module AppBuilder
28
28
  else
29
29
  ssh_start do |ssh|
30
30
  cmds.each do |cmd|
31
+ cmd = "cd #{options[:chdir]}; #{cmd}" if options.has_key?(:chdir)
31
32
  log(:info, "Execute command [#{address}]: #{cmd}")
32
33
  results << ssh.exec!(cmd).chomp
33
34
  end
@@ -77,8 +78,7 @@ module AppBuilder
77
78
  private
78
79
 
79
80
  def log(level, message)
80
- return unless logger
81
- logger.send(level, message)
81
+ logger&.send(level, message)
82
82
  end
83
83
 
84
84
  def ssh_options
@@ -10,38 +10,37 @@ module AppBuilder
10
10
 
11
11
  def initialize(conf = nil)
12
12
  case conf
13
- when AppBuilder::Archiver
13
+ when Archiver
14
14
  @builder = Builder.new(conf)
15
15
  conf = conf.config
16
- when AppBuilder::Builder
16
+ when Builder
17
17
  @builder = conf
18
18
  conf = conf.config
19
19
  end
20
20
  super(conf)
21
+ @builder ||= Builder.new(config)
21
22
  end
22
23
 
23
24
  def upload
25
+ upload_proc = s3? ? method(:upload_to_s3) : method(:upload_to_server)
24
26
  builder.build
25
- if s3?(src_url)
26
- upload_to_s3(builded_src_path, src_url)
27
- else
28
- upload_to_server(builded_src_path, remote_src_path)
29
- end
30
-
27
+ upload_proc.call(builded_src_path, remote_src_file)
31
28
  generate_manifest
32
- if s3?(manifest_url)
33
- upload_to_s3(builded_manifest_path, manifest_url)
34
- else
35
- upload_to_server(builded_manifest_path, remote_manifest_path)
36
- end
29
+ upload_proc.call(builded_manifest_path, remote_manifest_file)
37
30
  end
38
31
 
39
32
  def upload_to_s3(local, remote)
40
- execute("aws s3 cp #{local} #{remote}")
33
+ log(:info, "Upload #{local} to #{src_url}")
34
+ s3_client.put_object(
35
+ bucket: upload_id,
36
+ key: remote,
37
+ body: File.open(local),
38
+ )
41
39
  end
42
40
 
43
41
  def upload_to_server(local, remote)
44
- execute("scp -i #{identity_file} #{local} #{ssh_user}@#{resource_host}:#{remote}")
42
+ log(:info, "Upload #{local} to #{src_url}")
43
+ resource_server.upload(local, remote)
45
44
  end
46
45
 
47
46
  def generate_manifest
@@ -52,8 +51,25 @@ module AppBuilder
52
51
 
53
52
  private
54
53
 
55
- def s3?(url)
56
- url.to_s.start_with?("s3://")
54
+ def s3?
55
+ upload_type == :s3
56
+ end
57
+
58
+ def s3_client
59
+ @s3_client ||= Aws::S3::Client.new(
60
+ region: region,
61
+ access_key_id: access_key_id,
62
+ secret_access_key: secret_access_key
63
+ )
64
+ end
65
+
66
+ def resource_server
67
+ @resource_server ||= Server.new(
68
+ resource_host,
69
+ user: resource_user,
70
+ options: resource_ssh_options,
71
+ logger: logger
72
+ )
57
73
  end
58
74
  end
59
75
  end
@@ -1,3 +1,3 @@
1
1
  module AppBuilder
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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-10 00:00:00.000000000 Z
11
+ date: 2018-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-s3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -139,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
153
  version: '0'
140
154
  requirements: []
141
155
  rubyforge_project:
142
- rubygems_version: 2.6.11
156
+ rubygems_version: 2.7.6
143
157
  signing_key:
144
158
  specification_version: 4
145
159
  summary: Application build utilities.