def prepare_ngas_data_dir(): """Creates a new NGAS root directory""" info('Preparing NGAS root directory') nrd = APP_root_dir() tgt_cfg = os.path.join(nrd, 'cfg', 'ngamsServer.conf') with cd(APP_source_dir()): cmd = ['./prepare_ngas_root.sh'] if 'NGAS_OVERWRITE_ROOT' in env and env.NGAS_OVERWRITE_ROOT: cmd.append('-f') cmd.append(nrd) res = run(' '.join(cmd), quiet=True) if res.succeeded: success("NGAS data directory ready") env.tgt_cfg = tgt_cfg return tgt_cfg # Deal with the errors here error = 'NGAS root directory preparation under {0} failed.\n'.format(nrd) if res.return_code == 2: error = (nrd + " already exists. Specify NGAS_OVERWRITE_ROOT to overwrite, " "or a different NGAS_ROOT_DIR location") else: error = res abort(error)
def sysinitstop_EAGLE(): """ Stops the nginx and uwsgi services if running. NOTE: Requires sudo privilidges """ info('Check and stop {0}'.format(APP)) if env.FAB_TASK == 'docker_image': env.sudo_user = '******' elif env.FAB_TASK == 'aws_deploy': env.sudo_user = env.AWS_SUDO_USER try: u = urllib2.urlopen('http://{0}/static/html/index.html'. format(env.host_string)) except urllib2.URLError: red("EAGLE NOT running!") return r = u.read() u.close() if type(r) == bytes: # required for Python3 r = r.decode('UTF-8') assert r.find('eagle-s-user-documentation') > -1, red("EAGLE NOT running") with settings(user=env.sudo_user): sudo('service nginx stop') sudo('service uwsgi stop')
def sysinitstart_RASVAMT_and_check_status(): """ Starts the APP daemon 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 APPDaemon could detach, thus # resulting in no startup self. # # Please replace following line with something meaningful # virtualenv('ngamsDaemon start -cfg {0} && sleep 2'.format(tgt_cfg)) env.APP_INSTALL_DIR = os.path.abspath(os.path.join(home(), APP_INSTALL_DIR_NAME)) env.APP_ROOT_DIR = os.path.abspath(os.path.join(home(), APP_ROOT_DIR_NAME)) env.APP_SRC_DIR = os.path.abspath(os.path.join(home(), APP_SRC_DIR_NAME)) info('Start {0} and check'.format(APP)) start_unicorn() with settings(user=env.AWS_SUDO_USER): sudo('service nginx start') try: u = urllib2.urlopen('http://{0}/static/html/index.html'. format(env.host_string)) except urllib2.URLError: red("RASVAMT NOT running!") return r = u.read() u.close() assert r.find('rasvamt-s-user-documentation') > -1, red("RASVAMT NOT running")
def start_APP_and_check_status(): """ Starts the APP daemon process and checks that the server is up and running then it shuts down the server """ with cd('{0}/pyvospace/server/deploy'.format(APP_source_dir())): # >>>> Darwin docker shows a permission issue with keychain access <<<< if get_linux_flavor() != 'Darwin': virtualenv('docker-compose build') else: info('>>>> Darwin reuqires to build docker container manually') info('>>>> docker-compose build') virtualenv( 'docker run -d -p 5435:5432 pyvospace/pyvospace-db -h 0.0.0.0') time.sleep(10) with cd('{0}'.format(APP_source_dir())): virtualenv('python -m unittest discover test') # run('mkdir -p /tmp/fuse') # virtualenv('posix_space --cfg test_vo.ini > /tmp/space.log 2>&1') # time.sleep(2) # virtualenv('posix_storage --cfg test_vo.ini > /tmp/storage.log 2>&1') # time.sleep(2) # virtualenv('python -m pyvospace.client.fuse --host localhost --port 8080 --username test --password test --mountpoint /tmp/fuse/`` > /tmp/fusemnt.log 2>&1') # time.sleep(2) # run("cd /tmp/fuse && mkdir -p newdir && cd newdir && echo 'Hello World!' >> data && cat data") success('{0} is working...'.format(env.APP_NAME))
def sysinitstart_EAGLE_and_check_status(): """ Starts the APP daemon 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 APPDaemon could detach, thus # resulting in no startup self. # # Please replace following line with something meaningful # virtualenv('ngamsDaemon start -cfg {0} && sleep 2'.format(tgt_cfg)) info('Start {0} and check'.format(APP)) if env.FAB_TASK == 'docker_image': env.sudo_user = '******' elif env.FAB_TASK == 'aws_deploy': env.sudo_user = env.AWS_SUDO_USER with settings(user=env.sudo_user): sudo('service nginx start') sudo('service uwsgi start') try: u = urllib2.urlopen('http://{0}/static/html/index.html'. format(env.host_string)) except urllib2.URLError: red("EAGLE NOT running!") return r = u.read() u.close() if type(r) == bytes: # required for Python3 r = r.decode('UTF-8') if r.find('eagle-s-user-documentation') > -1: red("EAGLE NOT running") else: green("EAGLE running. Connect through: http://{0}/new".format(env.host_string))
def install_and_check(): """ Creates a virtualenv, installs APP on it, starts APP and checks that it is running """ copy_sources() if env.APP_PYTHON_URL: virtualenv_setup() # if APP needs python at all build() tgt_cfg = None if 'prepare_APP_data_dir' in env: tgt_cfg = env.prepare_APP_data_dir() install_user_profile() if 'APP_start_check_function' in env: env.APP_start_check_function() else: info('APP_start_check_function not defined in APPspecific') return APP_source_dir(), tgt_cfg
def prepare_install_and_check(): # Install system packages, create user if necessary, install and start APP nuser = APP_user() install_system_packages() create_user(nuser) # Execute addition sudo related functions env.APP_extra_sudo_function() # postfix_config() # Go, go, go! with settings(user=nuser): nsd, cfgfile = install_and_check() if 'APP_init_install_function' in env: env.APP_init_install_function(nsd, nuser, cfgfile) else: info('APP_init_install_function not defined in APPspecific') if 'sysinitAPP_start_check_function' in env: env.sysinitAPP_start_check_function() else: info('sysinitAPP_start_check_function not defined in APPspecific')
def build(): """ Builds and installs APP into the target virtualenv. """ info('Building {0}...'.format(env.APP_NAME)) with cd(APP_source_dir()): extra_pkgs = extra_python_packages() if extra_pkgs: virtualenv('pip install %s' % ' '.join(extra_pkgs)) else: info('No extra Python packages') with cd(APP_source_dir()): build_cmd = env.build_cmd() info('Build command: {0}'.format(build_cmd)) if build_cmd and build_cmd != '': virtualenv(build_cmd) if 'build_function' in env and env.build_function: res = env.build_function() # Install the /etc/init.d script for automatic start nsd = APP_source_dir() nuser = APP_user() cfgfile = None # success("{0} built and installed".format(env.APP_NAME))
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 prepare_APP_data_dir(): """Creates a new APP root directory""" info('Preparing {0} root directory'.format(env.APP_NAME)) nrd = APP_root_dir() # tgt_cfg = os.path.join(nrd, 'cfg', 'ngamsServer.conf') tgt_cfg = None res = run('mkdir -p {0}'.format(nrd)) with cd(APP_source_dir()): for d in env.APP_DATAFILES: res = run('scp -r {0} {1}/.'.format(d, nrd), quiet=True) if res.succeeded: success("{0} data directory ready".format(env.APP_NAME)) return tgt_cfg # Deal with the errors here error = '{0} root directory preparation under {1} failed.\n'.format( env.APP_NAME, nrd) if res.return_code == 2: error = (nrd + " already exists. Specify APP_OVERWRITE_ROOT to " "overwrite, or a different APP_ROOT_DIR location") else: error = res abort(error)
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)