|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020, NVIDIA CORPORATION. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.gpuopenanalytics.jenkins.remotedocker; |
| 26 | + |
| 27 | +import java.io.Serializable; |
| 28 | + |
| 29 | +public class DockerVersion implements Serializable { |
| 30 | + |
| 31 | + private int major; |
| 32 | + private int minor; |
| 33 | + private int patch; |
| 34 | + private String extra; |
| 35 | + private String build; |
| 36 | + |
| 37 | + private DockerVersion(int major, |
| 38 | + int minor, |
| 39 | + int patch, |
| 40 | + String extra, |
| 41 | + String build) { |
| 42 | + this.major = major; |
| 43 | + this.minor = minor; |
| 44 | + this.patch = patch; |
| 45 | + this.extra = extra; |
| 46 | + this.build = build; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Parse from output of <code>docker --version</code> such as <code>Docker |
| 51 | + * version 19.03.5, build 633a0ea</code> |
| 52 | + * |
| 53 | + * @return |
| 54 | + */ |
| 55 | + public static DockerVersion fromVersionString(String versionString) { |
| 56 | + String[] split = versionString.split("\\s"); |
| 57 | + String build = split[split.length - 1]; |
| 58 | + String[] version = split[2].substring(0, split[2].length() - 1).split( |
| 59 | + "\\."); |
| 60 | + int major = Integer.parseInt(version[0]); |
| 61 | + int minor = Integer.parseInt(version[1]); |
| 62 | + int patch = 0; |
| 63 | + String extra = null; |
| 64 | + if (version[2].contains("-")) { |
| 65 | + patch = Integer.parseInt( |
| 66 | + version[2].substring(0, version[2].indexOf('-'))); |
| 67 | + extra = version[2].substring(version[2].indexOf('-') + 1); |
| 68 | + } else { |
| 69 | + patch = Integer.parseInt(version[2]); |
| 70 | + } |
| 71 | + return new DockerVersion(major, minor, patch, extra, build); |
| 72 | + } |
| 73 | + |
| 74 | + public String getVersionString() { |
| 75 | + return String.format("%02d.%02d.%d", major, minor, patch); |
| 76 | + } |
| 77 | + |
| 78 | + public boolean hasGpuFlag() { |
| 79 | + return "19.03.0".compareTo(getVersionString()) < 0; |
| 80 | + } |
| 81 | + |
| 82 | + public String toString() { |
| 83 | + if (extra != null) { |
| 84 | + return String.format("Docker version %s-%s, build %s", |
| 85 | + getVersionString(), |
| 86 | + extra, |
| 87 | + build); |
| 88 | + } else { |
| 89 | + return String.format("Docker version %s, build %s", |
| 90 | + getVersionString(), |
| 91 | + build); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments