{"action":"created","comment":{"url":"https://api.github.com/repos/crowbar/crowbar-core/pulls/comments/38840985","id":38840985,"diff_hunk":"@@ -0,0 +1,139 @@\n+#\n+# Copyright 2011-2013, Dell\n+# Copyright 2013-2015, SUSE LINUX GmbH\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+#\n+#   http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# See the License for the specific language governing permissions and\n+# limitations under the License.\n+#\n+\n+module Crowbar\n+  class Lock::SharedNonBlocking < Lock\n+    # This class implements a shared (i.e. non-exclusive) lock, by\n+    # default on a remote node.  It is used to allow one or more\n+    # concurrent proposal applications to temporarily pause any\n+    # intervallic runs triggered by the chef-client daemon, to prevent\n+    # them from interfering with the orchestration of the proposals\n+    # being applied:\n+    #\n+    #   https://bugzilla.suse.com/show_bug.cgi?id=857375\n+    #\n+    # It has the same semantics as a shared read/write lock, except\n+    # that instead of readers reading, we have threads applying\n+    # proposals, and instead of a single writer writing, we have the\n+    # chef-client daemon triggering a chef-client run.\n+\n+    attr_reader :node, :owner, :reason, :executor\n+\n+    def initialize(options = {})\n+      super\n+      %w(node owner reason).each do |option|\n+        unless options[option.to_sym]\n+          raise \"#{self.class} can't be instantiated without \" + \\\n+                \":#{option} option (#{options.inspect})\"\n+        end\n+        instance_variable_set \"@#{option}\", options[option.to_sym]\n+      end\n+\n+      # We manage execution of the command via dependency injection,\n+      # so that the test suite can execute the commands locally.\n+      default_executor = lambda do |node, cmd, shell|\n+        RemoteNode.pipe_ssh_cmd(node, cmd) do |pipe|\n+          pipe.write(shell)\n+        end\n+        unless $?.success?\n+          raise \"pid #{$?.pid} returned #{$?.exitstatus}\"\n+        end\n+      end\n+      @executor = options.fetch :executor, default_executor\n+\n+      # To implement shared locking semantics, we keep track of all\n+      # \"readers\" via a 'foo.lock.d/' directory which contains one\n+      # file per \"reader\":\n+      @readers_dir = path + '.d'\n+\n+      # In order to provide a lock for the writer, we also ensure that\n+      # a 'foo.lock' file exists if and only if the 'foo.lock.d/'\n+      # directory contains one or more readers.  Doing this safely\n+      # requires a \"meta-lock\" in order to avoid races when we create\n+      # or remove the 'foo.lock' file.  We implement that meta-lock\n+      # using flock(1) on another 'foo.lock.meta' file.\n+      @meta_lock = path + '.meta'\n+    end\n+\n+    def acquire\n+      if locked?\n+        logger.warn(\"#{owner} tried to re-acquire remote lock #{path} on #{node}\")\n+        return\n+      end\n+\n+      logger.debug(\"#{owner} acquiring remote lock #{path} on #{node}\")\n+      shell = <<-EOSHELL.strip\n+        mkdir -p \"#{@readers_dir}\"\n+        file=\"#{@readers_dir}/#{owner}\"\n+        if [ -e \"$file\" ]; then\n+          echo \"ERROR: #{owner} already had lock via $file\" >&2\n+          exit 1\n+        fi\n+        echo \"#{reason}\" > \"$file\"\n+        echo \"Last required by #{owner}\" > \"#{path}\"\n+      EOSHELL\n+      run_with_exclusive_lock(shell)\n+      logger.debug(\"#{owner} acquired remote lock #{path} on #{node}\")\n+      @locked = true\n+      self\n+    end\n+\n+    def release\n+      logger.debug(\"#{owner} releasing remote lock #{path} on #{node}\")\n+      shell = <<-EOSHELL.strip\n+        rm -f #{@readers_dir}/#{owner}\n+        if [ -d #{@readers_dir} ] && ! ls #{@readers_dir} | grep -q .; then\n+          rm -f #{path}","path":"crowbar_framework/lib/crowbar/lock/shared_non_blocking.rb","position":100,"original_position":100,"commit_id":"99f52dab8fdcdd99a037c4e074e735c40cda5287","original_commit_id":"ec6992b6bbc4eb8bf461922d285f458d500a0d80","user":{"login":"vuntz","id":1758783,"avatar_url":"https://avatars.githubusercontent.com/u/1758783?v=3","gravatar_id":"","url":"https://api.github.com/users/vuntz","html_url":"https://github.com/vuntz","followers_url":"https://api.github.com/users/vuntz/followers","following_url":"https://api.github.com/users/vuntz/following{/other_user}","gists_url":"https://api.github.com/users/vuntz/gists{/gist_id}","starred_url":"https://api.github.com/users/vuntz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vuntz/subscriptions","organizations_url":"https://api.github.com/users/vuntz/orgs","repos_url":"https://api.github.com/users/vuntz/repos","events_url":"https://api.github.com/users/vuntz/events{/privacy}","received_events_url":"https://api.github.com/users/vuntz/received_events","type":"User","site_admin":false},"body":"Why would it be created again later? It's not likely that a proposal will be re-applied. On the other hand, I would say it makes things a bit confusing because it might look like there's a lock (if you look too quickly).\r\n\r\nFWIW, in general, I prefer such things to be automatically cleaned since they're just \"helpers\".","created_at":"2015-09-07T08:08:27Z","updated_at":"2015-09-07T08:08:27Z","html_url":"https://github.com/crowbar/crowbar-core/pull/14#discussion_r38840985","pull_request_url":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14","_links":{"self":{"href":"https://api.github.com/repos/crowbar/crowbar-core/pulls/comments/38840985"},"html":{"href":"https://github.com/crowbar/crowbar-core/pull/14#discussion_r38840985"},"pull_request":{"href":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14"}}},"pull_request":{"url":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14","id":43563047,"html_url":"https://github.com/crowbar/crowbar-core/pull/14","diff_url":"https://github.com/crowbar/crowbar-core/pull/14.diff","patch_url":"https://github.com/crowbar/crowbar-core/pull/14.patch","issue_url":"https://api.github.com/repos/crowbar/crowbar-core/issues/14","number":14,"state":"open","locked":false,"title":"enhance locking and pause chef-client daemon when applying proposals","user":{"login":"aspiers","id":100738,"avatar_url":"https://avatars.githubusercontent.com/u/100738?v=3","gravatar_id":"","url":"https://api.github.com/users/aspiers","html_url":"https://github.com/aspiers","followers_url":"https://api.github.com/users/aspiers/followers","following_url":"https://api.github.com/users/aspiers/following{/other_user}","gists_url":"https://api.github.com/users/aspiers/gists{/gist_id}","starred_url":"https://api.github.com/users/aspiers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspiers/subscriptions","organizations_url":"https://api.github.com/users/aspiers/orgs","repos_url":"https://api.github.com/users/aspiers/repos","events_url":"https://api.github.com/users/aspiers/events{/privacy}","received_events_url":"https://api.github.com/users/aspiers/received_events","type":"User","site_admin":false},"body":"Reworking of #5 to address concurrency issues, make the interface between the two locking subclasses consistent, and DRY up the calling code.","created_at":"2015-08-27T20:30:03Z","updated_at":"2015-09-07T08:08:27Z","closed_at":null,"merged_at":null,"merge_commit_sha":"755056385f9b781f602bec1db4b0889943d2a89b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14/commits","review_comments_url":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14/comments","review_comment_url":"https://api.github.com/repos/crowbar/crowbar-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/crowbar/crowbar-core/issues/14/comments","statuses_url":"https://api.github.com/repos/crowbar/crowbar-core/statuses/99f52dab8fdcdd99a037c4e074e735c40cda5287","head":{"label":"aspiers:feature/apply-lock","ref":"feature/apply-lock","sha":"99f52dab8fdcdd99a037c4e074e735c40cda5287","user":{"login":"aspiers","id":100738,"avatar_url":"https://avatars.githubusercontent.com/u/100738?v=3","gravatar_id":"","url":"https://api.github.com/users/aspiers","html_url":"https://github.com/aspiers","followers_url":"https://api.github.com/users/aspiers/followers","following_url":"https://api.github.com/users/aspiers/following{/other_user}","gists_url":"https://api.github.com/users/aspiers/gists{/gist_id}","starred_url":"https://api.github.com/users/aspiers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspiers/subscriptions","organizations_url":"https://api.github.com/users/aspiers/orgs","repos_url":"https://api.github.com/users/aspiers/repos","events_url":"https://api.github.com/users/aspiers/events{/privacy}","received_events_url":"https://api.github.com/users/aspiers/received_events","type":"User","site_admin":false},"repo":{"id":41430233,"name":"crowbar-core","full_name":"aspiers/crowbar-core","owner":{"login":"aspiers","id":100738,"avatar_url":"https://avatars.githubusercontent.com/u/100738?v=3","gravatar_id":"","url":"https://api.github.com/users/aspiers","html_url":"https://github.com/aspiers","followers_url":"https://api.github.com/users/aspiers/followers","following_url":"https://api.github.com/users/aspiers/following{/other_user}","gists_url":"https://api.github.com/users/aspiers/gists{/gist_id}","starred_url":"https://api.github.com/users/aspiers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspiers/subscriptions","organizations_url":"https://api.github.com/users/aspiers/orgs","repos_url":"https://api.github.com/users/aspiers/repos","events_url":"https://api.github.com/users/aspiers/events{/privacy}","received_events_url":"https://api.github.com/users/aspiers/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aspiers/crowbar-core","description":"Core deployment for Crowbar","fork":true,"url":"https://api.github.com/repos/aspiers/crowbar-core","forks_url":"https://api.github.com/repos/aspiers/crowbar-core/forks","keys_url":"https://api.github.com/repos/aspiers/crowbar-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aspiers/crowbar-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aspiers/crowbar-core/teams","hooks_url":"https://api.github.com/repos/aspiers/crowbar-core/hooks","issue_events_url":"https://api.github.com/repos/aspiers/crowbar-core/issues/events{/number}","events_url":"https://api.github.com/repos/aspiers/crowbar-core/events","assignees_url":"https://api.github.com/repos/aspiers/crowbar-core/assignees{/user}","branches_url":"https://api.github.com/repos/aspiers/crowbar-core/branches{/branch}","tags_url":"https://api.github.com/repos/aspiers/crowbar-core/tags","blobs_url":"https://api.github.com/repos/aspiers/crowbar-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aspiers/crowbar-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aspiers/crowbar-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/aspiers/crowbar-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aspiers/crowbar-core/statuses/{sha}","languages_url":"https://api.github.com/repos/aspiers/crowbar-core/languages","stargazers_url":"https://api.github.com/repos/aspiers/crowbar-core/stargazers","contributors_url":"https://api.github.com/repos/aspiers/crowbar-core/contributors","subscribers_url":"https://api.github.com/repos/aspiers/crowbar-core/subscribers","subscription_url":"https://api.github.com/repos/aspiers/crowbar-core/subscription","commits_url":"https://api.github.com/repos/aspiers/crowbar-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/aspiers/crowbar-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/aspiers/crowbar-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/aspiers/crowbar-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/aspiers/crowbar-core/contents/{+path}","compare_url":"https://api.github.com/repos/aspiers/crowbar-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aspiers/crowbar-core/merges","archive_url":"https://api.github.com/repos/aspiers/crowbar-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aspiers/crowbar-core/downloads","issues_url":"https://api.github.com/repos/aspiers/crowbar-core/issues{/number}","pulls_url":"https://api.github.com/repos/aspiers/crowbar-core/pulls{/number}","milestones_url":"https://api.github.com/repos/aspiers/crowbar-core/milestones{/number}","notifications_url":"https://api.github.com/repos/aspiers/crowbar-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aspiers/crowbar-core/labels{/name}","releases_url":"https://api.github.com/repos/aspiers/crowbar-core/releases{/id}","created_at":"2015-08-26T14:25:22Z","updated_at":"2015-08-26T14:25:24Z","pushed_at":"2015-09-07T01:19:47Z","git_url":"git://github.com/aspiers/crowbar-core.git","ssh_url":"git@github.com:aspiers/crowbar-core.git","clone_url":"https://github.com/aspiers/crowbar-core.git","svn_url":"https://github.com/aspiers/crowbar-core","homepage":null,"size":102688,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"crowbar:master","ref":"master","sha":"c33751d9bf81d163956986c357bbe5ca02558d09","user":{"login":"crowbar","id":2910205,"avatar_url":"https://avatars.githubusercontent.com/u/2910205?v=3","gravatar_id":"","url":"https://api.github.com/users/crowbar","html_url":"https://github.com/crowbar","followers_url":"https://api.github.com/users/crowbar/followers","following_url":"https://api.github.com/users/crowbar/following{/other_user}","gists_url":"https://api.github.com/users/crowbar/gists{/gist_id}","starred_url":"https://api.github.com/users/crowbar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crowbar/subscriptions","organizations_url":"https://api.github.com/users/crowbar/orgs","repos_url":"https://api.github.com/users/crowbar/repos","events_url":"https://api.github.com/users/crowbar/events{/privacy}","received_events_url":"https://api.github.com/users/crowbar/received_events","type":"Organization","site_admin":false},"repo":{"id":39003227,"name":"crowbar-core","full_name":"crowbar/crowbar-core","owner":{"login":"crowbar","id":2910205,"avatar_url":"https://avatars.githubusercontent.com/u/2910205?v=3","gravatar_id":"","url":"https://api.github.com/users/crowbar","html_url":"https://github.com/crowbar","followers_url":"https://api.github.com/users/crowbar/followers","following_url":"https://api.github.com/users/crowbar/following{/other_user}","gists_url":"https://api.github.com/users/crowbar/gists{/gist_id}","starred_url":"https://api.github.com/users/crowbar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crowbar/subscriptions","organizations_url":"https://api.github.com/users/crowbar/orgs","repos_url":"https://api.github.com/users/crowbar/repos","events_url":"https://api.github.com/users/crowbar/events{/privacy}","received_events_url":"https://api.github.com/users/crowbar/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/crowbar/crowbar-core","description":"Core deployment for Crowbar","fork":false,"url":"https://api.github.com/repos/crowbar/crowbar-core","forks_url":"https://api.github.com/repos/crowbar/crowbar-core/forks","keys_url":"https://api.github.com/repos/crowbar/crowbar-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crowbar/crowbar-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crowbar/crowbar-core/teams","hooks_url":"https://api.github.com/repos/crowbar/crowbar-core/hooks","issue_events_url":"https://api.github.com/repos/crowbar/crowbar-core/issues/events{/number}","events_url":"https://api.github.com/repos/crowbar/crowbar-core/events","assignees_url":"https://api.github.com/repos/crowbar/crowbar-core/assignees{/user}","branches_url":"https://api.github.com/repos/crowbar/crowbar-core/branches{/branch}","tags_url":"https://api.github.com/repos/crowbar/crowbar-core/tags","blobs_url":"https://api.github.com/repos/crowbar/crowbar-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crowbar/crowbar-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crowbar/crowbar-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/crowbar/crowbar-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crowbar/crowbar-core/statuses/{sha}","languages_url":"https://api.github.com/repos/crowbar/crowbar-core/languages","stargazers_url":"https://api.github.com/repos/crowbar/crowbar-core/stargazers","contributors_url":"https://api.github.com/repos/crowbar/crowbar-core/contributors","subscribers_url":"https://api.github.com/repos/crowbar/crowbar-core/subscribers","subscription_url":"https://api.github.com/repos/crowbar/crowbar-core/subscription","commits_url":"https://api.github.com/repos/crowbar/crowbar-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/crowbar/crowbar-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/crowbar/crowbar-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/crowbar/crowbar-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/crowbar/crowbar-core/contents/{+path}","compare_url":"https://api.github.com/repos/crowbar/crowbar-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crowbar/crowbar-core/merges","archive_url":"https://api.github.com/repos/crowbar/crowbar-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crowbar/crowbar-core/downloads","issues_url":"https://api.github.com/repos/crowbar/crowbar-core/issues{/number}","pulls_url":"https://api.github.com/repos/crowbar/crowbar-core/pulls{/number}","milestones_url":"https://api.github.com/repos/crowbar/crowbar-core/milestones{/number}","notifications_url":"https://api.github.com/repos/crowbar/crowbar-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crowbar/crowbar-core/labels{/name}","releases_url":"https://api.github.com/repos/crowbar/crowbar-core/releases{/id}","created_at":"2015-07-13T09:31:28Z","updated_at":"2015-08-20T13:43:47Z","pushed_at":"2015-09-07T01:19:47Z","git_url":"git://github.com/crowbar/crowbar-core.git","ssh_url":"git@github.com:crowbar/crowbar-core.git","clone_url":"https://github.com/crowbar/crowbar-core.git","svn_url":"https://github.com/crowbar/crowbar-core","homepage":null,"size":118933,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":15,"mirror_url":null,"open_issues_count":10,"forks":15,"open_issues":10,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14"},"html":{"href":"https://github.com/crowbar/crowbar-core/pull/14"},"issue":{"href":"https://api.github.com/repos/crowbar/crowbar-core/issues/14"},"comments":{"href":"https://api.github.com/repos/crowbar/crowbar-core/issues/14/comments"},"review_comments":{"href":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14/comments"},"review_comment":{"href":"https://api.github.com/repos/crowbar/crowbar-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/crowbar/crowbar-core/pulls/14/commits"},"statuses":{"href":"https://api.github.com/repos/crowbar/crowbar-core/statuses/99f52dab8fdcdd99a037c4e074e735c40cda5287"}}}}