Esempio n. 1
0
    def _running(self, con_obj, args, atomic):
        requested_image = self.has_image(args.image)
        if requested_image is not None and con_obj.image != requested_image.id:
            requested_image_fq_name = requested_image.fq_name
            raise AtomicError("Warning: container '{}' already points to {}\nRun 'atomic run {}' to run "
                                          "the existing container.\nRun 'atomic run --replace '{}' to replace "
                                          "it".format(con_obj.name,
                                                      con_obj.original_structure['Config']['Image'],
                                                      con_obj.name,
                                                      requested_image_fq_name))
        if con_obj.interactive:
            container_command = con_obj.command if not args.command else args.command
            container_command = container_command if not isinstance(container_command, list) else " ".join(container_command)
            cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + container_command.split()
            if args.display:
                return atomic.display(" ".join(cmd))
            else:
                return util.check_call(cmd, stderr=DEVNULL)
        else:
            command = con_obj.command if not args.command else args.command
            try:
                cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + command
            except TypeError:
                cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + command.split()

            if args.command:
                if args.display:
                    return util.write_out(" ".join(cmd))
                else:
                    return util.check_call(cmd, stderr=DEVNULL)
            else:
                if not args.display:
                    util.write_out("Container is running")
Esempio n. 2
0
    def _running(self, con_obj, args, atomic):
        if con_obj.interactive:
            container_command = con_obj.command if not args.command else args.command
            container_command = container_command if not isinstance(
                container_command, list) else " ".join(container_command)
            cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name
                   ] + container_command.split()
            if args.display:
                return atomic.display(" ".join(cmd))
            else:
                return util.check_call(cmd, stderr=DEVNULL)
        else:
            command = con_obj.command if not args.command else args.command
            try:
                cmd = [
                    atomic.docker_binary(), "exec", "-t", "-i", con_obj.name
                ] + command
            except TypeError:
                cmd = [
                    atomic.docker_binary(), "exec", "-t", "-i", con_obj.name
                ] + command.split()

            if args.command:
                if args.display:
                    return util.write_out(" ".join(cmd))
                else:
                    return util.check_call(cmd, stderr=DEVNULL)
            else:
                if not args.display:
                    util.write_out("Container is running")
Esempio n. 3
0
 def test_check_output(self):
     exception_raised = False
     try:
         util.check_call(['/usr/bin/does_not_exist'])
     except util.FileNotFound:
         exception_raised = True
     self.assertTrue(exception_raised)
Esempio n. 4
0
 def test_check_output(self):
     exception_raised = False
     try:
         util.check_call(['/usr/bin/does_not_exist'])
     except util.FileNotFound:
         exception_raised = True
     self.assertTrue(exception_raised)
Esempio n. 5
0
    def _running(self, con_obj, args, atomic):
        requested_image = self.has_image(args.image)
        if con_obj.image != requested_image.id:
            requested_image_fq_name = requested_image.fq_name
            raise AtomicError("Warning: container '{}' already points to {}\nRun 'atomic run {}' to run "
                                          "the existing container.\nRun 'atomic run --replace '{}' to replace "
                                          "it".format(con_obj.name,
                                                      con_obj.original_structure['Config']['Image'],
                                                      con_obj.name,
                                                      requested_image_fq_name))
        if con_obj.interactive:
            container_command = con_obj.command if not args.command else args.command
            container_command = container_command if not isinstance(container_command, list) else " ".join(container_command)
            cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + container_command.split()
            if args.display:
                return atomic.display(" ".join(cmd))
            else:
                return util.check_call(cmd, stderr=DEVNULL)
        else:
            command = con_obj.command if not args.command else args.command
            try:
                cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + command
            except TypeError:
                cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + command.split()

            if args.command:
                if args.display:
                    return util.write_out(" ".join(cmd))
                else:
                    return util.check_call(cmd, stderr=DEVNULL)
            else:
                if not args.display:
                    util.write_out("Container is running")
