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