コード例 #1
0
 def cp(self, pod_id, pod_path, current_path, direction='copy_from'):
     if direction == 'copy_from':
         (exc, out, err) = run_shell_cmd('kubectl cp {}/{}:{} {}'.format(
             self.namespace, pod_id, pod_path, current_path))
         return int(exc)
     elif direction == 'copy_to':
         (exc, out, err) = run_shell_cmd('kubectl cp {} {}/{}:{}'.format(
             current_path, self.namespace, pod_id, pod_path))
         return int(exc)
     else:
         raise OperetoRuntimeError(error='Invalid copy direction.')
コード例 #2
0
    def process(self):
        host_path = self.input['host_path']
        container_path = '{}:{}'.format(self.input['container_id'],
                                        self.input['container_path'])
        if self.input['copy_direction'] == 'host_to_container':
            self._print_step_title(
                'Coping from host file system to container..')
            source = host_path
            dest = container_path
        else:
            self._print_step_title(
                'Coping from container to host file system..')
            source = container_path
            dest = host_path

        print 'Source: {}'.format(source)
        print 'Destination: {}'.format(dest)

        (rc, out,
         error) = run_shell_cmd('docker cp {} {}'.format(source, dest))
        if rc:
            print >> sys.stderr, 'Failed to copy..'
            print >> sys.stderr, out + error
            return self.client.FAILURE

        return self.client.SUCCESS
コード例 #3
0
    def _run_task(self):

        command_prefix = ' -v {}:{}'.format(self.parser_results_directory, self.test_results_directory)

        ## add volume and env vars to command
        docker_cmd_splitted = re.split('docker\s+run', self.docker_command)
        command_postfix = docker_cmd_splitted[0]
        if len(docker_cmd_splitted) == 2:
            command_postfix = docker_cmd_splitted[1]

        ## modify docker command
        if self.docker_env_params:
            with open(self.docker_env_vars, 'a') as env_file:
                for name, value in self.docker_env_params.items():
                    env_file.write('{}={}\n'.format(name, value))
            command_prefix += ' --env-file {}'.format(self.docker_env_vars)
        if self.docker_image:
            if docker_cmd_splitted[1].find(self.docker_image) == 0:
                command_postfix += ' {}'.format(self.docker_image)
        if command_prefix:
            command_prefix += ' '
        docker_cmd = 'docker run {}{}'.format(command_prefix, command_postfix)

        ## run container
        self._print_step_title('Running docker container task..')
        print('Command: {}'.format(docker_cmd))
        (exc, out, error) = run_shell_cmd(docker_cmd, verbose=True)
        if exc!=0:
            return self.client.FAILURE
        return self.client.SUCCESS
コード例 #4
0
    def _run_task(self):

        command_prefix = ' -v {}:{}'.format(self.host_test_result_directory, self.input['test_results_directory'])

        ## add volume and env vars to command
        docker_cmd_splitted = re.split('docker\s+run', self.docker_command)
        command_postfix = docker_cmd_splitted[0]
        if len(docker_cmd_splitted) == 2:
            command_postfix = docker_cmd_splitted[1]

        ## modify docker command
        if self.docker_env_params:
            with open(self.docker_env_vars, 'a') as env_file:
                for name, value in self.docker_env_params.items():
                    env_file.write('{}={}\n'.format(name, value))
            command_prefix += ' --env-file {}'.format(self.docker_env_vars)
        if self.docker_image:
            if docker_cmd_splitted[1].find(self.docker_image) == 0:
                command_postfix += ' {}'.format(self.docker_image)
        if command_prefix:
            command_prefix += ' '
        docker_cmd = 'docker run {}{}'.format(command_prefix, command_postfix)

        ## run container
        print 'Running: ' + docker_cmd
        (exc, out, error) = run_shell_cmd(docker_cmd, verbose=True)
        self.client.modify_process_property('exitcode', exc)

        if exc not in self.valid_exit_codes:
            print >> sys.stderr, 'Docker execution failed..'
            self.exitcode = 2
コード例 #5
0
ファイル: osutil.py プロジェクト: oip-rnd/worker
def remove_package_cmd(package_name, sudo=True):
    if is_ubuntu():
        cmd = 'apt-get purge -y {}'.format(package_name)
    else:
        cmd = 'yum remove -y {}'.format(package_name)

    if sudo:
        cmd = 'sudo -iEn '+cmd

    (exc, out, error) = run_shell_cmd(cmd)
    return exc, out, error