Esempio n. 6
0
    def run(self, iobject, **kwargs):
        atomic = kwargs.get('atomic', None)
        args = kwargs.get('args')
        # atomic must be an instance of Atomic
        # args must be a argparse Namespace
        assert (isinstance(atomic, Atomic))
        assert (isinstance(args, argparse.Namespace))

        # The object is a container
        # If container exists and not started, start it
        # If container exists and is started, execute command inside it (docker exec)
        # If container doesn't exist, create one and start it
        if args.command:
            iobject.command = args.command
        if isinstance(iobject, Container):
            if iobject.running:
                return self._running(iobject, args, atomic)
            else:
                return self._start(iobject, args, atomic)

        # The object is an image

        if iobject.command:
            opts_file = iobject.get_label("RUN_OPTS_FILE")
            if opts_file:
                opts_file = atomic.sub_env_strings("".join(opts_file))
                if opts_file.startswith("/"):
                    if os.path.isfile(opts_file):
                        try:
                            atomic.run_opts = open(opts_file, "r").read()
                        except IOError:
                            raise ValueError(
                                "Failed to read RUN_OPTS_FILE %s" % opts_file)
                else:
                    raise ValueError(
                        "Will not read RUN_OPTS_FILE %s: not absolute path" %
                        opts_file)
        else:
            iobject.command = [atomic.docker_binary(), "run"]
            if os.isatty(0):
                iobject.command += ["-t"]
            if args.detach:
                iobject.command += ["-d"]
            iobject.command += atomic.SPC_ARGS if args.spc else atomic.RUN_ARGS

        if len(iobject.command) > 0 and iobject.command[0] == "docker":
            iobject.command[0] = atomic.docker_binary()

        _cmd = iobject.command if isinstance(
            iobject.command, list) else iobject.command.split()
        cmd = atomic.gen_cmd(_cmd)
        cmd = atomic.sub_env_strings(cmd)
        atomic.display(cmd)
        if atomic.args.display:
            return

        if not atomic.args.quiet:
            self.check_args(cmd)
        util.check_call(cmd, env=atomic.cmd_env())
Esempio n. 7
0
    def run(self, iobject, **kwargs):
        atomic = kwargs.get("atomic", None)
        args = kwargs.get("args")
        # atomic must be an instance of Atomic
        # args must be a argparse Namespace
        assert isinstance(atomic, Atomic)
        assert isinstance(args, argparse.Namespace)

        # The object is a container
        # If container exists and not started, start it
        # If container exists and is started, execute command inside it (docker exec)
        # If container doesn't exist, create one and start it
        if args.command:
            iobject.command = args.command
        if isinstance(iobject, Container):
            if iobject.running:
                return self._running(iobject, args, atomic)
            else:
                return self._start(iobject, args, atomic)

        # The object is an image

        if iobject.command:
            opts_file = iobject.get_label("RUN_OPTS_FILE")
            if opts_file:
                opts_file = atomic.sub_env_strings("".join(opts_file))
                if opts_file.startswith("/"):
                    if os.path.isfile(opts_file):
                        try:
                            atomic.run_opts = open(opts_file, "r").read()
                        except IOError:
                            raise ValueError("Failed to read RUN_OPTS_FILE %s" % opts_file)
                else:
                    raise ValueError("Will not read RUN_OPTS_FILE %s: not absolute path" % opts_file)
        else:
            iobject.command = [atomic.docker_binary(), "run"]
            if os.isatty(0):
                iobject.command += ["-t"]
            if args.detach:
                iobject.command += ["-d"]
            iobject.command += atomic.SPC_ARGS if args.spc else atomic.RUN_ARGS

        if len(iobject.command) > 0 and iobject.command[0] == "docker":
            iobject.command[0] = atomic.docker_binary()

        _cmd = iobject.command if isinstance(iobject.command, list) else iobject.command.split()
        cmd = atomic.gen_cmd(_cmd)
        cmd = atomic.sub_env_strings(cmd)
        atomic.display(cmd)
        if atomic.args.display:
            return

        if not atomic.args.quiet:
            self.check_args(cmd)
        util.check_call(cmd, env=atomic.cmd_env())
Esempio n. 8
0
 def _start(self, con_obj, args, atomic):
     if con_obj.interactive:
         if con_obj.command:
             util.check_call(
                 [atomic.docker_binary(), "start", con_obj.name],
                 stderr=DEVNULL)
             return util.check_call(
                 [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name
                  ] + con_obj.command)
         else:
             return util.check_call([
                 atomic.docker_binary(), "start", "-i", "-a", con_obj.name
             ],
                                    stderr=DEVNULL)
     else:
         if args.command:
             util.check_call(
                 [atomic.docker_binary(), "start", con_obj.name],
                 stderr=DEVNULL)
             return util.check_call(
                 [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name
                  ] + con_obj.command)
         else:
             return util.check_call(
                 [atomic.docker_binary(), "start", con_obj.name],
                 stderr=DEVNULL)
