Commit 9dd3970a authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

Keep container IP in config file

parent 251ca9d5
......@@ -35,6 +35,7 @@ env.docker_workspace = '/opt/sfl'
env.docker_site_root = '{}/src/drupal'.format(env.docker_workspace)
env.bind_port = 8001
env.apache_user = 'www-data'
env.container_ip = '172.0.0.0'
# Hook commands
......
......@@ -147,10 +147,12 @@ def container_start(role='local'):
if env.interactive_mode:
h.fab_update_hosts(CONTAINER_IP, env.site_hostname)
print(green('Docker container {}_container was build successful. '
print(green('Docker container {}_container was build successful. '
'To visit the Website open a web browser in http://{} or '
'http://localhost:{}.'.format(env.project_name, env.site_hostname, env.bind_port)))
h.fab_update_container_ip()
else:
print(red('Docker image {}/drupal not found and is a requirement to run the {}_container.'
'Please, run first "fab create" in order to build the {}/drupal '
......@@ -214,3 +216,17 @@ def image_remove(role='local'):
print(red('Docker image {}/drupal was not found'.format(env.project_name)))
@task
@roles('docker')
def update_host():
"""
Update hostname resolution in the container.
"""
site_hostname = run("hostname")
run("sed '/{}/c\{} {} localhost.domainlocal' "
"/etc/hosts > /root/hosts.backup".format(env.container_ip, env.container_ip, site_hostname))
run("cat /root/hosts.backup > /etc/hosts")
h.fab_update_container_ip()
......@@ -26,6 +26,8 @@ from fabric.contrib.files import exists
# Import socket to find the localhost IP address
import socket
import os
# Import datetime
from datetime import datetime
......@@ -47,7 +49,7 @@ host_name = local("hostname", capture=True)
# Set the env dict with the roles and the hosts
env.roledefs['local'] = ["{}@{}".format(user_name, host_name)]
env.roledefs['docker'] = ["root@{}".format(env.site_hostname)]
env.roledefs['docker'] = ["root@{}".format(env.container_ip)]
env.builddir = path.join(env.workspace, 'build')
env.makefile = '{}/{}/{}'.format(env.builddir, env.site_profile, env.site_profile_makefile)
......@@ -121,6 +123,15 @@ def fab_remove_from_hosts(site_hostname):
local('sudo sed -i "/{}/d" /etc/hosts'.format(site_hostname))
def fab_update_container_ip():
container_ip = fab_run('local', 'docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
'{}_container'.format(env.project_name), capture=True)
local('sed -i "/env.container_ip/d" {}/local_vars.py'.format(os.path.dirname(os.path.abspath(__file__))))
local('sed -i "/# Docker auto-added container IP/a env.container_ip = \'{}\'" {}/local_vars.py'.format(''.join(container_ip), os.path.dirname(os.path.abspath(__file__))))
def fab_update_hosts(ip, site_hostname):
"""
Helper function to update the file /etc/hosts
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment