Пример #1
0
    def _make_package(self, res_name, package):
        LOG.debug('Make package %(name)s begin...', {'name': res_name})

        # mkdir and copy files
        src_path = FAAS_PIPELINE_PATH
        base_path = src_path + res_name + '/'
        if not os.path.exists(base_path):
            os.makedirs(base_path)

        base_package_file = src_path + self.base_package
        package_name = os.path.basename(package)
        shutil.copy(base_package_file, base_path)
        shutil.copy(package, base_path)
        run_sh = src_path + 'make_package.sh'
        shutil.copy(run_sh, base_path)
        run_sh = base_path + 'make_package.sh'
        dockerfile_src_path = src_path + 'faas-worker'
        dockerfile_dst_path = base_path + 'faas-worker'
        if os.path.exists(dockerfile_dst_path):
            shutil.rmtree(dockerfile_dst_path)
        shutil.copytree(dockerfile_src_path, dockerfile_dst_path)

        # make package
        cur_dir = os.getcwd()
        os.chdir(base_path)
        execute(run_sh,
                self.base_package,
                package_name,
                check_exit_code=[0],
                run_as_root=True)
        os.chdir(cur_dir)

        # remove base_path
        # shutil.rmtree(base_path)
        LOG.debug('Make package %(name)s end.', {'name': res_name})
Пример #2
0
    def _check_host(self, host):
        LOG.debug('Chcke host %(host)s begin...', {'host': host['host_ip']})

        # host
        # host_par = '127.0.0.1'
        host_par = host['host_par']
        base_path = FAAS_PIPELINE_PATH
        hosts_file = base_path + 'hosts'
        fo = open(hosts_file, 'w')
        fo.write("[nodes]\n")
        fo.write(host_par)
        fo.flush()
        fo.close()

        # vars
        vars_file = base_path + 'host_vars.yml'
        for line in fileinput.input(vars_file, inplace=1):
            line = line.strip()
            strs = line.split(':')
            if 'dest_host' in strs[0]:
                line = strs[0] + ': ' + host['host_ip']
            print(line)

        # check
        ans_file = base_path + 'host_check.yml'
        execute('ansible-playbook',
                ans_file,
                '-i',
                hosts_file,
                check_exit_code=[0],
                run_as_root=True)

        LOG.debug('Check host %(host)s end.', {'host': host['host_ip']})
Пример #3
0
    def _build_image(self, res_name, image, tag, host):
        LOG.debug('Building image %(image)s:%(tag)s begin...', {
            'image': image,
            'tag': tag
        })

        # mkdir and copy files
        src_path = FAAS_PIPELINE_PATH
        base_path = src_path + res_name + '/'
        if not os.path.exists(base_path):
            os.makedirs(base_path)
        src_file = src_path + 'build_vars.yml'
        shutil.copy(src_file, base_path)
        src_file = src_path + 'image_build.yml'
        shutil.copy(src_file, base_path)

        # host
        # host_str = host + ' ansible_ssh_pass=cloud ansible_become_pass=cloud'
        host_par = host['host_par']
        hosts_file = base_path + 'hosts'
        fo = open(hosts_file, 'w')
        fo.write("[nodes]\n")
        fo.write(host_par)
        fo.flush()
        fo.close()

        # vars
        build_path = base_path + 'faas-worker/'
        vars_file = base_path + 'build_vars.yml'
        for line in fileinput.input(vars_file, inplace=1):
            line = line.strip()
            strs = line.split(':')
            if 'build_path' in strs[0]:
                line = strs[0] + ': ' + build_path
            if 'registry' in strs[0]:
                line = strs[0] + ': ' + self.registry
            if 'image_name' in strs[0]:
                line = strs[0] + ': ' + image
            if 'image_tag' in strs[0]:
                line = strs[0] + ': ' + tag
            print(line)

        # build
        ans_file = base_path + 'image_build.yml'
        execute('ansible-playbook',
                ans_file,
                '-i',
                hosts_file,
                check_exit_code=[0],
                run_as_root=True)

        # remove dir
        shutil.rmtree(base_path)
        LOG.debug('Building image %(image)s:%(tag)s end.', {
            'image': image,
            'tag': tag
        })
