-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhicloud.py
More file actions
executable file
·85 lines (67 loc) · 2.88 KB
/
hicloud.py
File metadata and controls
executable file
·85 lines (67 loc) · 2.88 KB
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
#!/usr/bin/env python3
# hicloud.py - Hetzner Cloud CLI Tool
# License: GPLv3
# Author: Robert Tulke rt@debian.sh
import os
import sys
import argparse
import warnings
# Suppress urllib3 NotOpenSSLWarning on macOS (uses LibreSSL instead of OpenSSL)
# This warning is harmless and does not affect functionality
warnings.filterwarnings('ignore', message='urllib3 v2 only supports OpenSSL 1.1.1+')
from lib.api import HetznerCloudManager
from lib.config import ConfigManager
from lib.console import InteractiveConsole
from utils.constants import DEFAULT_CONFIG_PATH, HISTORY_DIR, VERSION
def main():
"""Main function"""
parser = argparse.ArgumentParser(description="Hetzner Cloud CLI Tool")
parser.add_argument("--config", help="Path to configuration file")
parser.add_argument("--gen-config", help="Generate a sample configuration file")
parser.add_argument("--project", help="Project to use from config file", default="default")
parser.add_argument("--token", help="API token (overrides config file)")
parser.add_argument("--version", action="version", version=f"hicloud v{VERSION}")
parser.add_argument("--debug", action="store_true", help="Enable debug output")
args = parser.parse_args()
# Ensure history directory exists
if not os.path.exists(HISTORY_DIR):
try:
os.makedirs(HISTORY_DIR, exist_ok=True)
except Exception as e:
print(f"Warning: Could not create history directory: {str(e)}")
# Handle config generation
if args.gen_config:
ConfigManager.generate_config(args.gen_config)
return 0
# Load configuration
config_path = args.config if args.config else DEFAULT_CONFIG_PATH
config = {}
if os.path.exists(config_path):
config = ConfigManager.load_config(config_path)
# Get API token
api_token = None
project_name = "default"
if args.token:
api_token = args.token
elif args.project in config:
api_token = config[args.project].get("api_token")
project_name = config[args.project].get("project_name", args.project)
elif "default" in config:
api_token = config["default"].get("api_token")
project_name = config["default"].get("project_name", "default")
if not api_token:
if not os.path.exists(config_path):
print(f"No configuration file found at {config_path}")
print(f"Generate one with: hicloud.py --gen-config {config_path}")
print("Or provide an API token: hicloud.py --token YOUR_API_TOKEN")
else:
print(f"No API token found for project '{args.project}'")
return 1
# Create Hetzner Cloud manager
hetzner = HetznerCloudManager(api_token, project_name, debug=args.debug)
# Start interactive console
console = InteractiveConsole(hetzner, debug=args.debug)
console.start()
return 0
if __name__ == "__main__":
sys.exit(main())