Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Repositories for Addons #539

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from

Conversation

sean-e-dietrich
Copy link
Member

Started development for multiple repositories that can be used for addons.
Additionally, added ability to run updates between addons. Rather then
running the risk of the addons being out of sync

This is a rewrite of the PR #535 and I believe this solves #368 and solves #405

This takes information that was written through back channels and allows for GIT repos to be used when checking out addons instead of downloading raw files with only support for github.

Additionally, support for multiple repositories is added. The repositories must be prefixed with DOCKSAL_ADDONS_REPO_ in order to have them searched. The DOCKSAL_ADDONS_REPO which is the same repo at https://github.com/docksal/addons is searched first but can be overridden within the .docksal/docksal.env file.

Syntax:

DOCKSAL_ADDONS_REPO_[NAME]="[Repo URL]|[Branch]"

Because a repository can have multiple branches there is an additional options for the variable to designate which branch is selected.

[NAME] Should be replaced with a unique machine name for that repo
[Repo URL] Should be the GIT repo url that will be used to clone the repo.
[BRANCH] Is optional but designates what branch of the repo to use, if not included master branch will be used.

NOTE A pipe (|) is used to separate the [REPO URL] and [BRANCH]. If [BRANCH] is omitted then the pipe is not necessary

Examples:

 # Checks master branch
DOCKSAL_ADDONS_REPO_TOOLKIT="https://github.com/kanopi/docksal-toolkit.git"
# Checks sample branch
DOCKSAL_ADDONS_REPO_TOOLKIT_SAMPLE="https://github.com/kanopi/docksal-toolkit.git|sample" 

This also adds in a fin addon update command which will allow for the addon to run a similar process as the fin addon install. The update process will look for pre-update and post-update hooks as well.

The purpose for this is that if an addon has a single repo it can be tracked and to a certain extent versioned appropriately, but more so then that, if there is a need for developer to use their own personal addons in projects, but they also have a repo which is shared amongst a team then they can use both of the repositories.

Started development for multiple repositories that can be used for addons.
Additionally, added ability to run updates between addons. Rather then
running the risk of the addons being out of sync
@achekulaev
Copy link
Member

@sean-e-dietrich can you please rebase it on the develop branch that now has your commit to fix tests for external PRs?

@sean-e-dietrich
Copy link
Member Author

@achekulaev I already merged the changes in and am rerunning the tests.

bin/fin Outdated
@@ -167,6 +167,7 @@ URL_ADDONS_HOSTING="https://raw.githubusercontent.com"
URL_ADDONS_REPO="$URL_ADDONS_HOSTING/docksal/addons"
# Remove URL_REPO_ADDONS in Nov 2018
URL_REPO_ADDONS="https://raw.githubusercontent.com/docksal/addons"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is unused now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it, I was leaving it there because I know you said remove in Nov '18

bin/fin Outdated
@@ -1728,11 +1729,13 @@ show_help_addon ()
echo
echo-green "Commands:"
printh "install <name>" "Install addon"
printh "update <name>" "Update addon"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use tab for indentation

bin/fin Outdated
config="${config}|master"
local REPO_URL=$(echo $config | cut -d'|' -f1)
local REPO_BRANCH=$(echo $config | cut -d'|' -f2)
local uuid=$(uuidgen)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tabs everywhere for indentation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use uuidgen. I'd like to avoid relying on additional binaries when possible. Use $RANDOM bash variable. Search for /tmp/etc.exports.$RANDOM in fin to see usage example

bin/fin Outdated
local uuid=$(uuidgen)

# Check to make sure the provided Repo URL is valid and the Branch Exists
local valid=$(git ls-remote -h $REPO_URL --refs $REPO_BRANCH | wc -l)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. git's stderr should be redirected to dev/null
  2. since it's non-fatal I would avoid echo-error
  3. it's better to use grep -q $REPO_BRANCH to avoid counting some random output as a valid output
