aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/yum/Yum.java
blob: cb23f05308667f97784e93f97a41dbf9d7f9f3d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.yum;

import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandLine;
import com.yahoo.vespa.hosted.node.admin.task.util.process.Terminal;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author hakonhall
 */
public class Yum {
    // Note: "(?dm)" makes newline be \n (only), and enables multiline mode where ^$ match lines with find()
    private static final Pattern INSTALL_NOOP_PATTERN = Pattern.compile("(?dm)^Nothing to do$");
    private static final Pattern UPGRADE_NOOP_PATTERN = Pattern.compile("(?dm)^No packages marked for update$");
    private static final Pattern REMOVE_NOOP_PATTERN = Pattern.compile("(?dm)^No Packages marked for removal$");

    private static final Pattern UNKNOWN_PACKAGE_PATTERN = Pattern.compile(
            "(?dm)^No package ([^ ]+) available\\.$");

    private final Terminal terminal;

    public Yum(Terminal terminal) {
        this.terminal = terminal;
    }

    public GenericYumCommand install(YumPackageName... packages) {
        return newYumCommand("install", packages, INSTALL_NOOP_PATTERN);
    }

    public GenericYumCommand install(String package1, String... packages) {
        return install(toYumPackageNameArray(package1, packages));
    }

    public GenericYumCommand upgrade(YumPackageName... packages) {
        return newYumCommand("upgrade", packages, UPGRADE_NOOP_PATTERN);
    }

    public GenericYumCommand upgrade(String package1, String... packages) {
        return upgrade(toYumPackageNameArray(package1, packages));
    }

    public GenericYumCommand remove(YumPackageName... packages) {
        return newYumCommand("remove", packages, REMOVE_NOOP_PATTERN);
    }

    public GenericYumCommand remove(String package1, String... packages) {
        return remove(toYumPackageNameArray(package1, packages));
    }

    static YumPackageName[] toYumPackageNameArray(String package1, String... packages) {
        YumPackageName[] array = new YumPackageName[1 + packages.length];
        array[0] = YumPackageName.fromString(package1);
        for (int i = 0; i < packages.length; ++i) {
            array[1 + i] = YumPackageName.fromString(packages[i]);
        }
        return array;
    }

    private GenericYumCommand newYumCommand(String yumCommand,
                                            YumPackageName[] packages,
                                            Pattern noopPattern) {
        return new GenericYumCommand(
                terminal,
                yumCommand,
                Arrays.asList(packages),
                noopPattern);
    }

    public static class GenericYumCommand {
        private final Terminal terminal;
        private final String yumCommand;
        private final List<YumPackageName> packages;
        private final Pattern commandOutputNoopPattern;

        private Optional<String> enabledRepo = Optional.empty();
        private boolean lockVersion = false;

        private GenericYumCommand(Terminal terminal,
                                  String yumCommand,
                                  List<YumPackageName> packages,
                                  Pattern commandOutputNoopPattern) {
            this.terminal = terminal;
            this.yumCommand = yumCommand;
            this.packages = packages;
            this.commandOutputNoopPattern = commandOutputNoopPattern;

            if (packages.isEmpty() && ! "upgrade".equals(yumCommand)) {
                throw new IllegalArgumentException("No packages specified");
            }
        }

        @SuppressWarnings("unchecked")
        public GenericYumCommand enableRepo(String repo) {
            enabledRepo = Optional.of(repo);
            return this;
        }

        /**
         * Ensure the version of the installs are locked.
         *
         * <p>WARNING: In order to simplify the user interface of {@link #lockVersion()},
         * the package name specified in the command, e.g. {@link #install(String, String...)}, MUST be of
         * a simple format, see {@link YumPackageName#fromString(String)}.
         */
        public GenericYumCommand lockVersion() {
            // Verify each package has sufficient info to form a proper version lock name.
            packages.forEach(YumPackageName::toVersionLockName);
            lockVersion = true;
            return this;
        }

        public boolean converge(TaskContext context) {
            Set<String> packageNamesToLock = new HashSet<>();
            Set<String> fullPackageNamesToLock = new HashSet<>();

            if (lockVersion) {
                // Remove all locks for other version

                packages.forEach(packageName -> {
                            packageNamesToLock.add(packageName.getName());
                            fullPackageNamesToLock.add(packageName.toVersionLockName());
                        });

                terminal.newCommandLine(context)
                        .add("yum", "--quiet", "versionlock", "list")
                        .executeSilently()
                        .getOutputLinesStream()
                        .map(YumPackageName::parseString)
                        .filter(Optional::isPresent)
                        .map(Optional::get)
                        .forEach(packageName -> {
                            // Ignore lines for other packages
                            if (packageNamesToLock.contains(packageName.getName())) {
                                // If existing lock doesn't exactly match the full package name,
                                // it means it's locked to another version and we must remove that lock.
                                String versionLockName = packageName.toVersionLockName();
                                if (!fullPackageNamesToLock.remove(versionLockName)) {
                                    terminal.newCommandLine(context)
                                            .add("yum", "versionlock", "delete", versionLockName)
                                            .execute();
                                }
                            }
                        });
            }

            CommandLine commandLine = terminal.newCommandLine(context);
            commandLine.add("yum", yumCommand, "--assumeyes");
            enabledRepo.ifPresent(repo -> commandLine.add("--enablerepo=" + repo));
            commandLine.add(packages.stream().map(YumPackageName::toName).collect(Collectors.toList()));

            // There's no way to figure out whether a yum command would have been a no-op.
            // Therefore, run the command and parse the output to decide.
            boolean modifiedSystem = commandLine
                    .executeSilently()
                    .mapOutput(this::mapOutput);

            if (modifiedSystem) {
                commandLine.recordSilentExecutionAsSystemModification();
            }

            fullPackageNamesToLock.forEach(fullPackageName ->
                    terminal.newCommandLine(context)
                            .add("yum", "versionlock", "add", fullPackageName)
                            .execute());
            modifiedSystem |= !fullPackageNamesToLock.isEmpty();

            return modifiedSystem;
        }

        private boolean mapOutput(String output) {
            Matcher unknownPackageMatcher = UNKNOWN_PACKAGE_PATTERN.matcher(output);
            if (unknownPackageMatcher.find()) {
                throw new IllegalArgumentException("Unknown package: " + unknownPackageMatcher.group(1));
            }

            return !commandOutputNoopPattern.matcher(output).find();
        }
    }

}