{"action":"created","comment":{"url":"https://api.github.com/repos/rancher/hostname-update-service/pulls/comments/70172387","id":70172387,"diff_hunk":"@@ -0,0 +1,174 @@\n+package main\n+\n+import (\n+\t\"fmt\"\n+\t\"io/ioutil\"\n+\t\"os\"\n+\t\"strings\"\n+\t\"time\"\n+\n+\tlog \"github.com/Sirupsen/logrus\"\n+\t\"github.com/codegangsta/cli\"\n+\t\"github.com/rancher/go-rancher-metadata/metadata\"\n+)\n+\n+const (\n+\tVERSION     = \"0.0.1\"\n+\tmetadataURL = \"http://rancher-metadata/2015-12-19\"\n+)\n+\n+func main() {\n+\texit := func(err error) {\n+\t\tif err != nil {\n+\t\t\tlog.Fatal(err)\n+\t\t}\n+\t}\n+\n+\tapp := cli.NewApp()\n+\tapp.Author = \"Rancher Labs\"\n+\tapp.EnableBashCompletion = true\n+\tapp.Version = VERSION\n+\tapp.Usage = `Hostname Update Service\n+\n+\tPopulates /etc/hosts of a rancher managed container based on currently registered\n+\thosts in a given rancher environment`\n+\tapp.Flags = []cli.Flag{\n+\t\tcli.IntFlag{\n+\t\t\tName:  \"update-interval\",\n+\t\t\tValue: 5,\n+\t\t\tUsage: \"time interval between refreshes of host list (in seconds)\",\n+\t\t},\n+\t}\n+\tapp.Action = func(c *cli.Context) {\n+\t\texit(run(c))\n+\t}\n+\n+\texit(app.Run(os.Args))\n+}\n+\n+func run(c *cli.Context) error {\n+\tmetadataClient, err := metadata.NewClientAndWait(metadataURL)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\tinterval := c.Int(\"update-interval\")\n+\trancherHosts := map[string]string{}\n+\n+\tfor {\n+\t\terr = repeat(metadataClient, rancherHosts)\n+\t\tif err != nil {\n+\t\t\tlog.Errorf(\"Error updating /etc/hosts with hosts: [%v]\", err)\n+\t\t}\n+\t\t<-time.After(time.Duration(time.Duration(interval) * time.Second))\n+\t}\n+}\n+\n+func repeat(metadataClient *metadata.Client, rancherHosts map[string]string) error {\n+\thosts, err := metadataClient.GetHosts()\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\tchanged := false\n+\n+\thostsMap := map[string]string{}\n+\n+\tfor _, host := range hosts {\n+\t\tif _, ok := hostsMap[host.Hostname]; ok {\n+\t\t\t// Do not add subsequent hosts with the\n+\t\t\t// duplicate hostnames\n+\t\t\tcontinue\n+\t\t}\n+\t\tif ip, ok := rancherHosts[host.Hostname]; !ok || ip != host.AgentIP {\n+\t\t\t// If the current host is not a part of the\n+\t\t\t// previous set of rancher hosts, then a new host\n+\t\t\t// was added\n+\t\t\tchanged = true\n+\t\t}\n+\t\thostsMap[host.Hostname] = host.AgentIP\n+\t}\n+\n+\t// if a host was deleted, this will be true\n+\tif len(rancherHosts) != len(hostsMap) {\n+\t\tchanged = true\n+\t}\n+\n+\tif !changed {\n+\t\treturn nil\n+\t}\n+\n+\tlog.Info(\"change detected in rancher hosts\")\n+\n+\t// sycnchronize rancherHosts to be the same as\n+\t// the current view of rancher hosts from metadata service\n+\tfor k := range rancherHosts {\n+\t\tdelete(rancherHosts, k)\n+\t}\n+\n+\tfor k, v := range hostsMap {\n+\t\trancherHosts[k] = v\n+\t}\n+\n+\thostsWorkingFile := \"/etc/hosts.backup\"\n+\n+\tif _, err := os.Stat(hostsWorkingFile); os.IsNotExist(err) {\n+\t\t// This is done to maintain a copy of the original contents\n+\t\t// of the hosts file\n+\t\tfd, err := os.Open(\"/etc/hosts\")","path":"main.go","position":118,"original_position":118,"commit_id":"7ccd58c4be6aa23f395b7c70da9c66504d0f8403","original_commit_id":"7ccd58c4be6aa23f395b7c70da9c66504d0f8403","user":{"login":"ibuildthecloud","id":1754002,"avatar_url":"https://avatars.githubusercontent.com/u/1754002?v=3","gravatar_id":"","url":"https://api.github.com/users/ibuildthecloud","html_url":"https://github.com/ibuildthecloud","followers_url":"https://api.github.com/users/ibuildthecloud/followers","following_url":"https://api.github.com/users/ibuildthecloud/following{/other_user}","gists_url":"https://api.github.com/users/ibuildthecloud/gists{/gist_id}","starred_url":"https://api.github.com/users/ibuildthecloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ibuildthecloud/subscriptions","organizations_url":"https://api.github.com/users/ibuildthecloud/orgs","repos_url":"https://api.github.com/users/ibuildthecloud/repos","events_url":"https://api.github.com/users/ibuildthecloud/events{/privacy}","received_events_url":"https://api.github.com/users/ibuildthecloud/received_events","type":"User","site_admin":false},"body":"Just use `ioutil.ReadFile`","created_at":"2016-07-09T21:42:56Z","updated_at":"2016-07-09T21:42:56Z","html_url":"https://github.com/rancher/hostname-update-service/pull/1#discussion_r70172387","pull_request_url":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/rancher/hostname-update-service/pulls/comments/70172387"},"html":{"href":"https://github.com/rancher/hostname-update-service/pull/1#discussion_r70172387"},"pull_request":{"href":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1","id":76508607,"html_url":"https://github.com/rancher/hostname-update-service/pull/1","diff_url":"https://github.com/rancher/hostname-update-service/pull/1.diff","patch_url":"https://github.com/rancher/hostname-update-service/pull/1.patch","issue_url":"https://api.github.com/repos/rancher/hostname-update-service/issues/1","number":1,"state":"open","locked":false,"title":"create hostname update service","user":{"login":"wlan0","id":1050521,"avatar_url":"https://avatars.githubusercontent.com/u/1050521?v=3","gravatar_id":"","url":"https://api.github.com/users/wlan0","html_url":"https://github.com/wlan0","followers_url":"https://api.github.com/users/wlan0/followers","following_url":"https://api.github.com/users/wlan0/following{/other_user}","gists_url":"https://api.github.com/users/wlan0/gists{/gist_id}","starred_url":"https://api.github.com/users/wlan0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wlan0/subscriptions","organizations_url":"https://api.github.com/users/wlan0/orgs","repos_url":"https://api.github.com/users/wlan0/repos","events_url":"https://api.github.com/users/wlan0/events{/privacy}","received_events_url":"https://api.github.com/users/wlan0/received_events","type":"User","site_admin":false},"body":"@alena1108 @ibuildthecloud ","created_at":"2016-07-06T21:54:36Z","updated_at":"2016-07-09T21:42:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"822836065ba990c54f9b51b2d54fb8e383229df4","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1/commits","review_comments_url":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1/comments","review_comment_url":"https://api.github.com/repos/rancher/hostname-update-service/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rancher/hostname-update-service/issues/1/comments","statuses_url":"https://api.github.com/repos/rancher/hostname-update-service/statuses/7ccd58c4be6aa23f395b7c70da9c66504d0f8403","head":{"label":"wlan0:master","ref":"master","sha":"7ccd58c4be6aa23f395b7c70da9c66504d0f8403","user":{"login":"wlan0","id":1050521,"avatar_url":"https://avatars.githubusercontent.com/u/1050521?v=3","gravatar_id":"","url":"https://api.github.com/users/wlan0","html_url":"https://github.com/wlan0","followers_url":"https://api.github.com/users/wlan0/followers","following_url":"https://api.github.com/users/wlan0/following{/other_user}","gists_url":"https://api.github.com/users/wlan0/gists{/gist_id}","starred_url":"https://api.github.com/users/wlan0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wlan0/subscriptions","organizations_url":"https://api.github.com/users/wlan0/orgs","repos_url":"https://api.github.com/users/wlan0/repos","events_url":"https://api.github.com/users/wlan0/events{/privacy}","received_events_url":"https://api.github.com/users/wlan0/received_events","type":"User","site_admin":false},"repo":{"id":62755489,"name":"hostname-update-service","full_name":"wlan0/hostname-update-service","owner":{"login":"wlan0","id":1050521,"avatar_url":"https://avatars.githubusercontent.com/u/1050521?v=3","gravatar_id":"","url":"https://api.github.com/users/wlan0","html_url":"https://github.com/wlan0","followers_url":"https://api.github.com/users/wlan0/followers","following_url":"https://api.github.com/users/wlan0/following{/other_user}","gists_url":"https://api.github.com/users/wlan0/gists{/gist_id}","starred_url":"https://api.github.com/users/wlan0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wlan0/subscriptions","organizations_url":"https://api.github.com/users/wlan0/orgs","repos_url":"https://api.github.com/users/wlan0/repos","events_url":"https://api.github.com/users/wlan0/events{/privacy}","received_events_url":"https://api.github.com/users/wlan0/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wlan0/hostname-update-service","description":"populates /etc/hosts of a rancher container based on currently registered hosts in a given environment","fork":true,"url":"https://api.github.com/repos/wlan0/hostname-update-service","forks_url":"https://api.github.com/repos/wlan0/hostname-update-service/forks","keys_url":"https://api.github.com/repos/wlan0/hostname-update-service/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wlan0/hostname-update-service/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wlan0/hostname-update-service/teams","hooks_url":"https://api.github.com/repos/wlan0/hostname-update-service/hooks","issue_events_url":"https://api.github.com/repos/wlan0/hostname-update-service/issues/events{/number}","events_url":"https://api.github.com/repos/wlan0/hostname-update-service/events","assignees_url":"https://api.github.com/repos/wlan0/hostname-update-service/assignees{/user}","branches_url":"https://api.github.com/repos/wlan0/hostname-update-service/branches{/branch}","tags_url":"https://api.github.com/repos/wlan0/hostname-update-service/tags","blobs_url":"https://api.github.com/repos/wlan0/hostname-update-service/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wlan0/hostname-update-service/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wlan0/hostname-update-service/git/refs{/sha}","trees_url":"https://api.github.com/repos/wlan0/hostname-update-service/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wlan0/hostname-update-service/statuses/{sha}","languages_url":"https://api.github.com/repos/wlan0/hostname-update-service/languages","stargazers_url":"https://api.github.com/repos/wlan0/hostname-update-service/stargazers","contributors_url":"https://api.github.com/repos/wlan0/hostname-update-service/contributors","subscribers_url":"https://api.github.com/repos/wlan0/hostname-update-service/subscribers","subscription_url":"https://api.github.com/repos/wlan0/hostname-update-service/subscription","commits_url":"https://api.github.com/repos/wlan0/hostname-update-service/commits{/sha}","git_commits_url":"https://api.github.com/repos/wlan0/hostname-update-service/git/commits{/sha}","comments_url":"https://api.github.com/repos/wlan0/hostname-update-service/comments{/number}","issue_comment_url":"https://api.github.com/repos/wlan0/hostname-update-service/issues/comments{/number}","contents_url":"https://api.github.com/repos/wlan0/hostname-update-service/contents/{+path}","compare_url":"https://api.github.com/repos/wlan0/hostname-update-service/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wlan0/hostname-update-service/merges","archive_url":"https://api.github.com/repos/wlan0/hostname-update-service/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wlan0/hostname-update-service/downloads","issues_url":"https://api.github.com/repos/wlan0/hostname-update-service/issues{/number}","pulls_url":"https://api.github.com/repos/wlan0/hostname-update-service/pulls{/number}","milestones_url":"https://api.github.com/repos/wlan0/hostname-update-service/milestones{/number}","notifications_url":"https://api.github.com/repos/wlan0/hostname-update-service/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wlan0/hostname-update-service/labels{/name}","releases_url":"https://api.github.com/repos/wlan0/hostname-update-service/releases{/id}","deployments_url":"https://api.github.com/repos/wlan0/hostname-update-service/deployments","created_at":"2016-07-06T21:48:12Z","updated_at":"2016-07-06T21:53:12Z","pushed_at":"2016-07-08T17:37:15Z","git_url":"git://github.com/wlan0/hostname-update-service.git","ssh_url":"git@github.com:wlan0/hostname-update-service.git","clone_url":"https://github.com/wlan0/hostname-update-service.git","svn_url":"https://github.com/wlan0/hostname-update-service","homepage":null,"size":177,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"rancher:master","ref":"master","sha":"c747f86bf543666d4a8135f54e57a58a784e2411","user":{"login":"rancher","id":9343010,"avatar_url":"https://avatars.githubusercontent.com/u/9343010?v=3","gravatar_id":"","url":"https://api.github.com/users/rancher","html_url":"https://github.com/rancher","followers_url":"https://api.github.com/users/rancher/followers","following_url":"https://api.github.com/users/rancher/following{/other_user}","gists_url":"https://api.github.com/users/rancher/gists{/gist_id}","starred_url":"https://api.github.com/users/rancher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rancher/subscriptions","organizations_url":"https://api.github.com/users/rancher/orgs","repos_url":"https://api.github.com/users/rancher/repos","events_url":"https://api.github.com/users/rancher/events{/privacy}","received_events_url":"https://api.github.com/users/rancher/received_events","type":"Organization","site_admin":false},"repo":{"id":62755254,"name":"hostname-update-service","full_name":"rancher/hostname-update-service","owner":{"login":"rancher","id":9343010,"avatar_url":"https://avatars.githubusercontent.com/u/9343010?v=3","gravatar_id":"","url":"https://api.github.com/users/rancher","html_url":"https://github.com/rancher","followers_url":"https://api.github.com/users/rancher/followers","following_url":"https://api.github.com/users/rancher/following{/other_user}","gists_url":"https://api.github.com/users/rancher/gists{/gist_id}","starred_url":"https://api.github.com/users/rancher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rancher/subscriptions","organizations_url":"https://api.github.com/users/rancher/orgs","repos_url":"https://api.github.com/users/rancher/repos","events_url":"https://api.github.com/users/rancher/events{/privacy}","received_events_url":"https://api.github.com/users/rancher/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rancher/hostname-update-service","description":"populates /etc/hosts of a rancher container based on currently registered hosts in a given environment","fork":false,"url":"https://api.github.com/repos/rancher/hostname-update-service","forks_url":"https://api.github.com/repos/rancher/hostname-update-service/forks","keys_url":"https://api.github.com/repos/rancher/hostname-update-service/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rancher/hostname-update-service/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rancher/hostname-update-service/teams","hooks_url":"https://api.github.com/repos/rancher/hostname-update-service/hooks","issue_events_url":"https://api.github.com/repos/rancher/hostname-update-service/issues/events{/number}","events_url":"https://api.github.com/repos/rancher/hostname-update-service/events","assignees_url":"https://api.github.com/repos/rancher/hostname-update-service/assignees{/user}","branches_url":"https://api.github.com/repos/rancher/hostname-update-service/branches{/branch}","tags_url":"https://api.github.com/repos/rancher/hostname-update-service/tags","blobs_url":"https://api.github.com/repos/rancher/hostname-update-service/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rancher/hostname-update-service/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rancher/hostname-update-service/git/refs{/sha}","trees_url":"https://api.github.com/repos/rancher/hostname-update-service/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rancher/hostname-update-service/statuses/{sha}","languages_url":"https://api.github.com/repos/rancher/hostname-update-service/languages","stargazers_url":"https://api.github.com/repos/rancher/hostname-update-service/stargazers","contributors_url":"https://api.github.com/repos/rancher/hostname-update-service/contributors","subscribers_url":"https://api.github.com/repos/rancher/hostname-update-service/subscribers","subscription_url":"https://api.github.com/repos/rancher/hostname-update-service/subscription","commits_url":"https://api.github.com/repos/rancher/hostname-update-service/commits{/sha}","git_commits_url":"https://api.github.com/repos/rancher/hostname-update-service/git/commits{/sha}","comments_url":"https://api.github.com/repos/rancher/hostname-update-service/comments{/number}","issue_comment_url":"https://api.github.com/repos/rancher/hostname-update-service/issues/comments{/number}","contents_url":"https://api.github.com/repos/rancher/hostname-update-service/contents/{+path}","compare_url":"https://api.github.com/repos/rancher/hostname-update-service/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rancher/hostname-update-service/merges","archive_url":"https://api.github.com/repos/rancher/hostname-update-service/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rancher/hostname-update-service/downloads","issues_url":"https://api.github.com/repos/rancher/hostname-update-service/issues{/number}","pulls_url":"https://api.github.com/repos/rancher/hostname-update-service/pulls{/number}","milestones_url":"https://api.github.com/repos/rancher/hostname-update-service/milestones{/number}","notifications_url":"https://api.github.com/repos/rancher/hostname-update-service/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rancher/hostname-update-service/labels{/name}","releases_url":"https://api.github.com/repos/rancher/hostname-update-service/releases{/id}","deployments_url":"https://api.github.com/repos/rancher/hostname-update-service/deployments","created_at":"2016-07-06T21:43:27Z","updated_at":"2016-07-06T21:43:27Z","pushed_at":"2016-07-08T17:37:16Z","git_url":"git://github.com/rancher/hostname-update-service.git","ssh_url":"git@github.com:rancher/hostname-update-service.git","clone_url":"https://github.com/rancher/hostname-update-service.git","svn_url":"https://github.com/rancher/hostname-update-service","homepage":null,"size":177,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1"},"html":{"href":"https://github.com/rancher/hostname-update-service/pull/1"},"issue":{"href":"https://api.github.com/repos/rancher/hostname-update-service/issues/1"},"comments":{"href":"https://api.github.com/repos/rancher/hostname-update-service/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/rancher/hostname-update-service/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rancher/hostname-update-service/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/rancher/hostname-update-service/statuses/7ccd58c4be6aa23f395b7c70da9c66504d0f8403"}}}}
{"actor":{"display_login":"ibuildthecloud"}}