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

Add sshkey option for git based formula dependencies. #302

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/kitchen/provisioner/dependencies.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,23 @@ def install_dependencies
if formula.key?(:repo)
case formula[:repo]
when 'git'
script += <<-INSTALL
if formula[:source].start_with?("http")
script += <<-INSTALL
fetchGitFormula #{formula[:source]} "#{formula[:name]}" "#{formula[:branch] || 'master'}"
INSTALL
INSTALL
else
if formula[:ssh_key].nil? and config[:ssh_key].nil?
raise "No ssh_key specified for #{formula[:source]}"
end
if formula[:ssh_key].nil?
ssh_key = config[:root_path] + config[:ssh_home] + "/" + File.basename(config[:ssh_key])
else
ssh_key = config[:root_path] + config[:ssh_home] + "/" + File.basename(formula[:ssh_key])
end
script += <<-INSTALL
fetchGitFormula #{formula[:source]} "#{formula[:name]}" "#{formula[:branch] || 'master'}" "#{ssh_key}"
INSTALL
end
when 'spm'
if formula[:package].nil?
script += <<-INSTALL
Expand Down
14 changes: 12 additions & 2 deletions lib/kitchen/provisioner/formula-fetch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
# GIT_FORMULAS_PATH=/usr/share/salt-formulas/env/_formulas
# xargs -n1 ./formula-fetch.sh < dependencies.txt


# Parse git dependencies from metadata.yml
# $1 - path to <formula>/metadata.yml
# sample to output:
# https://github.com/salt-formulas/salt-formula-git git
# https://github.com/salt-formulas/salt-formula-salt salt

function fetchDependencies() {
METADATA="$1";
grep -E "^dependencies:" "$METADATA" >/dev/null || return 0
Expand All @@ -30,15 +30,25 @@ function fetchDependencies() {
# $1 - formula git repo url
# $2 - formula name (optional)
# $3 - branch (optional)
# $4 - path to deploykey
function fetchGitFormula() {
test -n "${FETCHED}" || declare -a FETCHED=()
export GIT_FORMULAS_PATH=${GIT_FORMULAS_PATH:-/usr/share/salt-formulas/env/_formulas}

if [[ -n $4 ]]
then
sshbin=$(command -v ssh)
export GIT_SSH_COMMAND="${sshbin} -o UserKnownHostsFile=/tmp/kitchen/ssh/known_hosts -o StrictHostKeyChecking=no -i ${4}"
export GIT_SSH="/tmp/kitchen/git_ssh.sh"
fi

mkdir -p "$GIT_FORMULAS_PATH"
if [ -n "$1" ]; then
source="$1"
name="$2"
test -n "$name" || name="${source//*salt-formula-}"
test -z "$3" && branch=master || branch=$3

if ! [[ "${FETCHED[*]}" =~ $name ]]; then # dependency not yet fetched
echo "Fetching: $name"
if test -e "$GIT_FORMULAS_PATH/$name"; then
Expand All @@ -47,7 +57,7 @@ function fetchGitFormula() {
popd &>/dev/null || exit
else
echo "git clone $source $GIT_FORMULAS_PATH/$name -b $branch"
git clone "$source" "$GIT_FORMULAS_PATH/$name" -b "$branch"
git clone "$source" "$GIT_FORMULAS_PATH/$name" -b "$branch" || exit 1
fi
# install dependencies
FETCHED+=("$name")
Expand Down
3 changes: 3 additions & 0 deletions lib/kitchen/provisioner/git_ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# Workaround: GIT_SSH_COMMAND is not supported by Git < 2.3
exec "${GIT_SSH_COMMAND:-ssh}" "$@"
5 changes: 5 additions & 0 deletions lib/kitchen/provisioner/known_hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81e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QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9
gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf
37 changes: 32 additions & 5 deletions lib/kitchen/provisioner/salt_solo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ class SaltSolo < Base
salt_spm_root: '/srv/spm',
salt_state_top: '/srv/salt/top.sls',
salt_version: 'latest',
salt_yum_repo_key: 'https://repo.saltproject.io/yum/redhat/$releasever/$basearch/archive/%s/SALTSTACK-GPG-KEY.pub',
salt_yum_repo_latest: 'https://repo.saltproject.io/yum/redhat/salt-repo-latest-2.el7.noarch.rpm',
salt_yum_repo: 'https://repo.saltproject.io/yum/redhat/$releasever/$basearch/archive/%s',
salt_yum_rpm_key: 'https://repo.saltproject.io/yum/redhat/7/x86_64/archive/%s/SALTSTACK-GPG-KEY.pub',
salt_yum_repo_key: 'https://repo.saltstack.com/yum/redhat/$releasever/$basearch/archive/%s/SALTSTACK-GPG-KEY.pub',
salt_yum_repo_latest: 'https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm',
salt_yum_repo: 'https://repo.saltstack.com/yum/redhat/$releasever/$basearch/archive/%s',
salt_yum_rpm_key: 'https://repo.saltstack.com/yum/redhat/7/x86_64/archive/%s/SALTSTACK-GPG-KEY.pub',
ssh_home: '/ssh',
ssh_key: nil,
state_collection: false,
state_top_from_file: false,
state_top: {},
Expand Down Expand Up @@ -429,6 +431,31 @@ def prepare_dependencies
# sub-directory
return if windows_os?

# Write ssh known_hosts
write_raw_file(File.join(sandbox_path, config[:ssh_home], "known_hosts"), File.read(File.expand_path("../known_hosts", __FILE__)))
# Write general deploy key.
unless config[:ssh_key].nil?
outfile = File.join(sandbox_path, config[:ssh_home], File.basename(config[:ssh_key]))
contents = File.read(File.expand_path(config[:ssh_key]))
if contents.include?("ENCRYPTED")
raise("Encrypted key not supported offending key: #{config[:ssh_key]}")
end
info("Copying #{config[:ssh_key]} to #{outfile}")
write_raw_file(outfile, contents)
end
# Write dependency overridden deploykey
config[:dependencies].each do |dependency|
unless dependency[:ssh_key].nil?
outfile = File.join(sandbox_path, config[:ssh_home], File.basename(dependency[:ssh_key]))
contents = File.read(File.expand_path(dependency[:ssh_key]))
if contents.include?("ENCRYPTED")
raise("Encrypted key not supported offending key: #{dependency[:ssh_key]}")
end
info("Copying #{dependency[:ssh_key]} to #{outfile}")
write_raw_file(outfile, contents)
end
end

# upload scripts
sandbox_scripts_path = File.join(sandbox_path, config[:salt_config], 'scripts')
info("Preparing scripts into #{config[:salt_config]}/scripts")
Expand All @@ -455,7 +482,7 @@ def prepare_dependencies
end

# upload scripts
%w[formula-fetch.sh repository-setup.sh].each do |script|
%w[formula-fetch.sh repository-setup.sh git_ssh.sh].each do |script|
write_raw_file(File.join(sandbox_path, script), File.read(File.expand_path("../#{script}", __FILE__)))
end
dependencies_script = File.expand_path('./../dependencies.erb', __FILE__)
Expand Down