]>
Commit | Line | Data |
---|---|---|
20effc67 | 1 | import os |
33c7a0ef | 2 | from typing import List, Union |
20effc67 | 3 | |
33c7a0ef TL |
4 | from util import ( |
5 | Config, | |
6 | Target, | |
7 | get_boxes_container_info, | |
8 | inside_container, | |
9 | run_cephadm_shell_command, | |
10 | run_dc_shell_command, | |
11 | run_shell_command, | |
12 | ) | |
20effc67 TL |
13 | |
14 | ||
15 | def _setup_ssh(container_index): | |
16 | if inside_container(): | |
17 | if not os.path.exists('/root/.ssh/known_hosts'): | |
18 | run_shell_command('ssh-keygen -A') | |
19 | ||
20 | run_shell_command('echo "root:root" | chpasswd') | |
21 | with open('/etc/ssh/sshd_config', 'a+') as f: | |
22 | f.write('PermitRootLogin yes\n') | |
23 | f.write('PasswordAuthentication yes\n') | |
24 | f.flush() | |
25 | run_shell_command('/usr/sbin/sshd') | |
26 | else: | |
33c7a0ef | 27 | print('Redirecting to _setup_ssh to container') |
20effc67 | 28 | verbose = '-v' if Config.get('verbose') else '' |
33c7a0ef TL |
29 | run_dc_shell_command( |
30 | f'/cephadm/box/box.py {verbose} host setup_ssh {container_index}', | |
31 | container_index, | |
32 | 'hosts', | |
33 | ) | |
20effc67 | 34 | |
33c7a0ef TL |
35 | |
36 | def _add_hosts(ips: Union[List[str], str], hostnames: Union[List[str], str]): | |
37 | if inside_container(): | |
38 | assert len(ips) == len(hostnames) | |
39 | for i in range(len(ips)): | |
40 | run_cephadm_shell_command(f'ceph orch host add {hostnames[i]} {ips[i]}') | |
41 | else: | |
42 | print('Redirecting to _add_hosts to container') | |
43 | verbose = '-v' if Config.get('verbose') else '' | |
44 | print(ips) | |
45 | ips = ' '.join(ips) | |
46 | ips = f'{ips}' | |
47 | hostnames = ' '.join(hostnames) | |
48 | hostnames = f'{hostnames}' | |
49 | run_dc_shell_command( | |
50 | f'/cephadm/box/box.py {verbose} host add_hosts 1 --ips {ips} --hostnames {hostnames}', | |
51 | 1, | |
52 | 'seed', | |
53 | ) | |
54 | ||
55 | ||
56 | def _copy_cluster_ssh_key(ips: Union[List[str], str]): | |
20effc67 TL |
57 | if inside_container(): |
58 | local_ip = run_shell_command('hostname -i') | |
59 | for ip in ips: | |
60 | if ip != local_ip: | |
33c7a0ef TL |
61 | run_shell_command( |
62 | ( | |
63 | 'sshpass -p "root" ssh-copy-id -f ' | |
64 | f'-o StrictHostKeyChecking=no -i /etc/ceph/ceph.pub "root@{ip}"' | |
65 | ) | |
66 | ) | |
20effc67 TL |
67 | |
68 | else: | |
33c7a0ef | 69 | print('Redirecting to _copy_cluster_ssh to container') |
20effc67 TL |
70 | verbose = '-v' if Config.get('verbose') else '' |
71 | print(ips) | |
72 | ips = ' '.join(ips) | |
33c7a0ef | 73 | ips = f'{ips}' |
20effc67 | 74 | # assume we only have one seed |
33c7a0ef TL |
75 | run_dc_shell_command( |
76 | f'/cephadm/box/box.py {verbose} host copy_cluster_ssh_key 1 --ips {ips}', | |
77 | 1, | |
78 | 'seed', | |
79 | ) | |
80 | ||
81 | ||
20effc67 TL |
82 | class Host(Target): |
83 | _help = 'Run seed/host related commands' | |
33c7a0ef | 84 | actions = ['setup_ssh', 'copy_cluster_ssh_key', 'add_hosts'] |
20effc67 TL |
85 | |
86 | def set_args(self): | |
87 | self.parser.add_argument('action', choices=Host.actions) | |
33c7a0ef TL |
88 | self.parser.add_argument( |
89 | 'host_container_index', type=str, help='box_host_{index}' | |
90 | ) | |
20effc67 | 91 | self.parser.add_argument('--ips', nargs='*', help='List of host ips') |
33c7a0ef TL |
92 | self.parser.add_argument( |
93 | '--hostnames', nargs='*', help='List of hostnames ips(relative to ip list)' | |
94 | ) | |
20effc67 TL |
95 | |
96 | def setup_ssh(self): | |
97 | _setup_ssh(Config.get('host_container_index')) | |
98 | ||
33c7a0ef TL |
99 | def add_hosts(self): |
100 | ips = Config.get('ips') | |
101 | if not ips: | |
102 | ips = get_boxes_container_info()['ips'] | |
103 | hostnames = Config.get('hostnames') | |
104 | if not hostnames: | |
105 | hostnames = get_boxes_container_info()['hostnames'] | |
106 | _add_hosts(ips, hostnames) | |
20effc67 TL |
107 | |
108 | def copy_cluster_ssh_key(self): | |
109 | ips = Config.get('ips') | |
110 | if not ips: | |
33c7a0ef | 111 | ips = get_boxes_container_info()['ips'] |
20effc67 | 112 | _copy_cluster_ssh_key(ips) |