From 66a196844a6e534f96764bdce8d3e8bd296212b0 Mon Sep 17 00:00:00 2001 From: Alex Dergachev Date: Tue, 18 Nov 2014 19:02:29 +0000 Subject: [PATCH] Automatically add each new host to ~/.ssh/known_hosts --- README.md | 16 ++++++++++++---- lib/repository_fetch/fetch.rb | 25 +++++++++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0a3a8d5..0882135 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,36 @@ redmine_repository_fetch ======================== -Redmine plugin to automatically clone and fetch referenced repositories +Redmine plugin to automatically clone and fetch referenced repositories. + +## Installation Currently the plugin hardcodes this config, change it for your use-case: ``` - PATTERNS = [ + PATTERNS = [ { :pattern => "/redmine_git_fetch/github.com/", :uri_prefix => "git@github.com:", + :host => "github.com", :key => "/home/redmine/data/keys/id_rsa" }, { :pattern => "/redmine_git_fetch/gitlab.com/", :uri_prefix => "git@gitlab.com:", + :host => "gitlab.com", :key => "/home/redmine/data/keys/id_rsa" }, { :pattern => "/redmine_git_fetch/git.ewdev.ca/", :uri_prefix => "git@git.ewdev.ca:", + :host => "git.ewdev.ca", :key => "/home/redmine/data/keys/id_rsa" } ] ``` -Once you have it setup, do the following: +Be sure to populate the appropriate keys for your redmine user (www-data, redmine, etc), +either in `~/.ssh` or in the place specified by the `PATTERNS[x][:key]` property. + +## Usage Add `/redmine_git_fetch/github.com/evolvingweb/sitediff.git` to a repo. The plugin will automatically detect the prefix `/redmine_git_fetch/github.com/` @@ -34,7 +42,7 @@ Note that `/redmine_git_fetch` folder will get auto-created. The plugin currently doesn't fetch any repos outside its purview. -It also needs to be run as follows: +It also needs to be run as follows, probably from cron: ``` bundle exec rails runner "RepositoryFetch.fetch" -e production diff --git a/lib/repository_fetch/fetch.rb b/lib/repository_fetch/fetch.rb index 31691d3..ac5a838 100644 --- a/lib/repository_fetch/fetch.rb +++ b/lib/repository_fetch/fetch.rb @@ -7,14 +7,17 @@ module RepositoryFetch PATTERNS = [ { :pattern => "/redmine_git_fetch/github.com/", :uri_prefix => "git@github.com:", + :host => "github.com", :key => "/home/redmine/data/keys/id_rsa" }, { :pattern => "/redmine_git_fetch/gitlab.com/", :uri_prefix => "git@gitlab.com:", + :host => "gitlab.com", :key => "/home/redmine/data/keys/id_rsa" }, { :pattern => "/redmine_git_fetch/git.ewdev.ca/", :uri_prefix => "git@git.ewdev.ca:", + :host => "git.ewdev.ca", :key => "/home/redmine/data/keys/id_rsa" } ] @@ -33,15 +36,17 @@ module RepositoryFetch return end + add_known_host(p[:host]) + # If dir exists and non-empty, should be safe to 'git fetch' if Dir.exists?(path) && Dir.entries(path) != [".", ".."] puts "Running git fetch on #{path}" - puts self.exec_with_key "git -C #{path} fetch --all", p[:key] + puts exec_with_key "git -C #{path} fetch --all", p[:key] else # try cloning the repo url = path.sub( p[:pattern], p[:uri_prefix]) puts "Matched new URL, trying to clone: " + url - puts self.exec_with_key "git clone --mirror #{url} #{path}", p[:key] + puts exec_with_key "git clone --mirror #{url} #{path}", p[:key] end end @@ -52,11 +57,23 @@ module RepositoryFetch def self.fetch Project.active.has_module(:repository).all.each do |project| project.repositories.each do |repository| - self.clone_or_fetch(repository) + clone_or_fetch(repository) end end end - class Fetcher + # Checks if host is in ~/.ssh/known_hosts, adds it if not present + def self.add_known_host(host) + # if not found... + if `ssh-keygen -F #{host} | grep 'found'` == "" + # hack to work with 'docker exec' where HOME isn't set (or set to /) + ssh_known_hosts = (ENV['HOME'] == "/" or ENV['HOME'] == nil ? "/root" : ENV['HOME']) + "/.ssh/known_hosts" + puts "Authorizing #{host}" + puts `ssh-keyscan #{host} >> #{ssh_known_hosts}` + end + end + + + class Fetch end end