Esempio n. 9
0
 def _start(self, con_obj, args, atomic):
     if con_obj.interactive:
         if args.command:
             util.check_call(
                 [atomic.docker_binary(), "start", con_obj.name],
                 stderr=DEVNULL)
             container_command = args.command if isinstance(args.command, list) else args.command.split()
             return util.check_call(
                 [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] +
                 container_command)
         else:
             return util.check_call(
                 [atomic.docker_binary(), "start", "-i", "-a", con_obj.name],
                 stderr=DEVNULL)
     else:
         if args.command:
             util.check_call(
                 [atomic.docker_binary(), "start", con_obj.name],
                 stderr=DEVNULL)
             return util.check_call(
                 [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] +
                 con_obj.command)
         else:
             return util.check_call(
                 [atomic.docker_binary(), "start", con_obj.name],
                 stderr=DEVNULL)
Esempio n. 10
0
    def uninstall(self, iobject, name=None, **kwargs):
        atomic = kwargs.get('atomic')
        assert (isinstance(atomic, Atomic))
        args = atomic.args
        con_obj = None if not name else self.has_container(name)
        # We have a container by that name, need to stop and delete it
        if con_obj:
            if con_obj.running:
                self.stop_container(con_obj)
            self.delete_container(con_obj.id)

        if args.force:
            self.delete_containers_by_image(iobject, force=True)

        uninstall_command = iobject.get_label('UNINSTALL')
        command_line_args = args.args

        cmd = []
        if uninstall_command:
            try:
                cmd = +uninstall_command
            except TypeError:
                cmd = cmd + uninstall_command.split()
        if command_line_args:
            cmd += command_line_args

        cmd = atomic.gen_cmd(cmd)
        cmd = atomic.sub_env_strings(cmd)
        atomic.display(cmd)
        if args.display:
            return 0
        if cmd:
            return util.check_call(cmd, env=atomic.cmd_env())
        return self.delete_image(iobject.image, force=args.force)
Esempio n. 11
0
    def uninstall(self, iobject, name=None, **kwargs):
        atomic = kwargs.get('atomic')
        ignore = kwargs.get('ignore')
        assert (isinstance(atomic, Atomic))
        args = atomic.args
        con_obj = None if not name else self.has_container(name)
        # We have a container by that name, need to stop and delete it
        if con_obj:
            if con_obj.running:
                self.stop_container(con_obj)
            self.delete_container(con_obj.id)

        if args.force:
            self.delete_containers_by_image(iobject, force=True)
        else:
            containers_by_image = self.get_containers_by_image(iobject)
            if len(containers_by_image) > 0:
                containers_active = ", ".join(
                    [i.name for i in containers_by_image])
                raise ValueError(
                    "Containers `%s` are using this image, delete them first or use --force"
                    % containers_active)

        uninstall_command = iobject.get_label('UNINSTALL')
        command_line_args = args.args

        cmd = []
        if uninstall_command:
            try:
                cmd = cmd + uninstall_command
            except TypeError:
                cmd = cmd + uninstall_command.split()
        if command_line_args:
            cmd += command_line_args

        cmd = atomic.gen_cmd(cmd)
        cmd = atomic.sub_env_strings(cmd)
        atomic.display(cmd)
        if args.display:
            return 0

        if cmd:
            result = util.check_call(cmd, env=atomic.cmd_env())
            if result == 0:
                util.InstallData.delete_by_id(iobject.id, name, ignore=ignore)
            return result

        system_package_nvra = None
        if not ignore:
            install_data = util.InstallData.get_install_data_by_id(iobject.id)
            system_package_nvra = install_data.get("system_package_nvra", None)
        if system_package_nvra:
            RPMHostInstall.uninstall_rpm(system_package_nvra)

        # Delete the entry in the install data
        last_image = util.InstallData.delete_by_id(iobject.id,
                                                   name,
                                                   ignore=ignore)
        if last_image:
            return self.delete_image(iobject.image, force=args.force)
