Parse the ContainerID from a Docker container and return None if the container was not launched from Mesos.
| 120 | // Parse the ContainerID from a Docker container and return None if |
| 121 | // the container was not launched from Mesos. |
| 122 | Option<ContainerID> parse(const Docker::Container& container) |
| 123 | { |
| 124 | Option<string> name = None(); |
| 125 | Option<ContainerID> containerId = None(); |
| 126 | |
| 127 | if (strings::startsWith(container.name, DOCKER_NAME_PREFIX)) { |
| 128 | name = strings::remove( |
| 129 | container.name, DOCKER_NAME_PREFIX, strings::PREFIX); |
| 130 | } else if (strings::startsWith(container.name, "/" + DOCKER_NAME_PREFIX)) { |
| 131 | name = strings::remove( |
| 132 | container.name, "/" + DOCKER_NAME_PREFIX, strings::PREFIX); |
| 133 | } |
| 134 | |
| 135 | if (name.isSome()) { |
| 136 | // For Mesos versions 0.23 to 1.3 (inclusive), the docker |
| 137 | // container name format was: |
| 138 | // DOCKER_NAME_PREFIX + SlaveID + DOCKER_NAME_SEPERATOR + ContainerID. |
| 139 | // |
| 140 | // In versions <= 0.22 or >= 1.4, the name format is: |
| 141 | // DOCKER_NAME_PREFIX + ContainerID. |
| 142 | // |
| 143 | // To be backward compatible during upgrade, we still have to |
| 144 | // support all formats. |
| 145 | if (!strings::contains(name.get(), DOCKER_NAME_SEPERATOR)) { |
| 146 | ContainerID id; |
| 147 | id.set_value(name.get()); |
| 148 | containerId = id; |
| 149 | } else { |
| 150 | vector<string> parts = strings::split(name.get(), DOCKER_NAME_SEPERATOR); |
| 151 | if (parts.size() == 2 || parts.size() == 3) { |
| 152 | ContainerID id; |
| 153 | id.set_value(parts[1]); |
| 154 | containerId = id; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Check if id is a valid UUID. |
| 159 | if (containerId.isSome()) { |
| 160 | Try<id::UUID> uuid = id::UUID::fromString(containerId->value()); |
| 161 | if (uuid.isError()) { |
| 162 | return None(); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return containerId; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | Try<DockerContainerizer*> DockerContainerizer::create( |
no test coverage detected