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,
].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.



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

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

Instance Method Details

#archive_pathObject



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

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

#build_nameObject



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

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

#build_pathObject



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

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

#builded_manifest_pathObject



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

def builded_manifest_path
  File.join(build_path, manifest_name)
end

#builded_src_pathObject



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

def builded_src_path
  File.join(build_path, build_name)
end

#manifest_nameObject



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

def manifest_name
  "#{build_id}.yml"
end

#manifest_urlObject



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

def manifest_url
  uploaded_url(remote_manifest_file)
end

#merge(params) ⇒ Object



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

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

#remote_app_homeObject



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

def remote_app_home
  File.join(remote_app_home_base, project_name)
end

#remote_manifest_fileObject



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

def remote_manifest_file
  File.join(remote_manifest_path, manifest_name)
end

#remote_src_fileObject



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

def remote_src_file
  File.join(remote_src_path, build_name)
end

#repo_pathObject



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

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

#resetObject



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

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



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

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

#src_urlObject



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

def src_url
  uploaded_url(remote_src_file)
end

#uploaded_url(path) ⇒ Object



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

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

#working_pathObject



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

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