Esempio n. 12
0
 def _running(self, con_obj, args, atomic):
     if con_obj.interactive:
         cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name, con_obj.command]
         if args.display:
             return atomic.display(cmd)
         else:
             return util.check_call(cmd, stderr=DEVNULL)
     else:
         cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + con_obj.command
         if args.command:
             if args.display:
                 return util.write_out(" ".join(cmd))
             else:
                 return util.check_call(cmd, stderr=DEVNULL)
         else:
             if not args.display:
                 util.write_out("Container is running")
Esempio n. 13
0
    def uninstall(self, iobject, name=None, **kwargs):
        atomic = kwargs.get('atomic')
        ignore = kwargs.get('ignore')
        assert(isinstance(atomic, Atomic))
        args = atomic.args
        con_obj = None if not name else self.has_container(name)
        # We have a container by that name, need to stop and delete it
        if con_obj:
            if con_obj.running:
                self.stop_container(con_obj)
            self.delete_container(con_obj.id)

        if args.force:
            self.delete_containers_by_image(iobject, force=True)
        else:
            containers_by_image = self.get_containers_by_image(iobject)
            if len(containers_by_image) > 0:
                containers_active = ", ".join([i.name for i in containers_by_image])
                raise ValueError("Containers `%s` are using this image, delete them first or use --force" % containers_active)

        uninstall_command = iobject.get_label('UNINSTALL')
        command_line_args = args.args

        cmd = []
        if uninstall_command:
            try:
                cmd = cmd + uninstall_command
            except TypeError:
                cmd = cmd + uninstall_command.split()
        if command_line_args:
            cmd += command_line_args

        cmd = atomic.gen_cmd(cmd)
        cmd = atomic.sub_env_strings(cmd)
        atomic.display(cmd)
        if args.display:
            return 0


        if cmd:
            result = util.check_call(cmd, env=atomic.cmd_env())
            if result == 0:
                util.InstallData.delete_by_id(iobject.id, ignore=ignore)
            return result

        system_package_nvra = None
        if not ignore:
            install_data = util.InstallData.get_install_data_by_id(iobject.id)
            system_package_nvra = install_data.get("system_package_nvra", None)
        if system_package_nvra:
            RPMHostInstall.uninstall_rpm(system_package_nvra)

        # Delete the entry in the install data
        util.InstallData.delete_by_id(iobject.id, ignore=ignore)
        return self.delete_image(iobject.image, force=args.force)
