|
1 | | -# Copyright 2025 Genesis Corporation. |
| 1 | +# Copyright 2025-2026 Genesis Corporation. |
2 | 2 | # |
3 | 3 | # All Rights Reserved. |
4 | 4 | # |
|
13 | 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 | 14 | # License for the specific language governing permissions and limitations |
15 | 15 | # under the License. |
16 | | - |
| 16 | +import grp |
| 17 | +import os |
| 18 | +import pwd |
17 | 19 | import ipaddress |
18 | 20 | import logging |
| 21 | +import pathlib |
19 | 22 | import typing as tp |
20 | 23 | import uuid as sys_uuid |
21 | 24 |
|
|
32 | 35 | from genesis_core.vs.dm import models as vs_models |
33 | 36 |
|
34 | 37 | LOG = logging.getLogger(__name__) |
| 38 | +USER = "ubuntu" |
35 | 39 |
|
36 | 40 |
|
37 | 41 | def add_core_set( |
@@ -128,6 +132,31 @@ def add_core_set( |
128 | 132 | return node_set |
129 | 133 |
|
130 | 134 |
|
| 135 | +def save_developer_keys(keys: str) -> None: |
| 136 | + """Save developer keys from the spec.""" |
| 137 | + if not keys: |
| 138 | + LOG.debug("No developer keys provided.") |
| 139 | + return |
| 140 | + |
| 141 | + pw = pwd.getpwnam(USER) |
| 142 | + uid, gid = pw.pw_uid, grp.getgrnam(USER).gr_gid |
| 143 | + ssh_dir = pathlib.Path(pw.pw_dir) / ".ssh" |
| 144 | + |
| 145 | + ssh_dir.mkdir(parents=True, exist_ok=True) |
| 146 | + os.chown(ssh_dir, uid, gid) |
| 147 | + ssh_dir.chmod(0o700) |
| 148 | + |
| 149 | + keys_path = ssh_dir / "authorized_keys" |
| 150 | + content = keys_path.read_text() if keys_path.exists() else "" |
| 151 | + if keys not in content: |
| 152 | + with keys_path.open("a") as f: |
| 153 | + if content and not content.endswith("\n"): |
| 154 | + f.write("\n") |
| 155 | + f.write(keys + "\n") |
| 156 | + os.chown(keys_path, uid, gid) |
| 157 | + keys_path.chmod(0o600) |
| 158 | + |
| 159 | + |
131 | 160 | def activate_profile(profile_name: str) -> bool: |
132 | 161 | # Check if there is already an active profile |
133 | 162 | active_profile = vs_models.Profile.objects.get_one_or_none( |
|
0 commit comments