diff --git a/main-cookbooks/teracy-dev/metadata.rb b/main-cookbooks/teracy-dev/metadata.rb index 01cc2149..8f9e87e7 100644 --- a/main-cookbooks/teracy-dev/metadata.rb +++ b/main-cookbooks/teracy-dev/metadata.rb @@ -16,6 +16,7 @@ recipe 'teracy-dev::directories', 'Manage directories.' recipe 'teracy-dev::env_vars', 'Configures environment variables.' recipe 'teracy-dev::docker', 'Installs Docker, docker-compose' +recipe 'teracy-dev::docker_registry', "Docker registry's tasks: login, ..." recipe 'teracy-dev::docker_machine', 'Installs docker-machine' recipe 'teracy-dev::inotify', 'Modify inotify, useful for development watching a lot of files' recipe 'teracy-dev::proxy', 'Create a reverse proxy with nginx' diff --git a/main-cookbooks/teracy-dev/recipes/default.rb b/main-cookbooks/teracy-dev/recipes/default.rb index dc6ad82c..dada2394 100644 --- a/main-cookbooks/teracy-dev/recipes/default.rb +++ b/main-cookbooks/teracy-dev/recipes/default.rb @@ -35,6 +35,7 @@ include_recipe 'teracy-dev::aliases' include_recipe 'teracy-dev::env_vars' include_recipe 'teracy-dev::docker' +include_recipe 'teracy-dev::docker_registry' include_recipe 'teracy-dev::docker_machine' include_recipe 'teracy-dev::inotify' include_recipe 'teracy-dev::proxy' diff --git a/main-cookbooks/teracy-dev/recipes/docker_registry.rb b/main-cookbooks/teracy-dev/recipes/docker_registry.rb new file mode 100644 index 00000000..3bf20266 --- /dev/null +++ b/main-cookbooks/teracy-dev/recipes/docker_registry.rb @@ -0,0 +1,52 @@ +# Author:: Hoat Le +# Cookbook:: teracy-dev +# Recipe:: docker_registry +# Login into the Docker registries + +docker_conf = node['docker'] + +docker_registry_conf = node['docker_registry'] + +if docker_conf['enabled'] == true + execute 'rm ~/.docker/config.json' do + command 'rm /home/vagrant/.docker/config.json || true' + only_if { + docker_registry_conf['force'] == true and + File.exist?('/home/vagrant/.docker/config.json') + } + end + + docker_registry_conf['entries'].each.with_index do |entry, index| + # private registry login + + username = entry['username'] ? entry['username'] : '' + + password = entry['password'] ? entry['password'] : '' + + if not username.empty? and not password.empty? + opt = [ + "-u #{username}", + "-p #{password}" + ].join(' '); + + execute 'docker login' do + command "docker login #{entry['host']} #{opt}" + # because we need root to execute docker-compose, not 'vagrant' + only_if { + docker_registry_conf['force'] == true or + not File.exist?('/root/.docker/config.json') + } + end + end + end + + execute 'copy /root/.docker/config.json to ~/.docker/config.json' do + command 'cp /root/.docker/config.json /home/vagrant/.docker/config.json' + only_if { + File.exist?('/root/.docker/config.json') and ( + docker_registry_conf['force'] == true or + not File.exist?('/home/vagrant/.docker/config.json') + ) + } + end +end diff --git a/vagrant_config.json b/vagrant_config.json index ac584586..90dc7d49 100644 --- a/vagrant_config.json +++ b/vagrant_config.json @@ -113,6 +113,24 @@ "members": ["vagrant"], // to append this member to "docker" group "action": "create" // one of create, delete. Default: create }, + "docker_registry": { + // set true to force re-login with all the defined entries, default: false (login once) + "force": false, + "entries": [] + // example: + // "entries": [ + // { + // "host": "https://index.docker.io/v1/", + // "username": "", + // "password": "" + // }, + // { + // "host": "registry.gitlab.com", + // "username": "", + // "password": "" + // } + // ] + }, "docker_compose": { "release": "", // more: https://github.com/docker/compose/releases/ "enabled": true // "docker" must be enabled to get this