{"action":"created","comment":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/38324686","id":38324686,"diff_hunk":"@@ -638,18 +625,95 @@ def _deploy_without_asking():\n         # hard update of manifest.json since we're about to force restart\n         # all services\n         _execute_with_timing(update_manifest)\n+        _execute_with_timing(clean_releases)\n+    except PreindexNotFinished:\n+        mail_admins(\n+            \" You can't deploy yet\",\n+            (\"Preindexing is taking a while, so hold tight \"\n+             \"and wait for an email saying it's done. \"\n+             \"Thank you for using AWESOME DEPLOY.\")\n+        )\n     except Exception:\n         _execute_with_timing(mail_admins, \"Deploy failed\", \"You had better check the logs.\")\n         # hopefully bring the server back to life\n         _execute_with_timing(services_restart)\n         raise\n     else:\n+        _execute_with_timing(update_current)\n         _execute_with_timing(services_restart)\n+        _execute_with_timing(record_successful_release)\n         url = _tag_commit()\n         _execute_with_timing(record_successful_deploy, url)\n \n \n @task\n+@roles(ROLES_ALL_SRC)\n+def update_current(release=None):\n+    sudo('ln -nfs {} {}'.format(release or env.code_root, env.code_current))\n+\n+\n+@task\n+@roles(ROLES_ALL_SRC)\n+def unlink_current():\n+    if files.exists(env.code_current):\n+        sudo('unlink {}'.format(env.code_current))\n+\n+\n+@task\n+@roles(ROLES_ALL_SRC)\n+@parallel\n+def create_code_dir():\n+    sudo('mkdir -p {}'.format(env.code_root))\n+\n+\n+@roles(ROLES_ALL_SRC)\n+def copy_localsettings():\n+    sudo('cp {}/localsettings.py {}/localsettings.py'.format(env.code_current, env.code_root))\n+\n+\n+@roles(ROLES_TOUCHFORMS)\n+def copy_tf_localsettings():\n+    sudo(\n+        'cp {}/submodules/touchforms-src/touchforms/backend/localsettings.py '\n+        '{}/submodules/touchforms-src/touchforms/backend/localsettings.py'.format(\n+            env.code_current, env.code_root\n+        ))\n+\n+\n+@task\n+@roles(ROLES_ALL_SRC)\n+@parallel\n+def clean_releases(keep=3):\n+    releases = sudo('ls {}'.format(env.releases)).split()\n+    current_release = os.path.basename(sudo('readlink {}'.format(env.code_current)))\n+\n+    to_remove = []\n+    valid_releases = 0\n+    with cd(env.root):\n+        for index, release in enumerate(reversed(releases)):\n+            if (release == current_release or release == os.path.basename(env.code_root)):\n+                valid_releases += 1\n+            elif (files.contains(RELEASE_RECORD, release)):\n+                valid_releases += 1\n+                if valid_releases > keep:\n+                    to_remove.append(release)\n+            else:\n+                # cleans all releases that were not successful deploys\n+                to_remove.append(release)\n+\n+    if len(to_remove) == len(releases):\n+        print 'Aborting, about to remove every release'\n+        exit()\n+\n+    if os.path.basename(env.code_root) in to_remove:\n+        print 'Aborting, about to remove current release'\n+        exit()","path":"fab/fabfile.py","position":404,"original_position":404,"commit_id":"89372ded6de96f066d09e2aea91b479a0b885100","original_commit_id":"89372ded6de96f066d09e2aea91b479a0b885100","user":{"login":"snopoke","id":249606,"avatar_url":"https://avatars.githubusercontent.com/u/249606?v=3","gravatar_id":"","url":"https://api.github.com/users/snopoke","html_url":"https://github.com/snopoke","followers_url":"https://api.github.com/users/snopoke/followers","following_url":"https://api.github.com/users/snopoke/following{/other_user}","gists_url":"https://api.github.com/users/snopoke/gists{/gist_id}","starred_url":"https://api.github.com/users/snopoke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/snopoke/subscriptions","organizations_url":"https://api.github.com/users/snopoke/orgs","repos_url":"https://api.github.com/users/snopoke/repos","events_url":"https://api.github.com/users/snopoke/events{/privacy}","received_events_url":"https://api.github.com/users/snopoke/received_events","type":"User","site_admin":false},"body":"Can always print in red or something to make it stand out: http://docs.fabfile.org/en/1.10/api/core/colors.html","created_at":"2015-08-31T15:39:46Z","updated_at":"2015-08-31T15:39:46Z","html_url":"https://github.com/dimagi/commcare-hq/pull/7711#discussion_r38324686","pull_request_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711","_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/38324686"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/7711#discussion_r38324686"},"pull_request":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711"}}},"pull_request":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711","id":41326150,"html_url":"https://github.com/dimagi/commcare-hq/pull/7711","diff_url":"https://github.com/dimagi/commcare-hq/pull/7711.diff","patch_url":"https://github.com/dimagi/commcare-hq/pull/7711.patch","issue_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/7711","number":7711,"state":"open","locked":false,"title":"Optimize deploy","user":{"login":"benrudolph","id":918514,"avatar_url":"https://avatars.githubusercontent.com/u/918514?v=3","gravatar_id":"","url":"https://api.github.com/users/benrudolph","html_url":"https://github.com/benrudolph","followers_url":"https://api.github.com/users/benrudolph/followers","following_url":"https://api.github.com/users/benrudolph/following{/other_user}","gists_url":"https://api.github.com/users/benrudolph/gists{/gist_id}","starred_url":"https://api.github.com/users/benrudolph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benrudolph/subscriptions","organizations_url":"https://api.github.com/users/benrudolph/orgs","repos_url":"https://api.github.com/users/benrudolph/repos","events_url":"https://api.github.com/users/benrudolph/events{/privacy}","received_events_url":"https://api.github.com/users/benrudolph/received_events","type":"User","site_admin":false},"body":"mainly just opened this for discussion. @dannyroberts my main optimization i want to do here is change the way we update the code. currently we have our django workers point to `code_root` and we also `git pull` to that same dir.\r\n\r\ni'm proposing we have a new structure:\r\n`www/production/releases/<timestamp>`\r\nthen we point our django workers to:\r\n`www/production/current`\r\nwhere `current` is a symlink to the most recent deploy in `releases`. this allows us to do two important things:\r\n1. Only reset the the symlink when we're ready, i.e. when the code updates have finished\r\n2. Rollback would be as simple as pointing the symlink to the previous deploy which would be ordered by timestamp.\r\n\r\ni actually don't think this change would be do invasive. would be interested to sit down with you to see if we can iterate on the deploy. (this setup is similar to how other deploy systems i've seen work)\r\n\r\nbuddy: @orangejenny, @TylerSheffels may be interested\r\n\r\nrelated: https://github.com/dimagi/commcarehq-ansible/pull/187/files","created_at":"2015-07-31T10:22:06Z","updated_at":"2015-08-31T15:39:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1e6d795266fa0779fe54f4d632b2955c056d5530","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711/commits","review_comments_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711/comments","review_comment_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/7711/comments","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/89372ded6de96f066d09e2aea91b479a0b885100","head":{"label":"dimagi:optmize-deploy","ref":"optmize-deploy","sha":"89372ded6de96f066d09e2aea91b479a0b885100","user":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"repo":{"id":247278,"name":"commcare-hq","full_name":"dimagi/commcare-hq","owner":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dimagi/commcare-hq","description":"A server-side tool to help manage community health workers, written in Django","fork":false,"url":"https://api.github.com/repos/dimagi/commcare-hq","forks_url":"https://api.github.com/repos/dimagi/commcare-hq/forks","keys_url":"https://api.github.com/repos/dimagi/commcare-hq/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dimagi/commcare-hq/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dimagi/commcare-hq/teams","hooks_url":"https://api.github.com/repos/dimagi/commcare-hq/hooks","issue_events_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/events{/number}","events_url":"https://api.github.com/repos/dimagi/commcare-hq/events","assignees_url":"https://api.github.com/repos/dimagi/commcare-hq/assignees{/user}","branches_url":"https://api.github.com/repos/dimagi/commcare-hq/branches{/branch}","tags_url":"https://api.github.com/repos/dimagi/commcare-hq/tags","blobs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dimagi/commcare-hq/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/refs{/sha}","trees_url":"https://api.github.com/repos/dimagi/commcare-hq/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/{sha}","languages_url":"https://api.github.com/repos/dimagi/commcare-hq/languages","stargazers_url":"https://api.github.com/repos/dimagi/commcare-hq/stargazers","contributors_url":"https://api.github.com/repos/dimagi/commcare-hq/contributors","subscribers_url":"https://api.github.com/repos/dimagi/commcare-hq/subscribers","subscription_url":"https://api.github.com/repos/dimagi/commcare-hq/subscription","commits_url":"https://api.github.com/repos/dimagi/commcare-hq/commits{/sha}","git_commits_url":"https://api.github.com/repos/dimagi/commcare-hq/git/commits{/sha}","comments_url":"https://api.github.com/repos/dimagi/commcare-hq/comments{/number}","issue_comment_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/comments{/number}","contents_url":"https://api.github.com/repos/dimagi/commcare-hq/contents/{+path}","compare_url":"https://api.github.com/repos/dimagi/commcare-hq/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dimagi/commcare-hq/merges","archive_url":"https://api.github.com/repos/dimagi/commcare-hq/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dimagi/commcare-hq/downloads","issues_url":"https://api.github.com/repos/dimagi/commcare-hq/issues{/number}","pulls_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls{/number}","milestones_url":"https://api.github.com/repos/dimagi/commcare-hq/milestones{/number}","notifications_url":"https://api.github.com/repos/dimagi/commcare-hq/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dimagi/commcare-hq/labels{/name}","releases_url":"https://api.github.com/repos/dimagi/commcare-hq/releases{/id}","created_at":"2009-07-09T17:00:07Z","updated_at":"2015-08-08T13:51:23Z","pushed_at":"2015-08-31T15:35:52Z","git_url":"git://github.com/dimagi/commcare-hq.git","ssh_url":"git@github.com:dimagi/commcare-hq.git","clone_url":"https://github.com/dimagi/commcare-hq.git","svn_url":"https://github.com/dimagi/commcare-hq","homepage":"http://www.commcarehq.org","size":408695,"stargazers_count":49,"watchers_count":49,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":61,"mirror_url":null,"open_issues_count":58,"forks":61,"open_issues":58,"watchers":49,"default_branch":"master"}},"base":{"label":"dimagi:master","ref":"master","sha":"97a251e2cfaa01af8f8177dbd5d1432ffa3c09a7","user":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"repo":{"id":247278,"name":"commcare-hq","full_name":"dimagi/commcare-hq","owner":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dimagi/commcare-hq","description":"A server-side tool to help manage community health workers, written in Django","fork":false,"url":"https://api.github.com/repos/dimagi/commcare-hq","forks_url":"https://api.github.com/repos/dimagi/commcare-hq/forks","keys_url":"https://api.github.com/repos/dimagi/commcare-hq/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dimagi/commcare-hq/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dimagi/commcare-hq/teams","hooks_url":"https://api.github.com/repos/dimagi/commcare-hq/hooks","issue_events_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/events{/number}","events_url":"https://api.github.com/repos/dimagi/commcare-hq/events","assignees_url":"https://api.github.com/repos/dimagi/commcare-hq/assignees{/user}","branches_url":"https://api.github.com/repos/dimagi/commcare-hq/branches{/branch}","tags_url":"https://api.github.com/repos/dimagi/commcare-hq/tags","blobs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dimagi/commcare-hq/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dimagi/commcare-hq/git/refs{/sha}","trees_url":"https://api.github.com/repos/dimagi/commcare-hq/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dimagi/commcare-hq/statuses/{sha}","languages_url":"https://api.github.com/repos/dimagi/commcare-hq/languages","stargazers_url":"https://api.github.com/repos/dimagi/commcare-hq/stargazers","contributors_url":"https://api.github.com/repos/dimagi/commcare-hq/contributors","subscribers_url":"https://api.github.com/repos/dimagi/commcare-hq/subscribers","subscription_url":"https://api.github.com/repos/dimagi/commcare-hq/subscription","commits_url":"https://api.github.com/repos/dimagi/commcare-hq/commits{/sha}","git_commits_url":"https://api.github.com/repos/dimagi/commcare-hq/git/commits{/sha}","comments_url":"https://api.github.com/repos/dimagi/commcare-hq/comments{/number}","issue_comment_url":"https://api.github.com/repos/dimagi/commcare-hq/issues/comments{/number}","contents_url":"https://api.github.com/repos/dimagi/commcare-hq/contents/{+path}","compare_url":"https://api.github.com/repos/dimagi/commcare-hq/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dimagi/commcare-hq/merges","archive_url":"https://api.github.com/repos/dimagi/commcare-hq/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dimagi/commcare-hq/downloads","issues_url":"https://api.github.com/repos/dimagi/commcare-hq/issues{/number}","pulls_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls{/number}","milestones_url":"https://api.github.com/repos/dimagi/commcare-hq/milestones{/number}","notifications_url":"https://api.github.com/repos/dimagi/commcare-hq/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dimagi/commcare-hq/labels{/name}","releases_url":"https://api.github.com/repos/dimagi/commcare-hq/releases{/id}","created_at":"2009-07-09T17:00:07Z","updated_at":"2015-08-08T13:51:23Z","pushed_at":"2015-08-31T15:35:52Z","git_url":"git://github.com/dimagi/commcare-hq.git","ssh_url":"git@github.com:dimagi/commcare-hq.git","clone_url":"https://github.com/dimagi/commcare-hq.git","svn_url":"https://github.com/dimagi/commcare-hq","homepage":"http://www.commcarehq.org","size":408695,"stargazers_count":49,"watchers_count":49,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":61,"mirror_url":null,"open_issues_count":58,"forks":61,"open_issues":58,"watchers":49,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/7711"},"issue":{"href":"https://api.github.com/repos/dimagi/commcare-hq/issues/7711"},"comments":{"href":"https://api.github.com/repos/dimagi/commcare-hq/issues/7711/comments"},"review_comments":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711/comments"},"review_comment":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/7711/commits"},"statuses":{"href":"https://api.github.com/repos/dimagi/commcare-hq/statuses/89372ded6de96f066d09e2aea91b479a0b885100"}}}}