Esempio n. 14
0
 def _start(self, con_obj, args, atomic):
     if con_obj.interactive:
         if con_obj.command:
             util.check_call([atomic.docker_binary(), "start", con_obj.name], stderr=DEVNULL)
             return util.check_call([atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + con_obj.command)
         else:
             return util.check_call([atomic.docker_binary(), "start", "-i", "-a", con_obj.name], stderr=DEVNULL)
     else:
         if args.command:
             util.check_call([atomic.docker_binary(), "start", con_obj.name], stderr=DEVNULL)
             return util.check_call([atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + con_obj.command)
         else:
             return util.check_call([atomic.docker_binary(), "start", con_obj.name], stderr=DEVNULL)
Esempio n. 15
0
 def _running(self, con_obj, args, atomic):
     if con_obj.interactive:
         cmd = [
             atomic.docker_binary(), "exec", "-t", "-i", con_obj.name,
             con_obj.command
         ]
         if args.display:
             return atomic.display(cmd)
         else:
             return util.check_call(cmd, stderr=DEVNULL)
     else:
         cmd = [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name
                ] + con_obj.command
         if args.command:
             if args.display:
                 return util.write_out(" ".join(cmd))
             else:
                 return util.check_call(cmd, stderr=DEVNULL)
         else:
             if not args.display:
                 util.write_out("Container is running")
Esempio n. 16
0
    def _start(self, con_obj, args, atomic):
        exec_error = "Failed to execute the command inside the existing container. In some situations " \
                     "this can happen because the entry point command of the container only runs for " \
                     "a short time. You might want to replace the container by executing your " \
                     "command with --replace. Note any updates to the existing container will be lost"

        if con_obj.interactive:
            if args.command:
                util.check_call(
                    [atomic.docker_binary(), "start", con_obj.name],
                    stderr=DEVNULL)
                container_command = args.command if isinstance(
                    args.command, list) else args.command.split()
                try:
                    return util.check_call([
                        atomic.docker_binary(), "exec", "-t", "-i",
                        con_obj.name
                    ] + container_command)
                except CalledProcessError as e:
                    if args.debug:
                        util.write_out(str(e))
                    raise AtomicError(exec_error)

            else:
                return util.check_call([
                    atomic.docker_binary(), "start", "-i", "-a", con_obj.name
                ],
                                       stderr=DEVNULL)
        else:
            if args.command:
                util.check_call(
                    [atomic.docker_binary(), "start", con_obj.name],
                    stderr=DEVNULL)
                try:
                    return util.check_call([
                        atomic.docker_binary(), "exec", "-t", "-i",
                        con_obj.name
                    ] + con_obj.command)
                except CalledProcessError as e:
                    if args.debug:
                        util.write_out(str(e))
                    raise AtomicError(exec_error)

            else:
                return util.check_call(
                    [atomic.docker_binary(), "start", con_obj.name],
                    stderr=DEVNULL)
Esempio n. 17
0
    def uninstall(self, iobject, name=None, **kwargs):
        atomic = kwargs.get('atomic')
        ignore = kwargs.get('ignore')
        assert(isinstance(atomic, Atomic))
        args = atomic.args
        con_obj = None if not name else self.has_container(name)
        # We have a container by that name, need to stop and delete it
        if con_obj:
            if con_obj.running:
                self.stop_container(con_obj)
            self.delete_container(con_obj.id)

        if args.force:
            self.delete_containers_by_image(iobject, force=True)

        uninstall_command = iobject.get_label('UNINSTALL')
        command_line_args = args.args

        cmd = []
        if uninstall_command:
            try:
                cmd = cmd + uninstall_command
            except TypeError:
                cmd = cmd + uninstall_command.split()
        if command_line_args:
            cmd += command_line_args

        cmd = atomic.gen_cmd(cmd)
        cmd = atomic.sub_env_strings(cmd)
        atomic.display(cmd)
        if args.display:
            return 0
        if cmd:
            return util.check_call(cmd, env=atomic.cmd_env())

        install_data = util.InstallData.get_install_data_by_id(iobject.id)
        system_package_nvra = install_data.get("system_package_nvra", None)
        if system_package_nvra:
            RPMHostInstall.uninstall_rpm(system_package_nvra)

        # Delete the entry in the install data
        util.InstallData.delete_by_id(iobject.id, ignore=ignore)
        return self.delete_image(iobject.image, force=args.force)
Esempio n. 18
0
    def _start(self, con_obj, args, atomic):
        exec_error = "Failed to execute the command inside the existing container. In some situations " \
                     "this can happen because the entry point command of the container only runs for " \
                     "a short time. You might want to replace the container by executing your " \
                     "command with --replace. Note any updates to the existing container will be lost"

        if con_obj.interactive:
            if args.command:
                util.check_call([atomic.docker_binary(), "start", con_obj.name], stderr=DEVNULL)
                container_command = args.command if isinstance(args.command, list) else args.command.split()
                try:
                    return util.check_call([atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] + container_command)
                except CalledProcessError as e:
                    if args.debug:
                        util.write_out(str(e))
                    raise AtomicError(exec_error)


            else:
                return util.check_call(
                    [atomic.docker_binary(), "start", "-i", "-a", con_obj.name],
                    stderr=DEVNULL)
        else:
            if args.command:
                util.check_call(
                    [atomic.docker_binary(), "start", con_obj.name],
                    stderr=DEVNULL)
                try:
                    return util.check_call(
                        [atomic.docker_binary(), "exec", "-t", "-i", con_obj.name] +
                        con_obj.command)
                except CalledProcessError as e:
                    if args.debug:
                        util.write_out(str(e))
                    raise AtomicError(exec_error)

            else:
                return util.check_call(
                    [atomic.docker_binary(), "start", con_obj.name],
                    stderr=DEVNULL)
Esempio n. 19
0
    def stop_container(self, con_obj, **kwargs):
        atomic = kwargs.get('atomic')
        args = kwargs.get('args')
        con_obj.stop_args = con_obj.get_label('stop')
        if con_obj.stop_args:
            try:
                cmd = atomic.gen_cmd(con_obj.stop_args.split() + atomic.quote(args.args))
            except TypeError:
                cmd = atomic.gen_cmd(con_obj.stop_args + atomic.quote(args.args))
            cmd = atomic.sub_env_strings(cmd)
            atomic.display(cmd)
            if args.display:
                return 0
            # There should be some error handling around this
            # in case it fails.  And what should then be done?
            return util.check_call(cmd, env=atomic.cmd_env())
        elif args.display:
            return 0

        return self.d.stop(con_obj.id)
Esempio n. 20
0
    def stop_container(self, con_obj, **kwargs):
        atomic = kwargs.get('atomic')
        args = kwargs.get('args')
        con_obj.stop_args = con_obj.get_label('stop')
        if con_obj.stop_args:
            try:
                cmd = atomic.gen_cmd(con_obj.stop_args.split() + atomic.quote(args.args))
            except TypeError:
                cmd = atomic.gen_cmd(con_obj.stop_args + atomic.quote(args.args))
            cmd = atomic.sub_env_strings(cmd)
            atomic.display(cmd)
            if args.display:
                return 0
            # There should be some error handling around this
            # in case it fails.  And what should then be done?
            return util.check_call(cmd, env=atomic.cmd_env())
        elif args.display:
            return 0

        return self.d.stop(con_obj.id)
Esempio n. 21
0
    def run(self, iobject, **kwargs):
        def add_string_or_list_to_list(list_item, value):
            if not isinstance(value, list):
                value = value.split()
            list_item += value
            return list_item

        atomic = kwargs.get('atomic', None)
        args = kwargs.get('args')
        # atomic must be an instance of Atomic
        # args must be a argparse Namespace
        assert(isinstance(atomic, Atomic))
        # The object is a container
        # If container exists and not started, start it
        # If container exists and is started, execute command inside it (docker exec)
        # If container doesn't exist, create one and start it
        if args.command:
            iobject.user_command = args.command
        if isinstance(iobject, Container):
            latest_image = self.inspect_image(iobject.image_name)
            if latest_image.id != iobject.image:
                util.write_out("The '{}' container is using an older version of the installed\n'{}' container image. If "
                               "you wish to use the newer image,\nyou must either create a new container with a "
                               "new name or\nuninstall the '{}' container. \n\n# atomic uninstall --name "
                               "{} {}\n\nand create new container on the {} image.\n\n# atomic update --force "
                               "{}s\n\n removes all containers based on an "
                               "image.".format(iobject.name, iobject.image_name, iobject.name, iobject.name,
                                               iobject.image_name, iobject.image_name, iobject.image_name))
            if iobject.running:
                return self._running(iobject, args, atomic)
            else:
                return self._start(iobject, args, atomic)

        if iobject.get_label('INSTALL') and not args.ignore and not util.InstallData.image_installed(iobject):
            raise ValueError("The image '{}' appears to have not been installed and has an INSTALL label.  You "
                             "should install this image first.  Re-run with --ignore to bypass this "
                             "error.".format(iobject.name or iobject.image))
        # The object is an image
        command = []
        if iobject.run_command:
            command = add_string_or_list_to_list(command, iobject.run_command)
            if iobject.user_command:
                command = add_string_or_list_to_list(command, iobject.user_command)
            opts_file = iobject.get_label("RUN_OPTS_FILE")
            if opts_file:
                opts_file = atomic.sub_env_strings("".join(opts_file))
                if opts_file.startswith("/"):
                    if os.path.isfile(opts_file):
                        try:
                            atomic.run_opts = open(opts_file, "r").read()
                        except IOError:
                            raise ValueError("Failed to read RUN_OPTS_FILE %s" % opts_file)
                else:
                    raise ValueError("Will not read RUN_OPTS_FILE %s: not absolute path" % opts_file)
        else:
            command += [atomic.docker_binary(), "run"]
            if os.isatty(0):
                command += ["-t"]
            if args.detach:
                command += ["-d"]
            command += atomic.SPC_ARGS if args.spc else atomic.RUN_ARGS
            if iobject.user_command:
                command = add_string_or_list_to_list(command, iobject.user_command)

        if len(command) > 0 and command[0] == "docker":
            command[0] = atomic.docker_binary()

        if iobject.cmd and not iobject.user_command and not iobject.run_command:
            cmd = iobject.cmd if isinstance(iobject.cmd, list) else iobject.cmd.split()
            command += cmd
        command = atomic.gen_cmd(command)
        command = atomic.sub_env_strings(command)
        atomic.display(command)
        if atomic.args.display:
            return

        if not atomic.args.quiet:
            self.check_args(command)
        return util.check_call(command, env=atomic.cmd_env())
Esempio n. 22
0
    def run(self, iobject, **kwargs):
        def add_string_or_list_to_list(list_item, value):
            if not isinstance(value, list):
                value = value.split()
            list_item += value
            return list_item

        atomic = kwargs.get('atomic', None)
        args = kwargs.get('args')
        # atomic must be an instance of Atomic
        # args must be a argparse Namespace
        assert(isinstance(atomic, Atomic))
        # The object is a container
        # If container exists and not started, start it
        # If container exists and is started, execute command inside it (docker exec)
        # If container doesn't exist, create one and start it
        if args.command:
            iobject.user_command = args.command
        if isinstance(iobject, Container):
            latest_image = self.inspect_image(iobject.image_name)
            if latest_image.id != iobject.image:
                util.write_out("The '{}' container is using an older version of the installed\n'{}' container image. If "
                               "you wish to use the newer image,\nyou must either create a new container with a "
                               "new name or\nuninstall the '{}' container. \n\n# atomic uninstall --name "
                               "{} {}\n\nand create new container on the {} image.\n\n# atomic update --force "
                               "{}s\n\n removes all containers based on an "
                               "image.".format(iobject.name, iobject.image_name, iobject.name, iobject.name,
                                               iobject.image_name, iobject.image_name, iobject.image_name))

            requested_image = self.has_image(args.image)
            if requested_image is None:
                requested_image = self.has_image(iobject.image)

            if iobject.running:
                if args.replace:
                    iobject = self.replace_existing_container(iobject, requested_image, args)
                    return self.run(iobject, args=args, atomic=atomic)
                return self._running(iobject, args, atomic)
            else:
                # Container with the name exists
                image_id = iobject.image
                if requested_image.id != image_id:
                    if args.replace:
                        iobject = self.replace_existing_container(iobject, requested_image, args)
                    else:
                        try:
                            requested_image_fq_name = requested_image.fq_name
                        except RegistryInspectError:
                            requested_image_fq_name = args.image
                        raise AtomicError("Warning: container '{}' already points to {}\nRun 'atomic run {}' to run "
                                          "the existing container.\nRun 'atomic run --replace '{}' to replace "
                                          "it".format(iobject.name,
                                                      iobject.original_structure['Config']['Image'],
                                                      iobject.name,
                                                      requested_image_fq_name))
                else:
                    if args.replace:
                        iobject = self.replace_existing_container(iobject, requested_image, args)
                    else:
                        return self._start(iobject, args, atomic)

        if iobject.get_label('INSTALL') and not args.ignore and not util.InstallData.image_installed(iobject):
            raise ValueError("The image '{}' appears to have not been installed and has an INSTALL label.  You "
                             "should install this image first.  Re-run with --ignore to bypass this "
                             "error.".format(iobject.name or iobject.image))
        # The object is an image
        command = []
        if iobject.run_command:
            command = add_string_or_list_to_list(command, iobject.run_command)
            if iobject.user_command:
                command = add_string_or_list_to_list(command, iobject.user_command)
            opts_file = iobject.get_label("RUN_OPTS_FILE")
            if opts_file:
                opts_file = atomic.sub_env_strings("".join(opts_file))
                if opts_file.startswith("/"):
                    if os.path.isfile(opts_file):
                        try:
                            atomic.run_opts = open(opts_file, "r").read()
                        except IOError:
                            raise ValueError("Failed to read RUN_OPTS_FILE %s" % opts_file)
                else:
                    raise ValueError("Will not read RUN_OPTS_FILE %s: not absolute path" % opts_file)
        else:
            command += [atomic.docker_binary(), "run"]
            if os.isatty(0):
                command += ["-t"]
            if args.detach:
                command += ["-d"]
            command += atomic.SPC_ARGS if args.spc else atomic.RUN_ARGS
            if iobject.user_command:
                command = add_string_or_list_to_list(command, iobject.user_command)

        if len(command) > 0 and command[0] == "docker":
            command[0] = atomic.docker_binary()

        if iobject.cmd and not iobject.user_command and not iobject.run_command:
            cmd = iobject.cmd if isinstance(iobject.cmd, list) else iobject.cmd.split()
            command += cmd
        command = atomic.gen_cmd(command)
        command = atomic.sub_env_strings(command)
        atomic.display(command)
        if atomic.args.display:
            return

        if not atomic.args.quiet:
            self.check_args(command)
        return util.check_call(command, env=atomic.cmd_env())