mag_core module
realm = click.option('-r', '--realm', default='magasin', show_default=True, help='magasin realm', callback=validate_realm_callback)
module-attribute
Adds the --realm option.
forward_port(realm, component, service_name, ports, verbose=False)
Forward ports for the specified realm, component, and service name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
A string representing the realm. |
required |
component |
str
|
A string representing the component. |
required |
service_name |
str
|
(str) A string representing the service name. The service name can be obtained using kubectl get services --namespace magasin-superset). |
required |
ports |
str
|
A string representing the ports to be forwarded (example: "8000:8000"). |
required |
verbose |
bool
|
A boolean indicating whether to enable verbose mode (default is False). |
False
|
Returns: None
Usage:
forward_port(realm, component, service_name, ports, verbose)
Example:
# Given this
kubectl get service -n magasin-superset
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
superset ClusterIP 10.100.96.47 <none> 8088/TCP 7d22h
forward_port("magasin", "superset", "superset", "8088:8088")
Notes:
- Assumes the port_forward_command function is defined elsewhere in the code.
- Uses subprocess.Popen to launch the port forwarding command in a subprocess.
- Registers the terminate_process function using atexit.register, ensuring that the port forwarding process is terminated when the script exits.
Source code in mag/mag_core/launch.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
generate_random_string(length=7)
Generate a random alphanumeric lowercase string of a specified length.
Parameters: - length (int): The desired length of the random string.
Returns: - str: A random string containing letters (both lowercase and uppercase) and digits.
Source code in mag/mag_core/random.py
4 5 6 7 8 9 10 11 12 13 14 15 |
|
get_namespace(component_name, realm='magasin')
Generate a namespace based on the component name and realm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
component_name |
str
|
The magasin component name (superset, daskhub, drill, ...) |
required |
realm |
str
|
The realm. Defaults to 'magasin'. |
'magasin'
|
Example
get_namespace("superset", "magasin")
-> "magasin-superset"get_namespace("superset", "magasin-postfix")
-> "magasin-superset-postfix"
Reference
For more information about magasin realms, please see the magasin realms documentation.
Source code in mag/mag_core/realm.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
is_port_open(host, port, timeout=15)
Check if a TCP port is open and responding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host |
str
|
A string representing the host to check. Example: localhost |
required |
port |
int
|
An integer representing the port to check. Example: 8080 |
required |
timeout |
int
|
An integer representing the timeout in seconds. Default is 15 seconds. |
15
|
Returns:
Name | Type | Description |
---|---|---|
bool |
Indicates whether the port is open and responding. |
Source code in mag/mag_core/launch.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
launch_command(realm, component, pod_name, command='/bin/bash')
Launches a command in a Kubernetes pod.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The magasin realm (e.g., magasin). |
required |
component |
str
|
The name of the magasin component. |
required |
pod_name |
str
|
The name of the pod. |
required |
command |
str
|
The command to be executed in the pod. Defaults to '/bin/bash'. |
'/bin/bash'
|
Returns:
Type | Description |
---|---|
None |
Example
launch_command('magasin', 'component_name', 'pod-1', 'ls -l') Running: kubectl exec pod-1 --namespace magasin -ti -- ls -l
Note
This function uses the kubectl
command-line tool to execute a command in a Kubernetes pod.
Make sure you have kubectl
installed and configured properly before using this function.
Source code in mag/mag_core/launch.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
launch_ui(realm, component, service_name, ports, protocol='http', verbose=False)
Launches the user interface for a given realm, component, and service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The realm of the magasin instance. |
required |
component |
str
|
The magasin component (f.i superset, daskhub, drill, ...) |
required |
service_name |
str
|
The name of the kubernetes service to forward. |
required |
ports |
str
|
The ports to forward, using the format "local_port:remote_port". |
required |
protocol |
str
|
The protocol to use (default is "http"). |
'http'
|
verbose |
bool
|
Whether to display verbose output (default is False). |
False
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Nothing |
Source code in mag/mag_core/launch.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
port_forward_command_arr(realm, component, service_name, ports, verbose=False)
Generate a command array for port forwarding.
This function generates a command array for port forwarding using kubectl
command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The magasin realm. |
required |
component |
str
|
The component of the command (f.i superset, daskhub, drill,...). |
required |
service_name |
str
|
The Kubernetes service name to forward. |
required |
ports |
str
|
The ports to forward. Follows the format "local_port:remote_port". |
required |
verbose |
bool
|
Whether to include verbose output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
List |
List
|
The generated command array. |
Source code in mag/mag_core/launch.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
port_forward_command_str(realm, component, service_name, ports, verbose=False)
Returns a string representation of the port forward command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The magasin realm. |
required |
component |
str
|
The component of the command (f.i superset, daskhub, drill,...). |
required |
service_name |
str
|
The Kubernetes service name to forward. |
required |
ports |
str
|
The ports to forward. Follows the format "local_port:remote_port". |
required |
verbose |
bool
|
Whether to include verbose output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The port forward command as a string. |
Example:
port_forward_command_str("magasin", "superset", "superset", "8088:8088")
Source code in mag/mag_core/launch.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
ports(default)
Adds the --ports option.
Validates the port format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
str
|
The default value for the option. |
required |
Returns:
Type | Description |
---|---|
click.Option: The click option object. |
Source code in mag/mag_core/options.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
split_ports(ports)
Split the input string with the format "number:number" into localhost and pod port.
Args: - ports (str): Input string with the format "number:number".
Returns: - tuple: A tuple containing localhost and pod port.
Raises: - ValueError: If the input string is not in the expected format or if ports are not valid.
Source code in mag/mag_core/ports.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
split_realm(realm)
Split the realm into prefix and suffix based on the last occurrence of "-".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The input realm string. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
A tuple containing prefix and suffix. |
Example
- split_realm("magasin") -> ("magasin", "")
- split_realm("magasin-post") -> ("magasin", "post")
- split_realm("magasin-1-post") -> ("magasin-1", "post")
- split_realm("dev-magasin-1") -> ("dev-magasin", "1")
Reference
For more information about magasin realms, please see the magasin realms documentation.
Source code in mag/mag_core/realm.py
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 |
|
terminate_process(process)
Terminate the given process if it is running.
Parameters: - process: A subprocess.Popen object representing the process to be terminated.
Usage: terminate_process(process)
Notes: - If the process is not running or is already terminated, this function returns without taking any action. - It checks if the process is running (poll() is None) and terminates it using terminate(). It then waits for the process to finish using wait().
Source code in mag/mag_core/launch.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
validate_pod_name(name)
Validates if a Kubernetes pod name is valid according to Kubernetes naming conventions.
Parameters: - name (str): The pod name to validate.
Returns: - bool: True if the pod name is valid, False otherwise.
Source code in mag/mag_core/validators.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
validate_port_callback(ctx, param, value)
Click callback to validate the port format
It validates the ports using mag_core.validators.validate_ports
Raises:
Type | Description |
---|---|
BadParameter
|
If port format is not valid |
Source code in mag/mag_core/options.py
38 39 40 41 42 43 44 45 46 47 |
|
validate_ports(ports)
Validate that the ports in the input string are numbers between 1 and 65535.
Args: - ports (str): Input string with the format "number:number".
Returns: - bool: True if the ports are valid, False otherwise.
Source code in mag/mag_core/validators.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
validate_realm(realm)
Verify if the realm contains only letters, numbers, "-", and "_".
Args: - realm (str): The input realm string.
Returns: - bool: True if the realm contains only valid characters, False otherwise.
Source code in mag/mag_core/validators.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
validate_realm_callback(ctx, param, value)
Validates if the realm has a correct value
See: https://click.palletsprojects.com/en/8.1.x/options/#callbacks-for-validation
Raises:
Type | Description |
---|---|
BadParameter
|
If name is not valid |
Source code in mag/mag_core/options.py
25 26 27 28 29 30 31 32 33 34 35 |
|
launch
This module provides functions for launching and managing services in a Kubernetes cluster.
It includes functions for generating port forwarding commands, checking if a TCP port is open, forwarding ports, launching user interfaces, and executing commands in Kubernetes pods.
forward_port(realm, component, service_name, ports, verbose=False)
Forward ports for the specified realm, component, and service name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
A string representing the realm. |
required |
component |
str
|
A string representing the component. |
required |
service_name |
str
|
(str) A string representing the service name. The service name can be obtained using kubectl get services --namespace magasin-superset). |
required |
ports |
str
|
A string representing the ports to be forwarded (example: "8000:8000"). |
required |
verbose |
bool
|
A boolean indicating whether to enable verbose mode (default is False). |
False
|
Returns: None
Usage:
forward_port(realm, component, service_name, ports, verbose)
Example:
# Given this
kubectl get service -n magasin-superset
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
superset ClusterIP 10.100.96.47 <none> 8088/TCP 7d22h
forward_port("magasin", "superset", "superset", "8088:8088")
Notes:
- Assumes the port_forward_command function is defined elsewhere in the code.
- Uses subprocess.Popen to launch the port forwarding command in a subprocess.
- Registers the terminate_process function using atexit.register, ensuring that the port forwarding process is terminated when the script exits.
Source code in mag/mag_core/launch.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
is_port_open(host, port, timeout=15)
Check if a TCP port is open and responding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host |
str
|
A string representing the host to check. Example: localhost |
required |
port |
int
|
An integer representing the port to check. Example: 8080 |
required |
timeout |
int
|
An integer representing the timeout in seconds. Default is 15 seconds. |
15
|
Returns:
Name | Type | Description |
---|---|---|
bool |
Indicates whether the port is open and responding. |
Source code in mag/mag_core/launch.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
launch_command(realm, component, pod_name, command='/bin/bash')
Launches a command in a Kubernetes pod.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The magasin realm (e.g., magasin). |
required |
component |
str
|
The name of the magasin component. |
required |
pod_name |
str
|
The name of the pod. |
required |
command |
str
|
The command to be executed in the pod. Defaults to '/bin/bash'. |
'/bin/bash'
|
Returns:
Type | Description |
---|---|
None |
Example
launch_command('magasin', 'component_name', 'pod-1', 'ls -l') Running: kubectl exec pod-1 --namespace magasin -ti -- ls -l
Note
This function uses the kubectl
command-line tool to execute a command in a Kubernetes pod.
Make sure you have kubectl
installed and configured properly before using this function.
Source code in mag/mag_core/launch.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
launch_ui(realm, component, service_name, ports, protocol='http', verbose=False)
Launches the user interface for a given realm, component, and service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The realm of the magasin instance. |
required |
component |
str
|
The magasin component (f.i superset, daskhub, drill, ...) |
required |
service_name |
str
|
The name of the kubernetes service to forward. |
required |
ports |
str
|
The ports to forward, using the format "local_port:remote_port". |
required |
protocol |
str
|
The protocol to use (default is "http"). |
'http'
|
verbose |
bool
|
Whether to display verbose output (default is False). |
False
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Nothing |
Source code in mag/mag_core/launch.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
port_forward_command_arr(realm, component, service_name, ports, verbose=False)
Generate a command array for port forwarding.
This function generates a command array for port forwarding using kubectl
command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The magasin realm. |
required |
component |
str
|
The component of the command (f.i superset, daskhub, drill,...). |
required |
service_name |
str
|
The Kubernetes service name to forward. |
required |
ports |
str
|
The ports to forward. Follows the format "local_port:remote_port". |
required |
verbose |
bool
|
Whether to include verbose output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
List |
List
|
The generated command array. |
Source code in mag/mag_core/launch.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
port_forward_command_str(realm, component, service_name, ports, verbose=False)
Returns a string representation of the port forward command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
realm |
str
|
The magasin realm. |
required |
component |
str
|
The component of the command (f.i superset, daskhub, drill,...). |
required |
service_name |
str
|
The Kubernetes service name to forward. |
required |
ports |
str
|
The ports to forward. Follows the format "local_port:remote_port". |
required |
verbose |
bool
|
Whether to include verbose output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The port forward command as a string. |
Example:
port_forward_command_str("magasin", "superset", "superset", "8088:8088")
Source code in mag/mag_core/launch.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
terminate_process(process)
Terminate the given process if it is running.
Parameters: - process: A subprocess.Popen object representing the process to be terminated.
Usage: terminate_process(process)
Notes: - If the process is not running or is already terminated, this function returns without taking any action. - It checks if the process is running (poll() is None) and terminates it using terminate(). It then waits for the process to finish using wait().
Source code in mag/mag_core/launch.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
options
Reusable command line options
These are options that can be included in a command and that can be reused in multiple commands
Example:
Add the --realm
```python from mag.mag_core import options
@cli.group('drill', cls=ClickAliasedGroup, aliases=['dr']) @options.realm def drill(realm): printf(realm)
realm = click.option('-r', '--realm', default='magasin', show_default=True, help='magasin realm', callback=validate_realm_callback)
module-attribute
Adds the --realm option.
ports(default)
Adds the --ports option.
Validates the port format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
str
|
The default value for the option. |
required |
Returns:
Type | Description |
---|---|
click.Option: The click option object. |
Source code in mag/mag_core/options.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
validate_port_callback(ctx, param, value)
Click callback to validate the port format
It validates the ports using mag_core.validators.validate_ports
Raises:
Type | Description |
---|---|
BadParameter
|
If port format is not valid |
Source code in mag/mag_core/options.py
38 39 40 41 42 43 44 45 46 47 |
|
validate_realm_callback(ctx, param, value)
Validates if the realm has a correct value
See: https://click.palletsprojects.com/en/8.1.x/options/#callbacks-for-validation
Raises:
Type | Description |
---|---|
BadParameter
|
If name is not valid |
Source code in mag/mag_core/options.py
25 26 27 28 29 30 31 32 33 34 35 |
|
std_aliases
Standard aliases for mag commands.
Sometimes it is difficult to remember if it is add, create or new the command. Aliases provide an
Aliases are also useful to help advanced users to work more efficiently. For example, instead of typing the "mag minio add bucket mybucket" command, you can just type "mag m a b mybucket".
This package defines some standard aliases which can be used consistently across the whole mag command line interface
Example:
from click_aliases import ClickAliasedGroup from mag.mag import cli cli.group('add', cls=ClickAliasedGroup, aliases=std_aliases.add) def add(): pass
This enables all these commands to be the same ```sh mag add mag a mag create mag c mag new mag n
add = ['a', 'create', 'c', 'new', 'n']
module-attribute
add command. Add a new item, create something.
shell = ['sh', 'bash', 'console', 'cmd']
module-attribute
shell command. Command line interfaces
validators
This module contains functions for validating different inputs in a magasin application.
validate_pod_name(name)
Validates if a Kubernetes pod name is valid according to Kubernetes naming conventions.
Parameters: - name (str): The pod name to validate.
Returns: - bool: True if the pod name is valid, False otherwise.
Source code in mag/mag_core/validators.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
validate_ports(ports)
Validate that the ports in the input string are numbers between 1 and 65535.
Args: - ports (str): Input string with the format "number:number".
Returns: - bool: True if the ports are valid, False otherwise.
Source code in mag/mag_core/validators.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
validate_realm(realm)
Verify if the realm contains only letters, numbers, "-", and "_".
Args: - realm (str): The input realm string.
Returns: - bool: True if the realm contains only valid characters, False otherwise.
Source code in mag/mag_core/validators.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|