]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
add support for per-environment platform pinning. Support pins are platform.version...
authorelijah <elijah@riseup.net>
Thu, 25 Sep 2014 07:54:09 +0000 (00:54 -0700)
committerelijah <elijah@riseup.net>
Thu, 25 Sep 2014 07:54:09 +0000 (00:54 -0700)
lib/leap/platform.rb
lib/leap_cli/commands/deploy.rb

index d97650b40120d92eff4b04f7163381d775ee9c79..89b1bc1a117af7b3ffa8ec0e2873303275b5b646 100644 (file)
@@ -44,6 +44,9 @@ module Leap
       # return true if the platform version is within the specified range.
       #
       def version_in_range?(range)
+        if range.is_a? String
+          range = range.split('..')
+        end
         minimum_platform_version = Versionomy.parse(range.first)
         maximum_platform_version = Versionomy.parse(range.last)
         @version >= minimum_platform_version && @version <= maximum_platform_version
index c214889a729d39cc40cd7bfe1aad5b3975d775c4..f68fe5db777f940910cf3a1f91f2635e0feb09fd 100644 (file)
@@ -44,6 +44,14 @@ module LeapCli
           end
         end
 
+        environments = nodes.field('environment').uniq
+        if environments.empty?
+          environments = [nil]
+        end
+        environments.each do |env|
+          check_platform_pinning(env)
+        end
+        quit!
         compile_hiera_files
 
         ssh_connect(nodes, connect_options(options)) do |ssh|
@@ -70,6 +78,79 @@ module LeapCli
 
     private
 
+    #
+    # The currently activated provider.json could have loaded some pinning
+    # information for the platform. If this is the case, refuse to deploy
+    # if there is a mismatch.
+    #
+    # For example:
+    #
+    # "platform": {
+    #   "branch": "develop"
+    #   "version": "1.0..99"
+    #   "commit": "e1d6280e0a8c565b7fb1a4ed3969ea6fea31a5e2..HEAD"
+    # }
+    #
+    def check_platform_pinning(environment)
+      provider = manager.env(environment).provider
+      return unless provider['platform']
+
+      if environment.nil? || environment == 'default'
+        provider_json = 'provider.json'
+      else
+        provider_json = 'provider.' + environment + '.json'
+      end
+
+      # can we have json schema verification already?
+      unless provider.platform.is_a? Hash
+        bail!('`platform` attribute in #{provider_json} must be a hash (was %s).' % provider.platform.inspect)
+      end
+
+      # check version
+      if provider.platform['version']
+        if !Leap::Platform.version_in_range?(provider.platform.version)
+          bail!("The platform is pinned to a version range of '#{provider.platform.version}' "+
+            "by the `platform.version` property in #{provider_json}, but the platform "+
+            "(#{Path.platform}) has version #{Leap::Platform.version}.")
+        end
+      end
+
+      # check branch
+      if provider.platform['branch']
+        if !is_git_directory?(Path.platform)
+          bail!("The platform is pinned to a particular branch by the `platform.branch` property "+
+            "in #{provider_json}, but the platform directory (#{Path.platform}) is not a git repository.")
+        end
+        unless provider.platform.branch == current_git_branch(Path.platform)
+          bail!("The platform is pinned to branch '#{provider.platform.branch}' by the `platform.branch` property "+
+            "in #{provider_json}, but the current branch is '#{current_git_branch(Path.platform)}' " +
+            "(for directory '#{Path.platform}')")
+        end
+      end
+
+      # check commit
+      if provider.platform['commit']
+        if !is_git_directory?(Path.platform)
+          bail!("The platform is pinned to a particular commit range by the `platform.commit` property "+
+            "in #{provider_json}, but the platform directory (#{Path.platform}) is not a git repository.")
+        end
+        current_commit = current_git_commit(Path.platform)
+        Dir.chdir(Path.platform) do
+          commit_range = assert_run!("git log --pretty='format:%H' '#{provider.platform.commit}'",
+            "The platform is pinned to a particular commit range by the `platform.commit` property "+
+            "in #{provider_json}, but git was not able to find commits in the range specified "+
+            "(#{provider.platform.commit}).")
+          commit_range = commit_range.split("\n")
+          if !commit_range.include?(current_commit) &&
+              provider.platform.commit.split('..').first != current_commit
+            bail!("The platform is pinned via the `platform.commit` property in #{provider_json} " +
+              "to a commit in the range #{provider.platform.commit}, but the current HEAD " +
+              "(#{current_commit}) is not in that range.")
+          end
+        end
+      end
+    end
+
     def sync_hiera_config(ssh)
       dest_dir = provider.hiera_sync_destination
       ssh.rsync.update do |server|
@@ -119,7 +200,12 @@ module LeapCli
       end
     end
 
+    #
+    # ensure submodules are up to date, if the platform is a git
+    # repository.
+    #
     def init_submodules
+      return unless is_git_directory?(Path.platform)
       Dir.chdir Path.platform do
         assert_run! "git submodule sync"
         statuses = assert_run! "git submodule status"