{"action":"created","comment":{"url":"https://api.github.com/repos/flynn/flynn/pulls/comments/43317286","id":43317286,"diff_hunk":"@@ -0,0 +1,358 @@\n+package domain_migration\n+\n+import (\n+\t\"encoding/json\"\n+\t\"errors\"\n+\t\"fmt\"\n+\t\"strings\"\n+\t\"time\"\n+\n+\t\"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/que-go\"\n+\t\"github.com/flynn/flynn/Godeps/_workspace/src/gopkg.in/inconshreveable/log15.v2\"\n+\t\"github.com/flynn/flynn/controller/client\"\n+\tct \"github.com/flynn/flynn/controller/types\"\n+\t\"github.com/flynn/flynn/pkg/postgres\"\n+\t\"github.com/flynn/flynn/pkg/tlscert\"\n+\t\"github.com/flynn/flynn/router/types\"\n+)\n+\n+type context struct {\n+\tdb     *postgres.DB\n+\tclient *controller.Client\n+\tlogger log15.Logger\n+}\n+\n+type migration struct {\n+\tdb     *postgres.DB\n+\tclient *controller.Client\n+\tlogger log15.Logger\n+\tdm     *ct.DomainMigration\n+}\n+\n+func JobHandler(db *postgres.DB, client *controller.Client, logger log15.Logger) func(*que.Job) error {\n+\treturn (&context{db, client, logger}).HandleDomainMigration\n+}\n+\n+func (c *context) HandleDomainMigration(job *que.Job) (err error) {\n+\tlog := c.logger.New(\"fn\", \"HandleDomainMigration\")\n+\tlog.Info(\"handling domain migration\", \"job_id\", job.ID, \"error_count\", job.ErrorCount)\n+\n+\tvar dm *ct.DomainMigration\n+\tif err := json.Unmarshal(job.Args, &dm); err != nil {\n+\t\tlog.Error(\"error unmarshaling job\", \"err\", err)\n+\t\treturn err\n+\t}\n+\n+\tlog = log.New(\"domain_migration\", dm.ID)\n+\n+\tm := &migration{\n+\t\tdb:     c.db,\n+\t\tclient: c.client,\n+\t\tlogger: log,\n+\t\tdm:     dm,\n+\t}\n+\n+\tif err := m.db.QueryRow(\"SELECT old_domain, domain, old_tls_cert, tls_cert, created_at, finished_at FROM domain_migrations WHERE migration_id = $1\", dm.ID).Scan(&dm.OldDomain, &dm.Domain, &dm.OldTLSCert, &dm.TLSCert, &dm.CreatedAt, &dm.FinishedAt); err != nil {\n+\t\tlog.Error(\"error fetching postgres record\", \"err\", err)\n+\t\tm.createEvent(err)\n+\t\treturn err\n+\t}\n+\n+\tif dm.FinishedAt != nil {\n+\t\t// Already done\n+\t\treturn nil\n+\t}\n+\n+\treturn m.Run()\n+}\n+\n+func (m *migration) Run() error {\n+\tlog := m.logger\n+\tdm := m.dm\n+\n+\t// Generate TLS Cert if not already present\n+\tvar cert *tlscert.Cert\n+\tif dm.TLSCert == nil {\n+\t\tvar err error\n+\t\tcert, err = m.generateTLSCert()\n+\t\tif err != nil {\n+\t\t\tlog.Error(\"error generating TLS cert\", \"err\", err)\n+\t\t\tm.createEvent(err)\n+\t\t\treturn err\n+\t\t}\n+\t\tdm.TLSCert = cert\n+\t}\n+\n+\tif err := m.maybeDeployController(); err != nil {\n+\t\tlog.Error(\"error deploying controller\", \"err\", err)\n+\t\tm.createEvent(err)\n+\t\treturn err\n+\t}\n+\n+\tif err := m.maybeDeployRouter(); err != nil {\n+\t\tlog.Error(\"error deploying router\", \"err\", err)\n+\t\tm.createEvent(err)\n+\t\treturn err\n+\t}\n+\n+\tif err := m.maybeDeployDashboard(); err != nil {\n+\t\tlog.Error(\"error deploying dashboard\", \"err\", err)\n+\t\tm.createEvent(err)\n+\t\treturn err\n+\t}\n+\n+\tif err := m.createMissingRoutes(); err != nil {\n+\t\tlog.Error(\"error creating missing routes\", \"err\", err)\n+\t\tm.createEvent(err)\n+\t\treturn err\n+\t}\n+\n+\tif err := m.db.QueryRow(\"UPDATE domain_migrations SET finished_at = now() RETURNING finished_at\").Scan(&dm.FinishedAt); err != nil {\n+\t\tlog.Error(\"error setting finished_at\", \"err\", err)\n+\t\tm.createEvent(err)\n+\t\treturn err\n+\t}\n+\n+\tlog.Info(\"domain migration complete\")\n+\n+\tm.createEvent(nil)\n+\n+\treturn nil\n+}\n+\n+func (m *migration) generateTLSCert() (*tlscert.Cert, error) {\n+\thosts := []string{\n+\t\tm.dm.Domain,\n+\t\tfmt.Sprintf(\"*.%s\", m.dm.Domain),\n+\t}\n+\tcert, err := tlscert.Generate(hosts)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\tif _, err := m.db.Query(\"UPDATE domain_migrations SET tls_cert = $1 WHERE migration_id = $2\", cert, m.dm.ID); err != nil {\n+\t\treturn nil, err\n+\t}\n+\treturn cert, nil\n+}\n+\n+func dupRelease(release *ct.Release) *ct.Release {\n+\treturn &ct.Release{\n+\t\tArtifactID: release.ArtifactID,\n+\t\tEnv:        release.Env,\n+\t\tMeta:       release.Meta,\n+\t\tProcesses:  release.Processes,\n+\t}\n+}\n+\n+func (m *migration) waitForDeployment(app string) error {\n+\tlist, err := m.client.DeploymentList(app)\n+\tif err != nil || len(list) == 0 {\n+\t\treturn err\n+\t}\n+\td := list[0]\n+\tif d.Status != \"pending\" && d.Status != \"running\" {\n+\t\treturn nil\n+\t}\n+\n+\tevents := make(chan *ct.Event)\n+\tstream, err := m.client.StreamEvents(controller.StreamEventsOptions{\n+\t\tAppID:       d.AppID,\n+\t\tObjectID:    d.ID,\n+\t\tObjectTypes: []ct.EventType{ct.EventTypeDeployment},\n+\t}, events)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tdefer stream.Close()\n+\n+\tlog := m.logger.New(\"app\", app, \"deployment\", d.ID)","path":"controller/worker/domain_migration/domain_migration.go","position":173,"original_position":168,"commit_id":"4bc36ee01e7123b0ecb17532bcb561dc27f48372","original_commit_id":"02b359c1ab83ace33b2f0fd2bf377c2dccf31fec","user":{"login":"lmars","id":488515,"avatar_url":"https://avatars.githubusercontent.com/u/488515?v=3","gravatar_id":"","url":"https://api.github.com/users/lmars","html_url":"https://github.com/lmars","followers_url":"https://api.github.com/users/lmars/followers","following_url":"https://api.github.com/users/lmars/following{/other_user}","gists_url":"https://api.github.com/users/lmars/gists{/gist_id}","starred_url":"https://api.github.com/users/lmars/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lmars/subscriptions","organizations_url":"https://api.github.com/users/lmars/orgs","repos_url":"https://api.github.com/users/lmars/repos","events_url":"https://api.github.com/users/lmars/events{/privacy}","received_events_url":"https://api.github.com/users/lmars/received_events","type":"User","site_admin":false},"body":"`app.name`?","created_at":"2015-10-28T21:00:06Z","updated_at":"2015-10-28T21:00:06Z","html_url":"https://github.com/flynn/flynn/pull/1957#discussion_r43317286","pull_request_url":"https://api.github.com/repos/flynn/flynn/pulls/1957","_links":{"self":{"href":"https://api.github.com/repos/flynn/flynn/pulls/comments/43317286"},"html":{"href":"https://github.com/flynn/flynn/pull/1957#discussion_r43317286"},"pull_request":{"href":"https://api.github.com/repos/flynn/flynn/pulls/1957"}}},"pull_request":{"url":"https://api.github.com/repos/flynn/flynn/pulls/1957","id":47207418,"html_url":"https://github.com/flynn/flynn/pull/1957","diff_url":"https://github.com/flynn/flynn/pull/1957.diff","patch_url":"https://github.com/flynn/flynn/pull/1957.patch","issue_url":"https://api.github.com/repos/flynn/flynn/issues/1957","number":1957,"state":"open","locked":false,"title":"Migrate cluster domain","user":{"login":"jvatic","id":171040,"avatar_url":"https://avatars.githubusercontent.com/u/171040?v=3","gravatar_id":"","url":"https://api.github.com/users/jvatic","html_url":"https://github.com/jvatic","followers_url":"https://api.github.com/users/jvatic/followers","following_url":"https://api.github.com/users/jvatic/following{/other_user}","gists_url":"https://api.github.com/users/jvatic/gists{/gist_id}","starred_url":"https://api.github.com/users/jvatic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jvatic/subscriptions","organizations_url":"https://api.github.com/users/jvatic/orgs","repos_url":"https://api.github.com/users/jvatic/repos","events_url":"https://api.github.com/users/jvatic/events{/privacy}","received_events_url":"https://api.github.com/users/jvatic/received_events","type":"User","site_admin":false},"body":"- [x] Test / Add integration test\r\n- [x] Expose via cli command\r\n\r\nCloses #1816 ","created_at":"2015-10-08T21:15:04Z","updated_at":"2015-10-28T21:00:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d018cbe824b4095167268292168a900505714a38","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/flynn/flynn/pulls/1957/commits","review_comments_url":"https://api.github.com/repos/flynn/flynn/pulls/1957/comments","review_comment_url":"https://api.github.com/repos/flynn/flynn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/flynn/flynn/issues/1957/comments","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/4bc36ee01e7123b0ecb17532bcb561dc27f48372","head":{"label":"flynn:migrate-cluster-domain","ref":"migrate-cluster-domain","sha":"4bc36ee01e7123b0ecb17532bcb561dc27f48372","user":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"repo":{"id":11290232,"name":"flynn","full_name":"flynn/flynn","owner":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/flynn/flynn","description":"A next generation open source platform as a service (PaaS)","fork":false,"url":"https://api.github.com/repos/flynn/flynn","forks_url":"https://api.github.com/repos/flynn/flynn/forks","keys_url":"https://api.github.com/repos/flynn/flynn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flynn/flynn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flynn/flynn/teams","hooks_url":"https://api.github.com/repos/flynn/flynn/hooks","issue_events_url":"https://api.github.com/repos/flynn/flynn/issues/events{/number}","events_url":"https://api.github.com/repos/flynn/flynn/events","assignees_url":"https://api.github.com/repos/flynn/flynn/assignees{/user}","branches_url":"https://api.github.com/repos/flynn/flynn/branches{/branch}","tags_url":"https://api.github.com/repos/flynn/flynn/tags","blobs_url":"https://api.github.com/repos/flynn/flynn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flynn/flynn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flynn/flynn/git/refs{/sha}","trees_url":"https://api.github.com/repos/flynn/flynn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/{sha}","languages_url":"https://api.github.com/repos/flynn/flynn/languages","stargazers_url":"https://api.github.com/repos/flynn/flynn/stargazers","contributors_url":"https://api.github.com/repos/flynn/flynn/contributors","subscribers_url":"https://api.github.com/repos/flynn/flynn/subscribers","subscription_url":"https://api.github.com/repos/flynn/flynn/subscription","commits_url":"https://api.github.com/repos/flynn/flynn/commits{/sha}","git_commits_url":"https://api.github.com/repos/flynn/flynn/git/commits{/sha}","comments_url":"https://api.github.com/repos/flynn/flynn/comments{/number}","issue_comment_url":"https://api.github.com/repos/flynn/flynn/issues/comments{/number}","contents_url":"https://api.github.com/repos/flynn/flynn/contents/{+path}","compare_url":"https://api.github.com/repos/flynn/flynn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flynn/flynn/merges","archive_url":"https://api.github.com/repos/flynn/flynn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flynn/flynn/downloads","issues_url":"https://api.github.com/repos/flynn/flynn/issues{/number}","pulls_url":"https://api.github.com/repos/flynn/flynn/pulls{/number}","milestones_url":"https://api.github.com/repos/flynn/flynn/milestones{/number}","notifications_url":"https://api.github.com/repos/flynn/flynn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flynn/flynn/labels{/name}","releases_url":"https://api.github.com/repos/flynn/flynn/releases{/id}","created_at":"2013-07-09T18:22:06Z","updated_at":"2015-10-28T15:48:37Z","pushed_at":"2015-10-28T20:31:58Z","git_url":"git://github.com/flynn/flynn.git","ssh_url":"git@github.com:flynn/flynn.git","clone_url":"https://github.com/flynn/flynn.git","svn_url":"https://github.com/flynn/flynn","homepage":"https://flynn.io","size":60607,"stargazers_count":3953,"watchers_count":3953,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":288,"mirror_url":null,"open_issues_count":220,"forks":288,"open_issues":220,"watchers":3953,"default_branch":"master"}},"base":{"label":"flynn:master","ref":"master","sha":"befac6db41b1de88d9db23ab9dc1b230b4ffb1a6","user":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"repo":{"id":11290232,"name":"flynn","full_name":"flynn/flynn","owner":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/flynn/flynn","description":"A next generation open source platform as a service (PaaS)","fork":false,"url":"https://api.github.com/repos/flynn/flynn","forks_url":"https://api.github.com/repos/flynn/flynn/forks","keys_url":"https://api.github.com/repos/flynn/flynn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flynn/flynn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flynn/flynn/teams","hooks_url":"https://api.github.com/repos/flynn/flynn/hooks","issue_events_url":"https://api.github.com/repos/flynn/flynn/issues/events{/number}","events_url":"https://api.github.com/repos/flynn/flynn/events","assignees_url":"https://api.github.com/repos/flynn/flynn/assignees{/user}","branches_url":"https://api.github.com/repos/flynn/flynn/branches{/branch}","tags_url":"https://api.github.com/repos/flynn/flynn/tags","blobs_url":"https://api.github.com/repos/flynn/flynn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flynn/flynn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flynn/flynn/git/refs{/sha}","trees_url":"https://api.github.com/repos/flynn/flynn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/{sha}","languages_url":"https://api.github.com/repos/flynn/flynn/languages","stargazers_url":"https://api.github.com/repos/flynn/flynn/stargazers","contributors_url":"https://api.github.com/repos/flynn/flynn/contributors","subscribers_url":"https://api.github.com/repos/flynn/flynn/subscribers","subscription_url":"https://api.github.com/repos/flynn/flynn/subscription","commits_url":"https://api.github.com/repos/flynn/flynn/commits{/sha}","git_commits_url":"https://api.github.com/repos/flynn/flynn/git/commits{/sha}","comments_url":"https://api.github.com/repos/flynn/flynn/comments{/number}","issue_comment_url":"https://api.github.com/repos/flynn/flynn/issues/comments{/number}","contents_url":"https://api.github.com/repos/flynn/flynn/contents/{+path}","compare_url":"https://api.github.com/repos/flynn/flynn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flynn/flynn/merges","archive_url":"https://api.github.com/repos/flynn/flynn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flynn/flynn/downloads","issues_url":"https://api.github.com/repos/flynn/flynn/issues{/number}","pulls_url":"https://api.github.com/repos/flynn/flynn/pulls{/number}","milestones_url":"https://api.github.com/repos/flynn/flynn/milestones{/number}","notifications_url":"https://api.github.com/repos/flynn/flynn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flynn/flynn/labels{/name}","releases_url":"https://api.github.com/repos/flynn/flynn/releases{/id}","created_at":"2013-07-09T18:22:06Z","updated_at":"2015-10-28T15:48:37Z","pushed_at":"2015-10-28T20:31:58Z","git_url":"git://github.com/flynn/flynn.git","ssh_url":"git@github.com:flynn/flynn.git","clone_url":"https://github.com/flynn/flynn.git","svn_url":"https://github.com/flynn/flynn","homepage":"https://flynn.io","size":60607,"stargazers_count":3953,"watchers_count":3953,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":288,"mirror_url":null,"open_issues_count":220,"forks":288,"open_issues":220,"watchers":3953,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/flynn/flynn/pulls/1957"},"html":{"href":"https://github.com/flynn/flynn/pull/1957"},"issue":{"href":"https://api.github.com/repos/flynn/flynn/issues/1957"},"comments":{"href":"https://api.github.com/repos/flynn/flynn/issues/1957/comments"},"review_comments":{"href":"https://api.github.com/repos/flynn/flynn/pulls/1957/comments"},"review_comment":{"href":"https://api.github.com/repos/flynn/flynn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/flynn/flynn/pulls/1957/commits"},"statuses":{"href":"https://api.github.com/repos/flynn/flynn/statuses/4bc36ee01e7123b0ecb17532bcb561dc27f48372"}}}}