def create_final_image(state): """Create docker image from container""" puts(blue("Building image")) # First need to cleanup container before we stop and commit it. # We execute most of the commands via ssh, until we actually remove ssh # itself and forcefully remove unnecessary system-level folders execute(cleanup_container) cont = state.container execOutput( cont, 'yum --assumeyes --quiet remove fipscheck fipscheck-lib openssh-server openssh-clients' ) execOutput(cont, 'rm -rf /var/log') execOutput(cont, 'rm -rf /var/lib/yum') conf = {'Cmd': ["/usr/bin/su", "-", APP_user(), "-c", "/home/{0}/{0}_rt/bin/ngamsServer -cfg /home/{0}/{1}/cfg/ngamsServer.conf -autoOnline -force -v 4".\ format(APP_user(), APP_name())]} image_repo = docker_image_repository() try: cont.stop() cont.commit(repository=image_repo, tag='latest', conf=conf) success("Created Docker image %s:latest" % (image_repo, )) except Exception as e: failure("Failed to build final image: %s" % (str(e))) raise finally: # Cleanup the docker environment from all our temporary stuff cont.remove()
def sysinitstart_NGAS_and_check_status(): """ Starts the ngamsDaemon process and checks that the server is up and running. Then it shuts down the server """ # We sleep 2 here as it was found on Mac deployment to docker container that the # shell would exit before the ngasDaemon could detach, thus resulting in no startup. sudo('service ngas-server start && sleep 2') try: res = sudo('service ngas-server status', warn_only=True) print(res) if res.failed: failure( "Couldn't contact NGAS server after starting it. " "Check log files under %s/log/ to find out what went wrong" % APP_source_dir(), with_stars=False) else: success('NGAS server started correctly :)') finally: info("Shutting NGAS server down now") sudo("service ngas-server stop ")
def start_APP_and_check_status(): """ Starts the ngamsDaemon process and checks that the server is up and running. Then it shuts down the server """ # We sleep 2 here as it was found on Mac deployment to docker container that the # shell would exit before the ngasDaemon could detach, thus resulting in no startup. virtualenv('ngamsDaemon start -cfg {0} && sleep 2'.format(env.tgt_cfg)) try: res = virtualenv('ngamsDaemon status -cfg {0}'.format(env.tgt_cfg), warn_only=True) if res.failed: failure( "Couldn't contact NGAS server after starting it. " "Check log files under %s/log/ to find out what went wrong" % APP_source_dir(), with_stars=False) else: success('NGAS server started correctly :)') finally: info("Shutting NGAS server down now") virtualenv("ngamsDaemon stop -cfg {0}".format(env.tgt_cfg))
def setup_container(): """Create and prepare a docker container and let Fabric point at it""" from docker.client import DockerClient image = 'library/centos:7' container_name = 'APP_installation_target' info("Creating docker container based on {0}".format(image)) info("Please stand-by....") cli = DockerClient.from_env(version='auto', timeout=60) # Create and start a container using the newly created stage1 image cont = cli.containers.run(image=image, name=container_name, remove=False, detach=True, tty=True, ports={22: 2222}) success("Created container %s from %s" % (container_name, image)) # Find out container IP, prepare container for APP installation try: host_ip = cli.api.inspect_container( cont.id)['NetworkSettings']['IPAddress'] # info("Updating and installing OpenSSH server in container") # execOutput(cont, 'yum -y update') info("Installing OpenSSH server...") execOutput(cont, 'yum -y install openssh-server sudo') info("Installing OpenSSH client...") execOutput(cont, 'yum -y install openssh-clients sudo') info("Installing initscripts...") execOutput(cont, 'yum -y install initscripts sudo') info("Cleaning up...") execOutput(cont, 'yum clean all') info('Configuring OpenSSH to allow connections to container') add_public_ssh_key(cont) execOutput( cont, 'sed -i "s/#PermitRootLogin yes/PermitRootLogin yes/" /etc/ssh/sshd_config' ) execOutput(cont, 'sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config') execOutput(cont, 'ssh-keygen -A') execOutput(cont, 'mkdir -p /root/.ssh') execOutput(cont, 'touch /root/.ssh/authorized_keys') execOutput(cont, 'chown root.root /root/.ssh/authorized_keys') execOutput(cont, 'chmod 600 /root/.ssh/authorized_keys') execOutput(cont, 'chmod 700 /root/.ssh') execOutput(cont, 'rm /run/nologin') info('Starting OpenSSH deamon in container') execOutput(cont, '/usr/sbin/sshd -D', detach=True) except: failure( "Error while preparing container for APP installation, cleaning up..." ) cont.stop() cont.remove() raise # From now on we connect to root@host_ip using our SSH key env.hosts = ['localhost'] env.docker = True env.port = 2222 env.user = '******' if 'key_filename' not in env and 'key' not in env: env.key_filename = os.path.expanduser("~/.ssh/id_rsa") # Make sure we can connect via SSH to the newly started container # We disable the known hosts check since docker containers created at # different times might end up having the same IP assigned to them, and the # ssh known hosts check will fail # # NOTE: This does NOT work on a Mac, because the docker0 network is not # available! with settings(disable_known_hosts=True): execute(check_ssh) success('Container successfully setup! {0} installation will start now'.\ format(APP_name())) return DockerContainerState(cli, cont)