Module: EcbSharedLib
- Defined in:
- lib/ebmsharedlib/options.rb,
lib/ebmsharedlib/utilities.rb
Overview
Greg Seitz
Copyright 2013, eBay Inc.
All rights reserved.
http://www.ebay.com
Defined Under Namespace
Constant Summary collapse
- ROOT_PATH =
File.("~/.ecb")
- CONFIG_DIR =
"buildsys_configs"- CONFIG_SUFFIX =
".config"- SETTINGS_FILE =
".ecb-settings.json"- REPO_COMMAND_DETAILS =
"Remote git repository for initial configs file download [mobi,corp,stash,<git url>]"- CONFIG_REPOS =
the key is the shortcut used for the config repos
{ "mobi" => "[email protected]:eBayMobile/buildsys_config.git", }
Class Method Summary collapse
-
.base_config_path(repo_url) ⇒ Object
return the base config path minus the git dir for the given url.
-
.config_name_from_dir ⇒ Object
expects us to be in the top level dir that has the same name as the config.
-
.full_config_path(repo_url) ⇒ Object
the full config path.
-
.get_config_from_top_dir(err_msg = nil) ⇒ Object
read the repo_config file for the prepared directory by first fetching the settings from this dir.
- .get_config_repo_url(config_repo) ⇒ Object
-
.get_current_branch(repo, repo_path) ⇒ Object
get the current banch.
- .get_repo_name(git_path) ⇒ Object
-
.path_to_provision_files(repo_url, config_name) ⇒ Object
the full config path.
-
.path_to_scripts(repo_url) ⇒ Object
the full config path.
-
.prepare_config_repo(config_repo_url) ⇒ Object
takes the repo name (shortcut or full url) and fetches config into appropriate location returns the full repo_url.
- .printer ⇒ Object
-
.read_build_config(repo_url, err_msg = nil) ⇒ Object
read and return the config info for this repo.
-
.read_json_file(file_path, err_msg = nil) ⇒ Object
read and parse a json file.
-
.read_repo_config(repo_url, config_name, err_msg = nil) ⇒ Object
read and return the config info for this repo.
-
.read_settings(err_msg = nil) ⇒ Object
read top level settings within a prepared dir lets us get to the appropriate config file.
-
.repo_url_hash(repo_url) ⇒ Object
compute a unique hash for this repo so we can store it in a subdir of configs.
-
.write_json_file(map, file_path, err_msg = nil) ⇒ Object
write the map as json into the specified file.
-
.write_settings(map, dir, err_msg = nil) ⇒ Object
write the prepared settings, expects us to pass dir to write into.
Class Method Details
.base_config_path(repo_url) ⇒ Object
return the base config path minus the git dir for the given url
104 105 106 107 108 |
# File 'lib/ebmsharedlib/utilities.rb', line 104 def self.base_config_path(repo_url) config_hash = repo_url_hash(repo_url) "#{ROOT_PATH}/repo_configs/#{config_hash}" end |
.config_name_from_dir ⇒ Object
expects us to be in the top level dir that has the same name as the config. Such as /Users/gseitz/Develop/ebay/iphone_3.1 will extract the config_name of iphone_3.1
208 209 210 |
# File 'lib/ebmsharedlib/utilities.rb', line 208 def self.config_name_from_dir config_name = "#{Dir.pwd}".split("/").last end |
.full_config_path(repo_url) ⇒ Object
the full config path
111 112 113 |
# File 'lib/ebmsharedlib/utilities.rb', line 111 def self.full_config_path(repo_url) "#{base_config_path(repo_url)}/#{CONFIG_DIR}" end |
.get_config_from_top_dir(err_msg = nil) ⇒ Object
read the repo_config file for the prepared directory by first fetching the settings from this dir
234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/ebmsharedlib/utilities.rb', line 234 def self.get_config_from_top_dir(err_msg = nil) begin # for now we still operate without settings by using defaults # this should be removed once everyone is on the new tool settings = read_settings(".ecb-settings not found, make sure you are in the top level directory.") repo_url = settings[:config_repo_url] config_name = settings[:config_name] rescue # use defaults since failed to load settings config_name = config_name_from_dir() repo_url = get_config_repo_url(nil) end read_repo_config(repo_url, config_name, "Config not found, make sure you are in the top level directory.") end |
.get_config_repo_url(config_repo) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/ebmsharedlib/utilities.rb', line 86 def self.get_config_repo_url(config_repo) # convert to lowercase for comparison below if non nil repo_alias = config_repo.nil? ? "default" : config_repo.downcase # loop up matching repo found_repo = CONFIG_REPOS[repo_alias] # if no match use the passed in repo config_repo = found_repo.nil? ? config_repo : found_repo end |
.get_current_branch(repo, repo_path) ⇒ Object
get the current banch
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/ebmsharedlib/utilities.rb', line 221 def self.get_current_branch(repo, repo_path) repo_name = EcbSharedLib.get_repo_name(repo[:git_path]) cmd = "git symbolic-ref HEAD" result = EcbSharedLib::CL.do_cmd_output(cmd, repo_path) if $?.exitstatus != 0 raise "Unable to get the current branch for #{repo_name}, you may be on a detached HEAD." end branch = result.rstrip.split("/").last end |
.get_repo_name(git_path) ⇒ Object
212 213 214 |
# File 'lib/ebmsharedlib/utilities.rb', line 212 def self.get_repo_name(git_path) repo_name = git_path.split("/").last.split(".git")[0] end |
.path_to_provision_files(repo_url, config_name) ⇒ Object
the full config path
121 122 123 |
# File 'lib/ebmsharedlib/utilities.rb', line 121 def self.path_to_provision_files(repo_url,config_name) File.(File.join(EcbSharedLib.full_config_path(repo_url), "provision_files/#{config_name}")) end |
.path_to_scripts(repo_url) ⇒ Object
the full config path
116 117 118 |
# File 'lib/ebmsharedlib/utilities.rb', line 116 def self.path_to_scripts(repo_url) File.(File.join(EcbSharedLib.full_config_path(repo_url), "scripts")) end |
.prepare_config_repo(config_repo_url) ⇒ Object
takes the repo name (shortcut or full url) and fetches config into appropriate location returns the full repo_url
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ebmsharedlib/utilities.rb', line 129 def self.prepare_config_repo(config_repo_url) # get the full url repo_url = EcbSharedLib.get_config_repo_url(config_repo_url) # get the local config dir base_config_path = base_config_path(repo_url) # make the root if missing FileUtils.mkpath(base_config_path) # and the full config path config_path = full_config_path(repo_url) # try to pull, if it fails could be due to repo not cloned cmd = "git pull" if EcbSharedLib::CL.do_cmd_result(cmd, config_path) != 0 # pull failed, try to clone cmd = "git clone #{repo_url} #{CONFIG_DIR}" if EcbSharedLib::CL.do_cmd_result(cmd, base_config_path) != 0 raise "Unable to clone #{CONFIG_DIR} repo into #{base_config_path}" end end repo_url end |
.printer ⇒ Object
216 217 218 |
# File 'lib/ebmsharedlib/utilities.rb', line 216 def self.printer @printer ||= Printer.new end |
.read_build_config(repo_url, err_msg = nil) ⇒ Object
read and return the config info for this repo
187 188 189 190 |
# File 'lib/ebmsharedlib/utilities.rb', line 187 def self.read_build_config(repo_url, err_msg = nil) config_file_path = "#{full_config_path(repo_url)}/configs/build#{EcbSharedLib::CONFIG_SUFFIX}" read_json_file(config_file_path, err_msg) end |
.read_json_file(file_path, err_msg = nil) ⇒ Object
read and parse a json file
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ebmsharedlib/utilities.rb', line 155 def self.read_json_file(file_path, err_msg = nil) begin json = File.open(file_path, 'r') {|f| f.read } info = JSON.parse(json) info.recursively_symbolize_keys! rescue msg = err_msg.nil? ? "Error opening config file JSON: #{file_path}" : err_msg raise msg end end |
.read_repo_config(repo_url, config_name, err_msg = nil) ⇒ Object
read and return the config info for this repo
193 194 195 196 |
# File 'lib/ebmsharedlib/utilities.rb', line 193 def self.read_repo_config(repo_url, config_name, err_msg = nil) config_file_path = "#{full_config_path(repo_url)}/configs/#{config_name}#{EcbSharedLib::CONFIG_SUFFIX}" read_json_file(config_file_path, err_msg) end |
.read_settings(err_msg = nil) ⇒ Object
read top level settings within a prepared dir lets us get to the appropriate config file
200 201 202 203 |
# File 'lib/ebmsharedlib/utilities.rb', line 200 def self.read_settings(err_msg = nil) settings_path = "#{Dir.pwd}/#{SETTINGS_FILE}" settings = read_json_file(settings_path, err_msg) end |
.repo_url_hash(repo_url) ⇒ Object
compute a unique hash for this repo so we can store it in a subdir of configs
98 99 100 |
# File 'lib/ebmsharedlib/utilities.rb', line 98 def self.repo_url_hash(repo_url) Digest::SHA1.hexdigest(repo_url) end |
.write_json_file(map, file_path, err_msg = nil) ⇒ Object
write the map as json into the specified file
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ebmsharedlib/utilities.rb', line 167 def self.write_json_file(map, file_path, err_msg = nil) begin json = JSON.pretty_generate(map) File.open(file_path, 'w') { |file| file.write(json) } rescue msg = err_msg.nil? ? "Error creating JSON file: #{file_path}" : err_msg raise msg end end |
.write_settings(map, dir, err_msg = nil) ⇒ Object
write the prepared settings, expects us to pass dir to write into
181 182 183 184 |
# File 'lib/ebmsharedlib/utilities.rb', line 181 def self.write_settings(map,dir,err_msg = nil) settings_path = "#{dir}/#{SETTINGS_FILE}" write_json_file(map,settings_path, err_msg) end |