# Check that provided repo URL is valid and the branch exists
git ls-remote -h "$REPO_URL" --refs "$REPO_BRANCH" 2>/dev/null | grep -q "$REPO_BRANCH"
if [[ "$?" != "0" ]]; then
  echo "$key defines an invalid repository/branch reference. Skipping..." 
  continue
else
...

bin/fin Outdated
git remote add origin $repo_url > /dev/null
git fetch -q origin $repo_branch > /dev/null
git checkout origin/${repo_branch} ${addon_name} 2> /dev/null
git checkout origin/${repo_branch} ${addon_name}.pre-install 2> /dev/null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every addon has pre-install hook. We should only check the main file

bin/fin Outdated
git init > /dev/null
git remote add origin $repo_url > /dev/null
git fetch -q origin $repo_branch > /dev/null
git checkout origin/${repo_branch} ${addon_name} 2> /dev/null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this command should actually be the last. If main addon file did check out then everything is in order. If it did not then it is an error.

Styling hint. Please don't put a space here 2>/dev/null

bin/fin Outdated
if_failed_error "Could not create $addon_path" "Check file permissions and try again"
fi

local tmp_addon="/tmp/${ADDON_UUID}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor var name to addons_repo_copy

bin/fin Outdated
if_failed_error "Download has failed" "Check log above for messages"
fi
echo-red "Addon already exists in $addon_path. Run ${NC}fin addon update ${addon_name}"
exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 1

bin/fin Outdated
git pull --depth=1 origin/${URL_ADDONS_BRANCH}
# Remove Git Repo that was cloned from.
rm -rf $tmp_addon/.git 2>/dev/null
mv -R $tmp_addon $addon_path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mv -R is an anti-pattern because of inability to properly handle errors (what failed, copy or delete?) and because it will leave leftovers if copying errors happen.
cp -R followed by rm -rf should be used instead.

bin/fin Outdated
local URL_ADDONS_REPO
local URL_ADDONS_BRANCH
local ADDON_UUID
_addon_search_repo $addon_name URL_ADDONS_REPO URL_ADDONS_BRANCH ADDON_UUID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should be moved down, below the check for addon existence

bin/fin Outdated
else
echo-red "Unknown error"
rm -rf $tmp_addon
exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo-error "Addon main file does not exist, is a symlink or is not set to be executable"
rm -rf "$tmp_addon" >/dev/null 2>&1
return 1

bin/fin Outdated
# If repo contains multiple addon let's only download the repo we need.
elif [[ -d $tmp_addon/$addon_name ]] && [[ -f $tmp_addon/$addon_name/$addon_name ]] && [[ -x $tmp_addon/$addon_name/$addon_name ]]; then
# Remove Git Repo that was cloned from.
rm -rf $tmp_addon/.git 2>/dev/null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both outputs should be redirected

rm -rf "$tmp_addon/.git" >/dev/null 2>&1

bin/fin Outdated
# Update addon
# $1 - addon name
# $2 - optional flag to install globally
addon_update () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this function different from addon_install?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achekulaev addon_update only handles the updating piece. I believe the goal would be to eventually make it so that there were pre-update and post-update hooks for addons. Eventually also there could be the capability of versioning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that in future there might be difference, but is there any real code difference with addon_install right now? Because in PR review they look identical to me. And if they are identical right now I would just call addon_install for fin addon update

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only difference would be the hooks. So seeing as the same code is ran for them both maybe refactoring the code and putting it in a function of it's own so it isn't duplicate coding.

bin/fin Outdated
git ls-remote -h "$REPO_URL" --refs "$REPO_BRANCH" 2>/dev/null | grep -q "$REPO_BRANCH"
if [[ "$?" != "0" ]]; then
echo "$key defines an invalid repository/branch reference. Skipping..."
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no continue here or rm -rf "/tmp/${uuid}" will not clean up.
Actually you might want to revisit this whole thing and move rm -rf into inside if-clauses.

