|
| 1 | +# |
| 2 | +# Copyright 2011-2013, Dell |
| 3 | +# Copyright 2013-2015, SUSE Linux GmbH |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +namespace :crowbar do |
| 19 | + require "git" |
| 20 | + require "octokit" |
| 21 | + require "netrc" |
| 22 | + require "yaml" |
| 23 | + require "deep_merge" |
| 24 | + |
| 25 | + task :configure do |
| 26 | + @git = Git::Lib.new |
| 27 | + |
| 28 | + general_path = File.expand_path("../config/barclamps.yml", __FILE__) |
| 29 | + |
| 30 | + general_config = if File.exist? general_path |
| 31 | + YAML.load_file( |
| 32 | + general_path |
| 33 | + ) |
| 34 | + else |
| 35 | + {} |
| 36 | + end |
| 37 | + |
| 38 | + local_path = File.expand_path("../config/barclamps.local.yml", __FILE__) |
| 39 | + |
| 40 | + local_config = if File.exist? local_path |
| 41 | + YAML.load_file( |
| 42 | + local_path |
| 43 | + ) |
| 44 | + else |
| 45 | + {} |
| 46 | + end |
| 47 | + |
| 48 | + @barclamps = general_config.deep_merge( |
| 49 | + local_config |
| 50 | + ).values.flatten |
| 51 | + end |
| 52 | + |
| 53 | + desc "Init all barclamps" |
| 54 | + task init: [:fork, :clone, :add_upstream, :add_susecloud] do |
| 55 | + # nothing to do here |
| 56 | + end |
| 57 | + |
| 58 | + desc "Update all clones" |
| 59 | + task update: [:pull_upstream] do |
| 60 | + # nothing to do here |
| 61 | + end |
| 62 | + |
| 63 | + desc "Forks all required barclamps to your home project" |
| 64 | + task fork: [:configure] do |
| 65 | + client = Octokit::Client.new(netrc: true) |
| 66 | + |
| 67 | + @barclamps.each do |barclamp| |
| 68 | + begin |
| 69 | + if client.repository? "#{client.login}/barclamp-#{barclamp}" |
| 70 | + puts "Barclamp #{barclamp} already forked. Skipping..." |
| 71 | + else |
| 72 | + puts "Forking barclamp #{barclamp}..." |
| 73 | + client.fork("crowbar/barclamp-#{barclamp}") |
| 74 | + end |
| 75 | + rescue Octokit::NotFound |
| 76 | + puts "Barclamp #{barclamp} does not exist" |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + desc "Clones all required barclamps into the barclamps folder" |
| 82 | + task clone: [:configure] do |
| 83 | + client = Octokit::Client.new(netrc: true) |
| 84 | + |
| 85 | + Dir.chdir "barclamps" do |
| 86 | + @barclamps.each do |barclamp| |
| 87 | + if Dir.exist? barclamp |
| 88 | + puts "Barclamp #{barclamp} already exists. Skipping..." |
| 89 | + else |
| 90 | + if client.repository? "#{client.login}/barclamp-#{barclamp}" |
| 91 | + puts "Cloning barclamp #{barclamp}..." |
| 92 | + Git.clone("[email protected]:#{client.login}/barclamp-#{barclamp}.git", barclamp) |
| 93 | + else |
| 94 | + puts "Barclamp #{barclamp} does not exist" |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + desc "Add upstream remote to the crowbar organisation" |
| 102 | + task add_upstream: [:configure] do |
| 103 | + client = Octokit::Client.new(netrc: true) |
| 104 | + |
| 105 | + @barclamps.each do |barclamp| |
| 106 | + barclamp_dir = File.join("barclamps", barclamp) |
| 107 | + |
| 108 | + unless Dir.exist? barclamp_dir |
| 109 | + puts "Barclamp #{barclamp} directory does not exist" |
| 110 | + next |
| 111 | + end |
| 112 | + |
| 113 | + Dir.chdir barclamp_dir do |
| 114 | + next if @git.remotes.include? "upstream" |
| 115 | + |
| 116 | + if client.repository? "crowbar/barclamp-#{barclamp}" |
| 117 | + puts "Adding remote upstream for #{barclamp}..." |
| 118 | + @git.remote_add("upstream", "[email protected]:crowbar/barclamp-#{barclamp}.git", fetch: true) |
| 119 | + else |
| 120 | + puts "Barclamp #{barclamp} doesn't exist within Crowbar org" |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + desc "Add upstream remote to the SUSE-Cloud organisation" |
| 127 | + task add_susecloud: [:configure] do |
| 128 | + client = Octokit::Client.new(netrc: true) |
| 129 | + |
| 130 | + @barclamps.each do |barclamp| |
| 131 | + barclamp_dir = File.join("barclamps", barclamp) |
| 132 | + |
| 133 | + unless Dir.exist? barclamp_dir |
| 134 | + puts "Barclamp #{barclamp} directory does not exist" |
| 135 | + next |
| 136 | + end |
| 137 | + |
| 138 | + Dir.chdir barclamp_dir do |
| 139 | + next if @git.remotes.include? "suse-cloud" |
| 140 | + |
| 141 | + if client.repository? "SUSE-Cloud/barclamp-#{barclamp}" |
| 142 | + puts "Adding remote suse-cloud for #{barclamp}..." |
| 143 | + @git.remote_add("suse-cloud", "[email protected]:SUSE-Cloud/barclamp-#{barclamp}.git", fetch: true) |
| 144 | + else |
| 145 | + puts "Barclamp #{barclamp} doesn't exist within SUSE-Cloud org" |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + desc "Update remotes repositories" |
| 152 | + task update_remotes: [:configure] do |
| 153 | + @barclamps.each do |barclamp| |
| 154 | + barclamp_dir = File.join("barclamps", barclamp) |
| 155 | + |
| 156 | + unless Dir.exist? barclamp_dir |
| 157 | + puts "Barclamp #{barclamp} directory does not exist" |
| 158 | + next |
| 159 | + end |
| 160 | + |
| 161 | + Dir.chdir barclamp_dir do |
| 162 | + ["upstream" "suse-cloud"].each do |remote| |
| 163 | + if @git.remotes.include? remote |
| 164 | + puts "Updating #{remote} of #{barclamp}..." |
| 165 | + msg = @git.fetch(remote, prune: true) |
| 166 | + puts msg unless msg.empty? |
| 167 | + else |
| 168 | + puts "Remote #{remote} of #{barclamp} does not exist" |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + desc "Pull from upstream/master" |
| 176 | + task pull_upstream: [:configure] do |
| 177 | + @barclamps.each do |barclamp| |
| 178 | + barclamp_dir = File.join("barclamps", barclamp) |
| 179 | + |
| 180 | + unless Dir.exist? barclamp_dir |
| 181 | + puts "Barclamp #{barclamp} directory does not exist" |
| 182 | + next |
| 183 | + end |
| 184 | + |
| 185 | + Dir.chdir barclamp_dir do |
| 186 | + current_branch = @git.branch_current |
| 187 | + stash = { saved: false, branch: current_branch } |
| 188 | + |
| 189 | + # save a stash if there are changes |
| 190 | + unless @git.diff_files.empty? |
| 191 | + puts "Saving stash of #{barclamp}..." |
| 192 | + @git.stash_save("Automatic stash by rake - #{Time.now}") |
| 193 | + stash[:saved] = true |
| 194 | + end |
| 195 | + |
| 196 | + # checkout master if on a different branch |
| 197 | + unless current_branch == "master" |
| 198 | + puts "Checking out master of #{barclamp}..." |
| 199 | + @git.checkout("master") |
| 200 | + end |
| 201 | + |
| 202 | + # pull changes from upstream/master |
| 203 | + if @git.remotes.include? "upstream" |
| 204 | + puts "Pull from upstream/master of #{barclamp}" |
| 205 | + msg = @git.pull("upstream") |
| 206 | + unless msg.include? "Already up-to-date" |
| 207 | + puts msg |
| 208 | + puts "^^^^^^^^^^^^^^^^^^^^^^^^^^^" |
| 209 | + end |
| 210 | + else |
| 211 | + puts "Remote upstream of #{barclamp} does not exist" |
| 212 | + end |
| 213 | + |
| 214 | + # reapply stash |
| 215 | + if stash[:saved] |
| 216 | + puts "Reapplying stash to #{stash[:branch]}..." |
| 217 | + @git.checkout(stash[:branch]) unless stash[:branch] == "master" |
| 218 | + @git.stash_apply |
| 219 | + @git.stash_clear |
| 220 | + end |
| 221 | + end |
| 222 | + end |
| 223 | + end |
| 224 | +end |
0 commit comments