vm-tool 1.0.4__tar.gz → 1.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: vm_tool
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Summary: A Comprehensive Tool for Setting Up Virtual Machines Using Ansible.
5
5
  License: MIT
6
6
  Description-Content-Type: text/markdown
@@ -7,7 +7,7 @@ with open(os.path.join(os.path.dirname(__file__), 'Readme.md'), encoding='utf-8'
7
7
 
8
8
  setup(
9
9
  name='vm_tool',
10
- version='1.0.4', # This will be updated by bump2version
10
+ version='1.0.6', # This will be updated by bump2version
11
11
  packages=find_packages(),
12
12
  description='A Comprehensive Tool for Setting Up Virtual Machines Using Ansible.',
13
13
  long_description=long_description,
@@ -0,0 +1,117 @@
1
+ import os
2
+ import paramiko
3
+
4
+ class SSHSetup:
5
+ """
6
+ A class to set up SSH configuration and keys for a VM.
7
+
8
+ Attributes:
9
+ hostname (str): The hostname of the VM.
10
+ username (str): The username for SSH login.
11
+ password (str): The password for SSH login.
12
+ email (str): The email for SSH key generation.
13
+ private_key_path (str): The path to the private SSH key.
14
+ client (paramiko.SSHClient): The SSH client.
15
+ """
16
+
17
+ def __init__(self, hostname, username, password, email):
18
+ """
19
+ The constructor for SSHSetup class.
20
+
21
+ Parameters:
22
+ hostname (str): The hostname of the VM.
23
+ username (str): The username for SSH login.
24
+ password (str): The password for SSH login.
25
+ email (str): The email for SSH key generation.
26
+ """
27
+ self.hostname = hostname
28
+ self.username = username
29
+ self.password = password
30
+ self.email = email
31
+ self.private_key_path = os.path.expanduser('~/.ssh/id_rsa')
32
+ self.client = paramiko.SSHClient()
33
+ self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
34
+
35
+ def generate_ssh_key(self, email):
36
+ """
37
+ Generates an SSH key pair.
38
+
39
+ Parameters:
40
+ email (str): The email for SSH key generation.
41
+ """
42
+ print(f"Generating SSH key for email: {email}")
43
+ os.system(f'ssh-keygen -t rsa -b 4096 -C "{email}"')
44
+
45
+ def read_or_generate_public_key(self, email):
46
+ """
47
+ Reads the public SSH key or generates a new one if it doesn't exist.
48
+
49
+ Parameters:
50
+ email (str): The email for SSH key generation.
51
+
52
+ Returns:
53
+ str: The public SSH key.
54
+ """
55
+ public_key_path = f'{self.private_key_path}.pub'
56
+ if not os.path.exists(public_key_path):
57
+ self.generate_ssh_key(email)
58
+ with open(public_key_path, 'r') as file:
59
+ print(f"Reading public key from {public_key_path}")
60
+ return file.read()
61
+
62
+ def configure_vm(self, vm_ip, vm_password, public_key):
63
+ """
64
+ Configures the VM by adding the public SSH key to the authorized keys.
65
+
66
+ Parameters:
67
+ vm_ip (str): The IP address of the VM.
68
+ vm_password (str): The password for the VM.
69
+ public_key (str): The public SSH key.
70
+ """
71
+ print(f"Configuring VM at {vm_ip} with provided public key.")
72
+ self.client.connect(vm_ip, username=self.username, password=vm_password)
73
+ self.client.exec_command(f'echo "{public_key}" >> ~/.ssh/authorized_keys')
74
+ self.client.close()
75
+
76
+ def update_ssh_config(self):
77
+ """
78
+ Updates the local SSH config file with the VM details.
79
+ """
80
+ config = f"""
81
+ Host {self.hostname}
82
+ HostName {self.hostname}
83
+ User {self.username}
84
+ StrictHostKeyChecking no
85
+ IdentityFile {self.private_key_path}
86
+ ForwardAgent yes
87
+ """
88
+ print(f"Updating SSH config for host {self.hostname}.")
89
+ with open(f'{os.path.expanduser("~")}/.ssh/config', 'a') as file:
90
+ file.write(config)
91
+
92
+ def establish_connection(self):
93
+ """
94
+ Establishes an SSH connection to the VM.
95
+ """
96
+ print(f"Establishing SSH connection to {self.hostname}.")
97
+ self.client.connect(self.hostname, username=self.username, key_filename=self.private_key_path)
98
+
99
+ def close_connection(self):
100
+ """
101
+ Closes the SSH connection.
102
+ """
103
+ if self.client:
104
+ print("Closing SSH connection.")
105
+ self.client.close()
106
+
107
+ def setup(self):
108
+ """
109
+ Sets up the SSH configuration and keys for the VM.
110
+ """
111
+ print("Starting SSH setup.")
112
+ public_key = self.read_or_generate_public_key(self.email)
113
+ self.configure_vm(self.hostname, self.password, public_key)
114
+ self.update_ssh_config()
115
+ self.establish_connection()
116
+ self.close_connection()
117
+ print("SSH setup completed.")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: vm_tool
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Summary: A Comprehensive Tool for Setting Up Virtual Machines Using Ansible.
5
5
  License: MIT
6
6
  Description-Content-Type: text/markdown
@@ -1,53 +0,0 @@
1
- import os
2
- import paramiko
3
-
4
- class SSHSetup:
5
- def __init__(self, hostname, username, password, email):
6
- self.hostname = hostname
7
- self.username = username
8
- self.password = password
9
- self.email = email
10
- self.private_key_path = os.path.expanduser('~/.ssh/id_rsa')
11
- self.client = paramiko.SSHClient()
12
- self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
13
-
14
- def _generate_ssh_key(self, email):
15
- os.system(f'ssh-keygen -t rsa -b 4096 -C "{email}"')
16
-
17
- def _read_or_generate_public_key(self, email):
18
- public_key_path = f'{self.private_key_path}.pub'
19
- if not os.path.exists(public_key_path):
20
- self._generate_ssh_key(email)
21
- with open(public_key_path, 'r') as file:
22
- return file.read()
23
-
24
- def _configure_vm(self, vm_ip, vm_password, public_key):
25
- self.client.connect(vm_ip, username=self.username, password=vm_password)
26
- self.client.exec_command(f'echo "{public_key}" >> ~/.ssh/authorized_keys')
27
- self.client.close()
28
-
29
- def _update_ssh_config(self):
30
- config = f"""
31
- Host {self.hostname}
32
- HostName {self.hostname}
33
- User {self.username}
34
- StrictHostKeyChecking no
35
- IdentityFile {self.private_key_path}
36
- ForwardAgent yes
37
- """
38
- with open(f'{os.path.expanduser("~")}/.ssh/config', 'a') as file:
39
- file.write(config)
40
-
41
- def _establish_connection(self):
42
- self.client.connect(self.hostname, username=self.username, key_filename=self.private_key_path)
43
-
44
- def _close_connection(self):
45
- if self.client:
46
- self.client.close()
47
-
48
- def setup(self):
49
- public_key = self._read_or_generate_public_key(self.email)
50
- self._configure_vm(self.hostname, self.password, public_key)
51
- self._update_ssh_config()
52
- self._establish_connection()
53
- self._close_connection()
File without changes
File without changes
File without changes
File without changes
File without changes