aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/DefaultEnvWriter.java
blob: 1889332ee49c560722e055e15b5b92f4dd20b53d (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
// Copyright Yahoo. 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;

import com.yahoo.vespa.hosted.node.admin.component.TaskContext;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;

import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.ifExists;
import static com.yahoo.yolean.Exceptions.uncheck;
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
import static java.util.stream.Collectors.joining;

/**
 * Rewrites default-env.txt files.
 *
 * @author bjorncs
 */
public class DefaultEnvWriter {

    private static final Logger logger = Logger.getLogger(DefaultEnvWriter.class.getName());

    private final Map<String, Operation> operations = new LinkedHashMap<>();

    public DefaultEnvWriter addOverride(String name, String value) {
        return addOperation("override", name, value);
    }

    public DefaultEnvWriter addFallback(String name, String value) {
        return addOperation("fallback", name, value);
    }

    public DefaultEnvWriter addUnset(String name) {
        return addOperation("unset", name, null);
    }

    private DefaultEnvWriter addOperation(String action, String name, String value) {
        if (operations.containsKey(name)) {
            throw new IllegalArgumentException(String.format("Operation on variable '%s' already added", name));
        }
        operations.put(name, new Operation(action, name, value));
        return this;
    }

    /**
     * Updates or created a default-env.txt file
     *
     * @return true if the file was modified
     */
    public boolean updateFile(TaskContext context, Path defaultEnvFile) {
        List<String> currentDefaultEnvLines = ifExists(() -> Files.readAllLines(defaultEnvFile)).orElse(List.of());
        List<String> newDefaultEnvLines = generateContent(currentDefaultEnvLines);
        if (currentDefaultEnvLines.equals(newDefaultEnvLines)) {
            return false;
        } else {
            context.log(logger, "Updating " + defaultEnvFile.toString());
            Path tempFile = defaultEnvFile.resolveSibling(defaultEnvFile.getFileName() + ".tmp");
            uncheck(() -> Files.write(tempFile, newDefaultEnvLines));
            uncheck(() -> Files.move(tempFile, defaultEnvFile, ATOMIC_MOVE));
            return true;
        }
    }

    /**
     * @return generated default-env.txt content
     */
    public String generateContent() {
        return generateContent(List.of()).stream()
                .collect(joining(System.lineSeparator(), "", System.lineSeparator()));
    }

    private List<String> generateContent(List<String> currentDefaultEnvLines) {
        List<String> newDefaultEnvLines = new ArrayList<>();
        Set<String> seenNames = new TreeSet<>();
        for (String line : currentDefaultEnvLines) {
            String[] items = line.split(" ");
            if (items.length < 2) {
                throw new IllegalArgumentException(String.format("Invalid line in file '%s': %s", currentDefaultEnvLines, line));
            }
            String name = items[1];
            if (!seenNames.contains(name)) { // implicitly removes duplicated variables
                seenNames.add(name);
                Operation operation = operations.get(name);
                if (operation != null) {
                    newDefaultEnvLines.add(operation.toLine());
                } else {
                    newDefaultEnvLines.add(line);
                }
            }
        }
        for (var operation : operations.values()) {
            if (!seenNames.contains(operation.name)) {
                newDefaultEnvLines.add(operation.toLine());
            }
        }
        return newDefaultEnvLines;
    }

    private record Operation(String action, String name, String value) {
        String toLine() {
            if (action.equals("unset")) {
                return "unset " + name;
            }
            return action + " " + name + " " + value;
        }
    }
}