Пример #4
0
    def destroy(self, ins_data):
        name = ins_data['name']
        host_ip = ins_data['host_ip']
        LOG.debug('Destroy instance %(name)s on host %(host)s begin...', {
            'name': name,
            'host': host_ip
        })

        # mkdir and copy files
        src_path = '/root/faas/deploy/'
        base_path = src_path + name + '/'
        if not os.path.exists(base_path):
            os.makedirs(base_path)
        src_file = src_path + 'destroy_vars.yml'
        shutil.copy(src_file, base_path)
        src_file = src_path + 'instance_destroy.yml'
        shutil.copy(src_file, base_path)

        # host
        # host_str = host + ' ansible_ssh_pass=cloud ansible_become_pass=cloud'
        host_par = ''
        for host in self.hosts_ok:
            if host_ip == host['host_ip']:
                host_par = host['host_par']
                break
        hosts_file = base_path + 'hosts'
        fo = open(hosts_file, 'w')
        fo.write("[nodes]\n")
        fo.write(host_par)
        fo.flush()
        fo.close()

        # vars
        vars_file = base_path + 'destroy_vars.yml'
        for line in fileinput.input(vars_file, inplace=1):
            line = line.strip()
            strs = line.split(':')
            if 'instance_name' in strs[0]:
                line = strs[0] + ': ' + name
            print(line)

        # destroy
        ans_file = base_path + 'instance_destroy.yml'
        execute('ansible-playbook',
                ans_file,
                '-i',
                hosts_file,
                check_exit_code=[0],
                run_as_root=True)

        # remove dir
        shutil.rmtree(base_path)
        LOG.debug('Delete instance %(name)s end.', {'name': name})
Пример #5
0
    def _deploy_host(self, host):
        LOG.debug('Deploy host %(host)s begin...', {'host': host['host_ip']})

        # host
        host_par = host['host_par']
        base_path = '/root/faas/deploy/'
        hosts_file = base_path + 'hosts'
        fo = open(hosts_file, 'w')
        fo.write("[nodes]\n")
        fo.write(host_par)
        fo.flush()
        fo.close()

        # deploy
        ans_file = base_path + 'host_deploy.yml'
        execute('ansible-playbook',
                ans_file,
                '-i',
                hosts_file,
                check_exit_code=[0],
                run_as_root=True)

        LOG.debug('Deploy host %(host)s end.', {'host': host['host_ip']})
Пример #6
0
    def _create_instance(self, name, image, host, port):
        LOG.debug(
            'Create instance %(name)s, port %(port)d for image %(image)s on host %(host)s begin...',
            {
                'name': name,
                'port': port,
                'image': image,
                'host': host['host_ip']
            })

        # mkdir and copy files
        src_path = '/root/faas/deploy/'
        base_path = src_path + name + '/'
        if not os.path.exists(base_path):
            os.makedirs(base_path)
        src_file = src_path + 'vars.yml'
        shutil.copy(src_file, base_path)
        src_file = src_path + 'image_deploy.yml'
        shutil.copy(src_file, base_path)

        # host
        # host_str = host + ' ansible_ssh_pass=cloud ansible_become_pass=cloud'
        host_par = host['host_par']
        hosts_file = base_path + 'hosts'
        fo = open(hosts_file, 'w')
        fo.write("[nodes]\n")
        fo.write(host_par)
        fo.flush()
        fo.close()

        # vars
        vars_file = base_path + 'vars.yml'
        log_path = '/root/faas/logs/' + name
        for line in fileinput.input(vars_file, inplace=1):
            line = line.strip()
            strs = line.split(':')
            if 'deploy_image' in strs[0]:
                line = strs[0] + ': ' + image
            if 'deploy_name' in strs[0]:
                line = strs[0] + ': ' + name
            if 'host_port' in strs[0]:
                line = strs[0] + ': ' + str(port)
            if 'log_path' in strs[0]:
                line = strs[0] + ': ' + log_path
            if 'life_cycle' in strs[0]:
                line = strs[0] + ': ' + str(WORKER_LIFE_CYCLE)
            if 'log_level' in strs[0]:
                line = strs[0] + ': ' + WORKER_LOG_LEVEL
            print(line)

        # deploy
        ans_file = base_path + 'image_deploy.yml'
        execute('ansible-playbook',
                ans_file,
                '-i',
                hosts_file,
                check_exit_code=[0],
                run_as_root=True)

        # remove dir
        shutil.rmtree(base_path)
        LOG.debug('Create instance %(name)s end.', {'name': name})