Class: AppBuilder::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/app_builder/config.rb

Constant Summary collapse

CHANGEABLE_PARAMETERS =
[
  :build_id,             # default: timestamp
  :project_name,         # default: repository name
  :remote_repository,    # default: remote origin
  :branch,               # default: TARGET_BRANCH or master
  :revision,             # default: commit hash
  :upload_type,          # :s3 or :http or :https (default: :s3)
  :upload_id,            # bucket name or remote host (default: none)
  :remote_app_home_base, # default: /var/www
  :logger,               # default: AppBuilder::Logger

  # hooks
  :before_archive,
  :after_archive,
  :before_build,
  :after_build,
  :before_upload,
  :after_upload,

  # source
  :remote_src_path, # default: assets

  # manifest
  :manifest_template_path, # default: lib/app_builder/template/manifest.yml.erb in this repository
  :remote_manifest_path,   # default: manifests

  # Only use when upload to S3
  :region,
  :access_key_id,
  :secret_access_key,

  # Only use when upload with scp
  :resource_host,
  :resource_user,
  :resource_ssh_options,
  :resource_document_root,
].freeze
PARAMETERS =
[
  :working_path,
  :repo_path,
  :archive_path,
  :build_path,
  :builded_src_path,
  :builded_manifest_path,
  :revision_path,
  :remote_src_file,
  :remote_manifest_file,
  :src_url,
  :manifest_url,
  :remote_app_home,
].concat(CHANGEABLE_PARAMETERS).freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



58
59
60
61
# File 'lib/app_builder/config.rb', line 58

def initialize(options = {})
  reset
  merge(options)
end

Instance Method Details

#archive_pathObject



86
87
88
# File 'lib/app_builder/config.rb', line 86

def archive_path
  File.join(working_path, "archive", build_id)
end

#build_nameObject



70
71
72
# File 'lib/app_builder/config.rb', line 70

def build_name
  "#{build_id}.tar.gz"
end

#build_pathObject



90
91
92
# File 'lib/app_builder/config.rb', line 90

def build_path
  File.join(working_path, "build", build_id)
end

#builded_manifest_pathObject



98
99
100
# File 'lib/app_builder/config.rb', line 98

def builded_manifest_path
  File.join(build_path, manifest_name)
end

#builded_src_pathObject



94
95
96
# File 'lib/app_builder/config.rb', line 94

def builded_src_path
  File.join(build_path, build_name)
end

#manifest_nameObject



74
75
76
# File 'lib/app_builder/config.rb', line 74

def manifest_name
  "#{build_id}.yml"
end

#manifest_urlObject



118
119
120
# File 'lib/app_builder/config.rb', line 118

def manifest_url
  uploaded_url(remote_manifest_file)
end

#merge(params) ⇒ Object



63
64
65
66
67
68
# File 'lib/app_builder/config.rb', line 63

def merge(params)
  params.each do |key, value|
    self.send("#{key}=", value)
  end
  self
end

#remote_app_homeObject



122
123
124
# File 'lib/app_builder/config.rb', line 122

def remote_app_home
  File.join(remote_app_home_base, project_name)
end

#remote_manifest_fileObject



110
111
112
# File 'lib/app_builder/config.rb', line 110

def remote_manifest_file
  File.join(remote_manifest_path, manifest_name)
end

#remote_src_fileObject



106
107
108
# File 'lib/app_builder/config.rb', line 106

def remote_src_file
  File.join(remote_src_path, build_name)
end

#repo_pathObject



82
83
84
# File 'lib/app_builder/config.rb', line 82

def repo_path
  File.join(working_path, "repo")
end

#resetObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/app_builder/config.rb', line 130

def reset
  @build_id               = Time.now.strftime("%Y%m%d%H%M%S")
  @project_name           = File.basename(`git rev-parse --show-toplevel`.chomp)
  @remote_repository      = `git remote get-url origin`.chomp
  @branch                 = ENV.fetch("TARGET_BRANCH", "master")
  @revision               = `git rev-parse #{branch}`.chomp
  @remote_src_path        = "assets"
  @manifest_template_path = File.expand_path("template/manifest.yml.erb", __dir__)
  @remote_manifest_path   = "manifests"
  @resource_user          = ENV.fetch("USER", nil)
  @resource_ssh_options   = {}
  @remote_app_home_base   = "/var/www"
  @logger                 = Logger.new(STDOUT)
  @upload_type            = :s3

  # for upload to S3 (from `.aws/config` and `.aws/credentials`)
  @region            = ENV.fetch("AWS_DEFAULT_REGION", aws_config("region") || "ap-northeast-1")
  @access_key_id     = ENV.fetch("AWS_ACCESS_KEY_ID", aws_credential("aws_access_key_id"))
  @secret_access_key = ENV.fetch("AWS_SECRET_ACCESS_KEY", aws_credential("aws_secret_access_key"))

  initialize_hooks
end

#revision_pathObject



102
103
104
# File 'lib/app_builder/config.rb', line 102

def revision_path
  File.join(archive_path, "revision.yml")
end

#src_urlObject



114
115
116
# File 'lib/app_builder/config.rb', line 114

def src_url
  uploaded_url(remote_src_file)
end

#uploaded_url(path) ⇒ Object



126
127
128
# File 'lib/app_builder/config.rb', line 126

def uploaded_url(path)
  "#{upload_type.to_s}://#{File.join(upload_id, path)}"
end

#working_pathObject



78
79
80
# File 'lib/app_builder/config.rb', line 78

def working_path
  File.join("/var/tmp", project_name)
end