summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/yum/AddYumRepo.java
blob: 9485e5d6728200b2fd78278fe8f9f7912107bd0c (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
// 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.file.FileWriter;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.regex.Pattern;

/**
 * @author hakonhall
 */
public class AddYumRepo {
    private static final Pattern REPOSITORY_ID_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]+$");

    private final String repositoryId; // e.g. "platform_rpms-latest"
    private final String name; // e.g. "Platform RPM Latest Repo"
    private final String baseurl;
    private final boolean enabled;
    private final FileSystem fileSystem;

    public AddYumRepo(String repositoryId,
                      String name,
                      String baseurl,
                      boolean enabled) {
        this(repositoryId, name, baseurl, enabled, FileSystems.getDefault());
    }

    public boolean converge(TaskContext context) {
        Path path = fileSystem.getPath("/etc/yum.repos.d",repositoryId + ".repo");

        FileWriter fileWriter = new FileWriter(path, this::getRepoFileContent)
                .withOwner("root")
                .withGroup("root")
                .withPermissions("rw-r--r--")
                .onlyIfFileDoesNotAlreadyExist();

        return fileWriter.converge(context);
    }

    private String getRepoFileContent() {
        return String.join("\n",
                "# This file was generated by node admin",
                "# Do NOT modify this file by hand",
                "",
                "[" + repositoryId + "]",
                "name=" + name,
                "baseurl=" + baseurl,
                "enabled=" + (enabled ? 1 : 0),
                "gpgcheck=0"
        ) + "\n";
    }

    private static void validateRepositoryId(String repositoryId) {
        if (!REPOSITORY_ID_PATTERN.matcher(repositoryId).matches()) {
            throw new IllegalArgumentException("Invalid repository ID '" + repositoryId + "'");
        }
    }

    // For testing
    AddYumRepo(String repositoryId,
               String name,
               String baseurl,
               boolean enabled,
               FileSystem fileSystem) {
        this.repositoryId = repositoryId;
        this.name = name;
        this.baseurl = baseurl;
        this.enabled = enabled;
        this.fileSystem = fileSystem;
        validateRepositoryId(repositoryId);
    }
}