コード例 #6
0
ファイル: osutil.py プロジェクト: oip-rnd/worker
def install_package_cmd(package_name, sudo=True, update_sources=False):
    cmd=''
    if is_ubuntu():
        if update_sources:
            cmd+='apt-get update ; '
        cmd += 'apt-get install -y {}'.format(package_name)
    else:
        if update_sources:
            cmd+='yum update ; '
        cmd = 'yum install -y {}'.format(package_name)

    if sudo:
        cmd = 'sudo -iEn '+cmd

    (exc, out, error) = run_shell_cmd(cmd)
    return exc, out, error
コード例 #7
0
    def process(self):
        self._print_step_title(
            'Uninstalling old docker installations if found..')
        (rc, out, error
         ) = remove_package_cmd('docker-ce docker docker-engine docker.io')
        print out + error

        (rc2, out2, error2) = run_shell_cmd('docker version')
        if rc2 == 0:
            print >> sys.stderr, 'Docker is still running on this host. Please re-try later or remove docker manually.'
            print out2 + error2
            return self.client.FAILURE

        self._print_step_title('Updating docker config files..')
        self.client.modify_agent_property(self.client.input['opereto_agent'],
                                          'opereto.docker.worker', False)

        print 'Docker was removed.'
        return self.client.SUCCESS
コード例 #8
0
    def process(self):

        (rc, out, error) = run_shell_cmd('docker version')
        if rc==0:
            sys.stderr.write('Docker is already installed. Please remove this version first.')
            sys.stderr.write(out+error)
            return self.client.FAILURE

        self._print_step_title('Updating docker package sources..')
        (rc, out, error) = install_package_cmd('apt-transport-https ca-certificates curl software-properties-common', update_sources=self.input['update_sources'])
        if rc:
            sys.stderr.write(out+error)
            return self.client.FAILURE
        commands = [
            'curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -',
            'add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"'
        ]
        for cmd in commands:
            (rc0, out0, error0) = run_shell_cmd('sudo -iEn '+cmd)
            if rc0:
                sys.stderr.write('Failed to run command: {}'.format(cmd))
                sys.stderr.write(out0+error0)
                return self.client.FAILURE

        self._print_step_title('Installing docker (make take a few minutes..)')

        install_command = 'docker-ce'
        if self.input['docker-ce-version']!='latest':
            install_command += '={}'.format(self.input['docker-ce-version'])

        (rc1, out1, error1) = install_package_cmd(install_command, update_sources=self.input['update_sources'])
        if rc1:
            sys.stderr.write(out1, error1)
            sys.stderr.write('Docker setup failed. Please retry to install a different version of docker.')
            return self.client.FAILURE


        self._print_step_title('Validating docker installation (running hello-world container)..')
        (rc2, out2, error2) = run_shell_cmd('sudo docker run hello-world')
        if rc2:
            sys.stderr.write(out2+error2)
            sys.stderr.write('Verification failed. Please retry to install a different version of docker.')
            return self.client.FAILURE
        else:
            sys.stderr.write('Docker is up and running.')
            run_shell_cmd('sudo docker kill hello-world')

        if self.input.get('dockerhub_credentials'):
            self._print_step_title('Login to docker hub..')
            try:
                json_config = self.input['dockerhub_credentials']
                if validate_string(json_config):
                    json_config=json.loads(json_config)
                login_cmd = 'sudo -iEn docker login -u {} -p {}'.format(json_config['username'], json_config['password'])
                if json_config.get('server'):
                    login_cmd += ' {}'.format(json_config['server'])
                (rc3, out3, error3) = run_shell_cmd(login_cmd)
                sys.stderr.write(sys.stderr, out3+error3)
                if rc3:
                    raise Exception('Failed to login to docker hub')
            except Exception as e:
                sys.stderr.write('Login failed: {}. Please retry to login manually.'.format(str(e)))
                return self.client.FAILURE

        (rc4, out4, error4) = run_shell_cmd('docker version')
        print(out4+error4)

        self._print_step_title('Updating docker worker agent property..')
        self.client.modify_agent_property(self.input['opereto_agent'], 'opereto.docker.worker', True)

        print('Docker worker added successfully.')
        return self.client.SUCCESS
