def setUpClass(cls): print( "\n################################################################################\n" ) print("#") print("# Initializing custom user test case") print("#\n", flush=True) sh.adduser("mycustomuser", system=True, group=True, shell="/bin/bash") uid = pwd.getpwnam("mycustomuser").pw_uid gid = grp.getgrnam("mycustomuser").gr_gid os.mkdir('wirecloud-data', 0o700) os.chown('wirecloud-data', uid, gid) os.mkdir('wirecloud-static', 0o700) os.chown('wirecloud-static', uid, gid) env = {} env.update(os.environ) env["WIRECLOUD_USER"] = "******".format(uid) sh.docker_compose("-f", "docker-compose-custom-user.yml", "up", d=True, remove_orphans=True, _env=env, _fg=True) wait_until_running() print(flush=True)
def _request_letsencrypt_certificate(domains, email, staging): logi("Requesting Let's Encrypt certificate for {} ...".format(domains)) #Join $domains to -d args domain_args = '-d ' + ','.join(domains) # Select appropriate email arg if email in "": email_arg = "--register-unsafely-without-email" else: email_arg = "--email " + email # Enable staging mode if needed if staging != 0: staging_arg = "--staging" else: staging_arg = "" command = "certbot certonly --webroot -w /var/www/certbot {} {} {} --rsa-key-size {} --agree-tos --force-renewal".format( staging_arg, email_arg, domain_args, RSA_KEY_SIZE) sh.docker_compose("-f", DOCKER_COMPOSE_FILE, "run", "--rm", "--entrypoint", command, "certbot") logi("Reloading nginx ...") sh.docker_compose("-f", DOCKER_COMPOSE_FILE, "exec", "nginx", "nginx", "-s", "reload")
def _deleting_dummy_certificate(domains): logi("Deleting dummy certificate for {} ...".format(domains)) base_domain = domains[0] command = "rm -Rf /etc/letsencrypt/live/{} && rm -Rf /etc/letsencrypt/archive/{} && rm -Rf /etc/letsencrypt/renewal/{}.conf".format( base_domain, base_domain, base_domain) sh.docker_compose("-f", DOCKER_COMPOSE_FILE, "run", "--rm", "--entrypoint", command, "certbot")
def setUpClass(cls): print("\n################################################################################\n") print("#") print("# Initializing simple test case") print("#\n", flush=True) sh.docker_compose("-f", "docker-compose-simple.yml", "up", d=True, remove_orphans=True, _fg=True) wait_until_running() print(flush=True)
def setUpClass(cls): print("\n################################################################################\n") print("#") print("# Initializing read-only config test case") print("#\n") sh.docker_compose("-f", "docker-compose-config-file.yml", "up", d=True, remove_orphans=True, _fg=True) wait_until_running() print()
def stop(name): """Stop a specific docker container server""" pwd = sh.pwd(_out=sys.stdout) path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) path = os.path.join(path, "server") sh.cd(path) sh.docker_compose("rm", "--force", "--stop", name, _out=sys.stdout) sh.cd(pwd)
def request_certificate(domains, email, staging): _init_certbot_env() _create_dummy_certificate(domains) logi("Starting nginx ...") sh.docker_compose("-f", DOCKER_COMPOSE_FILE, "up", "--force-recreate", "-d", "nginx") _deleting_dummy_certificate(domains) _request_letsencrypt_certificate(domains, email, staging)
def start(name): """Start a specific docker container server""" pwd = sh.pwd(_out=sys.stdout) path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) path = os.path.join(path, "server") sh.cd(path) sh.docker_compose("up", "-d", name, _out=sys.stdout) sh.docker_compose("up", "waiter", _out=sys.stdout) sh.cd(pwd)
def setUpClass(cls): print("#") print("# Initializing simple test case") print("#\n") sh.docker_compose("-f", "docker-compose-simple.yml", "up", d=True, remove_orphans=True, _fg=True) time.sleep(40) print()
def compose_action(name, action): files = find_yml_files(settings.COMPOSE_DIR) compose = get_compose(name) env = os.environ.copy() if action == "up": try: _action = docker_compose( action, "-d", _cwd=os.path.dirname(compose["path"]), _env=check_dockerhost(env), ) except Exception as exc: if hasattr(exc, "stderr"): raise HTTPException(400, exc.stderr.decode("UTF-8").rstrip()) else: raise HTTPException(400, exc) elif action == "create": try: _action = docker_compose( "up", "--no-start", _cwd=os.path.dirname(compose["path"]), _env=check_dockerhost(env), ) except Exception as exc: if hasattr(exc, "stderr"): raise HTTPException(400, exc.stderr.decode("UTF-8").rstrip()) else: raise HTTPException(400, exc) else: try: _action = docker_compose( action, _cwd=os.path.dirname(compose["path"]), _env=check_dockerhost(env), ) except Exception as exc: if hasattr(exc, "stderr"): raise HTTPException(400, exc.stderr.decode("UTF-8").rstrip()) else: raise HTTPException(400, exc) if _action.stdout.decode("UTF-8").rstrip(): _output = _action.stdout.decode("UTF-8").rstrip() elif _action.stderr.decode("UTF-8").rstrip(): _output = _action.stderr.decode("UTF-8").rstrip() else: _output = "No Output" print(f"""Project {compose['name']} {action} successful.""") print(f"""Output: """) print(_output) return get_compose_projects()
def _create_dummy_certificate(domains): logi("Creating dummy certificate for {} ...".format(domains)) base_domain = domains[0] CERTBOT_DOMAIN_DIR = CERTBOT_CONF_DIR + '/live/' + base_domain if not os.path.isdir(CERTBOT_DOMAIN_DIR): mkdir("-p", CERTBOT_DOMAIN_DIR) path = "/etc/letsencrypt/live/" + base_domain command = "openssl req -x509 -nodes -newkey rsa:1024 -days 1 -keyout '{}/privkey.pem' -out '{}/fullchain.pem' -subj '/CN=localhost'".format( path, path) sh.docker_compose("-f", DOCKER_COMPOSE_FILE, "run", "--rm", "--entrypoint", command, 'certbot')
def setUpClass(cls): print("\n################################################################################\n") print("#") print("# Initializing idm test case") print("#\n", flush=True) env = {} env.update(os.environ) env["FIWARE_IDM_SERVER"] = "https://accounts.example.com" env["SOCIAL_AUTH_FIWARE_KEY"] = "wirecloud_test_client_id" env["SOCIAL_AUTH_FIWARE_SECRET"] = "notused" sh.docker_compose("-f", "docker-compose-idm.yml", "up", d=True, remove_orphans=True, _env=env, _fg=True) wait_until_running() print(flush=True)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--update', help="Update CloudCompose", action="store_true") parser.add_argument('--init', help="Initialize CloudCompose", action="store_true") parser.add_argument('--ps', help="List containers", action="store_true") parser.add_argument('--start', help="Start containers", action="store_true") parser.add_argument('--stop', help="Stop containers", action="store_true") parser.add_argument('--kill', help="Kill containers", action="store_true") parser.add_argument('--rm', help="Remove containers", action="store_true") args = parser.parse_args() if args.init: compose_init() elif args.update: update_software() elif args.ps: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'ps') elif args.start: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'start') elif args.stop: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'stop') elif args.kill: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'kill') elif args.rm: print """ To remove the containers, please run:\ndocker-compose -p {} -f {} rm """.format(PROJECT, COMPOSEFILE) else: parser.print_help()
def compose_action(name, action): files = find_yml_files(settings.COMPOSE_DIR) compose = get_compose(name) if action == "up": try: _action = docker_compose( "-f", compose["path"], action, "-d", _cwd=os.path.dirname(compose["path"]), ) except Exception as exc: raise HTTPException(400, exc.stderr.decode("UTF-8").rstrip()) elif action == "create": try: _action = docker_compose( "-f", compose["path"], "up", "--no-start", _cwd=os.path.dirname(compose["path"]), ) except Exception as exc: raise HTTPException(400, exc.stderr.decode("UTF-8").rstrip()) else: try: _action = docker_compose( "-f", compose["path"], action, _cwd=os.path.dirname(compose["path"]) ) except Exception as exc: raise HTTPException(400, exc.stderr.decode("UTF-8").rstrip()) if _action.stdout.decode("UTF-8").rstrip(): output = _action.stdout.decode("UTF-8").rstrip() elif _action.stderr.decode("UTF-8").rstrip(): output = _action.stderr.decode("UTF-8").rstrip() else: output = "No Output" print(f"""Project {compose['name']} {action} successful.""") print(f"""Output: """) print(output) return get_compose_projects()
def compose_init(): logger('Initializing CloudCompose...') compose_url = None compose_url_ip = '{}/{}'.format(BASEURL, IP) compose_url_fallback = '{}/{}'.format(BASEURL, 'docker-compose.yml') give_up_at = datetime.datetime.utcnow() + datetime.timedelta( minutes=int(RETRY_PERIOD)) # Keep retrying during RETRY_PERIOD. while give_up_at > datetime.datetime.utcnow(): if requests.head(compose_url_ip).status_code == 200: compose_url = compose_url_ip break else: logger( 'Unable to fetch IP specific compose file. Giving in {}.)' .format(give_up_at - datetime.datetime.utcnow())) time.sleep(10) if not compose_url: if requests.head(compose_url_fallback).status_code == 200: logger('Unable to fetch IP specific file. Using fallback-file.') compose_url = compose_url_fallback else: logger('Unable to retrieve compose file...', error=True) sys.exit(1) get_compose_file = requests.get(compose_url) if get_compose_file.status_code == 200: logger('Successfully fetched compose file from {}...' .format(compose_url)) with open(COMPOSEFILE, 'w') as f: f.write(get_compose_file.content) try: compose = sh.docker_compose( '-p', PROJECT, '-f', COMPOSEFILE, 'up', '-d' ) logger( 'Compose exited with exit code {}'.format(compose.exit_code) ) return compose except: logger('Unable to launch Docker Compose.') else: logger('Unable to retrieve compose file...', error=True) sys.exit(1)
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--update', help="Update CloudCompose", action="store_true" ) parser.add_argument( '--init', help="Initialize CloudCompose", action="store_true" ) parser.add_argument( '--ps', help="List containers", action="store_true" ) parser.add_argument( '--start', help="Start containers", action="store_true" ) parser.add_argument( '--stop', help="Stop containers", action="store_true" ) parser.add_argument( '--kill', help="Kill containers", action="store_true" ) parser.add_argument( '--rm', help="Remove containers", action="store_true" ) args = parser.parse_args() if args.init: compose_init() elif args.update: update_software() elif args.ps: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'ps') elif args.start: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'start') elif args.stop: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'stop') elif args.kill: print sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'kill') elif args.rm: print """ To remove the containers, please run:\ndocker-compose -p {} -f {} rm """.format(PROJECT, COMPOSEFILE) else: parser.print_help()
def compose_init(): logger('Initializing CloudCompose...') compose_url = None compose_url_ip = '{}/{}'.format(BASEURL, IP) compose_url_fallback = '{}/{}'.format(BASEURL, 'docker-compose.yml') give_up_at = datetime.datetime.utcnow() + datetime.timedelta( minutes=int(RETRY_PERIOD)) # Keep retrying during RETRY_PERIOD. while give_up_at > datetime.datetime.utcnow(): if requests.head(compose_url_ip).status_code == 200: compose_url = compose_url_ip break else: logger('Unable to fetch IP specific compose file. Giving in {}.)'. format(give_up_at - datetime.datetime.utcnow())) time.sleep(10) if not compose_url: if requests.head(compose_url_fallback).status_code == 200: logger('Unable to fetch IP specific file. Using fallback-file.') compose_url = compose_url_fallback else: logger('Unable to retrieve compose file...', error=True) sys.exit(1) get_compose_file = requests.get(compose_url) if get_compose_file.status_code == 200: logger( 'Successfully fetched compose file from {}...'.format(compose_url)) with open(COMPOSEFILE, 'w') as f: f.write(get_compose_file.content) try: compose = sh.docker_compose('-p', PROJECT, '-f', COMPOSEFILE, 'up', '-d') logger('Compose exited with exit code {}'.format( compose.exit_code)) return compose except: logger('Unable to launch Docker Compose.') else: logger('Unable to retrieve compose file...', error=True) sys.exit(1)
def run_docker_dev_test(path, coverage=False): """ Method to check that docker runs with dev.yml """ try: # build django, power up the stack and run the test sh.docker_compose("--file", "{}/dev.yml".format(path), "build", "django") sh.docker_compose("--file", "{}/dev.yml".format(path), "build") if coverage: sh.docker_compose("--file", "{}/dev.yml".format(path), "run", "django", "coverage", "run", "manage.py", "test") sh.docker_compose("--file", "{}/dev.yml".format(path), "run", "django", "coverage", "xml", "-o", "coverage.xml") shutil.copyfile(os.path.join(str(path), ".coverage"), os.path.join(PROJECT_DIR, ".coverage")) shutil.copyfile(os.path.join(str(path), "coverage.xml"), os.path.join(PROJECT_DIR, "coverage.xml")) else: sh.docker_compose("--file", "{}/dev.yml".format(path), "run", "django", "python", "manage.py", "test") # test that the development server is running sh.docker_compose("--file", "{}/dev.yml".format(path), "up", "-d") time.sleep(10) curl = sh.curl("-I", "http://localhost:8000/") assert "200 OK" in curl assert "Server: Werkzeug" in curl # since we are running a lot of tests with different configurations, # we need to clean up the environment. Stop all running containers, # remove them and remove the postgres_data volume. sh.docker_compose("--file", "{}/dev.yml".format(path), "stop") sh.docker_compose("--file", "{}/dev.yml".format(path), "rm", "-f") sh.docker("volume", "rm", "cookiecuttersaastestproject_postgres_data_dev") except sh.ErrorReturnCode as e: # in case there are errors it's good to have full output of # stdout and stderr. pytest.fail("STDOUT: {} \n\n\n STDERR: {}".format( e.stdout.decode("utf-8"), e.stderr.decode("utf-8")))
def compose(args=None, opt=None): """ Invoke docker-compose command using Swarm. """ puts(">>> docker-compose #{' '.join(args)} (with --host=#{swarm_url})") sh.docker_compose(*args, _ok_code=[0,1], _env={'DOCKER_HOST': swarm_url}, **LIVE)