|
| 1 | +""" |
| 2 | +This plugin allows users to access the metadata service while in a Linode. |
| 3 | +
|
| 4 | +Usage: |
| 5 | +
|
| 6 | + linode-cli metadata [ENDPOINT] |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import sys |
| 11 | + |
| 12 | +from linode_metadata import MetadataClient |
| 13 | +from linode_metadata.objects.error import ApiError |
| 14 | +from linode_metadata.objects.instance import ResponseBase |
| 15 | +from requests import ConnectTimeout |
| 16 | +from rich import print as rprint |
| 17 | +from rich.table import Table |
| 18 | + |
| 19 | +PLUGIN_BASE = "linode-cli metadata" |
| 20 | + |
| 21 | + |
| 22 | +def process_sub_columns(subcolumn: ResponseBase, table: Table, values_row): |
| 23 | + """ |
| 24 | + Helper method to process embedded ResponseBase objects |
| 25 | + """ |
| 26 | + for key, value in vars(subcolumn).items(): |
| 27 | + if isinstance(value, ResponseBase): |
| 28 | + process_sub_columns(value, table, values_row) |
| 29 | + else: |
| 30 | + table.add_column(key) |
| 31 | + values_row.append(str(value)) |
| 32 | + |
| 33 | + |
| 34 | +def print_instance_table(data): |
| 35 | + """ |
| 36 | + Prints the table that contains information about the current instance |
| 37 | + """ |
| 38 | + attributes = vars(data) |
| 39 | + values_row = [] |
| 40 | + |
| 41 | + table = Table() |
| 42 | + |
| 43 | + for key, value in attributes.items(): |
| 44 | + if isinstance(value, ResponseBase): |
| 45 | + process_sub_columns(value, table, values_row) |
| 46 | + else: |
| 47 | + table.add_column(key) |
| 48 | + values_row.append(str(value)) |
| 49 | + |
| 50 | + table.add_row(*values_row) |
| 51 | + rprint(table) |
| 52 | + |
| 53 | + |
| 54 | +def print_ssh_keys_table(data): |
| 55 | + """ |
| 56 | + Prints the table that contains information about the SSH keys for the current instance |
| 57 | + """ |
| 58 | + table = Table(show_lines=True) |
| 59 | + |
| 60 | + table.add_column("ssh keys") |
| 61 | + |
| 62 | + if data.users.root is not None: |
| 63 | + for key in data.users.root: |
| 64 | + table.add_row(key) |
| 65 | + |
| 66 | + rprint(table) |
| 67 | + |
| 68 | + |
| 69 | +def print_networking_tables(data): |
| 70 | + """ |
| 71 | + Prints the table that contains information about the network of the current instance |
| 72 | + """ |
| 73 | + interfaces = Table(title="Interfaces", show_lines=True) |
| 74 | + |
| 75 | + interfaces.add_column("label") |
| 76 | + interfaces.add_column("purpose") |
| 77 | + interfaces.add_column("ipam addresses") |
| 78 | + |
| 79 | + for interface in data.interfaces: |
| 80 | + attributes = vars(interface) |
| 81 | + interface_row = [] |
| 82 | + for _, value in attributes.items(): |
| 83 | + interface_row.append(str(value)) |
| 84 | + interfaces.add_row(*interface_row) |
| 85 | + |
| 86 | + ipv4 = Table(title="IPv4") |
| 87 | + ipv4.add_column("ip address") |
| 88 | + ipv4.add_column("type") |
| 89 | + attributes = vars(data.ipv4) |
| 90 | + for key, value in attributes.items(): |
| 91 | + for address in value: |
| 92 | + ipv4.add_row(*[address, key]) |
| 93 | + |
| 94 | + ipv6 = Table(title="IPv6") |
| 95 | + ipv6_data = data.ipv6 |
| 96 | + ipv6.add_column("slaac") |
| 97 | + ipv6.add_column("link local") |
| 98 | + ipv6.add_column("ranges") |
| 99 | + ipv6.add_column("shared ranges") |
| 100 | + ipv6.add_row( |
| 101 | + *[ |
| 102 | + ipv6_data.slaac, |
| 103 | + ipv6_data.link_local, |
| 104 | + str(ipv6_data.ranges), |
| 105 | + str(ipv6_data.shared_ranges), |
| 106 | + ] |
| 107 | + ) |
| 108 | + |
| 109 | + rprint(interfaces) |
| 110 | + rprint(ipv4) |
| 111 | + rprint(ipv6) |
| 112 | + |
| 113 | + |
| 114 | +def get_instance(client: MetadataClient): |
| 115 | + """ |
| 116 | + Get information about your instance, including plan resources |
| 117 | + """ |
| 118 | + data = client.get_instance() |
| 119 | + print_instance_table(data) |
| 120 | + |
| 121 | + |
| 122 | +def get_user_data(client: MetadataClient): |
| 123 | + """ |
| 124 | + Get your user data |
| 125 | + """ |
| 126 | + data = client.get_user_data() |
| 127 | + rprint(data) |
| 128 | + |
| 129 | + |
| 130 | +def get_network(client: MetadataClient): |
| 131 | + """ |
| 132 | + Get information about your instance’s IP addresses |
| 133 | + """ |
| 134 | + data = client.get_network() |
| 135 | + print_networking_tables(data) |
| 136 | + |
| 137 | + |
| 138 | +def get_ssh_keys(client: MetadataClient): |
| 139 | + """ |
| 140 | + Get information about public SSH Keys configured on your instance |
| 141 | + """ |
| 142 | + data = client.get_ssh_keys() |
| 143 | + print_ssh_keys_table(data) |
| 144 | + |
| 145 | + |
| 146 | +COMMAND_MAP = { |
| 147 | + "instance": get_instance, |
| 148 | + "user-data": get_user_data, |
| 149 | + "networking": get_network, |
| 150 | + "sshkeys": get_ssh_keys, |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +def print_help(parser: argparse.ArgumentParser): |
| 155 | + """ |
| 156 | + Print out the help info to the standard output |
| 157 | + """ |
| 158 | + parser.print_help() |
| 159 | + |
| 160 | + # additional help |
| 161 | + print() |
| 162 | + print("Available endpoints: ") |
| 163 | + |
| 164 | + command_help_map = [ |
| 165 | + [name, func.__doc__.strip()] |
| 166 | + for name, func in sorted(COMMAND_MAP.items()) |
| 167 | + ] |
| 168 | + |
| 169 | + tab = Table(show_header=False) |
| 170 | + for row in command_help_map: |
| 171 | + tab.add_row(*row) |
| 172 | + rprint(tab) |
| 173 | + |
| 174 | + |
| 175 | +def get_metadata_parser(): |
| 176 | + """ |
| 177 | + Builds argparser for Metadata plug-in |
| 178 | + """ |
| 179 | + parser = argparse.ArgumentParser(PLUGIN_BASE, add_help=False) |
| 180 | + |
| 181 | + parser.add_argument( |
| 182 | + "endpoint", |
| 183 | + metavar="ENDPOINT", |
| 184 | + nargs="?", |
| 185 | + type=str, |
| 186 | + help="The API endpoint to be called from the Metadata service.", |
| 187 | + ) |
| 188 | + |
| 189 | + return parser |
| 190 | + |
| 191 | + |
| 192 | +def call(args, _): |
| 193 | + """ |
| 194 | + The entrypoint for this plugin |
| 195 | + """ |
| 196 | + |
| 197 | + parser = get_metadata_parser() |
| 198 | + parsed, args = parser.parse_known_args(args) |
| 199 | + |
| 200 | + if not parsed.endpoint in COMMAND_MAP or len(args) != 0: |
| 201 | + print_help(parser) |
| 202 | + sys.exit(0) |
| 203 | + |
| 204 | + # make a client, but only if we weren't printing help and endpoint is valid |
| 205 | + if "--help" not in args: |
| 206 | + try: |
| 207 | + client = MetadataClient() |
| 208 | + except ConnectTimeout as exc: |
| 209 | + raise ConnectionError( |
| 210 | + "Can't access Metadata service. Please verify that you are inside a Linode." |
| 211 | + ) from exc |
| 212 | + else: |
| 213 | + print_help(parser) |
| 214 | + sys.exit(0) |
| 215 | + |
| 216 | + try: |
| 217 | + COMMAND_MAP[parsed.endpoint](client) |
| 218 | + except ApiError as e: |
| 219 | + sys.exit(f"Error: {e}") |
0 commit comments