bin/fin Outdated
local repo_uuid=$4

echo-green "Searching for ${addon} in predefined repositories..."
for key in "${repos[@]}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for config in "${repos[@]}"

bin/fin Outdated
echo-green "Searching for ${addon} in predefined repositories..."
for key in "${repos[@]}"
do
local config=${!key}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this, it should be for config in "${repos[@]}"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achekulaev

repos is an array of variable names
When looping through those variables, key refers to the variable name that is being used.
config is referring to the value for the variable that is stored.

Example:

DOCKSAL_ADDONS_REPO="https://github.com/docksal/addons"
DOCKSAL_ADDONS_REPO_X="https://github.com/kanopi/docksal-toolkit.git|sample"
eval 'repos=(${!'"DOCKSAL_ADDONS_REPO"'@})'
# should produce something like the following array
# repos=(DOCKSAL_ADDONS_REPO DOCKSAL_ADDONS_REPO_X)
for key in "${repos[@]}"
do
	local config=${!key} #when looped will now grab the value for that variable
done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Then let's refactor to
for repo in "${repos[@]}"
key is just too generic.

bin/fin Outdated
done

if [[ -z "${REPO_URL}" ]]; then
echo-red "Addon not found to install."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo-error "Could not find addon ${addon_name} in any of the pre-defined repositories." \
  "Check repositories and branches spelling"
return 1

@achekulaev achekulaev added this to To do in Docksal 1.9.0 via automation May 23, 2018
@docksal docksal deleted a comment from sean-e-dietrich May 24, 2018
@@ -5167,47 +5238,94 @@ addon_install ()
echo-red "Pre-install hook has failed and aborted the installation."
# Delete downloaded files if pre-install hook fails
[[ -d "$addon_path" ]] && [[ "$addon_path" =~ ".docksal" ]] &&
rm -r "$addon_path"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that indentation was intentional

@achekulaev
Copy link
Member

@sean-e-dietrich could you give me write access to your repo fork? I'd like to download and test first and I think I will have more fixes afterwards.

@sean-e-dietrich
Copy link
Member Author

@achekulaev I couldn't give you access to the Kanopi's org but I cloned the repo into a personal account and made it private. Also added you on the repo as a collaborator

@achekulaev
Copy link
Member

achekulaev commented May 31, 2018

Thanks. I don't need access to Kanopi.org, the access you've given to https://github.com/sean-e-dietrich/docksal which you have raised the PR from is enough.

@achekulaev achekulaev added this to To do in Docksal 1.10.0 via automation May 31, 2018
@achekulaev achekulaev removed this from To do in Docksal 1.9.0 May 31, 2018
@achekulaev achekulaev added this to To do in Docksal 1.11.0 via automation Jun 26, 2018
@achekulaev achekulaev removed this from To do in Docksal 1.10.0 Jun 26, 2018
@lmakarov lmakarov added this to To do in Docksal 1.12.0 via automation Oct 31, 2018
@lmakarov lmakarov removed this from To do in Docksal 1.11.0 Oct 31, 2018
@achekulaev achekulaev added the size/large Effort in T-Shirt scale label Nov 16, 2018
@lmakarov lmakarov added this to To do in Docksal 1.13.0 via automation Jan 25, 2019
@lmakarov lmakarov removed this from To do in Docksal 1.12.0 Jan 25, 2019
@lmakarov
Copy link
Member

lmakarov commented Oct 3, 2019

Needs to be rebased into develop.

@lmakarov lmakarov added this to To do in 1.14.0 via automation Oct 4, 2019
@lmakarov lmakarov removed this from To do in Docksal 1.13.0 Oct 4, 2019
@lmakarov lmakarov removed this from To do in 1.14.0 May 13, 2020
@lmakarov
Copy link
Member

This PR has been open for 2.5 years. @sean-e-dietrich do you still intend finalizing it? If not, please close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/large Effort in T-Shirt scale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants