Example #1
0
    def build_runtime(self, docker_image_name, dockerfile):
        """
        Builds a new runtime from a Docker file and pushes it to the Docker hub
        """
        logger.info('Building a new docker image from Dockerfile')
        logger.info('Docker image name: {}'.format(docker_image_name))

        # Project ID can contain '-'
        expression = '^([-a-z0-9]+)/([-a-z0-9]+)(:[a-z0-9]+)?'
        result = re.match(expression, docker_image_name)

        if not result or result.group() != docker_image_name:
            raise Exception("Invalid docker image name: '.' or '_' characters are not allowed")

        entry_point = os.path.join(os.path.dirname(__file__), 'entry_point.py')
        create_function_handler_zip(cr_config.FH_ZIP_LOCATION, entry_point, 'lithopsproxy.py')

        # Dockerfile has to be called "Dockerfile" (and in cwd) for 'gcloud builds submit' to work
        if dockerfile != "Dockerfile":
            shutil.copyfile(dockerfile, "Dockerfile")
        cmd = 'gcloud builds submit -t gcr.io/{}'.format(docker_image_name)

        if not self.log_active:
            cmd = cmd + " >{} 2>&1".format(os.devnull)

        res = os.system(cmd)
        if res != 0:
            raise Exception('There was an error building the runtime')

        self._delete_function_handler_zip()
Example #2
0
    def build_runtime(self, docker_image_name, dockerfile):
        """
        Builds a new runtime from a Docker file and pushes it to the Docker hub
        """
        logger.info('Building a new docker image from Dockerfile')
        logger.info('Docker image name: {}'.format(docker_image_name))

        entry_point = os.path.join(os.path.dirname(__file__), 'entry_point.py')
        create_function_handler_zip(codeengine_config.FH_ZIP_LOCATION,
                                    entry_point, 'lithopsentry.py')

        if dockerfile:
            cmd = 'docker build -t {} -f {} .'.format(docker_image_name,
                                                      dockerfile)
        else:
            cmd = 'docker build -t {} .'.format(docker_image_name)

        if not self.log_active:
            cmd = cmd + " >{} 2>&1".format(os.devnull)
        print(cmd)
        res = os.system(cmd)
        if res != 0:
            raise Exception('There was an error building the runtime')

        self._delete_function_handler_zip()

        cmd = 'docker push {}'.format(docker_image_name)
        if not self.log_active:
            cmd = cmd + " >{} 2>&1".format(os.devnull)
        res = os.system(cmd)
        if res != 0:
            raise Exception(
                'There was an error pushing the runtime to the container registry'
            )
Example #3
0
    def create_runtime(self, docker_image_name, memory, timeout):
        """
        Creates a new runtime into IBM CF namespace from an already built Docker image
        """
        if docker_image_name == 'default':
            docker_image_name = self._get_default_runtime_image_name()

        runtime_meta = self._generate_runtime_meta(docker_image_name)

        logger.info(
            'Creating new Lithops runtime based on Docker image {}'.format(
                docker_image_name))

        self.cf_client.create_package(self.package)
        action_name = self._format_action_name(docker_image_name, memory)

        entry_point = os.path.join(os.path.dirname(__file__), 'entry_point.py')
        create_function_handler_zip(ibmcf_config.FH_ZIP_LOCATION, entry_point,
                                    '__main__.py')

        with open(ibmcf_config.FH_ZIP_LOCATION, "rb") as action_zip:
            action_bin = action_zip.read()
        self.cf_client.create_action(self.package,
                                     action_name,
                                     docker_image_name,
                                     code=action_bin,
                                     memory=memory,
                                     is_binary=True,
                                     timeout=timeout * 1000)
        self._delete_function_handler_zip()
        return runtime_meta
Example #4
0
    def _setup_proxy(self):
        logger.info('Installing Lithops proxy in VM instance')

        unix_service = """
        [Unit]
        Description=Lithops Proxy
        After=network.target

        [Service]
        ExecStart=/usr/bin/python3 {}/proxy.py
        Restart=always

        [Install]
        WantedBy=multi-user.target
        """.format(REMOTE_INSTALL_DIR)
        service_file = '/etc/systemd/system/{}'.format(PROXY_SERVICE_NAME)
        self.ssh_client.upload_data_to_file(self.ip_address, textwrap.dedent(unix_service), service_file)

        cmd = 'mkdir -p {}; '.format(REMOTE_INSTALL_DIR)
        cmd += 'systemctl daemon-reload '
        cmd += '&& systemctl stop {} > /dev/null 2>&1'.format(PROXY_SERVICE_NAME)
        self.ssh_client.run_remote_command(self.ip_address, cmd)

        config_file = os.path.join(REMOTE_INSTALL_DIR, 'config')
        self.ssh_client.upload_data_to_file(self.ip_address, json.dumps(self.config), config_file)

        src_proxy = os.path.join(os.path.dirname(__file__), 'proxy.py')
        create_function_handler_zip(FH_ZIP_LOCATION, src_proxy)
        self.ssh_client.upload_local_file(self.ip_address, FH_ZIP_LOCATION, '/tmp/lithops_standalone.zip')
        os.remove(FH_ZIP_LOCATION)

        cmd = 'apt-get remove docker docker-engine docker.io containerd runc -y '
        cmd += '&& apt-get update '
        cmd += '&& apt-get install unzip python3-pip apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y '
        cmd += '&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - > /dev/null 2>&1 '
        cmd += '&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" '
        cmd += '&& apt-get update '
        cmd += '&& apt-get install docker-ce docker-ce-cli containerd.io -y '

        cmd += '&& pip3 install -U lithops flask '
        cmd += '&& pip3 uninstall lithops -y '

        cmd += '&& mkdir -p {} '.format(REMOTE_INSTALL_DIR)
        cmd += '&& unzip -o /tmp/lithops_standalone.zip -d {} > /dev/null 2>&1 '.format(REMOTE_INSTALL_DIR)
        cmd += '&& rm /tmp/lithops_standalone.zip; '

        cmd += 'chmod 644 {} '.format(service_file)
        cmd += '&& systemctl daemon-reload '
        cmd += '&& systemctl stop {} '.format(PROXY_SERVICE_NAME)
        cmd += '&& systemctl enable {} '.format(PROXY_SERVICE_NAME)
        cmd += '&& systemctl start {} '.format(PROXY_SERVICE_NAME)
        self.ssh_client.run_remote_command(self.ip_address, cmd, background=True)
Example #5
0
    def build_runtime(self, docker_image_name, dockerfile):
        """
        Builds a new runtime from a Docker file and pushes it to the Docker hub
        """
        logger.info('Building a new docker image from Dockerfile')
        logger.info('Docker image name: {}'.format(docker_image_name))

        expression = '^([a-z0-9]+)/([-a-z0-9]+)(:[a-z0-9]+)?'
        result = re.match(expression, docker_image_name)

        if not result or result.group() != docker_image_name:
            raise Exception(
                "Invalid docker image name: '.' or '_' characters are not allowed"
            )

        entry_point = os.path.join(os.path.dirname(__file__), 'entry_point.py')
        create_function_handler_zip(kconfig.FH_ZIP_LOCATION, entry_point,
                                    'lithopsproxy.py')

        if dockerfile:
            cmd = 'docker build -t {} -f {} .'.format(docker_image_name,
                                                      dockerfile)
        else:
            cmd = 'docker build -t {} .'.format(docker_image_name)

        if not self.log_active:
            cmd = cmd + " >{} 2>&1".format(os.devnull)

        res = os.system(cmd)
        if res != 0:
            raise Exception('There was an error building the runtime')

        self._delete_function_handler_zip()

        cmd = 'docker push {}'.format(docker_image_name)
        if not self.log_active:
            cmd = cmd + " >{} 2>&1".format(os.devnull)
        res = os.system(cmd)
        if res != 0:
            raise Exception(
                'There was an error pushing the runtime to the container registry'
            )
Example #6
0
    def _setup_proxy(self):
        logger.info('Installing Lithops proxy in the VM instance')
        logger.debug(
            'Be patient, installation process can take up to 3 '
            'minutes if this is the first time you use the VM instance')

        service_file = '/etc/systemd/system/{}'.format(PROXY_SERVICE_NAME)
        self.ssh_client.upload_data_to_file(self.ip_address,
                                            PROXY_SERVICE_FILE, service_file)

        cmd = 'mkdir -p {}; '.format(REMOTE_INSTALL_DIR)
        cmd += 'systemctl daemon-reload; systemctl stop {}; '.format(
            PROXY_SERVICE_NAME)
        self.ssh_client.run_remote_command(self.ip_address, cmd)

        config_file = os.path.join(REMOTE_INSTALL_DIR, 'config')
        self.ssh_client.upload_data_to_file(self.ip_address,
                                            json.dumps(self.config),
                                            config_file)

        src_proxy = os.path.join(os.path.dirname(__file__), 'proxy.py')
        create_function_handler_zip(FH_ZIP_LOCATION, src_proxy)
        self.ssh_client.upload_local_file(self.ip_address, FH_ZIP_LOCATION,
                                          '/tmp/lithops_standalone.zip')
        os.remove(FH_ZIP_LOCATION)

        cmd = 'apt-get update; apt-get install unzip python3-pip -y; '
        cmd += 'pip3 install flask gevent pika==0.13.1; '
        cmd += 'unzip -o /tmp/lithops_standalone.zip -d {} > /dev/null 2>&1; '.format(
            REMOTE_INSTALL_DIR)
        cmd += 'rm /tmp/lithops_standalone.zip; '
        cmd += 'chmod 644 {}; '.format(service_file)
        cmd += 'systemctl daemon-reload; '
        cmd += 'systemctl stop {}; '.format(PROXY_SERVICE_NAME)
        cmd += 'systemctl enable {}; '.format(PROXY_SERVICE_NAME)
        cmd += 'systemctl start {}; '.format(PROXY_SERVICE_NAME)
        self.ssh_client.run_remote_command(self.ip_address,
                                           cmd,
                                           background=True)