service_install/install/
init.rs

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
190
191
192
193
194
195
196
197
198
199
use std::path::{Path, PathBuf};

pub mod cron;
pub(crate) mod extract_path;
pub mod systemd;

use sysinfo::Pid;

use crate::install::RemoveStep;

use self::systemd::FindExeError;

use super::builder::Trigger;
use super::files::{DisableError, NoHomeError, TargetInUseError};
use super::{InstallStep, Mode};

type Steps = Vec<Box<dyn InstallStep>>;
type RSteps = Vec<Box<dyn RemoveStep>>;

/// Allowed init systems, set using: [`install::Spec::allowed_inits()`](super::Spec::allowed_inits)
#[derive(Debug, Clone)]
pub enum System {
    Systemd,
    Cron,
}

type ExeLocation = PathBuf;
impl System {
    pub(crate) fn name(&self) -> &'static str {
        match self {
            System::Systemd => "Systemd",
            System::Cron => "Cron",
        }
    }
    pub(crate) fn not_available(&self) -> Result<bool, SetupError> {
        match self {
            System::Systemd => systemd::not_available(),
            System::Cron => Ok(cron::not_available()),
        }
    }
    pub(crate) fn disable_steps(
        &self,
        target: &Path,
        pid: Pid,
        mode: Mode,
        run_as: Option<&str>,
    ) -> Result<Vec<Box<dyn InstallStep>>, TargetInUseError> {
        match self {
            System::Systemd => Ok(systemd::disable_step(target, mode).map_err(DisableError::from)?),
            System::Cron => {
                Ok(cron::disable::step(target, pid, run_as).map_err(DisableError::from)?)
            }
        }
    }
    pub(crate) fn set_up_steps(&self, params: &Params) -> Result<Steps, SetupError> {
        match self {
            System::Systemd => systemd::set_up_steps(params),
            System::Cron => cron::set_up_steps(params),
        }
    }
    pub(crate) fn tear_down_steps(
        &self,
        bin_name: &str,
        mode: Mode,
        user: Option<&str>,
    ) -> Result<Option<(RSteps, ExeLocation)>, TearDownError> {
        match self {
            System::Systemd => systemd::tear_down_steps(mode),
            System::Cron => cron::tear_down_steps(bin_name, mode, user),
        }
    }

    pub(crate) fn all() -> Vec<System> {
        vec![Self::Systemd, Self::Cron]
    }

    pub(crate) fn is_init_path(&self, path: &Path) -> Result<bool, PathCheckError> {
        match self {
            System::Systemd => systemd::path_is_systemd(path),
            System::Cron => Ok(cron::is_init_path(path)),
        }
    }
}

#[derive(Debug, thiserror::Error)]
#[error("The path could not be resolved, error {0}")]
pub struct PathCheckError(std::io::Error);

#[derive(thiserror::Error, Debug)]
pub enum SetupError {
    #[error("systemd specific error")]
    Systemd(
        #[from]
        #[source]
        systemd::Error,
    ),
    #[error("Error while setting up crontab rule")]
    Cron(
        #[from]
        #[source]
        cron::setup::Error,
    ),
    #[error("could not find current users home dir")]
    NoHome(#[from] NoHomeError),
}

#[derive(thiserror::Error, Debug)]
pub enum TearDownError {
    #[error("Cron specific error")]
    Cron(
        #[from]
        #[source]
        cron::teardown::Error,
    ),
    #[error("Error while setting up systemd service")]
    Systemd(
        #[from]
        #[source]
        systemd::Error,
    ),
    #[error("Could not find current users home dir")]
    NoHome(
        #[from]
        #[source]
        NoHomeError,
    ),
    #[error("No service file while there is a timer file")]
    TimerWithoutService,
    #[error("Could not find path to executable")]
    FindingExePath(
        #[from]
        #[source]
        FindExeError,
    ),
    #[error(
        "Found multiple different paths in services, do not know which to remove, paths: {0:?}"
    )]
    MultipleExePaths(Vec<PathBuf>),
}

#[derive(Debug, Clone)]
pub(crate) struct Params {
    pub(crate) name: String,
    pub(crate) bin_name: &'static str,
    pub(crate) description: Option<String>,

    pub(crate) exe_path: PathBuf,
    pub(crate) exe_args: Vec<String>,
    pub(crate) working_dir: Option<PathBuf>,

    pub(crate) trigger: Trigger,
    pub(crate) run_as: Option<String>,
    pub(crate) mode: Mode,
}

impl Params {
    fn description(&self) -> String {
        self.description
            .clone()
            .unwrap_or_else(|| format!("starts {}", self.name))
    }
}

pub(crate) const COMMENT_PREAMBLE: &str = "# created by: ";
pub(crate) const COMMENT_SUFFIX: &str = " during its installation\n# might get removed by it in the future.\n# Remove this comment to prevent that";

fn autogenerated_comment(bin_name: &str) -> String {
    format!("{COMMENT_PREAMBLE}'{bin_name}'{COMMENT_SUFFIX}")
}

trait EscapedPath {
    fn shell_escaped(&self) -> String;
}

impl EscapedPath for std::path::PathBuf {
    fn shell_escaped(&self) -> String {
        let path = self.display().to_string();
        let path = std::borrow::Cow::Owned(path);
        let path = shell_escape::unix::escape(path);
        path.into_owned()
    }
}

impl EscapedPath for &std::path::Path {
    fn shell_escaped(&self) -> String {
        let path = self.display().to_string();
        let path = std::borrow::Cow::Owned(path);
        let path = shell_escape::unix::escape(path);
        path.into_owned()
    }
}

impl EscapedPath for String {
    fn shell_escaped(&self) -> String {
        let s = std::borrow::Cow::Borrowed(self.as_str());
        let s = shell_escape::unix::escape(s);
        s.into_owned()
    }
}