{"action":"created","comment":{"url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/comments/35119607","id":35119607,"diff_hunk":"@@ -0,0 +1,104 @@\n+require 'fileutils'\n+\n+class TsuruDeployClient\n+\n+  def initialize(\n+    api_client:, logger:, environment:, host:, tsuru_home:, protocol: \"https://\"\n+  )\n+    target = URI.parse(protocol + environment + \"-api.\" + host)\n+\n+    targets_path = File.join(ENV[\"HOME\"], \".tsuru_targets\")\n+    unless File.exists?(targets_path) and \\\n+    File.read(targets_path).include?(target.to_s)\n+      system \"tsuru target-add #{environment} #{target}\"\n+    end\n+\n+    target_path = File.join(ENV[\"HOME\"], \".tsuru_target\")\n+    unless File.exists?(target_path) and \\\n+    File.read(target_path).include?(target.to_s)\n+      system \"tsuru target-set #{environment}\"\n+    end\n+\n+    @api_client = api_client\n+    @logger = logger\n+    @tsuru_home = tsuru_home\n+  end\n+\n+  def api_client\n+    return @api_client\n+  end\n+\n+  def logger\n+    return @logger\n+  end\n+\n+  def deploy_app(user:, app:, env_vars: {}, postgres: '', git: false, units: 3)\n+    self.logger.info(\"Going to deploy #{app[:name]}\")\n+    self.logger.debug(\"Login user #{user[:email]} of the team #{user[:team]}\")\n+    self.api_client.login(user[:email], user[:password])\n+\n+    if not self.api_client.list_apps().include? app[:name]\n+      self.logger.info(\"Create application #{app[:name]} \" \\\n+        \"on the platform #{app[:platform]}\")\n+      self.api_client.create_app(app[:name], app[:platform])\n+    end\n+\n+    # Set environment variables, if needed\n+    if env_vars.length > 0\n+      env_vars.each do |key,value|\n+        self.api_client.set_env_var(app[:name], key, value)\n+      end\n+    end\n+\n+    if postgres != ''\n+      instance_name = postgres\n+      unless self.api_client.list_service_instances().include? instance_name\n+          self.logger.debug(\"Add postgres service instance #{instance_name}\")\n+          self.api_client.add_service_instance(\"postgresql\", instance_name)\n+      end\n+\n+      unless self.api_client.app_has_service(app[:name], instance_name)\n+        self.logger.debug(\"Bind service #{instance_name} to #{app[:name]}\")\n+        self.api_client.bind_service_to_app(instance_name, app[:name])\n+      end\n+    end\n+\n+    system \"echo '#{@api_client.get_token}' > ${HOME}/.tsuru_token\"\n+\n+    if git\n+      self.logger.debug(\"Deploy #{app[:name]} via git\")\n+      git_deploy(\n+        app[:dir],\n+        self.api_client.get_app_repository(app[:name]),\n+        user[:key]\n+      )\n+    else\n+      self.logger.debug(\"Deploy #{app[:name]} via app-deploy\")\n+      app_deploy(app[:dir], app[:name])\n+    end\n+\n+    deployed_units = self.api_client.get_app_info(app[:name])[\"units\"].length\n+    if deployed_units < units\n+      self.api_client.add_units(units - deployed_units, app[:name])\n+    end\n+  end\n+\n+  private\n+\n+  def app_deploy(path, app_name)\n+    FileUtils.cd(path)\n+    system \"tsuru app-deploy * -a #{app_name}\"","path":"bulk_deploy/tsuru_deploy_client.rb","position":85,"original_position":90,"commit_id":"766b146f9610af6f01dc5bcbd3e83e4251286f09","original_commit_id":"873a39765979c4e40e22fbbb4bed6805d413b296","user":{"login":"RichardKnop","id":2325888,"avatar_url":"https://avatars.githubusercontent.com/u/2325888?v=3","gravatar_id":"","url":"https://api.github.com/users/RichardKnop","html_url":"https://github.com/RichardKnop","followers_url":"https://api.github.com/users/RichardKnop/followers","following_url":"https://api.github.com/users/RichardKnop/following{/other_user}","gists_url":"https://api.github.com/users/RichardKnop/gists{/gist_id}","starred_url":"https://api.github.com/users/RichardKnop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RichardKnop/subscriptions","organizations_url":"https://api.github.com/users/RichardKnop/orgs","repos_url":"https://api.github.com/users/RichardKnop/repos","events_url":"https://api.github.com/users/RichardKnop/events{/privacy}","received_events_url":"https://api.github.com/users/RichardKnop/received_events","type":"User","site_admin":false},"body":"Yes, I need to work on better error handling.","created_at":"2015-07-21T15:59:21Z","updated_at":"2015-07-21T15:59:21Z","html_url":"https://github.com/alphagov/tsuru-performance-tests/pull/2#discussion_r35119607","pull_request_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/comments/35119607"},"html":{"href":"https://github.com/alphagov/tsuru-performance-tests/pull/2#discussion_r35119607"},"pull_request":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2","id":40010775,"html_url":"https://github.com/alphagov/tsuru-performance-tests/pull/2","diff_url":"https://github.com/alphagov/tsuru-performance-tests/pull/2.diff","patch_url":"https://github.com/alphagov/tsuru-performance-tests/pull/2.patch","issue_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/2","number":2,"state":"open","locked":false,"title":"[#97906714] Feature/deploy lots of teams with lost of apps","user":{"login":"RichardKnop","id":2325888,"avatar_url":"https://avatars.githubusercontent.com/u/2325888?v=3","gravatar_id":"","url":"https://api.github.com/users/RichardKnop","html_url":"https://github.com/RichardKnop","followers_url":"https://api.github.com/users/RichardKnop/followers","following_url":"https://api.github.com/users/RichardKnop/following{/other_user}","gists_url":"https://api.github.com/users/RichardKnop/gists{/gist_id}","starred_url":"https://api.github.com/users/RichardKnop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RichardKnop/subscriptions","organizations_url":"https://api.github.com/users/RichardKnop/orgs","repos_url":"https://api.github.com/users/RichardKnop/repos","events_url":"https://api.github.com/users/RichardKnop/events{/privacy}","received_events_url":"https://api.github.com/users/RichardKnop/received_events","type":"User","site_admin":false},"body":"What\r\n-------\r\n\r\nWe have added a new Ruby script to deploy lots of teams and apps. Code is located inside `bulk_deploy` directory.\r\n\r\nYou can see all options of the script by calling it without arguments:\r\n\r\n```\r\nbundle exec ruby deploy.rb -e richard -h tsuru2.paas.alphagov.co.uk\r\nUsage: bundle exec deploy.rb action [options]\r\nExample: bundle exec deploy.rb --environment perf-env --host-suffix tsuru2.paas.alphagov.co.uk apply\r\n    -e, --environment=e              Environment [Required]\r\n    -h, --host-suffix=h              Host suffix [Required]\r\n    -T, --api-token=T                API token [Required]\r\n    -S, --search-api-token=S         Search API token [Required]\r\n    -t, --team-count=t               Team count [Default: 2]\r\n    -u, --users-per-team=u           Users per team [Default: 7]\r\n    -a, --apps-per-team=a            Applications per team [Default: 5]\r\n    -l, --log-level=l                Log level [Default: info]\r\n    -n, --units_per_app=n            Units per application [Default: 3]\r\n    -s, --state-file=f               State file [Default: /Users/richardknop/github.com/alphagov/tsuru-performance-tests/bulk_deploy/deploy.state]\r\n```\r\n\r\nThere are two actions:\r\n\r\n  * apply: deploys teams and apps\r\n  * destroy: removes deployed teams and apps (based on the state file)\r\n\r\nBy default, these 5 apps are deployed right now:\r\n\r\n  * example Java Jetty app\r\n  * example Flask app\r\n  * Digital Marketplace admin frontend app\r\n  * Digital Marketplace admin suppliers app\r\n  * Digital Marketplace admin buyers app\r\n\r\nDigital Marketplace backend apps are not deployed yet, so frontend apps use preview environment.\r\n\r\nUsage\r\n---------\r\n\r\n```\r\nbundle exec ruby deploy.rb -e richard -h tsuru2.paas.alphagov.co.uk apply -t 2 -n 3 -T apitoken -S searchapitoken\r\n```\r\n\r\nWill create 2 teams, each with 7 users and deploy 5 apps (listed above) for the each team with 3 units per app.\r\n\r\nIt will use `richard` environment on GCE.\r\n\r\nDeployed apps can be removed by running the destroy command:\r\n\r\n```\r\nbundle exec ruby deploy.rb -e richard -h tsuru2.paas.alphagov.co.uk destroy -t 2 -n 3\r\n```\r\n\r\nReview\r\n----------\r\n\r\nAnybody except Richard or Colin should review this PR.","created_at":"2015-07-15T11:01:59Z","updated_at":"2015-07-21T15:59:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d96774c17258bbbe35e9594a751a41af5d9285a2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2/commits","review_comments_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2/comments","review_comment_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/comments{/number}","comments_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/2/comments","statuses_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/statuses/766b146f9610af6f01dc5bcbd3e83e4251286f09","head":{"label":"alphagov:feature/deploy-lots-of-teams-with-lost-of-apps","ref":"feature/deploy-lots-of-teams-with-lost-of-apps","sha":"766b146f9610af6f01dc5bcbd3e83e4251286f09","user":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":38481089,"name":"tsuru-performance-tests","full_name":"alphagov/tsuru-performance-tests","owner":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/tsuru-performance-tests","description":"","fork":false,"url":"https://api.github.com/repos/alphagov/tsuru-performance-tests","forks_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/forks","keys_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/teams","hooks_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/hooks","issue_events_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/events","assignees_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/tags","blobs_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/languages","stargazers_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/stargazers","contributors_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/contributors","subscribers_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/subscribers","subscription_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/subscription","commits_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/merges","archive_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/downloads","issues_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/releases{/id}","created_at":"2015-07-03T08:15:28Z","updated_at":"2015-07-08T12:59:38Z","pushed_at":"2015-07-21T15:58:15Z","git_url":"git://github.com/alphagov/tsuru-performance-tests.git","ssh_url":"git@github.com:alphagov/tsuru-performance-tests.git","clone_url":"https://github.com/alphagov/tsuru-performance-tests.git","svn_url":"https://github.com/alphagov/tsuru-performance-tests","homepage":null,"size":324,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"alphagov:master","ref":"master","sha":"2f3f89f31329cd5075df35db66a950cdcc07c835","user":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":38481089,"name":"tsuru-performance-tests","full_name":"alphagov/tsuru-performance-tests","owner":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/tsuru-performance-tests","description":"","fork":false,"url":"https://api.github.com/repos/alphagov/tsuru-performance-tests","forks_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/forks","keys_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/teams","hooks_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/hooks","issue_events_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/events","assignees_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/tags","blobs_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/languages","stargazers_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/stargazers","contributors_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/contributors","subscribers_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/subscribers","subscription_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/subscription","commits_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/merges","archive_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/downloads","issues_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/tsuru-performance-tests/releases{/id}","created_at":"2015-07-03T08:15:28Z","updated_at":"2015-07-08T12:59:38Z","pushed_at":"2015-07-21T15:58:15Z","git_url":"git://github.com/alphagov/tsuru-performance-tests.git","ssh_url":"git@github.com:alphagov/tsuru-performance-tests.git","clone_url":"https://github.com/alphagov/tsuru-performance-tests.git","svn_url":"https://github.com/alphagov/tsuru-performance-tests","homepage":null,"size":324,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2"},"html":{"href":"https://github.com/alphagov/tsuru-performance-tests/pull/2"},"issue":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/2"},"comments":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/alphagov/tsuru-performance-tests/statuses/766b146f9610af6f01dc5bcbd3e83e4251286f09"}}}}