~starkingdoms/starkingdoms

ref: 61bde6c6a816bdd3794bb419b7080289354f7f68 starkingdoms/spacetime_rs/src/commands/docker.rs -rw-r--r-- 5.5 KiB
61bde6c6 — ghostlyzsh gravity is now affecting attached modules 2 years ago
                                                                                
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
use crate::commands::api::build_api_prod;
use crate::commands::client::build_client_prod;
use crate::commands::server::build_server_prod;
use crate::ninja::exec;
use std::error::Error;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use sedregex::find_and_replace;

fn _build(img: &str, channel: &str, root: &PathBuf) -> Result<(), Box<dyn Error>> {
    let mut patched = "".to_string();
    // compile the various thingies
    if img == "server" {
        build_server_prod(vec![], root.clone())?;
    } else if img == "api" {
        build_api_prod(vec![], root.clone())?;
    } else if img == "web" {
        // we need to swap out the urls
        // TODO
        // for now i am just adding all three to all clients

        // "s/let api_server = \"http:\\/\\/localhost:8080\";/let api_server = \"https:\\/\\/api.${1}.${2}\";/" "$SCRIPT_DIR/client/index.html"
        // "s/let servers = \[\"localhost:3000\"\];/let servers = [\"${1}.${2}\"];/" "$SCRIPT_DIR/client/index.html"

        let (a, b) = match channel {
            "stable" => ("starkingdoms", "tk"),
            _ => (channel, "starkingdoms.io")
        };

        let index_html_path = root.clone().join("client/").join("index.html");

        let index_html_src = fs::read_to_string(&index_html_path)?;
        let index_html_patched = find_and_replace(&index_html_src, &[
            format!("s/let api_server = \"http:\\/\\/localhost:8080\";/let api_server = \"https:\\/\\/api.{}.{}\";/", a, b)
        ])?;

        fs::write(&index_html_path, index_html_patched.as_bytes())?;

        fs::write(root.clone().join("client/").join("index.html.orig"), index_html_src.clone())?;

        patched = index_html_src.clone();

        build_client_prod(vec![], root.clone())?
    }

    let git_commit_id = String::from_utf8(
        Command::new("git")
            .args(["rev-parse", "--short", "HEAD"])
            .current_dir(root)
            .output()
            .unwrap()
            .stdout,
    )
    .unwrap()
    .replace('\n', "");
    exec(
        "docker",
        root,
        vec![
            "buildx",
            "build",
            "-f",
            root.join(format!("{}.Dockerfile", img)).to_str().unwrap(),
            "-t",
            &format!(
                "registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:{}-{}",
                img, git_commit_id
            ),
            root.to_str().unwrap(),
        ]
        .iter()
        .map(|u| u.to_string())
        .collect(),
    )?;
    exec(
        "docker",
        root,
        vec![
            "buildx",
            "build",
            "-f",
            root.join(format!("{}.Dockerfile", img)).to_str().unwrap(),
            "-t",
            &format!(
                "registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:{}-{}",
                img, channel
            ),
            root.to_str().unwrap(),
        ]
        .iter()
        .map(|u| u.to_string())
        .collect(),
    )?;

    exec(
        "docker",
        root,
        vec![
            "push",
            &format!(
                "registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:{}-{}",
                img, git_commit_id
            ),
        ]
        .iter()
        .map(|u| u.to_string())
        .collect(),
    )?;
    exec(
        "docker",
        root,
        vec![
            "push",
            &format!(
                "registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:{}-{}",
                img, channel
            ),
        ]
        .iter()
        .map(|u| u.to_string())
        .collect(),
    )?;

    if img == "web" {
        fs::write(root.clone().join("client/").join("index.html"), patched)?;
    }

    Ok(())
}

pub fn build_docker_api(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("api", "bleeding", &root)
}
pub fn build_docker_server(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("server", "bleeding", &root)
}
pub fn build_docker_web(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("web", "bleeding", &root)
}
pub fn build_docker(_a: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    build_docker_api(_a.clone(), root.clone())?;
    build_docker_server(_a.clone(), root.clone())?;
    build_docker_web(_a, root)
}

pub fn build_docker_api_beta(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("api", "beta", &root)
}
pub fn build_docker_server_beta(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("server", "beta", &root)
}
pub fn build_docker_web_beta(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("web", "beta", &root)
}
pub fn build_docker_beta(_a: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    build_docker_api_beta(_a.clone(), root.clone())?;
    build_docker_server_beta(_a.clone(), root.clone())?;
    build_docker_web_beta(_a, root)
}

pub fn build_docker_api_stable(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("api", "stable", &root)
}
pub fn build_docker_server_stable(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("server", "stable", &root)
}
pub fn build_docker_web_stable(_: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    _build("web", "stable", &root)
}
pub fn build_docker_stable(_a: Vec<String>, root: PathBuf) -> Result<(), Box<dyn Error>> {
    build_docker_api_stable(_a.clone(), root.clone())?;
    build_docker_server_stable(_a.clone(), root.clone())?;
    build_docker_web_stable(_a, root)
}