コード例 #9
0
ファイル: run.py プロジェクト: oip-rnd/docker
    def process(self):

        self._print_step_title('Setup container..')
        image_name = self.input['docker_image']
        if self.input['fetch_from_dockerhub']:
            self._print_step_title('Fetching image from docker hub..')
            (rc, out,
             error) = run_shell_cmd('docker pull {}'.format(image_name))
            if rc:
                print >> sys.stderr, 'Failed to fetch image from docker hub'
                print >> sys.stderr, out + error
                return self.client.FAILURE

        additional_config = self.input['container_config'] or {}
        install_agent = False

        if self.input['embedded_agent']:
            agent_cred = {
                'opereto_host': self.input['opereto_host'],
                'opereto_user': self.input['opereto_user'],
                'opereto_password': self.input['opereto_password'],
                'agent_name': self.agent_id,
                'log_level': 'info',
                'javaParams': '-Xms1000m -Xmx1000m'
            }
            if additional_config.get('environment'):
                additional_config['environment'].update(agent_cred)
            else:
                additional_config['environment'] = agent_cred
        else:
            install_agent = True

        self.container = self.dockereto.setup_container(
            image_name, **additional_config)

        container_cred = {
            'container_id': self.container.id,
            'host_agent': self.input['opereto_agent'],
            'container_agent': self.agent_id
        }
        self.client.modify_process_property('container_credentials',
                                            container_cred)

        WAIT_AGENT_ITERATIONS = 10
        if install_agent:
            WAIT_AGENT_ITERATIONS = 20
            self._print_step_title('Installing opereto agent on container..')

            def copy_files(file):
                source = os.path.join(self.input['opereto_workspace'], file)
                dest = os.path.join('/', file)
                copy_pid = self.client.create_process(
                    'docker_copy',
                    container_id=self.container.id,
                    host_path=source,
                    container_path=dest,
                    copy_direction='host_to_container')
                if not self.client.is_success(copy_pid):
                    self.cleanup = True
                    return self.client.FAILURE

            if copy_files('opereto-agent.jar'):
                return self.client.FAILURE
            if copy_files('run-opereto-agent.sh'):
                return self.client.FAILURE

            cmd_pid0 = self.client.create_process(
                'docker_exec_cmd',
                workdir='/',
                container_id=self.container.id,
                command='chmod 777 run-opereto-agent.sh')
            if not self.client.is_success(cmd_pid0):
                self.cleanup = True
                return self.client.FAILURE

            install_cmd = '/run-opereto-agent.sh -b {} -u {} -p {} -n {} -o root -g root'.format(
                self.input['opereto_host'], self.input['opereto_user'],
                self.input['opereto_password'], agent_id)
            cmd_pid = self.client.create_process(
                'docker_exec_cmd',
                detach=True,
                workdir='/',
                container_id=self.container.id,
                command=install_cmd)
            if not self.client.is_success(cmd_pid):
                self.cleanup = True
                return self.client.FAILURE

        ## check agent availability
        self._print_step_title('Waiting for container agent to connect..')
        agent_is_up = False
        for i in range(WAIT_AGENT_ITERATIONS):
            try:
                agent_status = self.client.get_agent_status(self.agent_id)
                if agent_status['online']:
                    agent_is_up = True
                    print 'Container agent is up and running.'
                    break
            except:
                pass
            time.sleep(10)

        if not agent_is_up:
            print >> sys.stderr, 'Container agent is not connected.'
            self.cleanup = True
            return self.client.FAILURE

        ## create/modify agent
        permissions = {'owners': self.users, 'users': self.owners}
        agent_properties = self.input['agent_properties']
        agent_properties.update({
            'container_host_agent':
            self.input['opereto_agent'],
            'container_id':
            self.container.id
        })

        self.client.modify_agent_properties(self.agent_id, agent_properties)
        self.client.modify_agent(self.agent_id,
                                 name=self.input['agent_name'],
                                 description=self.input['agent_description'],
                                 permissions=permissions)

        ## run post install services
        for service in self.input['post_operations']:
            input = service.get('input') or {}
            agent = service.get('agents') or self.agent_id
            pid = self.client.create_process(service=service['service'],
                                             agent=agent,
                                             title=service.get('title'),
                                             **input)
            if not self.client.is_success(pid):
                self.cleanup = True
                return self.client.FAILURE

        return self.client.SUCCESS