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

Classes: CL, Options

Constant Summary collapse

ROOT_PATH =
File.expand_path("~/.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

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_dirObject

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



212
213
214
# File 'lib/ebmsharedlib/utilities.rb', line 212

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



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/ebmsharedlib/utilities.rb', line 238

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



225
226
227
228
229
230
231
232
233
234
# File 'lib/ebmsharedlib/utilities.rb', line 225

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



216
217
218
# File 'lib/ebmsharedlib/utilities.rb', line 216

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.expand_path(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.expand_path(File.join(EcbSharedLib.full_config_path(repo_url), "scripts"))
end

.prepare_config_repo(config_branch) ⇒ 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
153
154
155
156
# File 'lib/ebmsharedlib/utilities.rb', line 129

def self.prepare_config_repo(config_branch)
  # get the full url
  repo_url = EcbSharedLib.get_config_repo_url(nil)

  # 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
  unless config_branch.nil?
    cmd = "git checkout #{config_branch}"
    EcbSharedLib::CL.do_cmd(cmd, config_path)
  end
  repo_url
end

.printerObject



220
221
222
# File 'lib/ebmsharedlib/utilities.rb', line 220

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



191
192
193
194
# File 'lib/ebmsharedlib/utilities.rb', line 191

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



159
160
161
162
163
164
165
166
167
168
# File 'lib/ebmsharedlib/utilities.rb', line 159

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



197
198
199
200
# File 'lib/ebmsharedlib/utilities.rb', line 197

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



204
205
206
207
# File 'lib/ebmsharedlib/utilities.rb', line 204

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



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ebmsharedlib/utilities.rb', line 171

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



185
186
187
188
# File 'lib/ebmsharedlib/utilities.rb', line 185

def self.write_settings(map,dir,err_msg = nil)
  settings_path = "#{dir}/#{SETTINGS_FILE}"
  write_json_file(map,settings_path, err_msg)
end