| 1 | package WWW::Hetzner::CLI::Cmd::Server::Cmd::Create; | ||||||||||||
| 2 | # ABSTRACT: Create a server | ||||||||||||
| 3 | |||||||||||||
| 4 | 1 | our $VERSION = '0.102'; | |||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use MooX::Cmd; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use MooX::Options usage_string => 'USAGE: hcloud.pl server create --name <name> --type <type> --image <image> [options]'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use JSON::MaybeXS qw(encode_json); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Path::Tiny qw(path); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | with 'WWW::Hetzner::CLI::Role::WaitsForAction'; | |||||||||||
| 12 | |||||||||||||
| 13 | # Required options | ||||||||||||
| 14 | 1 | option name => ( | |||||||||||
| 15 | is => 'ro', | ||||||||||||
| 16 | format => 's', | ||||||||||||
| 17 | required => 1, | ||||||||||||
| 18 | doc => 'Server name', | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | 1 | option type => ( | |||||||||||
| 22 | is => 'ro', | ||||||||||||
| 23 | format => 's', | ||||||||||||
| 24 | required => 1, | ||||||||||||
| 25 | doc => 'Server type (e.g., cx22, cx32, cpx11)', | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | 1 | option image => ( | |||||||||||
| 29 | is => 'ro', | ||||||||||||
| 30 | format => 's', | ||||||||||||
| 31 | required => 1, | ||||||||||||
| 32 | doc => 'Image (e.g., debian-12, ubuntu-24.04)', | ||||||||||||
| 33 | ); | ||||||||||||
| 34 | |||||||||||||
| 35 | # Location options (mutually exclusive) | ||||||||||||
| 36 | 1 | option location => ( | |||||||||||
| 37 | is => 'ro', | ||||||||||||
| 38 | format => 's', | ||||||||||||
| 39 | doc => 'Location (e.g., fsn1, nbg1, hel1, ash, hil)', | ||||||||||||
| 40 | ); | ||||||||||||
| 41 | |||||||||||||
| 42 | 1 | option datacenter => ( | |||||||||||
| 43 | is => 'ro', | ||||||||||||
| 44 | format => 's', | ||||||||||||
| 45 | doc => 'Datacenter (e.g., fsn1-dc14)', | ||||||||||||
| 46 | ); | ||||||||||||
| 47 | |||||||||||||
| 48 | # SSH and security | ||||||||||||
| 49 | 1 | option ssh_key => ( | |||||||||||
| 50 | is => 'ro', | ||||||||||||
| 51 | format => 's@', | ||||||||||||
| 52 | doc => 'SSH key name or ID (repeatable)', | ||||||||||||
| 53 | autosplit => ',', | ||||||||||||
| 54 | ); | ||||||||||||
| 55 | |||||||||||||
| 56 | 1 | option firewall => ( | |||||||||||
| 57 | is => 'ro', | ||||||||||||
| 58 | format => 's@', | ||||||||||||
| 59 | doc => 'Firewall ID (repeatable)', | ||||||||||||
| 60 | autosplit => ',', | ||||||||||||
| 61 | ); | ||||||||||||
| 62 | |||||||||||||
| 63 | # Labels | ||||||||||||
| 64 | 1 | option label => ( | |||||||||||
| 65 | is => 'ro', | ||||||||||||
| 66 | format => 's@', | ||||||||||||
| 67 | doc => 'Labels (key=value, repeatable)', | ||||||||||||
| 68 | autosplit => ',', | ||||||||||||
| 69 | ); | ||||||||||||
| 70 | |||||||||||||
| 71 | # Network options | ||||||||||||
| 72 | 1 | option network => ( | |||||||||||
| 73 | is => 'ro', | ||||||||||||
| 74 | format => 's@', | ||||||||||||
| 75 | doc => 'Network ID to attach (repeatable)', | ||||||||||||
| 76 | autosplit => ',', | ||||||||||||
| 77 | ); | ||||||||||||
| 78 | |||||||||||||
| 79 | 1 | option without_ipv4 => ( | |||||||||||
| 80 | is => 'ro', | ||||||||||||
| 81 | doc => 'Create without public IPv4', | ||||||||||||
| 82 | default => 0, | ||||||||||||
| 83 | ); | ||||||||||||
| 84 | |||||||||||||
| 85 | 1 | option without_ipv6 => ( | |||||||||||
| 86 | is => 'ro', | ||||||||||||
| 87 | doc => 'Create without public IPv6', | ||||||||||||
| 88 | default => 0, | ||||||||||||
| 89 | ); | ||||||||||||
| 90 | |||||||||||||
| 91 | 1 | option primary_ipv4 => ( | |||||||||||
| 92 | is => 'ro', | ||||||||||||
| 93 | format => 's', | ||||||||||||
| 94 | doc => 'Primary IPv4 ID or IP to assign', | ||||||||||||
| 95 | ); | ||||||||||||
| 96 | |||||||||||||
| 97 | 1 | option primary_ipv6 => ( | |||||||||||
| 98 | is => 'ro', | ||||||||||||
| 99 | format => 's', | ||||||||||||
| 100 | doc => 'Primary IPv6 ID or IP to assign', | ||||||||||||
| 101 | ); | ||||||||||||
| 102 | |||||||||||||
| 103 | # Storage | ||||||||||||
| 104 | 1 | option volume => ( | |||||||||||
| 105 | is => 'ro', | ||||||||||||
| 106 | format => 's@', | ||||||||||||
| 107 | doc => 'Volume ID to attach (repeatable)', | ||||||||||||
| 108 | autosplit => ',', | ||||||||||||
| 109 | ); | ||||||||||||
| 110 | |||||||||||||
| 111 | 1 | option automount => ( | |||||||||||
| 112 | is => 'ro', | ||||||||||||
| 113 | doc => 'Automount volumes after attach', | ||||||||||||
| 114 | default => 0, | ||||||||||||
| 115 | ); | ||||||||||||
| 116 | |||||||||||||
| 117 | # Placement | ||||||||||||
| 118 | 1 | option placement_group => ( | |||||||||||
| 119 | is => 'ro', | ||||||||||||
| 120 | format => 's', | ||||||||||||
| 121 | doc => 'Placement group ID or name', | ||||||||||||
| 122 | ); | ||||||||||||
| 123 | |||||||||||||
| 124 | # User data | ||||||||||||
| 125 | 1 | option user_data_from_file => ( | |||||||||||
| 126 | is => 'ro', | ||||||||||||
| 127 | format => 's', | ||||||||||||
| 128 | doc => 'Read cloud-init user data from file', | ||||||||||||
| 129 | ); | ||||||||||||
| 130 | |||||||||||||
| 131 | # Startup | ||||||||||||
| 132 | 1 | option start_after_create => ( | |||||||||||
| 133 | is => 'ro', | ||||||||||||
| 134 | doc => 'Start server after create (default: true)', | ||||||||||||
| 135 | default => 1, | ||||||||||||
| 136 | negativable => 1, | ||||||||||||
| 137 | ); | ||||||||||||
| 138 | |||||||||||||
| 139 | 1 | option allow_deprecated_image => ( | |||||||||||
| 140 | is => 'ro', | ||||||||||||
| 141 | doc => 'Allow deprecated images', | ||||||||||||
| 142 | default => 0, | ||||||||||||
| 143 | ); | ||||||||||||
| 144 | |||||||||||||
| 145 | 1 | ▶ | sub execute { | ||||||||||
|
Subroutine
execute: called CC=20 SCAR=33.6
Pod
undocumented
| |||||||||||||
| 146 | 1 | my ($self, $args, $chain) = @_; | |||||||||||
| 147 | |||||||||||||
| 148 | 1 | my $main = $chain->[0]; | |||||||||||
| 149 | 1 | my $cloud = $main->cloud; | |||||||||||
| 150 | |||||||||||||
| 151 | 1 | my %params = ( | |||||||||||
| 152 | name => $self->name, | ||||||||||||
| 153 | server_type => $self->type, | ||||||||||||
| 154 | image => $self->image, | ||||||||||||
| 155 | ); | ||||||||||||
| 156 | |||||||||||||
| 157 | # Location/Datacenter | ||||||||||||
| 158 | 1 | ▶ | $params{location} = $self->location if $self->location; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | 1 | ▶ | $params{datacenter} = $self->datacenter if $self->datacenter; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | |||||||||||||
| 161 | # SSH Keys | ||||||||||||
| 162 | 1 | ▶ | $params{ssh_keys} = $self->ssh_key if $self->ssh_key; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | |||||||||||||
| 164 | # Firewalls | ||||||||||||
| 165 | 1 | ▶ | $params{firewalls} = $self->firewall if $self->firewall; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | |||||||||||||
| 167 | # Labels | ||||||||||||
| 168 | 1 | ▶ | if ($self->label) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | 1 | my %labels; | |||||||||||
| 170 | 1 | for my $l (@{$self->label}) { | |||||||||||
| 171 | 1 | my ($k, $v) = split /=/, $l, 2; | |||||||||||
| 172 | 1 | $labels{$k} = $v; | |||||||||||
| 173 | } | ||||||||||||
| 174 | 1 | $params{labels} = \%labels; | |||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | # Networks | ||||||||||||
| 178 | 1 | ▶ | $params{networks} = $self->network if $self->network; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | |||||||||||||
| 180 | # IPv4/IPv6 options | ||||||||||||
| 181 | 1 | ▶ | $params{enable_ipv4} = 0 if $self->without_ipv4; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | 1 | ▶ | $params{enable_ipv6} = 0 if $self->without_ipv6; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 1 | ▶ | $params{ipv4} = $self->primary_ipv4 if $self->primary_ipv4; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | 1 | ▶ | $params{ipv6} = $self->primary_ipv6 if $self->primary_ipv6; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | |||||||||||||
| 186 | # Volumes | ||||||||||||
| 187 | 1 | ▶ | $params{volumes} = $self->volume if $self->volume; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 1 | ▶ | $params{automount} = 1 if $self->automount; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 189 | |||||||||||||
| 190 | # Placement group | ||||||||||||
| 191 | 1 | ▶ | $params{placement_group} = $self->placement_group if $self->placement_group; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 192 | |||||||||||||
| 193 | # User data | ||||||||||||
| 194 | 1 | ▶ | if ($self->user_data_from_file) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | 0 | $params{user_data} = path($self->user_data_from_file)->slurp_utf8; | |||||||||||
| 196 | } | ||||||||||||
| 197 | |||||||||||||
| 198 | # Startup | ||||||||||||
| 199 | 1 | $params{start_after_create} = $self->start_after_create; | |||||||||||
| 200 | |||||||||||||
| 201 | 1 | print "Creating server '$params{name}'...\n"; | |||||||||||
| 202 | |||||||||||||
| 203 | 1 | my $server = $cloud->servers->create(%params); | |||||||||||
| 204 | 1 | $self->handle_action($server->action); | |||||||||||
| 205 | |||||||||||||
| 206 | 1 | ▶ | if ($main->output eq 'json') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | 0 | print encode_json($server->data), "\n"; | |||||||||||
| 208 | 0 | return; | |||||||||||
| 209 | } | ||||||||||||
| 210 | |||||||||||||
| 211 | 1 | print "Server created:\n"; | |||||||||||
| 212 | 1 | printf " ID: %s\n", $server->id; | |||||||||||
| 213 | 1 | printf " Name: %s\n", $server->name; | |||||||||||
| 214 | 1 | printf " Status: %s\n", $server->status; | |||||||||||
| 215 | 1 | ▶ | printf " IPv4: %s\n", $server->ipv4 // 'none'; | ||||||||||
|
Condition50%1/2
$server->ipv4 // "none"
MC/DC0%0/1
$server->ipv4 // "none"
$server->ipv4
Truth table50%1/2
A
A= $server->ipv4
| |||||||||||||
| 216 | 1 | ▶ | printf " IPv6: %s\n", $server->ipv6 // 'none'; | ||||||||||
|
Condition50%1/2
$server->ipv6 // "none"
MC/DC0%0/1
$server->ipv6 // "none"
$server->ipv6
Truth table50%1/2
A
A= $server->ipv6
| |||||||||||||
| 217 | 1 | ▶ | printf " Datacenter: %s\n", $server->datacenter // '-'; | ||||||||||
|
Condition50%1/2
$server->datacenter // "-"
MC/DC0%0/1
$server->datacenter // "-"
$server->datacenter
Truth table50%1/2
A
A= $server->datacenter
| |||||||||||||
| 218 | } | ||||||||||||
| 219 | |||||||||||||
| 220 | 1 | 1; | |||||||||||
| 221 | |||||||||||||
| 222 | __END__ | ||||||||||||