| 107 | |
| 108 | |
| 109 | Try<Nothing> initialize(const Flags& flags) |
| 110 | { |
| 111 | static Once* initialized = new Once(); |
| 112 | |
| 113 | if (initialized->once()) { |
| 114 | return Nothing(); |
| 115 | } |
| 116 | |
| 117 | if (!systemd::exists()) { |
| 118 | return Error("systemd does not exist on this system"); |
| 119 | } |
| 120 | |
| 121 | systemd_flags = new Flags(flags); |
| 122 | |
| 123 | // Do not initialize any state if we do not have systemd support enabled. |
| 124 | if (!systemd_flags->enabled) { |
| 125 | initialized->done(); |
| 126 | return Nothing(); |
| 127 | } |
| 128 | |
| 129 | // If flags->runtime_directory doesn't exist, then we can't proceed. |
| 130 | if (!os::exists(CHECK_NOTNULL(systemd_flags)->runtime_directory)) { |
| 131 | return Error("Failed to locate systemd runtime directory: " + |
| 132 | CHECK_NOTNULL(systemd_flags)->runtime_directory); |
| 133 | } |
| 134 | |
| 135 | // On systemd environments we currently migrate executor pids and processes |
| 136 | // that need to live alongside the executor into a separate executor slice. |
| 137 | // This allows the life-time of the process to be extended past the life-time |
| 138 | // of the slave. See MESOS-3352. |
| 139 | // This function takes responsibility for creating and starting this slice. |
| 140 | // We inject a `Subprocess::ParentHook` into the `subprocess` function that |
| 141 | // migrates pids into this slice if the `EXTEND_LIFETIME` option is set on the |
| 142 | // `subprocess` call. |
| 143 | |
| 144 | // Ensure that the `MESOS_EXECUTORS_SLICE` exists and is running. |
| 145 | // TODO(jmlvanre): Prevent racing between multiple agents for this creation |
| 146 | // logic. |
| 147 | |
| 148 | // Check whether the `MESOS_EXECUTORS_SLICE` already exists. Create it if |
| 149 | // it does not exist. |
| 150 | // We explicitly don't modify the file if it exists in case operators want |
| 151 | // to over-ride the settings for the slice that we provide when we create |
| 152 | // the `Unit` below. |
| 153 | const Path path(path::join( |
| 154 | systemd::runtimeDirectory(), |
| 155 | mesos::MESOS_EXECUTORS_SLICE)); |
| 156 | |
| 157 | if (!systemd::slices::exists(path)) { |
| 158 | // A simple systemd file to allow us to start a new slice. |
| 159 | string unit = "[Unit]\nDescription=Mesos Executors Slice\n"; |
| 160 | |
| 161 | Try<Nothing> create = systemd::slices::create(path, unit); |
| 162 | |
| 163 | if (create.isError()) { |
| 164 | return Error("Failed to create systemd slice '" + |
| 165 | stringify(mesos::MESOS_EXECUTORS_SLICE) + |
| 166 | "': " + create.error()); |